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