KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdnc > JNComponent


1 /*
2  * $Id: JNComponent.java,v 1.4 2004/09/01 05:00:33 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.jdnc;
9
10 import org.jdesktop.swing.JXStatusBar;
11 import org.jdesktop.swing.MouseMessagingHandler;
12
13 import org.jdesktop.swing.event.MessageListener;
14 import org.jdesktop.swing.event.MessageSource;
15 import org.jdesktop.swing.event.MessageSourceSupport;
16
17 import java.awt.BorderLayout JavaDoc;
18 import java.awt.Container JavaDoc;
19 import java.awt.Component JavaDoc;
20 import java.awt.Dimension JavaDoc;
21 import java.awt.*;
22 import java.awt.Graphics JavaDoc;
23
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26 import java.awt.event.MouseAdapter JavaDoc;
27
28 import javax.swing.Action JavaDoc;
29 import javax.swing.ActionMap JavaDoc;
30 import javax.swing.Icon JavaDoc;
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.JComponent JavaDoc;
33 import javax.swing.JLabel JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import javax.swing.JPopupMenu JavaDoc;
36 import javax.swing.JRootPane JavaDoc;
37 import javax.swing.JToolBar JavaDoc;
38
39 import org.jdesktop.swing.actions.Targetable;
40
41 /**
42  * Base class for all JN components.
43  *
44  * @author Ramesh Gupta
45  * @author Amy Fowler
46  * @author Mark Davidson
47  *
48  * @javabean.class
49  * displayName="Base JDNC Component"
50  * name="JNComponent"
51  * shortDesctiption="The base component"
52  */

