KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > multiview > TabsComponent


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.multiview;
21
22 import java.awt.*;
23 import java.awt.event.*;
24 import java.awt.event.KeyEvent JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.swing.*;
29 import javax.swing.JToggleButton.ToggleButtonModel;
30 import javax.swing.KeyStroke JavaDoc;
31 import javax.swing.border.Border JavaDoc;
32 import javax.swing.border.CompoundBorder JavaDoc;
33 import javax.swing.border.EmptyBorder JavaDoc;
34 import javax.swing.plaf.metal.MetalLookAndFeel JavaDoc;
35 import javax.swing.text.Keymap JavaDoc;
36 import org.netbeans.core.spi.multiview.MultiViewDescription;
37 import org.netbeans.core.spi.multiview.MultiViewElement;
38 import org.openide.util.Lookup;
39 import org.openide.util.NbBundle;
40 import org.openide.util.actions.CallbackSystemAction;
41
42
43 /**
44  * Temporary solution tomultiview tabs..
45  * @author mkleint
46  */

47 class TabsComponent extends JPanel {
48     
49     private JComponent EMPTY;
50     private final static String JavaDoc TOOLBAR_MARKER = "MultiViewPanel"; //NOI18N
51

52     MultiViewModel model;
53     private ActionListener listener;
54     private MouseListener buttonMouseListener = null;
55     private JComponent toolbarPanel;
56     private JPanel componentPanel;
57     private CardLayout cardLayout;
58     private Set JavaDoc alreadyAddedElements;
59     private JToolBar bar;
60     
61     private static final boolean AQUA = "Aqua".equals(UIManager.getLookAndFeel().getID()); //NOI18N
62

63     private boolean toolbarVisible = true;
64     
65     /** Creates a new instance of TabsComponent */
66     public TabsComponent(boolean toolVis) {
67         super();
68         bar = AQUA ? new TB() : new JToolBar();
69         Border JavaDoc b = (Border JavaDoc)UIManager.get("Nb.Editor.Toolbar.border"); //NOI18N
70
bar.setBorder(b);
71         bar.setFloatable(false);
72         bar.setFocusable(true);
73         
74         setLayout(new BorderLayout());
75         add(bar, BorderLayout.NORTH);
76         startToggling();
77         setToolbarBarVisible(toolVis);
78     }
79     
80     
81     
82     public void setModel(MultiViewModel model) {
83         if (this.model != null) {
84             bar.removeAll();
85         }
86         this.model = model;
87         
88         componentPanel = new JPanel();
89         cardLayout = new CardLayout();
90         componentPanel.setLayout(cardLayout);
91         add(componentPanel, BorderLayout.CENTER);
92         alreadyAddedElements = new HashSet JavaDoc();
93         
94         MultiViewDescription[] descs = model.getDescriptions();
95         MultiViewDescription def = model.getActiveDescription();
96         GridBagLayout grid = new GridBagLayout();
97         bar.setLayout(grid);
98         JToggleButton active = null;
99         int prefHeight = -1;
100         int prefWidth = -1;
101         for (int i = 0; i < descs.length; i++) {
102             JToggleButton button = createButton(descs[i]);
103             model.getButtonGroup().add(button);
104             GridBagConstraints cons = new GridBagConstraints();
105             cons.anchor = GridBagConstraints.WEST;
106             prefHeight = Math.max(button.getPreferredSize().height, prefHeight);
107             bar.add(button, cons);
108             prefWidth = Math.max(button.getPreferredSize().width, prefWidth);
109             if (descs[i] == model.getActiveDescription()) {
110                 active = button;
111                 
112             }
113         }
114         Enumeration JavaDoc en = model.getButtonGroup().getElements();
115         while (en.hasMoreElements()) {
116             JToggleButton but = (JToggleButton)en.nextElement();
117             Insets ins = but.getBorder().getBorderInsets(but);
118             but.setPreferredSize(new Dimension(prefWidth + 10, prefHeight));
119             but.setMinimumSize(new Dimension(prefWidth + 10, prefHeight));
120             
121         }
122         if (active != null) {
123             active.setSelected(true);
124         }
125         toolbarPanel = getEmptyInnerToolBar();
126         GridBagConstraints cons = new GridBagConstraints();
127         cons.anchor = GridBagConstraints.EAST;
128         cons.fill = GridBagConstraints.BOTH;
129         cons.gridwidth = GridBagConstraints.REMAINDER;
130         cons.weightx = 1;
131
132         bar.add(toolbarPanel, cons);
133     }
134
135     
136     void switchToCard(MultiViewElement elem, String JavaDoc id) {
137         if (! alreadyAddedElements.contains(elem)) {
138             componentPanel.add(elem.getVisualRepresentation(), id);
139             alreadyAddedElements.add(elem);
140         }
141         cardLayout.show(componentPanel, id);
142     }
143     
144     
145     void changeActiveManually(MultiViewDescription desc) {
146         Enumeration JavaDoc en = model.getButtonGroup().getElements();
147         while (en.hasMoreElements()) {
148             JToggleButton obj = (JToggleButton)en.nextElement();
149             
150             if (obj.getModel() instanceof TabsComponent.TabsButtonModel) {
151                 TabsButtonModel btnmodel = (TabsButtonModel)obj.getModel();
152                 if (btnmodel.getButtonsDescription().equals(desc)) {
153                     obj.setSelected(true);
154                     MultiViewElement elem = model.getElementForDescription(desc);
155                     elem.getVisualRepresentation().requestFocus();
156                     break;
157                 }
158             }
159         }
160     }
161
162     void changeVisibleManually(MultiViewDescription desc) {
163         Enumeration JavaDoc en = model.getButtonGroup().getElements();
164         while (en.hasMoreElements()) {
165             JToggleButton obj = (JToggleButton)en.nextElement();
166             
167             if (obj.getModel() instanceof TabsComponent.TabsButtonModel) {
168                 TabsButtonModel btnmodel = (TabsButtonModel)obj.getModel();
169                 if (btnmodel.getButtonsDescription().equals(desc)) {
170                     obj.setSelected(true);
171                     break;
172                 }
173             }
174         }
175     }
176     
177     private JToggleButton createButton(MultiViewDescription description) {
178         final JToggleButton button = new JToggleButton(description.getDisplayName());
179         button.setModel(new TabsButtonModel(description));
180         button.setRolloverEnabled(true);
181         Border JavaDoc b = (getButtonBorder());
182         if (b != null) {
183            button.setBorder(b);
184         }
185           
186         if (buttonMouseListener == null) {
187             buttonMouseListener = new ButtonMouseListener();
188         }
189         button.addMouseListener (buttonMouseListener);
190         //
191
Font font = button.getFont();
192         FontMetrics fm = button.getFontMetrics(font);
193         int height = fm.getHeight();
194
195         //HACK start - now find the global action shortcut
196
Keymap JavaDoc map = (Keymap JavaDoc)Lookup.getDefault().lookup(Keymap JavaDoc.class);
197         KeyStroke JavaDoc stroke = null;
198         KeyStroke JavaDoc stroke2 = null;
199 //in tests map can be null, that's why the check..
200
if (map != null) {
201             // map is null in tests..
202
Action[] acts = map.getBoundActions();
203             for (int i = 0; i < acts.length;i++) {
204                 if (acts[i] instanceof CallbackSystemAction) {
205                     CallbackSystemAction sa = (CallbackSystemAction)acts[i];
206                     if ("NextViewAction".equals(sa.getActionMapKey())) { //NOI18N
207
KeyStroke JavaDoc[] strokes = map.getKeyStrokesForAction(acts[i]);
208                         if (strokes != null && strokes.length > 0) {
209                             stroke = strokes[0];
210                         }
211                     }
212                     if ("PreviousViewAction".equals(sa.getActionMapKey())) { //NOI18N
213
KeyStroke JavaDoc[] strokes = map.getKeyStrokesForAction(acts[i]);
214                         if (strokes != null && strokes.length > 0) {
215                             stroke2 = strokes[0];
216                         }
217                     }
218                 }
219             }
220         }
221         //HACK end
222
String JavaDoc key1 = stroke == null ? "" : KeyEvent.getKeyModifiersText(stroke.getModifiers()) + "+" + KeyEvent.getKeyText(stroke.getKeyCode());//NOI18N
223
String JavaDoc key2 = stroke2 == null ? "" : KeyEvent.getKeyModifiersText(stroke2.getModifiers()) + "+" + KeyEvent.getKeyText(stroke2.getKeyCode());//NOI18N
224
button.setToolTipText(NbBundle.getMessage(TabsComponent.class, "TabButton.tooltip",//NOI18N
225
description.getDisplayName(),
226                               key1,
227                               key2));
228         button.setFocusable(true);
229         button.setFocusPainted(true);
230         return button;
231     }
232
233     void setInnerToolBar(JComponent innerbar) {
234         synchronized (getTreeLock()) {
235             if (toolbarPanel != null) {
236                 bar.remove(toolbarPanel);
237             }
238             if (innerbar == null) {
239                 innerbar = getEmptyInnerToolBar();
240             }
241             innerbar.putClientProperty(TOOLBAR_MARKER, "X"); //NOI18N
242
// need to set it to null, because CloneableEditor set's the border for the editor bar part only..
243
if (!AQUA) {
244                 innerbar.setBorder(null);
245             } else {
246                 innerbar.setBorder (BorderFactory.createEmptyBorder(2, 0, 2, 0));
247             }
248             toolbarPanel = innerbar;
249             if (toolbarPanel != null) {
250                 GridBagConstraints cons = new GridBagConstraints();
251                 cons.anchor = GridBagConstraints.EAST;
252                 cons.fill = GridBagConstraints.BOTH;
253                 cons.weightx = 1;
254                 toolbarPanel.setMinimumSize(new Dimension(10, 10));
255                 cons.gridwidth = GridBagConstraints.REMAINDER;
256                 
257                 bar.add(toolbarPanel, cons);
258             }
259             // rootcycle is the tabscomponent..
260
// toolbarPanel.setFocusCycleRoot(false);
261
bar.revalidate();
262             bar.repaint();
263         }
264     }
265     
266     void setToolbarBarVisible(boolean visible) {
267         if (toolbarVisible == visible) {
268             return;
269         }
270         toolbarVisible = visible;
271         bar.setVisible(visible);
272     }
273     
274     
275     
276     JComponent getEmptyInnerToolBar() {
277         if (EMPTY == null) {
278             EMPTY = new JPanel();
279         }
280         return EMPTY;
281     }
282     
283     
284     void requestFocusForSelectedButton() {
285         bar.setFocusable(true);
286         Enumeration JavaDoc en = model.getButtonGroup().getElements();
287         while (en.hasMoreElements()) {
288             JToggleButton but = (JToggleButton)en.nextElement();
289             if (model.getButtonGroup().isSelected(but.getModel())) {
290                 but.requestFocus();
291                 return;
292             }
293         }
294         throw new IllegalStateException JavaDoc("How come none of the buttons is selected?");
295     }
296
297     void requestFocusForPane() {
298         bar.setFocusable(false);
299         componentPanel.requestFocus();
300     }
301     
302     
303     private Border JavaDoc buttonBorder = null;
304     private boolean isMetal = false;
305     private boolean isWindows = false;
306     private Border JavaDoc getButtonBorder() {
307         if (buttonBorder == null) {
308             //For some lf's, core will supply one
309
buttonBorder = UIManager.getBorder ("nb.tabbutton.border"); //NOI18N
310
}
311         
312         return buttonBorder;
313     }
314     
315     public static boolean isXPTheme () {
316         Boolean JavaDoc isXP = (Boolean JavaDoc)Toolkit.getDefaultToolkit().
317                         getDesktopProperty("win.xpstyle.themeActive"); //NOI18N
318
return isXP == null ? false : isXP.booleanValue();
319     }
320     
321   
322     void startToggling() {
323         ActionMap map = bar.getActionMap();
324         Action act = new TogglesGoEastAction();
325         // JToolbar action name
326
map.put("navigateRight", act);
327         InputMap input = bar.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
328         
329         act = new TogglesGoWestAction();
330         // JToolbar action name
331
map.put("navigateLeft", act);
332         
333         act = new TogglesGoDownAction();
334         map.put("TogglesGoDown", act);
335         // JToolbar action name
336
map.put("navigateUp", act);
337         KeyStroke JavaDoc stroke = KeyStroke.getKeyStroke("ESCAPE"); //NOI18N
338
input.put(stroke, "TogglesGoDown");
339     }
340
341     
342     private class TogglesGoWestAction extends AbstractAction {
343         
344         public void actionPerformed(ActionEvent e) {
345             MultiViewDescription[] descs = model.getDescriptions();
346             MultiViewDescription active = model.getActiveDescription();
347             for (int i = 0; i < descs.length; i++) {
348                 if (descs[i] == active) {
349                     int next = i - 1;
350                     if (next < 0) {
351                         next = descs.length - 1;
352                     }
353                     changeVisibleManually(descs[next]);
354                     requestFocusForSelectedButton();
355                 }
356             }
357         }
358     }
359     
360     private class TogglesGoEastAction extends AbstractAction {
361         
362         public void actionPerformed(ActionEvent e) {
363             MultiViewDescription[] descs = model.getDescriptions();
364             MultiViewDescription active = model.getActiveDescription();
365             for (int i = 0; i < descs.length; i++) {
366                 if (descs[i] == active) {
367                     int next = i + 1;
368                     if (next >= descs.length) {
369                         next = 0;
370                     }
371                     changeVisibleManually(descs[next]);
372                     requestFocusForSelectedButton();
373                 }
374             }
375         }
376     }
377
378     private class TogglesGoDownAction extends AbstractAction {
379         
380         public void actionPerformed(ActionEvent e) {
381             changeActiveManually(model.getActiveDescription());
382             model.getActiveElement().getVisualRepresentation().requestFocusInWindow();
383         }
384     }
385     
386     
387 /**
388  * used in
389  */

390     static class TabsButtonModel extends ToggleButtonModel {
391
392         private MultiViewDescription desc;
393         public TabsButtonModel(MultiViewDescription description) {
394             super();
395             desc = description;
396         }
397         
398         public MultiViewDescription getButtonsDescription() {
399             return desc;
400         }
401     }
402     
403     class ButtonMouseListener extends MouseAdapter {
404         public void mouseEntered(MouseEvent e) {
405             AbstractButton b = (AbstractButton)e.getComponent();
406             b.getModel().setRollover(true);
407         }
408         public void mouseExited(MouseEvent e) {
409             AbstractButton b = (AbstractButton)e.getComponent();
410             b.getModel().setRollover(false);
411         }
412         
413         /** for user triggered clicks, do activate the current element..
414             make it on mousePressed to be in synch with the topcpomponent activation code in the winsys impl #68505
415          */

416         public void mousePressed(MouseEvent e) {
417             AbstractButton b = (AbstractButton)e.getComponent();
418             MultiViewModel model = TabsComponent.this.model;
419             if (model != null) {
420                 model.getButtonGroup().setSelected(b.getModel(), true);
421                 model.fireActivateCurrent();
422             }
423
424         }
425         
426     }
427    
428     
429     private static final class TB extends JToolBar {
430         private boolean updating = false;
431         
432         public TB() {
433             //Aqua UI will look for this value to ensure the
434
//toolbar is tall enough that the "glow" which paints
435
//outside the combo box bounds doesn't make a mess painting
436
//into other components
437
setName("editorToolbar");
438         }
439         
440         public void setBorder (Border JavaDoc b) {
441             if (!updating) {
442                 return;
443             }
444             super.setBorder(b);
445         }
446         
447         public void updateUI() {
448             updating = true;
449             try {
450                 super.updateUI();
451             } finally {
452                 updating = false;
453             }
454         }
455         
456         public String JavaDoc getUIClassID() {
457             return UIManager.get("Nb.Toolbar.ui") == null ?
458                 super.getUIClassID() : "Nb.Toolbar.ui";
459         }
460     }
461 }
462
Popular Tags