001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2011-2015 ForgeRock AS
026 */
027
028package org.opends.guitools.uninstaller.ui;
029
030import org.opends.quicksetup.CurrentInstallStatus;
031import org.opends.quicksetup.Installation;
032import org.opends.quicksetup.Configuration;
033import org.opends.quicksetup.ui.FieldName;
034import org.opends.quicksetup.ui.GuiApplication;
035import org.opends.quicksetup.ui.QuickSetupStepPanel;
036import org.opends.quicksetup.ui.UIFactory;
037import org.opends.quicksetup.util.Utils;
038
039import javax.swing.*;
040import javax.swing.border.EmptyBorder;
041import java.awt.*;
042import java.util.HashMap;
043import java.util.HashSet;
044import java.util.Set;
045
046import org.forgerock.i18n.LocalizableMessage;
047import org.forgerock.i18n.slf4j.LocalizedLogger;
048import java.io.IOException;
049
050import static org.opends.messages.AdminToolMessages.*;
051
052/**
053 * This is the panel displayed when the user is uninstalling Open DS.  It is
054 * basically a panel with the text informing of the consequences of uninstalling
055 * the server and asking for confirmation.
056 *
057 */
058public class ConfirmUninstallPanel extends QuickSetupStepPanel
059{
060  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
061
062  private static final long serialVersionUID = 81730510134697056L;
063
064  private Set<String> outsideDbs;
065  private Set<String> outsideLogs;
066
067  private HashMap<FieldName, JCheckBox> hmCbs = new HashMap<>();
068
069  /**
070   * The constructor of this class.
071   * @param application Application this panel represents
072   * @param installStatus the object describing the current installation status.
073   *
074   */
075  public ConfirmUninstallPanel(GuiApplication application,
076                               CurrentInstallStatus installStatus)
077  {
078    super(application);
079  }
080
081  /** {@inheritDoc} */
082  @Override
083  public Object getFieldValue(FieldName fieldName)
084  {
085    switch (fieldName)
086    {
087    case EXTERNAL_DB_DIRECTORIES:
088      Set<String> s1 = new HashSet<>();
089      if (outsideDbs.size() > 0
090          && getCheckBox(FieldName.EXTERNAL_DB_DIRECTORIES).isSelected())
091      {
092        s1.addAll(outsideDbs);
093      }
094      return s1;
095
096    case EXTERNAL_LOG_FILES:
097      Set<String> s2 = new HashSet<>();
098      if (outsideLogs.size() > 0
099          && getCheckBox(FieldName.EXTERNAL_LOG_FILES).isSelected())
100      {
101        s2.addAll(outsideLogs);
102      }
103      return s2;
104    default:
105      JCheckBox cb = getCheckBox(fieldName);
106      return cb.isSelected();
107    }
108  }
109
110  /** {@inheritDoc} */
111  @Override
112  protected LocalizableMessage getTitle()
113  {
114    return INFO_CONFIRM_UNINSTALL_PANEL_TITLE.get();
115  }
116
117  /** {@inheritDoc} */
118  @Override
119  protected Component createInputPanel()
120  {
121    FieldName[] fieldNames = {
122        FieldName.REMOVE_LIBRARIES_AND_TOOLS,
123        FieldName.REMOVE_DATABASES,
124        FieldName.REMOVE_LOGS,
125        FieldName.REMOVE_CONFIGURATION_AND_SCHEMA,
126        FieldName.REMOVE_BACKUPS,
127        FieldName.REMOVE_LDIFS,
128    };
129
130    LocalizableMessage[] labels = {
131        INFO_REMOVE_LIBRARIES_AND_TOOLS_LABEL.get(),
132        INFO_REMOVE_DATABASES_LABEL.get(),
133        INFO_REMOVE_LOGS_LABEL.get(),
134        INFO_REMOVE_SCHEMA_AND_CONFIGURATION_LABEL.get(),
135        INFO_REMOVE_BACKUPS_LABEL.get(),
136        INFO_REMOVE_LDIFS_LABEL.get()
137    };
138
139    LocalizableMessage[] tooltips = {
140        INFO_REMOVE_LIBRARIES_AND_TOOLS_TOOLTIP.get(),
141        INFO_REMOVE_DATABASES_TOOLTIP.get(),
142        INFO_REMOVE_LOGS_TOOLTIP.get(),
143        INFO_REMOVE_SCHEMA_AND_CONFIGURATION_TOOLTIP.get(),
144        INFO_REMOVE_BACKUPS_TOOLTIP.get(),
145        INFO_REMOVE_LDIFS_TOOLTIP.get()
146    };
147
148    for (int i=0; i<fieldNames.length; i++)
149    {
150      JCheckBox cb = UIFactory.makeJCheckBox(labels[i], tooltips[i],
151          UIFactory.TextStyle.INSTRUCTIONS);
152      cb.setSelected(true);
153      hmCbs.put(fieldNames[i], cb);
154    }
155
156
157    JPanel panel = new JPanel(new GridBagLayout());
158    panel.setOpaque(false);
159
160    GridBagConstraints gbc = new GridBagConstraints();
161    gbc.insets = UIFactory.getEmptyInsets();
162
163    JPanel p = new JPanel(new GridBagLayout());
164    p.setOpaque(false);
165    gbc.weightx = 0.0;
166    gbc.gridwidth = GridBagConstraints.RELATIVE;
167    gbc.anchor = GridBagConstraints.WEST;
168    p.add(UIFactory.makeJLabel(UIFactory.IconType.NO_ICON,
169        INFO_SERVER_PATH_LABEL.get(),
170        UIFactory.TextStyle.PRIMARY_FIELD_VALID), gbc);
171    gbc.gridwidth = GridBagConstraints.REMAINDER;
172    gbc.insets.left = UIFactory.LEFT_INSET_SECONDARY_FIELD;
173    p.add(UIFactory.makeJLabel(UIFactory.IconType.NO_ICON,
174        LocalizableMessage.raw(Utils.getInstallPathFromClasspath()),
175        UIFactory.TextStyle.INSTRUCTIONS),
176        gbc);
177
178    FieldName[] names = {
179        FieldName.REMOVE_LIBRARIES_AND_TOOLS,
180        FieldName.REMOVE_DATABASES,
181        FieldName.REMOVE_LOGS,
182        FieldName.REMOVE_CONFIGURATION_AND_SCHEMA,
183        FieldName.REMOVE_BACKUPS,
184        FieldName.REMOVE_LDIFS
185    };
186
187    for (int i=0; i<names.length; i++)
188    {
189      gbc.gridwidth = GridBagConstraints.RELATIVE;
190      p.add(Box.createHorizontalGlue(), gbc);
191      gbc.insets.left = 0;
192      gbc.gridwidth = GridBagConstraints.REMAINDER;
193      gbc.insets.left = UIFactory.LEFT_INSET_SECONDARY_FIELD;
194      p.add(getCheckBox(names[i]), gbc);
195    }
196
197    gbc.weightx = 1.0;
198    gbc.fill = GridBagConstraints.NONE;
199    gbc.gridwidth = GridBagConstraints.REMAINDER;
200    gbc.anchor = GridBagConstraints.WEST;
201    gbc.insets.left = 0;
202
203    panel.add(p, gbc);
204
205    Installation installation = Installation.getLocal();
206    Configuration config = installation.getCurrentConfiguration();
207    try {
208      outsideDbs = config.getOutsideDbs();
209    } catch (IOException ioe) {
210      logger.info(LocalizableMessage.raw("Unable to determin outside databases", ioe));
211    }
212
213    try {
214      outsideLogs = config.getOutsideLogs();
215    } catch (IOException ioe) {
216      logger.info(LocalizableMessage.raw("Unable to determin outside logs", ioe));
217    }
218
219
220    gbc.insets.top = UIFactory.TOP_INSET_PRIMARY_FIELD;
221    gbc.fill = GridBagConstraints.HORIZONTAL;
222    gbc.weightx = 1.0;
223    if (outsideDbs.size() > 0)
224    {
225      JPanel dbPanel = createDbPanel();
226      panel.add(dbPanel, gbc);
227    }
228
229    if (outsideLogs.size() > 0)
230    {
231      JPanel logPanel = createLogPanel();
232      panel.add(logPanel, gbc);
233    }
234
235    addVerticalGlue(panel);
236
237    return panel;
238  }
239
240  /** {@inheritDoc} */
241  @Override
242  protected LocalizableMessage getInstructions()
243  {
244    return INFO_CONFIRM_UNINSTALL_PANEL_INSTRUCTIONS.get();
245  }
246
247  /**
248   * Creates a panel to ask the user if (s)he wants to remove the databases
249   * located outside the installation path.
250   * @return a panel to ask the user if (s)he wants to remove the databases
251   * located outside the installation path.
252   */
253  private JPanel createDbPanel()
254  {
255    JCheckBox cbOutsideDbs = UIFactory.makeJCheckBox(
256        INFO_DELETE_OUTSIDE_DBS_LABEL.get(),
257        INFO_DELETE_OUTSIDE_DBS_TOOLTIP.get(),
258            UIFactory.TextStyle.INSTRUCTIONS);
259    cbOutsideDbs.setSelected(true);
260    hmCbs.put(FieldName.EXTERNAL_DB_DIRECTORIES, cbOutsideDbs);
261
262    return createOutsidePathPanel(cbOutsideDbs, outsideDbs,
263        INFO_DELETE_OUTSIDE_DBS_MSG.get());
264  }
265
266  /**
267   * Creates a panel to ask the user if (s)he wants to remove the logs located
268   * outside the installation path.
269   * @return a panel to ask the user if (s)he wants to remove the logs located
270   * outside the installation path.
271   */
272  private JPanel createLogPanel()
273  {
274    JCheckBox cbOutsideLogs = UIFactory.makeJCheckBox(
275        INFO_DELETE_OUTSIDE_LOGS_LABEL.get(),
276        INFO_DELETE_OUTSIDE_LOGS_TOOLTIP.get(),
277        UIFactory.TextStyle.INSTRUCTIONS);
278    cbOutsideLogs.setSelected(true);
279    hmCbs.put(FieldName.EXTERNAL_LOG_FILES, cbOutsideLogs);
280
281    return createOutsidePathPanel(cbOutsideLogs, outsideLogs,
282        INFO_DELETE_OUTSIDE_LOGS_MSG.get());
283  }
284
285  private JPanel createOutsidePathPanel(JCheckBox cb, Set<String> paths,
286      LocalizableMessage msg)
287  {
288    JPanel panel = new JPanel(new GridBagLayout());
289    panel.setOpaque(false);
290
291    GridBagConstraints gbc = new GridBagConstraints();
292    gbc.insets = UIFactory.getEmptyInsets();
293    gbc.weightx = 1.0;
294    gbc.gridwidth = GridBagConstraints.REMAINDER;
295    gbc.anchor = GridBagConstraints.WEST;
296    gbc.fill = GridBagConstraints.HORIZONTAL;
297
298    panel.add(UIFactory.makeJLabel(UIFactory.IconType.NO_ICON, msg,
299        UIFactory.TextStyle.INSTRUCTIONS), gbc);
300    DefaultListModel listModel = new DefaultListModel();
301    for (String path : paths)
302    {
303      listModel.addElement(path);
304    }
305    JList list = UIFactory.makeJList(UIFactory.TextStyle.INSTRUCTIONS);
306    list.setModel(listModel);
307    list.setBackground(UIFactory.CURRENT_STEP_PANEL_BACKGROUND);
308    list.setVisibleRowCount(Math.min(3, listModel.getSize()));
309    JScrollPane scroll = new JScrollPane(list);
310    scroll.setViewportBorder(new EmptyBorder(0, 0, 0, 0));
311    gbc.insets.left = UIFactory.LEFT_INSET_RADIO_SUBORDINATE;
312    panel.add(scroll, gbc);
313
314    gbc.insets.left = 0;
315    panel.add(cb, gbc);
316
317    return panel;
318  }
319
320  /**
321   * Returns the checkbox corresponding to the provided FieldName.
322   * @param fieldName the FieldName object.
323   * @return the checkbox corresponding to the provided FieldName.
324   */
325  private JCheckBox getCheckBox(FieldName fieldName)
326  {
327    JCheckBox cb = hmCbs.get(fieldName);
328    if (cb == null)
329    {
330      throw new IllegalArgumentException("The FieldName "+fieldName+
331          " has no checkbox associated.");
332    }
333    return cb;
334  }
335}