KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > ui > slides > TabbedSlideAdapter


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.core.windows.view.ui.slides;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Graphics2D JavaDoc;
24 import java.awt.GraphicsConfiguration JavaDoc;
25 import java.awt.GraphicsEnvironment JavaDoc;
26 import java.awt.Image JavaDoc;
27 import java.awt.Point JavaDoc;
28 import java.awt.Rectangle JavaDoc;
29 import java.awt.Shape JavaDoc;
30 import java.awt.Toolkit JavaDoc;
31 import java.awt.event.ActionEvent JavaDoc;
32 import java.awt.event.ActionListener JavaDoc;
33 import java.awt.image.BufferedImage JavaDoc;
34 import java.beans.PropertyChangeListener JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collections JavaDoc;
37 import java.util.List JavaDoc;
38 import javax.swing.Action JavaDoc;
39 import javax.swing.DefaultSingleSelectionModel JavaDoc;
40 import javax.swing.Icon JavaDoc;
41 import javax.swing.ImageIcon JavaDoc;
42 import javax.swing.JLabel JavaDoc;
43 import javax.swing.SingleSelectionModel JavaDoc;
44 import javax.swing.event.ChangeEvent JavaDoc;
45 import javax.swing.event.ChangeListener JavaDoc;
46 import org.netbeans.core.windows.Constants;
47 import org.netbeans.core.windows.WindowManagerImpl;
48 import org.netbeans.core.windows.actions.ActionUtils;
49 import org.netbeans.core.windows.view.ui.Tabbed;
50 import org.netbeans.swing.tabcontrol.SlideBarDataModel;
51 import org.netbeans.swing.tabcontrol.TabData;
52 import org.netbeans.swing.tabcontrol.TabDataModel;
53 import org.netbeans.swing.tabcontrol.TabbedContainer;
54 import org.openide.windows.TopComponent;
55
56 /*
57  * Adapts SlideBar to match Tabbed interface, which is used by TabbedHandler
58  * for talking to component containers. SlideBar is driven indirectly,
59  * through modifications of its model.
60  *
61  * @author Dafe Simonek
62  */

