KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > FolderMenu


1 package net.suberic.pooka.gui;
2
3 import javax.swing.*;
4 import java.util.*;
5 import net.suberic.pooka.Pooka;
6 import net.suberic.pooka.FolderInfo;
7 import net.suberic.util.*;
8
9 /**
10  * A Menu that shows the list of all folders available.
11  */

12 public class FolderMenu extends net.suberic.util.gui.ConfigurableMenu {
13
14   Vector folderList;
15   FolderPanel fPanel;
16   
17   //Hashtable oldCommands = new Hashtable();
18

19   /**
20    * This creates a new FolderMenu.
21    */

22   public FolderMenu() {
23   }
24   
25   /**
26    * Overrides ConfigurableMenu.configureComponent().
27    */

28   public void configureComponent(String JavaDoc key, VariableBundle vars) {
29     try {
30       setText(vars.getProperty(key + ".Label"));
31     } catch (MissingResourceException mre) {
32     }
33     
34     this.setActionCommand(vars.getProperty(key + ".Action", "message-move"));
35     
36     fPanel = Pooka.getMainPanel().getFolderPanel();
37     
38     MailTreeNode root = (MailTreeNode)fPanel.getFolderTree().getModel().getRoot();
39     buildMenu(root, this);
40   }
41   
42   /**
43    * This recursively builds the menu
44    */

45   protected void buildMenu(MailTreeNode mtn, JMenu currentMenu)
46   {
47     Enumeration children = mtn.children();
48     while(children.hasMoreElements())
49       {
50     MailTreeNode curNode = (MailTreeNode)children.nextElement();
51     if(curNode.isLeaf())
52       {
53         JMenuItem mi = new FolderMenuItem(curNode.toString(), ((FolderNode)curNode).getFolderInfo());
54         mi.setActionCommand(getActionCommand());
55         currentMenu.add(mi);
56       }
57     else // Create a submenu
58
{
59         JMenu newMenu = new JMenu(curNode.toString());
60         currentMenu.add(newMenu);
61         buildMenu(curNode, newMenu);
62       }
63       }
64     }
65   
66   
67   /**
68    * Recursively sets the child menu items to enabled or disabled as appropriate.
69    * Also sets up their command handlers for the right folder.
70    */

71   protected void setActiveSubMenuItems(JMenu curMenu) {
72     
73     for (int j = 0; j < curMenu.getItemCount(); j++) {
74       if(curMenu.getItem(j) instanceof FolderMenuItem)
75     {
76       JMenuItem mi = curMenu.getItem(j);
77       java.awt.event.ActionListener JavaDoc[] oldActionListeners = mi.getActionListeners();
78       for (int i = 0; i < oldActionListeners.length; i++) {
79         mi.removeActionListener(oldActionListeners[i]);
80       }
81
82       Action a = getAction(getActionCommand());
83       if (a != null) {
84         Action newAction = a;
85         if (a instanceof net.suberic.util.DynamicAbstractAction) {
86           try {
87         newAction = (Action)((net.suberic.util.DynamicAbstractAction)a).cloneDynamicAction();
88           } catch (CloneNotSupportedException JavaDoc cnse) {
89                 // sigh. this is a really bad idea.
90

91         System.out.println("cnse hit.");
92           }
93         } else {
94           System.out.println("action is not a DynamicAbstractAction.");
95         }
96         newAction.putValue("target", ((FolderMenuItem)mi).getFolderInfo());
97         /*
98           Object o = oldCommands.get(mi);
99           if (o != null)
100           mi.removeActionListener((Action)o);
101         */

102         mi.addActionListener(newAction);
103
104         //oldCommands.put(mi, newAction);
105

106         mi.setEnabled(true);
107       } else {
108         mi.setEnabled(false);
109       }
110     }
111       else
112     setActiveSubMenuItems((JMenu)curMenu.getItem(j));
113     }
114   }
115   
116   public void setActiveMenuItems()
117   {
118     setActiveSubMenuItems(this);
119   }
120   
121   /**
122    * Your basic menu item plus room to hang a FolderInfo class.
123    */

124   class FolderMenuItem extends JMenuItem
125   {
126     public FolderMenuItem(String JavaDoc text, FolderInfo fi)
127     {
128       super(text);
129       folderInfo = fi;
130     }
131     
132     public FolderInfo getFolderInfo() { return folderInfo; }
133     
134     private FolderInfo folderInfo;
135   }
136 }
137
138
139
Popular Tags