KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > tabcontrol > plaf > TabControlButtonFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.swing.tabcontrol.plaf;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Image JavaDoc;
24 import java.awt.Toolkit JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27 import java.awt.event.FocusEvent JavaDoc;
28 import java.awt.event.MouseEvent JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32 import javax.swing.Action JavaDoc;
33 import javax.swing.Icon JavaDoc;
34 import javax.swing.ImageIcon JavaDoc;
35 import javax.swing.Timer JavaDoc;
36 import javax.swing.ToolTipManager JavaDoc;
37 import org.netbeans.swing.tabcontrol.TabData;
38 import org.netbeans.swing.tabcontrol.TabDisplayer;
39 import org.netbeans.swing.tabcontrol.TabListPopupAction;
40 import org.netbeans.swing.tabcontrol.WinsysInfoForTabbed;
41
42 /**
43  * A factory to create tab control buttons.
44  *
45  * @author S. Aubrecht
46  */

47 class TabControlButtonFactory {
48     
49     private static IconLoader iconCache;
50     
51     private TabControlButtonFactory() {
52     }
53     
54     public static Icon JavaDoc getIcon( String JavaDoc iconPath ) {
55         if( null == iconCache )
56             iconCache = new IconLoader();
57         return iconCache.obtainIcon( iconPath );
58     }
59     
60     /**
61      * Create default close button.
62      */

63     public static TabControlButton createCloseButton( TabDisplayer displayer ) {
64         return new CloseButton( displayer );
65     }
66     
67     /**
68      * Create default auto-hide/pin button. The button changes icons depending
69      * on the state of tab component.
70      */

71     public static TabControlButton createSlidePinButton( TabDisplayer displayer ) {
72         return new SlidePinButton( displayer );
73     }
74     
75     /**
76      * Create default maximize/restore button. The button changes icons depending
77      * on the state of tab component.
78      */

79     public static TabControlButton createMaximizeRestoreButton( TabDisplayer displayer ) {
80         return new MaximizeRestoreButton( displayer );
81     }
82     
83     public static TabControlButton createScrollLeftButton( TabDisplayer displayer, Action JavaDoc scrollAction ) {
84         TabControlButton button = new TimerButton( TabControlButton.ID_SCROLL_LEFT_BUTTON, displayer, scrollAction );
85         button.setToolTipText( java.util.ResourceBundle.getBundle("org/netbeans/swing/tabcontrol/plaf/Bundle").getString("Tip_Scroll_Documents_Left") );
86         return button;
87     }
88     
89     public static TabControlButton createScrollRightButton( TabDisplayer displayer, Action JavaDoc scrollAction ) {
90         TabControlButton button = new TimerButton( TabControlButton.ID_SCROLL_RIGHT_BUTTON, displayer, scrollAction );
91         button.setToolTipText( java.util.ResourceBundle.getBundle("org/netbeans/swing/tabcontrol/plaf/Bundle").getString("Tip_Scroll_Documents_Right") );
92         return button;
93     }
94     
95     public static TabControlButton createDropDownButton( TabDisplayer displayer ) {
96         return new DropDownButton( displayer );
97     }
98     
99     private static class CloseButton extends TabControlButton {
100         
101         public CloseButton( TabDisplayer displayer ) {
102             super( TabControlButton.ID_CLOSE_BUTTON, displayer );
103             setToolTipText( java.util.ResourceBundle.getBundle("org/netbeans/swing/tabcontrol/plaf/Bundle").getString("Tip_Close_Window") );
104         }
105         
106         protected String JavaDoc getTabActionCommand( ActionEvent JavaDoc e ) {
107 // if( (e.getModifiers() & ActionEvent.SHIFT_MASK) > 0 )
108
// return TabDisplayer.COMMAND_CLOSE_ALL;
109
// else if( (e.getModifiers() & ActionEvent.ALT_MASK) > 0 )
110
// return TabDisplayer.COMMAND_CLOSE_ALL_BUT_THIS;
111
return TabDisplayer.COMMAND_CLOSE;
112         }
113     }
114     
115     private static class SlidePinButton extends TabControlButton {
116         
117         public SlidePinButton( TabDisplayer displayer ) {
118             super( displayer );
119             ToolTipManager JavaDoc toolTipManager = ToolTipManager.sharedInstance();
120             toolTipManager.registerComponent( this );
121         }
122         
123         protected String JavaDoc getTabActionCommand( ActionEvent JavaDoc e ) {
124             if( getButtonId() == TabControlButton.ID_PIN_BUTTON )
125                 return TabDisplayer.COMMAND_DISABLE_AUTO_HIDE;
126             return TabDisplayer.COMMAND_ENABLE_AUTO_HIDE;
127         }
128
129         protected int getButtonId() {
130             int retValue = TabControlButton.ID_PIN_BUTTON;
131             Component JavaDoc currentTab = getActiveTab( getTabDisplayer() );
132             if( null != currentTab ) {
133                 WinsysInfoForTabbed winsysInfo = getTabDisplayer().getWinsysInfo();
134                 if( null != winsysInfo ) {
135                     Object JavaDoc orientation = winsysInfo.getOrientation( currentTab );
136                     if( TabDisplayer.ORIENTATION_EAST.equals( orientation ) )
137                         retValue = TabControlButton.ID_SLIDE_RIGHT_BUTTON;
138                     else if( TabDisplayer.ORIENTATION_WEST.equals( orientation ) )
139                         retValue = TabControlButton.ID_SLIDE_LEFT_BUTTON;
140                     else if( TabDisplayer.ORIENTATION_SOUTH.equals( orientation ) )
141                         retValue = TabControlButton.ID_SLIDE_DOWN_BUTTON;
142                 }
143             }
144             
145             return retValue;
146         }
147
148         public String JavaDoc getToolTipText() {
149             if( getButtonId() == TabControlButton.ID_PIN_BUTTON )
150                 return java.util.ResourceBundle.getBundle("org/netbeans/swing/tabcontrol/plaf/Bundle").getString("Tip_Pin");
151             return java.util.ResourceBundle.getBundle("org/netbeans/swing/tabcontrol/plaf/Bundle").getString("Tip_Minimize_Window");
152         }
153     }
154     
155     private static class MaximizeRestoreButton extends TabControlButton {
156         
157         public MaximizeRestoreButton( TabDisplayer displayer ) {
158             super( displayer );
159             ToolTipManager JavaDoc toolTipManager = ToolTipManager.sharedInstance();
160             toolTipManager.registerComponent( this );
161         }
162         
163         protected String JavaDoc getTabActionCommand( ActionEvent JavaDoc e ) {
164             return TabDisplayer.COMMAND_MAXIMIZE;
165         }
166
167         protected int getButtonId() {
168             int retValue = TabControlButton.ID_MAXIMIZE_BUTTON;
169             Component JavaDoc currentTab = getActiveTab( getTabDisplayer() );
170             if( null != currentTab ) {
171                 WinsysInfoForTabbed winsysInfo = getTabDisplayer().getWinsysInfo();
172                 if( null != winsysInfo ) {
173                     if( winsysInfo.inMaximizedMode( currentTab ) ) {
174                         retValue = TabControlButton.ID_RESTORE_BUTTON;
175                     }
176                 }
177             }
178             
179             return retValue;
180         }
181
182         public String JavaDoc getToolTipText() {
183             if( getButtonId() == TabControlButton.ID_MAXIMIZE_BUTTON )
184                 return java.util.ResourceBundle.getBundle("org/netbeans/swing/tabcontrol/plaf/Bundle").getString("Tip_Maximize_Window");
185             return java.util.ResourceBundle.getBundle("org/netbeans/swing/tabcontrol/plaf/Bundle").getString("Tip_Restore_Window");
186         }
187     }
188
189     private static Component JavaDoc getActiveTab( TabDisplayer displayer ) {
190         Component JavaDoc res = null;
191         int selIndex = displayer.getSelectionModel().getSelectedIndex();
192         if( selIndex >= 0 ) {
193             TabData tab = displayer.getModel().getTab( selIndex );
194             res = tab.getComponent();
195         }
196         return res;
197     }
198
199
200     /**
201      * A convenience button class which will continue re-firing its action
202      * on a timer for as long as the button is depressed. Used for left-right scroll
203      * buttons.
204      */

205     private static class TimerButton extends TabControlButton implements ActionListener JavaDoc {
206         Timer JavaDoc timer = null;
207
208         public TimerButton( int buttonId, TabDisplayer displayer, Action JavaDoc a ) {
209             super( buttonId, displayer );
210             setAction( a );
211         }
212
213         private Timer JavaDoc getTimer() {
214             if (timer == null) {
215                 timer = new Timer JavaDoc(400, this);
216                 timer.setRepeats(true);
217             }
218             return timer;
219         }
220
221         int count = 0;
222
223         public void actionPerformed( ActionEvent JavaDoc e ) {
224             count++;
225             if (count > 2) {
226                 if (count > 5) {
227                     timer.setDelay(75);
228                 } else {
229                     timer.setDelay(200);
230                 }
231             }
232             performAction();
233         }
234
235         private void performAction() {
236             if (!isEnabled()) {
237                 stopTimer();
238                 return;
239             }
240             getAction().actionPerformed(new ActionEvent JavaDoc(this,
241                                                         ActionEvent.ACTION_PERFORMED,
242                                                         getActionCommand()));
243         }
244
245         private void startTimer() {
246             Timer JavaDoc t = getTimer();
247             if (t.isRunning()) {
248                 return;
249             }
250             repaint();
251             t.setDelay(400);
252             t.start();
253         }
254
255         private void stopTimer() {
256             if (timer != null) {
257                 timer.stop();
258             }
259             repaint();
260             count = 0;
261         }
262
263         protected void processMouseEvent(MouseEvent JavaDoc me) {
264             if (isEnabled() && me.getID() == me.MOUSE_PRESSED) {
265                 startTimer();
266             } else if (me.getID() == me.MOUSE_RELEASED) {
267                 stopTimer();
268             }
269             super.processMouseEvent(me);
270         }
271
272         protected void processFocusEvent(FocusEvent JavaDoc fe) {
273             super.processFocusEvent(fe);
274             if (fe.getID() == fe.FOCUS_LOST) {
275                 stopTimer();
276             }
277         }
278
279         protected String JavaDoc getTabActionCommand(ActionEvent JavaDoc e) {
280             return null;
281         }
282     }
283
284     /**
285      * A button for editor tab control to show a list of opened documents.
286      */

287     private static class DropDownButton extends TabControlButton {
288         
289         private boolean forcePressedIcon = false;
290         
291         public DropDownButton( TabDisplayer displayer ) {
292             super( TabControlButton.ID_DROP_DOWN_BUTTON, displayer );
293             setAction( new TabListPopupAction( displayer ) );
294             setToolTipText( java.util.ResourceBundle.getBundle("org/netbeans/swing/tabcontrol/plaf/Bundle").getString("Tip_Show_Opened_Documents_List") );
295         }
296
297         protected void processMouseEvent(MouseEvent JavaDoc me) {
298             super.processMouseEvent(me);
299             if (isEnabled() && me.getID() == me.MOUSE_PRESSED) {
300                 forcePressedIcon = true;
301                 repaint();
302                 getAction().actionPerformed(new ActionEvent JavaDoc(this,
303                                                             ActionEvent.ACTION_PERFORMED,
304                                                             "pressed"));
305             } else if (isEnabled() && me.getID() == me.MOUSE_RELEASED) {
306                 forcePressedIcon = false;
307                 repaint();
308             }
309         }
310
311         protected String JavaDoc getTabActionCommand(ActionEvent JavaDoc e) {
312             return null;
313         }
314         
315         void performAction( ActionEvent JavaDoc e ) {
316         }
317
318         public Icon JavaDoc getRolloverIcon() {
319             if( forcePressedIcon )
320                 return getPressedIcon();
321             
322             return super.getRolloverIcon();
323         }
324
325         public Icon JavaDoc getIcon() {
326             if( forcePressedIcon )
327                 return getPressedIcon();
328             
329             return super.getIcon();
330         }
331     }
332
333
334     /**
335      * Loader for icons. Caches loaded icons using hash map.
336      */

337     final private static class IconLoader {
338         /* mapping <String, Icon> from resource paths to icon objects, used as cache */
339         private Map JavaDoc<String JavaDoc,Icon JavaDoc> paths2Icons;
340
341         /**
342          * Finds and returns icon instance from cache, if present. Otherwise
343          * loads icon using given resource path and stores icon into cache for
344          * next access.
345          *
346          * @return icon image
347          */

348         public Icon JavaDoc obtainIcon(String JavaDoc iconPath) {
349             if (paths2Icons == null) {
350                 paths2Icons = new HashMap JavaDoc<String JavaDoc,Icon JavaDoc>(6);
351             }
352             Icon JavaDoc icon = paths2Icons.get(iconPath);
353             if (icon == null) {
354                 // not yet in cache, load and store
355
Image JavaDoc image = loadImage(iconPath);
356                 if (image == null) {
357                     throw new IllegalArgumentException JavaDoc("Icon with resource path: "
358                                                        + iconPath
359                                                        + " can't be loaded, probably wrong path.");
360                 }
361                 icon = new ImageIcon JavaDoc(image);
362                 paths2Icons.put(iconPath, icon);
363             }
364             return icon;
365         }
366
367     } // end of IconLoader
368

369     private static Image JavaDoc loadImage(String JavaDoc path) {
370         try {
371             URL JavaDoc url = TabControlButtonFactory.class.getResource("/"+path);
372             return Toolkit.getDefaultToolkit().createImage(url);
373         } catch (Exception JavaDoc e) {
374             e.printStackTrace();
375             return null;
376         }
377     }
378 }
379
Popular Tags