1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.  Oracle designates this
  10  * particular file as subject to the "Classpath" exception as provided
  11  * by Oracle in the LICENSE file that accompanied this code.
  12  *
  13  * This code is distributed in the hope that it will be useful, but WITHOUT
  14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16  * version 2 for more details (a copy is included in the LICENSE file that
  17  * accompanied this code).
  18  *
  19  * You should have received a copy of the GNU General Public License version
  20  * 2 along with this work; if not, write to the Free Software Foundation,
  21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22  *
  23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  24  * or visit www.oracle.com if you need additional information or have any
  25  * questions.
  26  */
  27 package com.sun.javatest.exec;
  28 
  29 import java.awt.event.ActionEvent;
  30 import java.util.Collections;
  31 import java.util.LinkedHashMap;
  32 import java.util.Map;
  33 
  34 import javax.swing.AbstractAction;
  35 import javax.swing.Action;
  36 import javax.swing.JCheckBoxMenuItem;
  37 import javax.swing.JMenu;
  38 import javax.swing.JMenuItem;
  39 import javax.swing.SwingUtilities;
  40 
  41 import com.sun.javatest.tool.UIFactory;
  42 
  43 /**
  44  * The "Tool Bar Manager" tool, which allows a user to add, remove,
  45  * and get custom toolbars.
  46  */
  47 public class ToolBarManager {
  48     public ToolBarManager() {
  49     }
  50 
  51     /**
  52      * Adds specified toolbar.
  53      * @param theBar the tool bar which should be added to the TestManager,
  54      * can't be null
  55      * @param toolBarID unique string ID of theBar, can't be null or empty
  56      * @return <tt>true</tt> if the bar was successfully added
  57      **/
  58     public boolean addToolBar(final JavaTestToolBar theBar, String toolBarID) {
  59         if (!store.containsKey(toolBarID)) {
  60             store.put(toolBarID, theBar);
  61 
  62             if (panel != null) {
  63                 SwingUtilities.invokeLater(new Runnable() {
  64                     public void run() {
  65                         panel.add(theBar);
  66                         mmanager.addToolbar(theBar);
  67                     }
  68                 });
  69             }
  70             else {
  71                 // will occur in setPanel()
  72             }
  73 
  74             return true;
  75         }
  76         return false;
  77     }
  78 
  79     /**
  80      * Finds toolbar by ID and returns it.
  81      * @param toolBarID unique string ID of theBar, can't be null or empty
  82      * @return the toolbar object, or <tt>null</tt> if the manager contains
  83      * no tool bar for this key.
  84      **/
  85     public JavaTestToolBar getToolBar(String toolBarID) {
  86         return (JavaTestToolBar) store.get(toolBarID);
  87     }
  88 
  89     /**
  90      * Returns an array of currently registered toolbars.
  91      * @return array of currently registered toolbars
  92      **/
  93     public JavaTestToolBar[] getToolBars() {
  94         JavaTestToolBar[] ret = new JavaTestToolBar[0];
  95         ret = (JavaTestToolBar[]) store.values().toArray(ret);
  96         return ret;
  97     }
  98 
  99     /**
 100      * Removes the toolbar by ID.
 101      * @param toolBarID unique string ID of theBar, can't be null or empty
 102      * @return <tt>true</tt> if the bar was successfully removed and <tt>false</tt>
 103      * if the specified ID was not be found.
 104      **/
 105     boolean removeToolBar(String toolBarID) {
 106         if (store.containsKey(toolBarID)) {
 107             final JavaTestToolBar tb = (JavaTestToolBar) store.get(toolBarID);
 108 
 109             if (panel != null)
 110                 SwingUtilities.invokeLater(new Runnable() {
 111                     public void run() {
 112                         panel.remove(tb);
 113                         mmanager.removeToolbar(tb);
 114                     }
 115                 });
 116             else {
 117                 mmanager.removeToolbar(tb);
 118             }
 119 
 120             store.remove(toolBarID);
 121             return true;
 122         }
 123         return false;
 124     }
 125 
 126     /**
 127      * Accessor for the toolbar submenu in View menu
 128      **/
 129     JMenu getToolbarMenu() {
 130         if (toolBarMenu == null) {
 131             toolBarMenu = uif.createMenu("tbmanager.viewmenu");
 132             toolBarMenu.setVisible(false);
 133         }
 134         return toolBarMenu;
 135     }
 136 
 137     /**
 138      * Saves visible state.
 139      * Invoked from ExecTool's save
 140      **/
 141     void save(Map<String, Object> m) {
 142         if (m != null) {
 143             for (JavaTestToolBar tb : store.values()) {
 144                 tb.save(m);
 145             }
 146         }
 147     }
 148 
 149     void load(Map<String, Object> m) {
 150         if (m != null) {
 151             for (JavaTestToolBar tb : store.values()) {
 152                 tb.load(m);
 153             }
 154         }
 155     }
 156 
 157     void setUIFactory(UIFactory u) {
 158         uif = u;
 159     }
 160 
 161     void setPanel(ToolBarPanel p) {
 162         if (p == null)
 163             throw new NullPointerException();
 164 
 165         panel = p;
 166 
 167         if (store.size() > 0) {
 168             JavaTestToolBar[] tbs = getToolBars();
 169             for (int i = 0; i < tbs.length; i++) {
 170                 panel.add(tbs[i]);
 171                 mmanager.addToolbar(tbs[i]);
 172             }   // for
 173         }
 174     }
 175 
 176     void setVisibleFromPrefs(boolean visible) {
 177         int count = toolBarMenu.getItemCount();
 178         for (int i = 0; i < count; i++) {
 179             JCheckBoxMenuItem mi = (JCheckBoxMenuItem)toolBarMenu.getItem(i);
 180             if (mi != null) {
 181                 ToolbarMenuAction a = (ToolbarMenuAction)mi.getAction();
 182                 JavaTestToolBar bar = a.getBar();
 183                 if (!visible) {
 184                     bar.setVisibleNoStateAffect(visible);
 185                 }
 186                 else {
 187                     bar.setVisibleNoStateAffect(bar.readVisibleState());
 188                 }
 189                 mi.setState(bar.isVisible());
 190             }
 191         }
 192     }
 193 
 194     /**
 195      * TBMenuManager manages toolbar menu and processes adding/removing toolbars
 196      **/
 197     class TBMenuManager {
 198         /**
 199          * Process adding toolbar to the panel.
 200          **/
 201         public void addToolbar(JavaTestToolBar t) {
 202             if (t.isMenuControlled()) {
 203                 JMenuItem m = getToolbarMenu();
 204                 ToolbarMenuAction ac = new ToolbarMenuAction(t);
 205                 JCheckBoxMenuItem mi = new JCheckBoxMenuItem(ac);
 206                 mi.setState(t.isVisible());
 207                 m.add(mi);
 208                 toolBarMenu.setVisible(true);
 209             }
 210         }
 211         /**
 212          * Process removing toolbar from the panel.
 213          **/
 214         public void removeToolbar(JavaTestToolBar t) {
 215             if (t.isMenuControlled()) {
 216                 JMenu m = getToolbarMenu();
 217                 for (int i = 0; i < m.getItemCount() ; i++) {
 218                     JMenuItem it = m.getItem(i);
 219                     Action act = it.getAction();
 220                     if (act instanceof ToolbarMenuAction) {
 221                         if (((ToolbarMenuAction) act).getBar() == t) {
 222                             m.remove(i);
 223                             break;
 224                         }
 225                     }
 226                 }
 227                 if (m.getItemCount() == 0) {
 228                     m.setVisible(false);
 229                 }
 230             }
 231         }
 232 
 233     }
 234 
 235     /**
 236      *ToolbarMenuAction reflects changing of visible state to the menu
 237      **/
 238     class ToolbarMenuAction extends AbstractAction {
 239         public JavaTestToolBar getBar() {
 240             return theBar;
 241         }
 242         public ToolbarMenuAction(JavaTestToolBar bar) {
 243             super(bar.getName());
 244             theBar = bar;
 245         }
 246         public void actionPerformed(ActionEvent evt) {
 247             theBar.setVisible(!theBar.isVisible());
 248         }
 249         private JavaTestToolBar theBar;
 250     }
 251 
 252     private UIFactory uif;
 253     private JMenu toolBarMenu;
 254     private TBMenuManager mmanager = new TBMenuManager();
 255     private Map<String, JavaTestToolBar> store =
 256         Collections.synchronizedMap(new LinkedHashMap<String, JavaTestToolBar>());
 257     private ToolBarPanel panel;
 258 
 259 }