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

Quick Search    Search Deep

Source code: com/memoire/bu/BuActionRemover.java


1   /**
2    * @modification $Date: 2001/12/03 16:28:06 $
3    * @statut       unstable
4    * @file         BuActionRemover.java
5    * @version      0.36
6    * @author       Guillaume Desnoix
7    * @email        guillaume@desnoix.com
8    * @license      GNU General Public License 2 (GPL2)
9    * @copyright    1998-2001 Guillaume Desnoix
10   */
11  
12  package com.memoire.bu;
13  
14  import com.memoire.bu.*;
15  import com.memoire.dnd.*;
16  import com.memoire.fu.*;
17  import com.memoire.re.*;
18  
19  
20  import java.awt.*;
21  import java.awt.event.*;
22  import javax.swing.*;
23  
24  /**
25   * An utility class to remove controls by their command name.
26   * The command name is a string defined for each control
27   * using setActionCommand().
28   */
29  
30  public class BuActionRemover
31  {
32  
33    // Menu
34  
35    public static void removeAction
36      (JMenuBar _bar, String _action)
37    {
38      MenuElement[] c=_bar.getSubElements();
39      for(int i=0; i<c.length; i++)
40        if(c[i] instanceof JMenu)
41    removeAction((JMenu)c[i],_action);
42    }
43  
44    public static void removeAction
45      (JMenu _menu, String _action)
46    {
47      if(_menu.getActionCommand().equals(_action))
48      {
49        Container c=_menu.getParent();
50        c.remove(_menu);
51  
52        // bug of swing ?
53        try
54        {
55    //c.doLayout();
56    //c.validate();
57        }
58        catch(ArrayIndexOutOfBoundsException ex) { }
59      }
60  
61      MenuElement[] c=_menu.getSubElements();
62      for(int i=0; i<c.length; i++)
63      {
64        if(c[i] instanceof JMenu)
65    removeAction((JMenu)c[i],_action);
66        else
67        if(c[i] instanceof JPopupMenu)
68    removeAction((JPopupMenu)c[i],_action);
69        else
70        if(c[i] instanceof JMenuItem)
71    removeAction((JMenuItem)c[i],_action);
72        else
73    System.err.println("??? "+c[i]);
74      }
75    }
76  
77    public static void removeAction
78      (JPopupMenu _menu, String _action)
79    {
80      MenuElement[] c=_menu.getSubElements();
81      for(int i=0; i<c.length; i++)
82      {
83        if(c[i] instanceof JMenu)
84    removeAction((JMenu)c[i],_action);
85        else
86        if(c[i] instanceof JPopupMenu)
87    removeAction((JPopupMenu)c[i],_action);
88        else
89        if(c[i] instanceof JMenuItem)
90    removeAction((JMenuItem)c[i],_action);
91        else
92    System.err.println("??? "+c[i]);
93      }
94    }
95  
96    public static void removeAction
97      (JMenuItem _item, String _action)
98    {
99      if(_item.getActionCommand().equals(_action))
100     {
101       Container c=_item.getParent();
102       c.remove(_item);
103 
104       // bug of swing ?
105       try
106       {
107   c.doLayout();
108   c.validate();
109       }
110       catch(ArrayIndexOutOfBoundsException ex) { }
111     }
112   }
113 
114   // Tool
115 
116   public static void removeAction
117     (JToolBar _bar, String _action)
118   {
119     Component[] c=_bar.getComponents();
120     for(int i=0; i<c.length; i++)
121       if(c[i] instanceof JButton)
122   removeAction((JButton)c[i],_action);
123   }
124 
125   public static void removeAction
126     (JButton _item, String _action)
127   {
128     if(_item.getActionCommand().equals(_action))
129     {
130       Container c=_item.getParent();
131       c.remove(_item);
132 
133       // bug of swing ?
134       try
135       {
136   c.doLayout();
137   c.validate();
138       }
139       catch(ArrayIndexOutOfBoundsException ex) { }
140     }
141   }
142 }