KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > tabcontrol > TabDisplayerUI


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  * TabDisplayerUI.java
21  *
22  * Created on March 16, 2004, 5:55 PM
23  */

24
25 package org.netbeans.swing.tabcontrol;
26
27 import org.netbeans.swing.tabcontrol.event.TabActionEvent;
28
29 import javax.swing.*;
30 import javax.swing.plaf.ComponentUI JavaDoc;
31 import java.awt.*;
32 import java.awt.event.MouseEvent JavaDoc;
33
34 /**
35  * The basic UI of a tab displayer component. Defines the API of the UI for
36  * TabDisplayers, which may be called by TabDisplayer.
37  *
38  * @author Tim Boudreau
39  * @see org.netbeans.swing.tabcontrol.plaf.AbstractTabDisplayerUI
40  * @see org.netbeans.swing.tabcontrol.plaf.BasicTabDisplayerUI
41  */

42 public abstract class TabDisplayerUI extends ComponentUI JavaDoc {
43     protected SingleSelectionModel selectionModel = null;
44     protected final TabDisplayer displayer;
45
46     /**
47      * Creates a new instance of TabDisplayerUI
48      */

49     protected TabDisplayerUI(TabDisplayer displayer) {
50         this.displayer = displayer;
51     }
52
53
54     public void installUI(JComponent c) {
55         assert c == displayer;
56         selectionModel = displayer.getSelectionModel();
57         
58         //Will only be non-null if we are in the middle of an L&F change - don't
59
//replace it so listeners are not clobbered
60
if (selectionModel == null) {
61             selectionModel = createSelectionModel();
62         }
63         
64         installSelectionModel();
65     }
66
67     public void uninstallUI(JComponent c) {
68         assert c == displayer;
69     }
70
71     /**
72      * Get a shape representing the exact outline of the numbered tab. The
73      * implementations in the package will return instances of
74      * <code>EqualPolygon</code> from this method; other implementations may
75      * return what they want, but for performance reasons, it is highly
76      * desirable that the shape object returned honor <code>equals()</code> and
77      * <code>hashCode()</code>, as there are significant optimizations in
78      * NetBeans' drag and drop support that depend on this.
79      */

80     public abstract Polygon getExactTabIndication(int index);
81
82     /**
83      * Get a shape representing the area of visual feedback during a drag and
84      * drop operation, which represents where a tab will be inserted if a drop
85      * operation is performed over the indicated tab. <p>The implementations in
86      * the package will return instances of <code>EqualPolygon</code> from this
87      * method; other implementations may return what they want, but for
88      * performance reasons, it is highly desirable that the shape object
89      * returned honor <code>equals()</code> and <code>hashCode()</code>, as
90      * there are significant optimizations in NetBeans' drag and drop support
91      * that depened on this.
92      *
93      * @return Shape representing feedback shape
94      */

95     public abstract Polygon getInsertTabIndication(int index);
96
97     /**
98      * Returns the index of the tab at the passed point, or -1 if no tab is at
99      * that location. Note that this method may return -1 for coordinates which
100      * are within a tab as returned by getTabRect(), but are not within the
101      * visible shape of the tab as the UI paints it.
102      */

103     public abstract int tabForCoordinate(Point p);
104
105     /**
106      * Configure the passed rectangle with the shape of the tab at the given
107      * index.
108      */

109     public abstract Rectangle getTabRect(int index,
110                                          final Rectangle destination);
111
112     /**
113      * Returns an image suitable for use in drag and drop operations,
114      * representing the tab at this index. The default implementation returns null.
115      *
116      * @param index A tab index
117      * @throws IllegalArgumentException if no tab is at the passed index
118      */

119     public Image createImageOfTab(int index) {
120         return null;
121     }
122
123     /**
124      * Create the selection model which will handle selection for the
125      * TabDisplayer. SPI method located here because TabDisplayer.setSelectionModel
126      * is package private.
127      */

128     protected abstract SingleSelectionModel createSelectionModel();
129
130     /**
131      * Allows ActionListeners attached to the container to determine if the
132      * event should be acted on. Delegates to <code>displayer.postActionEvent()</code>.
133      * This method will create a TabActionEvent with the passed string as an
134      * action command, and cause the displayer to fire this event. It will
135      * return true if no listener on the displayer consumed the TabActionEvent;
136      * consuming the event is the way a listener can veto a change, or provide
137      * special handling for it.
138      *
139      * @param command The action command - this should be TabDisplayer.COMMAND_SELECT
140      * or TabDisplayer.COMMAND_CLOSE, but private contracts
141      * between custom UIs and components are also an option.
142      * @param tab The index of the tab upon which the action should act, or
143      * -1 if non-applicable
144      * @param event A mouse event which initiated the action, or null
145      * @return true if the event posted was not consumed by any listener
146      */

147     protected final boolean shouldPerformAction(String JavaDoc command, int tab,
148                                                 MouseEvent JavaDoc event) {
149         TabActionEvent evt = new TabActionEvent(displayer, command, tab, event);
150         displayer.postActionEvent(evt);
151         return !evt.isConsumed();
152     }
153
154     /**
155      * Instruct the UI to ensure that the tab at the given index is visible.
156      * Some UIs allow scrolling or otherwise hiding tabs. The default
157      * implementation is a no-op.
158      *
159      * @param index The index of the tab that should be made visible, which
160      * should be within the range of 0 to the count of tabs in the
161      * model
162      */

163     public void makeTabVisible(int index) {
164         //do nothing
165
}
166
167     /**
168      * Installs the selection model into the tab control via a package private
169      * method.
170      */

171     private void installSelectionModel() {
172         displayer.setSelectionModel(selectionModel);
173     }
174
175     /**
176      * The index a tab would acquire if dropped at a given point
177      *
178      * @param p A point
179      * @return An index which may be equal to the size of the data model
180      */

181     public abstract int dropIndexOfPoint (Point p);
182     
183     public abstract void registerShortcuts (JComponent comp);
184         
185     public abstract void unregisterShortcuts (JComponent comp);
186     
187     
188     protected abstract void requestAttention (int tab);
189     
190     protected abstract void cancelRequestAttention (int tab);
191     
192     public abstract Icon getButtonIcon( int buttonId, int buttonState );
193     
194     public void postTabAction( TabActionEvent e ) {
195         if( shouldPerformAction( e.getActionCommand(), e.getTabIndex(), e.getMouseEvent() ) ) {
196             
197             //TODO do something here??
198
}
199     }
200 }
201
Popular Tags