1   /*
2    * JScroll - the scrollable desktop pane for Java.
3    * Copyright (C) 2003 Tom Tessier
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   *
19   */
20  
21  package org.jscroll.widgets;
22  
23  import javax.swing.*;
24  
25  import java.awt.event.*;
26  
27  import java.util.*;
28  
29  
30  /***
31   * This class provides the optional "Window" menu for the scrollable desktop.
32   *
33   * @author <a href="mailto:tessier@gabinternet.com">Tom Tessier</a>
34   * @version 1.0  11-Aug-2001
35   */
36  public class DesktopMenu extends JMenu implements ActionListener {
37      private DesktopMediator desktopMediator;
38      private boolean tileMode;
39      private int baseItemsEndIndex;
40      private ButtonGroup frameRadioButtonMenuItemGroup;
41  
42      /***
43       * creates the DesktopMenu object
44       *
45       * @param desktopMediator a reference to the DesktopMediator object
46       */
47      public DesktopMenu(DesktopMediator desktopMediator) {
48          this(desktopMediator, false);
49      }
50  
51      /***
52       * creates the DesktopMenu object with the specified tileMode
53       *
54       * @param desktopMediator a reference to the DesktopMediator object
55       * @param tileMode the tile mode to use (<code>true</code> = tile
56       *      internal frames, <code>false</code> = cascade internal frames)
57       */
58      public DesktopMenu(DesktopMediator desktopMediator, boolean tileMode) {
59          super("Window");
60          setMnemonic(KeyEvent.VK_W);
61  
62          this.desktopMediator = desktopMediator;
63          this.tileMode = tileMode;
64  
65          frameRadioButtonMenuItemGroup = new ButtonGroup();
66  
67          new ConstructWindowMenu(this, desktopMediator, tileMode);
68  
69          // set the default item count (ie: number of items comprising
70          // current menu contents)
71          baseItemsEndIndex = getItemCount();
72      }
73  
74      /***
75       * adds a
76       * {@link org.jscroll.widgets.RootRadioButtonMenuItem
77       * RootRadioButtonMenuItem} to the menu and associates it with
78       * an internal frame
79       *
80       * @param associatedFrame the internal frame to associate with
81       *         the menu item
82       */
83      public void add(JScrollInternalFrame associatedFrame) {
84          int displayedCount = getItemCount() - baseItemsEndIndex + 1;
85          int currentMenuCount = displayedCount;
86  
87          // compute the key mnemonic based upon the currentMenuCount
88          if (currentMenuCount > 9) {
89              currentMenuCount /= 10;
90          }
91  
92          RootRadioButtonMenuItem menuButton = new RootRadioButtonMenuItem(this,
93                  displayedCount + " " + associatedFrame.getTitle(),
94                  KeyEvent.VK_0 + currentMenuCount, -1, true, associatedFrame);
95  
96          associatedFrame.setAssociatedMenuButton(menuButton);
97  
98          add(menuButton);
99          frameRadioButtonMenuItemGroup.add(menuButton);
100 
101         menuButton.setSelected(true); // and reselect here, so that the 
102                                       // buttongroup recognizes the change
103     }
104 
105     /***
106      * removes the specified radio menu button from the menu
107      *
108      * @param menuButton the JRadioButtonMenuItem to remove
109      */
110     public void remove(JRadioButtonMenuItem menuButton) {
111         frameRadioButtonMenuItemGroup.remove(menuButton);
112         super.remove(menuButton);
113 
114         // cannot simply remove the radio menu button, as need to 
115         // renumber the keyboard shortcut keys as well. Hence, a 
116         // call to refreshMenu is in order...
117         refreshMenu(); // refresh the mnemonics associated 
118                        // with the other items
119     }
120 
121     /***
122      * refreshes a given menu item
123      */
124     private void refreshMenu() {
125         // refresh the associated mnemonics, so that the keyboard shortcut 
126         // keys are properly renumbered...
127         // get an enumeration to the elements of the current button group
128         Enumeration e = frameRadioButtonMenuItemGroup.getElements();
129 
130         int displayedCount = 1;
131         int currentMenuCount = 0;
132 
133         while (e.hasMoreElements()) {
134             RootRadioButtonMenuItem b = (RootRadioButtonMenuItem) e.nextElement();
135 
136             // compute the key mnemonic based upon the currentMenuCount
137             currentMenuCount = displayedCount;
138 
139             if (currentMenuCount > 9) {
140                 currentMenuCount /= 10;
141             }
142 
143             b.setMnemonic(KeyEvent.VK_0 + currentMenuCount);
144             b.setText(displayedCount + b.getAssociatedFrame().getTitle());
145             displayedCount++;
146         }
147     }
148 
149     /***
150      * propogates the actionPerformed menu event to DesktopMediator
151      *
152      * @param e the ActionEvent to propogate
153      */
154     public void actionPerformed(ActionEvent e) {
155         desktopMediator.actionPerformed(e);
156     }
157 }
This page was automatically generated by Maven