53 public abstract class JNComponent extends JPanel JavaDoc
54     implements Targetable, MessageSource {
55
56     private JComponent JavaDoc component;
57     private JPopupMenu JavaDoc popup;
58
59     private PopupHandler popupHandler;
60     private MouseMessagingHandler messageHandler;
61
62     /**
63      * A reference to the MessageSourceSupport. Use the fire methods on
64      * this class to send messages to the status bar.
65      */

66     protected MessageSourceSupport support;
67
68     /**
69      * Creates a new JNComponent configured with an instance of
70      * {@link java.awt.BorderLayout}.
71      */

72     protected JNComponent() {
73         super(new BorderLayout JavaDoc());
74     }
75
76     /**
77      * Set the component that this JNComponent wraps.
78      *
79      * @javabean.property
80      * shortDescription="Set the component that this JNComponent wraps"
81      * bound="true"
82      */

83     protected void setComponent(JComponent JavaDoc component) {
84         JComponent JavaDoc oldComponent = this.component;
85         this.component = component;
86         component.setFont(getFont());
87         component.setForeground(getForeground());
88         component.setBackground(getBackground());
89
90         // register/unregister
91
if (popupHandler != null) {
92             if (oldComponent != null) {
93                 oldComponent.removeMouseListener(popupHandler);
94             }
95             if (component != null) {
96                 component.addMouseListener(popupHandler);
97             }
98         }
99         firePropertyChange("component", oldComponent, component);
100     }
101
102     public JComponent JavaDoc getComponent() {
103         return component;
104     }
105
106     //
107
// Targetable implementation
108
//
109

110     public boolean doCommand(Object JavaDoc command, Object JavaDoc value) {
111         // Look at the internal component first.
112
ActionMap JavaDoc map = component.getActionMap();
113         Action JavaDoc action = map.get(command);
114
115         if (action == null) {
116             // look in the action map of the JNComponent
117
map = getActionMap();
118             action = map.get(command);
119         }
120
121         if (action != null) {
122             if (value instanceof ActionEvent JavaDoc) {
123                 action.actionPerformed( (ActionEvent JavaDoc) value);
124             }
125             else {
126                 // XXX should the value represent the event source?
127
action.actionPerformed(new ActionEvent JavaDoc(value, 0,
128                     command.toString()));
129             }
130             return true;
131         }
132         return false;
133     }
134
135     public Object JavaDoc[] getCommands() {
136         ActionMap JavaDoc map = component.getActionMap();
137         return map.keys();
138         // TODO: Should Return the keys the current JNComponent as well.
139
}
140
141     public boolean hasCommand(Object JavaDoc command) {
142         Object JavaDoc[] commands = getCommands();
143         for (int i = 0; i < commands.length; i++) {
144             if (commands[i].equals(command)) {
145                 return true;
146             }
147         }
148         return false;
149     }
150
151     //
152
// MessageSource implementation
153
//
154

155     public void addMessageListener(MessageListener l) {
156         if (support == null) {
157             support = new MessageSourceSupport(this);
158         }
159         if (messageHandler == null) {
160             messageHandler = new MouseMessagingHandler(this, support);
161             if (toolBar != null) {
162                 // XXX This may go away when if we dont support component
163
// level toolbars.
164
messageHandler.registerListeners(toolBar.getComponents());
165             }
166             if (popup != null) {
167                 messageHandler.registerListeners(popup.getSubElements());
168             }
169         }
170         support.addMessageListener(l);
171     }
172
173     public void removeMessageListener(MessageListener l) {
174         if (support == null) {
175             return;
176         }
177         support.removeMessageListener(l);
178     }
179
180     public MessageListener[] getMessageListeners() {
181         if (support == null) {
182             support = new MessageSourceSupport(this);
183         }
184         return support.getMessageListeners();
185     }
186
187     /**
188      * Convenience method to send a transient message to the MessageListeners
189      *
190      * @param message text of message to send
191      */

192     protected void sendMessage(String JavaDoc message) {
193         if (support != null) {
194             support.fireMessage(message);
195         }
196     }
197
198     /**
199      * Adds the specified action to the end of the tool bar for this component.
200      * If no tool bar exists, a new tool bar is automatically created and added
201      * to the top of this component.
202      *
203      * @param action the action added to the tool bar
204      * @return the buton that must be clicked to perform the specified action
205      */

206     public JButton JavaDoc addAction(Action JavaDoc action) {
207         if (toolBar == null) {
208             addToolBar();
209         }
210
211         Object JavaDoc delegate = action.getValue("delegate");
212         if (delegate == null) {
213             action.putValue("delegate", this);
214         }
215
216         /*
217              Since JDK 1.3, this is how we should be adding an Action to a JToolBar:
218                  JButton button = new JButton(action);
219                  toolBar.add(button); // shows both icon and title
220          */

221         JButton JavaDoc button = toolBar.add(action); // NOT the same as commented code;
222
// shows only icon; no title
223

224         button.setToolTipText( (String JavaDoc) action.getValue(Action.
225             SHORT_DESCRIPTION));
226         return button;
227     }
228
229     /**
230      * Adds a separator to the end of the tool bar, if any, for this component.
231      * This method does nothing if there is no tool bar for this component.
232      */

233     public void addSeparator() {
234         if (toolBar != null) {
235             toolBar.addSeparator();
236         }
237     }
238
239     public void addToolBarComponent(JComponent JavaDoc component) {
240         if (toolBar == null) {
241             addToolBar();
242         }
243         toolBar.add(component);
244     }
245
246     public void setFont(Font font) {
247         super.setFont(font);
248         if (component != null) {
249             component.setFont(font);
250         }
251     }
252
253     /**
254      * Sets the popup menu for this component.
255      * TODO: if this is not the first time this method is called,
256      * should unregister the previous popup listener.
257      */

258     public void setPopupMenu(JPopupMenu JavaDoc popup) {
259         JPopupMenu JavaDoc oldPopup = this.popup;
260         this.popup = popup;
261
262         if (oldPopup != null) {
263             // Unregister the old popup
264
if (messageHandler != null) {
265                 messageHandler.unregisterListeners(oldPopup.getSubElements());
266             }
267             if (getComponent() != null) {
268                 getComponent().removeMouseListener(popupHandler);
269                 popupHandler = null;
270             }
271         }
272
273         if (popup != null) {
274             // Register the new popup
275
if (messageHandler != null) {
276                 messageHandler.registerListeners(popup.getSubElements());
277             }
278             if (getComponent() != null) {
279                 popupHandler = new PopupHandler(popup);
280                 getComponent().addMouseListener(popupHandler);
281             }
282         }
283     }
284
285     public JPopupMenu JavaDoc getPopupMenu() {
286         return popup;
287     }
288
289     /**
290      * Handles the popup mouse gestures on the component.
291      */

292     private class PopupHandler extends MouseAdapter JavaDoc {
293
294         private JPopupMenu JavaDoc popup;
295
296         public PopupHandler(JPopupMenu JavaDoc popup) {
297             setPopup(popup);
298         }
299
300         public void setPopup(JPopupMenu JavaDoc popup) {
301             this.popup = popup;
302         }
303
304         public void mousePressed(MouseEvent JavaDoc e) {
305             showPopup(e);
306         }
307
308         public void mouseReleased(MouseEvent JavaDoc e) {
309             showPopup(e);
310         }
311
312         private void showPopup(MouseEvent JavaDoc e) {
313             if (e.isPopupTrigger() && popup != null) {
314                 popup.show( (Component JavaDoc) e.getSource(), e.getX(), e.getY());
315             }
316         }
317     }
318
319     /**
320      * Returns the background image for the component.
321      *
322      * @return the background image for the component.
323      */

324     public Icon JavaDoc getBackgroundImage() {
325         return image;
326     }
327
328     /**
329      * Sets the background image for a transparent or translucent component.
330      *
331      * @param image specifies the background image for the component
332      *
333      * @javabean.property
334      * shortDescription="Set the background image for the component"
335      *
336      */

337     public void setBackgroundImage(Icon JavaDoc image) {
338         if (this.image != image) {
339             this.image = image;
340             repaint();
341         }
342     }
343
344     public void paint(Graphics JavaDoc g) {
345         super.paint(g);
346         if (image != null) {
347             Dimension JavaDoc size = this.getSize();
348             int x, y;
349             x = (size.width - image.getIconWidth()) >> 1;
350             y = (size.height - image.getIconHeight()) >> 1;
351             image.paintIcon(this, g, x, y);
352         }
353     }
354
355     protected JToolBar JavaDoc addToolBar() {
356         toolBar = new JToolBar JavaDoc();
357         toolBar.setFloatable(false);
358         add(toolBar, BorderLayout.NORTH);
359         return toolBar;
360     }
361
362     protected JToolBar JavaDoc toolBar = null;
363     protected Icon JavaDoc image = null;
364 }
365
Popular Tags