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