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