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-2010 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.*;
021
022import java.awt.event.ActionEvent;
023import java.awt.event.ActionListener;
024import java.awt.event.KeyEvent;
025
026import javax.swing.JMenu;
027import javax.swing.JMenuBar;
028import javax.swing.JMenuItem;
029
030import org.opends.guitools.controlpanel.datamodel.ControlPanelInfo;
031import org.opends.guitools.controlpanel.util.BackgroundTask;
032import org.opends.guitools.controlpanel.util.Utilities;
033import org.opends.quicksetup.ui.WebBrowserErrorDialog;
034import org.opends.quicksetup.util.Utils;
035import org.opends.quicksetup.util.WebBrowserException;
036import org.opends.quicksetup.util.WebBrowserLauncher;
037import org.opends.server.util.DynamicConstants;
038
039/**
040 * An abstract class that the different menu bars in the Control Panel extend.
041 *
042 */
043
044public abstract class GenericMenuBar extends JMenuBar
045{
046  private static final long serialVersionUID = -7289801307628271507L;
047
048  private ControlPanelInfo info;
049
050  /**
051   * The URL to the administration guide.
052   */
053  protected final String ADMINISTRATION_GUIDE_URL =
054    Utils.getCustomizedObject("ADMINISTRATION_GUIDE_URL",
055        DynamicConstants.ADMINISTRATION_GUIDE_URL, String.class);
056
057  /**
058   * The URL to the wiki main page.
059   */
060  protected final String DOC_REFERENCE_WIKI =
061    Utils.getCustomizedObject("DOC_REFERENCE_WIKI",
062        DynamicConstants.DOC_REFERENCE_WIKI, String.class);
063
064  /**
065   * Constructor of the menu bar.
066   * @param info the control panel information.
067   */
068  protected GenericMenuBar(ControlPanelInfo info)
069  {
070    this.info = info;
071  }
072
073  /**
074   * Returns the control panel information.
075   * @return the control panel information.
076   */
077  public ControlPanelInfo getInfo()
078  {
079    return info;
080  }
081
082  /**
083   * Creates the Help menu bar.
084   * @return the Help menu bar.
085   */
086  protected JMenu createHelpMenuBar()
087  {
088    JMenu menu = Utilities.createMenu(INFO_CTRL_PANEL_HELP_MENU.get(),
089        INFO_CTRL_PANEL_HELP_MENU_DESCRIPTION.get());
090    menu.setMnemonic(KeyEvent.VK_H);
091    JMenuItem menuItem = Utilities.createMenuItem(
092        INFO_CTRL_PANEL_ADMINISTRATION_GUIDE_MENU.get());
093    menuItem.addActionListener(new ActionListener()
094    {
095      public void actionPerformed(ActionEvent ev)
096      {
097        displayURL(ADMINISTRATION_GUIDE_URL);
098      }
099    });
100    menu.add(menuItem);
101    menuItem = Utilities.createMenuItem(
102        INFO_CTRL_PANEL_DOCUMENTATION_WIKI_MENU.get());
103    menuItem.addActionListener(new ActionListener()
104    {
105      public void actionPerformed(ActionEvent ev)
106      {
107        displayURL(DOC_REFERENCE_WIKI);
108      }
109    });
110    menu.add(menuItem);
111    return menu;
112  }
113
114  /**
115   * Tries to display a URL in the systems default WEB browser.
116   * @param url the URL to be displayed.
117   */
118  protected void displayURL(final String url)
119  {
120    BackgroundTask<Void> worker = new BackgroundTask<Void>()
121    {
122      /** {@inheritDoc} */
123      public Void processBackgroundTask() throws WebBrowserException
124      {
125        try
126        {
127          WebBrowserLauncher.openURL(url);
128          return null;
129        } catch (Throwable t)
130        {
131          throw new WebBrowserException(url,
132              ERR_CTRL_PANEL_UNEXPECTED_DETAILS.get(t), t);
133        }
134      }
135
136      /** {@inheritDoc} */
137      public void backgroundTaskCompleted(Void returnValue, Throwable throwable)
138      {
139        WebBrowserException ex = (WebBrowserException) throwable;
140        if (ex != null)
141        {
142          WebBrowserErrorDialog dlg = new WebBrowserErrorDialog(
143              Utilities.getFrame(GenericMenuBar.this), ex);
144          Utilities.centerGoldenMean(dlg,
145              Utilities.getParentDialog(GenericMenuBar.this));
146          dlg.setModal(true);
147          dlg.packAndShow();
148        }
149      }
150    };
151    worker.startBackgroundTask();
152  }
153}