KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > swing > ActionToolBar


1 package com.sshtools.ui.swing;
2
3 import java.awt.Component JavaDoc;
4 import java.awt.Dimension JavaDoc;
5 import java.awt.Insets JavaDoc;
6 import java.awt.event.ActionEvent JavaDoc;
7 import java.awt.event.ActionListener JavaDoc;
8 import java.awt.event.MouseAdapter JavaDoc;
9 import java.awt.event.MouseEvent JavaDoc;
10 import javax.swing.AbstractButton JavaDoc;
11 import javax.swing.Action JavaDoc;
12 import javax.swing.JButton JavaDoc;
13 import javax.swing.JPopupMenu JavaDoc;
14 import javax.swing.JToolBar JavaDoc;
15 import javax.swing.SwingConstants JavaDoc;
16
17 public class ActionToolBar extends JToolBar JavaDoc {
18   private ExpandToolBarToggleButton button;
19
20   private int buttonIndex;
21
22   /**
23    *
24    */

25   public ActionToolBar() {
26     super();
27     init();
28   }
29
30   /**
31    * @param orientation
32    */

33   public ActionToolBar(int orientation) {
34     super(orientation);
35     init();
36   }
37
38   /**
39    * @param name
40    */

41   public ActionToolBar(String JavaDoc name) {
42     super(name);
43     init();
44   }
45
46   /**
47    * @param name
48    * @param orientation
49    */

50   public ActionToolBar(String JavaDoc name, int orientation) {
51     super(name, orientation);
52     init();
53   }
54
55   public void setWrap(boolean wrap) {
56     boolean oldWrap = isWrap();
57     if (wrap != oldWrap) {
58       ((ActionToolBarLayout) getLayout()).setWrap(wrap);
59       checkButton();
60     }
61   }
62
63   public boolean isWrap() {
64     return getLayout() instanceof ActionToolBarLayout && ((ActionToolBarLayout) getLayout()).isWrap();
65   }
66
67   private void init() {
68     ActionToolBarLayout layout = new ActionToolBarLayout();
69     setLayout(layout);
70     checkButton();
71     setWrap(false);
72   }
73
74   private void checkButton() {
75     invalidate();
76     if(getLayout() instanceof ActionToolBarLayout) { // Hack to cope with L&F changes
77
if (button != null) {
78         remove(button);
79         ((ActionToolBarLayout) getLayout()).setExpandComponent(null);
80         buttonIndex = -1;
81       }
82       if (!isWrap()) {
83         button = new ExpandToolBarToggleButton();
84         button.setFocusPainted(false);
85         ((ActionToolBarLayout) getLayout()).setExpandComponent(button);
86         add(button);
87         buttonIndex = getComponentIndex(button);
88       }
89     }
90     validate();
91     repaint();
92   }
93   
94   class ExpandToolBarToggleButton extends JButton JavaDoc implements ActionListener JavaDoc {
95     JPopupMenu JavaDoc popup;
96
97     ExpandToolBarToggleButton() {
98       super(new ArrowIcon(SwingConstants.SOUTH));
99       popup = new JPopupMenu JavaDoc();
100       addActionListener(this);
101       addMouseListener(new MouseAdapter JavaDoc() {
102
103         public void mouseEntered(MouseEvent JavaDoc e) {
104             if(isEnabled()) {
105                 setBorderPainted(true);
106                 setContentAreaFilled(true);
107             }
108         }
109
110         public void mouseExited(MouseEvent JavaDoc e) {
111             setBorderPainted(false);
112             setContentAreaFilled(false);
113         }
114     });
115     setBorderPainted(false);
116     setContentAreaFilled(false);
117     }
118
119     public Insets JavaDoc getMargin() {
120         return new Insets JavaDoc(0, 0, 0, 0);
121     }
122
123     public boolean isRequestFocusEnabled() {
124         return false;
125     }
126
127     public boolean isFocusTraversable() {
128         return false;
129     }
130
131     public void actionPerformed(ActionEvent JavaDoc evt) {
132       int overun = ((ActionToolBarLayout) ActionToolBar.this.getLayout())
133           .getOverunIndex();
134       if (overun != -1) {
135         popup.invalidate();
136         popup.removeAll();
137         int count = ActionToolBar.this.getComponentCount();
138         Component JavaDoc c;
139         for (int i = overun; i < count; i++) {
140           c = ActionToolBar.this.getComponent(i);
141           if (c != this) {
142             if (c instanceof ToolBarSeparator) {
143               popup.addSeparator();
144             } else {
145               AbstractButton JavaDoc button = (AbstractButton JavaDoc) c;
146               Action JavaDoc appAction = (Action JavaDoc) button.getAction();
147               if (appAction != null) {
148                 popup.add(appAction);
149               }
150             }
151           }
152         }
153         popup.show(this, getSize().width - popup.getPreferredSize().width,
154             getSize().height);
155       }
156     }
157   }
158   
159   public Dimension JavaDoc getMinimumSize() {
160       return new Dimension JavaDoc(1, super.getMinimumSize().height);
161   }
162
163   /*
164    * (non-Javadoc)
165    *
166    * @see java.awt.Container#remove(java.awt.Component)
167    */

168   public void remove(Component JavaDoc comp) {
169     if (comp != button) {
170       super.remove(comp);
171     }
172   }
173
174   /*
175    * (non-Javadoc)
176    *
177    * @see java.awt.Container#remove(int)
178    */

179   public void remove(int index) {
180     if (index != buttonIndex) {
181       super.remove(index);
182     }
183   }
184
185   /*
186    * (non-Javadoc)
187    *
188    * @see java.awt.Container#removeAll()
189    */

190   public void removeAll() {
191     super.removeAll();
192     checkButton();
193   }
194 }
Popular Tags