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 2008-2009 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017
018package org.opends.guitools.controlpanel.ui;
019
020import static org.opends.messages.AdminToolMessages.*;
021import static com.forgerock.opendj.util.OperatingSystem.isWindows;
022
023import java.awt.Component;
024import java.awt.GridBagConstraints;
025import java.awt.GridBagLayout;
026import java.awt.event.ActionEvent;
027import java.awt.event.ActionListener;
028import java.awt.event.MouseAdapter;
029import java.awt.event.MouseEvent;
030import java.lang.reflect.Constructor;
031import java.util.ArrayList;
032import java.util.Collection;
033import java.util.HashMap;
034import java.util.Map;
035
036import javax.swing.Box;
037import javax.swing.ButtonGroup;
038import javax.swing.JPanel;
039
040import org.forgerock.i18n.LocalizableMessage;
041import org.opends.guitools.controlpanel.datamodel.Action;
042import org.opends.guitools.controlpanel.datamodel.Category;
043import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
044import org.opends.guitools.controlpanel.ui.components.ActionButton;
045import org.opends.guitools.controlpanel.ui.components.CategoryPanel;
046import org.opends.guitools.controlpanel.util.Utilities;
047
048/**
049 * The panel on the left side of the main Control Center dialog.  It contains
050 * all the actions on the pane divided in categories.
051 */
052public class MainActionsPane extends StatusGenericPanel
053{
054  private static final long serialVersionUID = 7616418700758530191L;
055
056  /** Default constructor. */
057  public MainActionsPane()
058  {
059    super();
060
061    setBackground(ColorAndFontConstants.greyBackground);
062    GridBagConstraints gbc1 = new GridBagConstraints();
063    gbc1.gridx = 0;
064    gbc1.gridy = 0;
065    gbc1.fill = GridBagConstraints.HORIZONTAL;
066    gbc1.weightx = 1;
067    ArrayList<Category> categories = createCategories();
068    ButtonGroup group = new ButtonGroup();
069    int maxWidth = 0;
070    final Map<Action, GenericFrame> frames = new HashMap<>();
071    ArrayList<ActionButton> actions = new ArrayList<>();
072    for(Category category: categories)
073    {
074      JPanel categoryPanel = new JPanel(new GridBagLayout());
075      GridBagConstraints gbc2 = new GridBagConstraints();
076      gbc2.gridx = 0;
077      gbc2.gridy = 0;
078      gbc2.weightx = 1;
079      gbc2.fill = GridBagConstraints.HORIZONTAL;
080      for (Action action : category.getActions())
081      {
082        final ActionButton b = new ActionButton(action);
083        actions.add(b);
084        b.addActionListener(new ActionListener()
085        {
086          @Override
087          public void actionPerformed(ActionEvent ev)
088          {
089            // Constructs the panels using reflection.
090            Action action = b.getActionObject();
091            GenericFrame frame = frames.get(action);
092            if (frame == null)
093            {
094              Class<? extends StatusGenericPanel> panelClass =
095                action.getAssociatedPanelClass();
096              try
097              {
098                Constructor<? extends StatusGenericPanel> constructor =
099                  panelClass.getDeclaredConstructor();
100                StatusGenericPanel panel = constructor.newInstance();
101                if (getInfo() != null)
102                {
103                  panel.setInfo(getInfo());
104                }
105                frame = createFrame(panel);
106
107                frames.put(action, frame);
108                Utilities.centerGoldenMean(frame,
109                    Utilities.getFrame(MainActionsPane.this));
110              }
111              catch (Throwable t)
112              {
113                // Bug
114                t.printStackTrace();
115              }
116            }
117            if (!frame.isVisible())
118            {
119              frame.setVisible(true);
120            }
121            else
122            {
123              frame.toFront();
124            }
125          }
126        });
127        categoryPanel.add(b, gbc2);
128        gbc2.gridy++;
129        group.add(b);
130        maxWidth = Math.max(maxWidth, b.getPreferredSize().width);
131      }
132      CategoryPanel p = new CategoryPanel(categoryPanel, category);
133      maxWidth = Math.max(maxWidth, p.getPreferredSize().width);
134      p.setExpanded(false);
135      add(p, gbc1);
136      gbc1.gridy++;
137
138      if (category.getName().equals(
139          INFO_CTRL_PANEL_CATEGORY_DIRECTORY_DATA.get()))
140      {
141        p.setExpanded(true);
142      }
143    }
144    add(Box.createHorizontalStrut(maxWidth), gbc1);
145    gbc1.gridy ++;
146    gbc1.weighty = 1.0;
147    add(Box.createVerticalGlue(), gbc1);
148    createActionButtonListeners(actions);
149  }
150
151  @Override
152  public Component getPreferredFocusComponent()
153  {
154    return null;
155  }
156
157  /**
158   * Creates the frame to be displayed using the provided panel.
159   * @param panel the panel that will be contained in the frame.
160   * @return the frame to be displayed using the provided panel.
161   */
162  private GenericFrame createFrame(StatusGenericPanel panel)
163  {
164    return new GenericFrame(panel);
165  }
166
167  /**
168   * Creates the categories contained by this panel.
169   * @return the categories contained by this panel.
170   */
171  private ArrayList<Category> createCategories()
172  {
173    ArrayList<Category> categories = new ArrayList<>();
174    LocalizableMessage[][] labels;
175    if (isWindows())
176    {
177      labels = new LocalizableMessage[][] {
178          {
179            INFO_CTRL_PANEL_CATEGORY_DIRECTORY_DATA.get(),
180            INFO_CTRL_PANEL_ACTION_MANAGE_ENTRIES.get(),
181            INFO_CTRL_PANEL_ACTION_NEW_BASEDN.get(),
182            INFO_CTRL_PANEL_ACTION_IMPORT_LDIF.get(),
183            INFO_CTRL_PANEL_ACTION_EXPORT_LDIF.get(),
184            INFO_CTRL_PANEL_ACTION_BACKUP.get(),
185            INFO_CTRL_PANEL_ACTION_RESTORE.get()
186          },
187          {
188            INFO_CTRL_PANEL_CATEGORY_SCHEMA.get(),
189            INFO_CTRL_PANEL_ACTION_MANAGE_SCHEMA.get()
190          },
191          {
192            INFO_CTRL_PANEL_CATEGORY_INDEXES.get(),
193            INFO_CTRL_PANEL_ACTION_MANAGE_INDEXES.get(),
194            INFO_CTRL_PANEL_ACTION_VERIFY_INDEXES.get(),
195            INFO_CTRL_PANEL_ACTION_REBUILD_INDEXES.get()
196          },
197          {
198            INFO_CTRL_PANEL_CATEGORY_MONITORING.get(),
199            INFO_CTRL_PANEL_BROWSE_GENERAL_MONITORING.get(),
200            INFO_CTRL_PANEL_CONNECTION_HANDLER_MONITORING.get(),
201            INFO_CTRL_PANEL_MANAGE_TASKS.get()
202          },
203          {
204            INFO_CTRL_PANEL_CATEGORY_RUNTIME_OPTIONS.get(),
205            INFO_CTRL_PANEL_ACTION_JAVA_SETTINGS.get(),
206            INFO_CTRL_PANEL_ACTION_WINDOWS_SERVICE.get()
207          }
208      };
209    }
210    else
211    {
212      labels = new LocalizableMessage[][] {
213          {
214            INFO_CTRL_PANEL_CATEGORY_DIRECTORY_DATA.get(),
215            INFO_CTRL_PANEL_ACTION_MANAGE_ENTRIES.get(),
216            INFO_CTRL_PANEL_ACTION_NEW_BASEDN.get(),
217            INFO_CTRL_PANEL_ACTION_IMPORT_LDIF.get(),
218            INFO_CTRL_PANEL_ACTION_EXPORT_LDIF.get(),
219            INFO_CTRL_PANEL_ACTION_BACKUP.get(),
220            INFO_CTRL_PANEL_ACTION_RESTORE.get()
221          },
222          {
223            INFO_CTRL_PANEL_CATEGORY_SCHEMA.get(),
224            INFO_CTRL_PANEL_ACTION_MANAGE_SCHEMA.get()
225          },
226          {
227            INFO_CTRL_PANEL_CATEGORY_INDEXES.get(),
228            INFO_CTRL_PANEL_ACTION_MANAGE_INDEXES.get(),
229            INFO_CTRL_PANEL_ACTION_VERIFY_INDEXES.get(),
230            INFO_CTRL_PANEL_ACTION_REBUILD_INDEXES.get()
231          },
232          {
233            INFO_CTRL_PANEL_CATEGORY_MONITORING.get(),
234            INFO_CTRL_PANEL_BROWSE_GENERAL_MONITORING.get(),
235            INFO_CTRL_PANEL_CONNECTION_HANDLER_MONITORING.get(),
236            INFO_CTRL_PANEL_MANAGE_TASKS.get()
237          },
238          {
239            INFO_CTRL_PANEL_CATEGORY_RUNTIME_OPTIONS.get(),
240            INFO_CTRL_PANEL_ACTION_JAVA_SETTINGS.get()
241          }
242      };
243    }
244    ArrayList<Class<? extends StatusGenericPanel>> classes = new ArrayList<>();
245    classes.add(BrowseEntriesPanel.class);
246    classes.add(NewBaseDNPanel.class);
247    classes.add(ImportLDIFPanel.class);
248    classes.add(ExportLDIFPanel.class);
249    classes.add(BackupPanel.class);
250    classes.add(RestorePanel.class);
251    classes.add(BrowseSchemaPanel.class);
252    classes.add(BrowseIndexPanel.class);
253    classes.add(VerifyIndexPanel.class);
254    classes.add(RebuildIndexPanel.class);
255    classes.add(BrowseGeneralMonitoringPanel.class);
256    classes.add(ConnectionHandlerMonitoringPanel.class);
257    classes.add(ManageTasksPanel.class);
258    classes.add(JavaPropertiesPanel.class);
259    if (isWindows())
260    {
261      classes.add(WindowsServicePanel.class);
262    }
263    int classIndex = 0;
264    for (LocalizableMessage[] label : labels)
265    {
266      Category category = new Category();
267      category.setName(label[0]);
268      for (int j = 1; j < label.length; j++)
269      {
270        Action action = new Action();
271        action.setName(label[j]);
272        action.setAssociatedPanel(classes.get(classIndex));
273        classIndex ++;
274
275        category.getActions().add(action);
276      }
277      categories.add(category);
278    }
279    return categories;
280  }
281
282  /**
283   * This is required because in some desktops we might encounter a case
284   * where several actions are highlighted.
285   * @param actions the actions
286   */
287  private void createActionButtonListeners(
288      final Collection<ActionButton> actions)
289  {
290    ActionListener actionListener = new ActionListener()
291    {
292      @Override
293      public void actionPerformed(ActionEvent ev)
294      {
295        for (ActionButton button : actions)
296        {
297          if (ev.getSource() == button)
298          {
299            button.actionPerformed(ev);
300            break;
301          }
302        }
303      }
304    };
305
306    MouseAdapter mouseListener = new MouseAdapter()
307    {
308      @Override
309      public void mousePressed(MouseEvent ev)
310      {
311        for (ActionButton button : actions)
312        {
313          if (ev.getSource() == button)
314          {
315            button.mousePressed(ev);
316            break;
317          }
318        }
319      }
320
321      @Override
322      public void mouseReleased(MouseEvent ev)
323      {
324        for (ActionButton button : actions)
325        {
326          if (ev.getSource() == button)
327          {
328            button.mouseReleased(ev);
329            break;
330          }
331        }
332      }
333
334      @Override
335      public void mouseExited(MouseEvent ev)
336      {
337        for (ActionButton button : actions)
338        {
339          if (ev.getSource() == button)
340          {
341            button.mouseExited(ev);
342            break;
343          }
344        }
345      }
346
347      @Override
348      public void mouseEntered(MouseEvent ev)
349      {
350        for (ActionButton button : actions)
351        {
352          if (ev.getSource() == button)
353          {
354            button.mouseEntered(ev);
355          }
356          else
357          {
358            button.mouseExited(ev);
359          }
360        }
361      }
362    };
363
364    for (ActionButton button : actions)
365    {
366      button.addActionListener(actionListener);
367      button.addMouseListener(mouseListener);
368    }
369  }
370
371  @Override
372  public LocalizableMessage getTitle()
373  {
374    return null;
375  }
376
377  @Override
378  public void configurationChanged(ConfigurationChangeEvent ev)
379  {
380  }
381
382  @Override
383  public void okClicked()
384  {
385  }
386}