63 public final class TabbedSlideAdapter implements Tabbed {
64     
65     /** data model of informations about top components in container */
66     private TabDataModel dataModel;
67     /** selection model which contains selection info in container */
68     private SingleSelectionModel JavaDoc selModel;
69     /** Visual component for displaying box for sliding windows */
70     private SlideBar slideBar;
71     /** List of action listeners */
72     private List JavaDoc<ActionListener JavaDoc> actionListeners;
73     /** List of selection listeners */
74     private List JavaDoc<ChangeListener JavaDoc> selectionListeners;
75     /** selection change event - stateless, so we can cache */
76     private final ChangeEvent JavaDoc selectionEvt = new ChangeEvent JavaDoc(this);
77     
78     /** Creates a new instance of SlideBarTabs */
79     public TabbedSlideAdapter(String JavaDoc side) {
80         dataModel = new SlideBarDataModel.Impl();
81         setSide(side);
82         selModel = new DefaultSingleSelectionModel JavaDoc();
83         slideBar = new SlideBar(this, (SlideBarDataModel)dataModel, selModel);
84     }
85     
86     public void requestAttention (TopComponent tc) {
87         slideBar.setBlinking(tc, true);
88     }
89     
90     public void cancelRequestAttention (TopComponent tc) {
91         slideBar.setBlinking(tc, false);
92     }
93     
94
95     private void setSide (String JavaDoc side) {
96         int orientation = SlideBarDataModel.WEST;
97         if (Constants.LEFT.equals(side)) {
98             orientation = SlideBarDataModel.WEST;
99         } else if (Constants.RIGHT.equals(side)) {
100             orientation = SlideBarDataModel.EAST;
101         } else if (Constants.BOTTOM.equals(side)) {
102             orientation = SlideBarDataModel.SOUTH;
103         }
104         ((SlideBarDataModel)dataModel).setOrientation(orientation);
105     }
106
107     public final synchronized void addActionListener(ActionListener JavaDoc listener) {
108         if (actionListeners == null) {
109             actionListeners = new ArrayList JavaDoc<ActionListener JavaDoc>();
110         }
111         actionListeners.add(listener);
112     }
113
114     /**
115      * Remove an action listener.
116      *
117      * @param listener The listener to remove.
118      */

119     public final synchronized void removeActionListener(ActionListener JavaDoc listener) {
120         if (actionListeners != null) {
121             actionListeners.remove(listener);
122             if (actionListeners.isEmpty()) {
123                 actionListeners = null;
124             }
125         }
126     }
127
128     final void postActionEvent(ActionEvent JavaDoc event) {
129         List JavaDoc<ActionListener JavaDoc> list;
130         synchronized (this) {
131             if (actionListeners == null)
132                 return;
133             list = Collections.unmodifiableList(actionListeners);
134         }
135         for (int i = 0; i < list.size(); i++) {
136             list.get(i).actionPerformed(event);
137         }
138     }
139     
140     public void addChangeListener(ChangeListener JavaDoc listener) {
141         if (selectionListeners == null) {
142             selectionListeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
143         }
144         selectionListeners.add(listener);
145     }
146     
147     public void removeChangeListener(ChangeListener JavaDoc listener) {
148         if (selectionListeners != null) {
149             selectionListeners.remove(listener);
150             if (selectionListeners.isEmpty()) {
151                 selectionListeners = null;
152             }
153         }
154     }
155     
156     final void postSelectionEvent() {
157         List JavaDoc<ChangeListener JavaDoc> list;
158         synchronized (this) {
159             if (selectionListeners == null)
160                 return;
161             list = Collections.unmodifiableList(selectionListeners);
162         }
163         for (int i = 0; i < list.size(); i++) {
164             list.get(i).stateChanged(selectionEvt);
165         }
166     }
167     
168     public void addPropertyChangeListener(String JavaDoc name, PropertyChangeListener JavaDoc listener) {
169         slideBar.addPropertyChangeListener(name, listener);
170     }
171     
172     public void removePropertyChangeListener(String JavaDoc name, PropertyChangeListener JavaDoc listener) {
173         slideBar.removePropertyChangeListener(name, listener);
174     }
175     
176     public void addTopComponent(String JavaDoc name, Icon JavaDoc icon, TopComponent tc, String JavaDoc toolTip) {
177         dataModel.addTab(dataModel.size(), new TabData(tc, icon, name, toolTip));
178     }
179     
180     public TopComponent getSelectedTopComponent() {
181         int index = selModel.getSelectedIndex();
182         return index < 0 ? null : (TopComponent)dataModel.getTab(index).getComponent();
183     }
184     
185     public TopComponent getTopComponentAt(int index) {
186         return (TopComponent)dataModel.getTab(index).getComponent();
187     }
188     
189     public TopComponent[] getTopComponents() {
190         int size = dataModel.size();
191         TopComponent[] result = new TopComponent[size];
192         for (int i=0; i < size; i++) {
193             result[i] = (TopComponent) dataModel.getTab(i).getComponent();
194         }
195         return result;
196     }
197     
198     public void setActive(boolean active) {
199         slideBar.setActive(active);
200     }
201     
202     public void setIconAt(int index, Icon JavaDoc icon) {
203         dataModel.setIcon(index, icon);
204     }
205     
206     public void setTitleAt(int index, String JavaDoc title) {
207         dataModel.setText(index, title);
208     }
209     
210     public void setToolTipTextAt(int index, String JavaDoc toolTip) {
211         // XXX - not supported yet
212
}
213     
214     public void setTopComponents(TopComponent[] tcs, TopComponent selected) {
215         TabData[] data = new TabData[tcs.length];
216         int toSelect=-1;
217         for(int i = 0; i < tcs.length; i++) {
218             TopComponent tc = tcs[i];
219             Image JavaDoc icon = tc.getIcon();
220             String JavaDoc displayName = WindowManagerImpl.getInstance().getTopComponentDisplayName(tc);
221             data[i] = new TabData(
222                 tc,
223                 icon == null ? null : new ImageIcon JavaDoc(icon),
224                 displayName == null ? "" : displayName, // NOI18N
225
tc.getToolTipText());
226             if (selected == tcs[i]) {
227                 toSelect = i;
228             }
229         }
230
231         dataModel.setTabs(data);
232         setSelectedComponent(selected);
233     }
234     
235     public int getTabCount() {
236         return dataModel.size();
237     }
238     
239     public int indexOf(Component JavaDoc tc) {
240         int size = dataModel.size();
241         for (int i=0; i < size; i++) {
242             if (tc == dataModel.getTab(i).getComponent()) return i;
243         }
244         return -1;
245     }
246     
247     public void insertComponent(String JavaDoc name, Icon JavaDoc icon, Component JavaDoc comp, String JavaDoc toolTip, int position) {
248         dataModel.addTab(position, new TabData(comp, icon, name, toolTip));
249     }
250     
251     public void removeComponent(Component JavaDoc comp) {
252         int i = indexOf(comp);
253         dataModel.removeTab(i);
254     }
255     
256     public void setSelectedComponent(Component JavaDoc comp) {
257         int newIndex = indexOf(comp);
258         if (selModel.getSelectedIndex() != newIndex) {
259             selModel.setSelectedIndex(newIndex);
260         }
261         if (comp instanceof TopComponent) {
262             //Inelegant to do this here, but it guarantees blinking stops
263
TopComponent tc = (TopComponent) comp;
264             tc.cancelRequestAttention();
265         }
266     }
267     
268     public int tabForCoordinate(Point JavaDoc p) {
269         return slideBar.tabForCoordinate(p.x, p.y);
270     }
271     
272     public Component JavaDoc getComponent() {
273         return slideBar;
274     }
275
276 /*************** No DnD support yet **************/
277     
278     public Object JavaDoc getConstraintForLocation(Point JavaDoc location, boolean attachingPossible) {
279         int tab = slideBar.nextTabForCoordinate(location.x, location.y);
280         return Integer.valueOf(tab);
281     }
282     
283     public Shape JavaDoc getIndicationForLocation(Point JavaDoc location, TopComponent startingTransfer, Point JavaDoc startingPoint, boolean attachingPossible) {
284         
285 // int tab = tabForCoordinate(location);
286
int nextTab = slideBar.nextTabForCoordinate(location.x, location.y);
287         SlideBarDataModel sbdm = (SlideBarDataModel)dataModel;
288         if (getTabCount() != 0) {
289             if (nextTab == 0) {
290                 Rectangle JavaDoc rect = getTabBounds(0);
291                 if (sbdm.getOrientation() == SlideBarDataModel.SOUTH) {
292                     rect.x = 0;
293                     rect.width = rect.width / 2;
294                 } else {
295                     rect.y = 0;
296                     rect.height = rect.height / 2;
297                 }
298                 return rect;
299             } else if (nextTab < getTabCount()) {
300                 Rectangle JavaDoc rect1 = getTabBounds(nextTab - 1);
301                 Rectangle JavaDoc rect2 = getTabBounds(nextTab);
302                 Rectangle JavaDoc result = new Rectangle JavaDoc();
303                 if (sbdm.getOrientation() == SlideBarDataModel.SOUTH) {
304                     result.y = rect1.y;
305                     result.height = rect1.height;
306                     result.x = rect1.x + (rect1.width / 2);
307                     result.width = rect2.x + (rect2.width / 2) - result.x;
308                 } else {
309                     result.x = rect1.x;
310                     result.width = rect1.width;
311                     result.y = rect1.y + (rect1.height / 2);
312                     result.height = rect2.y + (rect2.height / 2) - result.y;
313                 }
314                 return result;
315             } else if (nextTab == getTabCount()) {
316                 Rectangle JavaDoc rect = getTabBounds(getTabCount() - 1);
317                 if (sbdm.getOrientation() == SlideBarDataModel.SOUTH) {
318                     rect.x = rect.x + rect.width;
319                 } else {
320                     rect.y = rect.y + rect.height;
321                 }
322                 return rect;
323             }
324         }
325         Rectangle JavaDoc rect = slideBar.getBounds();
326         if (sbdm.getOrientation() == SlideBarDataModel.SOUTH) {
327             return new Rectangle JavaDoc(10, 0, 50, 20);
328         }
329         return new Rectangle JavaDoc(0, 10, 20, 50);
330     }
331     
332     public Image JavaDoc createImageOfTab(int tabIndex) {
333         TabData dt = slideBar.getModel().getTab(tabIndex);
334         if (dt.getComponent() instanceof TopComponent) {
335             
336             JLabel JavaDoc lbl = new JLabel JavaDoc(dt.getText());
337             int width = lbl.getFontMetrics(lbl.getFont()).stringWidth(dt.getText());
338             int height = lbl.getFontMetrics(lbl.getFont()).getHeight();
339             Image JavaDoc img = ((TopComponent)dt.getComponent()).getIcon();
340             lbl.setIcon(new ImageIcon JavaDoc(img));
341             width = width + (img.getWidth(null) == -1 ? 16 : img.getWidth(null)) + 6;
342             height = Math.max(height + 5, img.getHeight(null) == -1 ? 21 : 5 + img.getHeight(null));
343             
344             GraphicsConfiguration JavaDoc config = GraphicsEnvironment.getLocalGraphicsEnvironment()
345                         .getDefaultScreenDevice().getDefaultConfiguration();
346             
347             
348             BufferedImage JavaDoc image = config.createCompatibleImage(width, height);
349             Graphics2D JavaDoc g = image.createGraphics();
350             g.setColor(lbl.getForeground());
351             g.setFont(lbl.getFont());
352             g.drawImage(img, 0, 0, null);
353             g.drawString(dt.getText(), 18, height / 2);
354             
355             return image;
356         }
357         
358         return null;
359     }
360     
361     /** Add action for disabling slide */
362     public Action JavaDoc[] getPopupActions(Action JavaDoc[] defaultActions, int tabIndex) {
363         boolean isMDI = WindowManagerImpl.getInstance().getEditorAreaState() == Constants.EDITOR_AREA_JOINED;
364         Action JavaDoc[] result = new Action JavaDoc[defaultActions.length + (isMDI ? 1 : 0)];
365         System.arraycopy(defaultActions, 0, result, 0, defaultActions.length);
366         if (isMDI) {
367             result[defaultActions.length] =
368                 new ActionUtils.AutoHideWindowAction(slideBar, tabIndex, true);
369         }
370         return result;
371     }
372     
373     public Rectangle JavaDoc getTabBounds(int tabIndex) {
374         return slideBar.getTabBounds(tabIndex);
375     }
376     
377 }
378
379
Popular Tags