Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/virtuosotechnologies/lib/basiccommand/builder/MenuBuilderNode.java


1   /*
2   ================================================================================
3   
4     FILE:  MenuBuilderNode.java
5     
6     PROJECT:
7     
8       Virtuoso Utilities
9     
10    CONTENTS:
11    
12      A builder that creates a menu or submenu
13    
14    PROGRAMMERS:
15    
16      Daniel Azuma (DA)  <dazuma@kagi.com>
17    
18    COPYRIGHT:
19    
20      Copyright (C) 2003  Daniel Azuma  (dazuma@kagi.com)
21      
22      This program is free software; you can redistribute it and/or
23      modify it under the terms of the GNU General Public License as
24      published by the Free Software Foundation; either version 2
25      of the License, or (at your option) any later version.
26      
27      This program is distributed in the hope that it will be useful,
28      but WITHOUT ANY WARRANTY; without even the implied warranty of
29      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30      GNU General Public License for more details.
31      
32      You should have received a copy of the GNU General Public
33      License along with this program; if not, write to
34        Free Software Foundation, Inc.
35        59 Temple Place, Suite 330
36        Boston, MA 02111-1307 USA
37  
38  ================================================================================
39  */
40  
41  
42  package com.virtuosotechnologies.lib.basiccommand.builder;
43  
44  
45  import javax.swing.JMenu;
46  import javax.swing.JSeparator;
47  import java.awt.Component;
48  
49  import com.virtuosotechnologies.lib.command.CommandNode;
50  import com.virtuosotechnologies.lib.command.CommandNodeFlavor;
51  import com.virtuosotechnologies.lib.basiccommand.BasicCommandNode;
52  
53  
54  /**
55   * A builder that creates a menu or submenu
56   */
57  public class MenuBuilderNode
58  extends AbstractAWTContainerBuilderNode
59  {
60    private boolean isHiddenBecauseEmpty_ = false;
61    private SwingButtonUpdater updater_;
62    
63    
64    /**
65     * Public constructor used to make standalone JMenus
66     */
67    public MenuBuilderNode(
68      CommandNode commandNode)
69    {
70      super(commandNode, null, END_POSITION);
71      checkEmptiness();
72    }
73    
74    
75    /**
76     * Public constructor used to make standalone JMenus
77     */
78    public MenuBuilderNode(
79      CommandNode commandNode,
80      JMenu menu)
81    {
82      super(commandNode, null, END_POSITION, menu);
83      checkEmptiness();
84    }
85    
86    
87    /**
88     * Internal constructor used to make menubar menus and submenus
89     */
90    protected MenuBuilderNode(
91      CommandNode commandNode,
92      AbstractBranchBuilderNode parent,
93      int index)
94    {
95      super(commandNode, parent, index);
96      checkEmptiness();
97    }
98    
99    
100   private void checkEmptiness()
101   {
102     boolean shouldBeHidden = true;
103     JMenu menu = (JMenu)getElement();
104     for (int i=menu.getMenuComponentCount()-1; i>=0; --i)
105     {
106       Component comp = menu.getMenuComponent(i);
107       if (!(comp instanceof JSeparator))
108       {
109         shouldBeHidden = false;
110         break;
111       }
112     }
113     if (shouldBeHidden != isHiddenBecauseEmpty_)
114     {
115       isHiddenBecauseEmpty_ = shouldBeHidden;
116       if (isHiddenBecauseEmpty_)
117       {
118         incMaskLevel();
119       }
120       else
121       {
122         decMaskLevel();
123       }
124     }
125   }
126   
127   
128   /**
129    * Add indexed element.
130    * Overridden so that we hide ourself when the menu is empty.
131    */
132   protected void addElementAt(
133     int pos,
134     Object element)
135   {
136     super.addElementAt(pos, element);
137     if (isHiddenBecauseEmpty_ && !(element instanceof JSeparator))
138     {
139       isHiddenBecauseEmpty_ = false;
140       decMaskLevel();
141     }
142   }
143   
144   
145   /**
146    * Remove indexed element.
147    * Overridden so that we hide ourself when the menu is empty.
148    */
149   protected void removeElementAt(
150     int pos)
151   {
152     super.removeElementAt(pos);
153     if (!isHiddenBecauseEmpty_)
154     {
155       checkEmptiness();
156     }
157   }
158   
159   
160   /**
161    * Remove all elements.
162    * Overridden so that we hide ourself when the menu is empty.
163    */
164   protected void removeAllElements()
165   {
166     super.removeAllElements();
167     if (!isHiddenBecauseEmpty_)
168     {
169       isHiddenBecauseEmpty_ = true;
170       incMaskLevel();
171     }
172   }
173   
174   
175   /**
176    * Override this method to create the initial element object.
177    */
178   protected Object createInitialElement()
179   {
180     JMenu menu = new JMenu();
181     updater_ = new SwingButtonUpdater(menu);
182     updater_.updateAll(getCommandNode());
183     return menu;
184   }
185   
186   
187   /**
188    * Create a child node
189    */
190   protected AbstractBuilderNode createChildNode(
191     CommandNode cn,
192     int index)
193   {
194     AbstractBuilderNode child = null;
195     CommandNodeFlavor flavor = cn.getFlavor();
196     if (flavor.equalsOrExtends(BasicCommandNode.SEPARATOR_FLAVOR))
197     {
198       child = new MenuSeparatorBuilderNode(cn, this, index);
199     }
200     else if (flavor.equalsOrExtends(BasicCommandNode.ACTIONITEM_FLAVOR))
201     {
202       child = new MenuItemBuilderNode(cn, this, index);
203     }
204     else if (flavor.equalsOrExtends(BasicCommandNode.APPEARANCECHANGING_TOGGLEITEM_FLAVOR))
205     {
206       child = new MenuToggleItemBuilderNode(cn, this, index);
207     }
208     else if (flavor.equalsOrExtends(BasicCommandNode.TOGGLEITEM_FLAVOR))
209     {
210       child = new MenuCheckBoxItemBuilderNode(cn, this, index);
211     }
212     else if (flavor.equalsOrExtends(BasicCommandNode.RADIOGROUP_FLAVOR))
213     {
214       child = new MenuRadioGroupBuilderNode(cn, this, index);
215     }
216     else if (flavor.equalsOrExtends(BasicCommandNode.GROUP_FLAVOR))
217     {
218       child = new MenuGroupBuilderNode(cn, this, index);
219     }
220     else if (flavor.equalsOrExtends(BasicCommandNode.RADIOCONTAINER_FLAVOR))
221     {
222       child = new MenuRadioMenuBuilderNode(cn, this, index);
223     }
224     else if (flavor.equalsOrExtends(BasicCommandNode.CONTAINER_FLAVOR))
225     {
226       child = new MenuBuilderNode(cn, this, index);
227     }
228     return child;
229   }
230   
231   
232   /**
233    * Get the built menu
234    */
235   public JMenu getJMenu()
236   {
237     return (JMenu)getElement();
238   }
239 }