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