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