KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > ui > AbstractModeContainer


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
21 package org.netbeans.core.windows.view.ui;
22
23
24 import java.lang.reflect.Field JavaDoc;
25 import org.netbeans.core.windows.Constants;
26 import org.netbeans.core.windows.ModeImpl;
27 import org.netbeans.core.windows.WindowManagerImpl;
28 import org.netbeans.core.windows.view.ModeContainer;
29 import org.netbeans.core.windows.view.ModeView;
30 import org.netbeans.core.windows.view.dnd.TopComponentDroppable;
31 import org.netbeans.core.windows.view.dnd.WindowDnDManager;
32 import org.openide.util.Utilities;
33 import org.openide.windows.TopComponent;
34
35 import javax.swing.*;
36 import java.awt.*;
37 import java.util.Arrays JavaDoc;
38
39
40 /**
41  * Abstract helper implementation of <code>ModeContainer</code>.
42  * PENDING: It provides also support for TopComponentDroppable.
43  *
44  * @author Peter Zavadsky
45  */

46 public abstract class AbstractModeContainer implements ModeContainer {
47     
48     /** Associated mode view. */
49     protected final ModeView modeView;
50
51     protected final TabbedHandler tabbedHandler;
52     
53     // PENDING
54
protected final WindowDnDManager windowDnDManager;
55     
56     // kind of mode, editor or view
57
private final int kind;
58     
59
60     public AbstractModeContainer(ModeView modeView, WindowDnDManager windowDnDManager, int kind) {
61         this.modeView = modeView;
62         this.windowDnDManager = windowDnDManager;
63         this.kind = kind;
64         this.tabbedHandler = new TabbedHandler(modeView, kind, createTabbed());
65     }
66
67
68     public ModeView getModeView() {
69         return modeView;
70     }
71     
72     /** */
73     public Component getComponent() {
74         return getModeComponent();
75     }
76     
77     protected abstract Component getModeComponent();
78     
79     protected abstract Tabbed createTabbed();
80
81     public void addTopComponent(TopComponent tc) {
82         tabbedHandler.addTopComponent(tc, kind);
83     }
84
85     public void removeTopComponent(TopComponent tc) {
86         tabbedHandler.removeTopComponent(tc);
87
88         TopComponent selected = tabbedHandler.getSelectedTopComponent();
89         updateTitle(selected == null
90             ? "" : WindowManagerImpl.getInstance().getTopComponentDisplayName(selected)); // NOI18N
91
}
92     
93     public void setSelectedTopComponent(TopComponent tc) {
94         tabbedHandler.setSelectedTopComponent(tc);
95         
96         updateTitle(WindowManagerImpl.getInstance().getTopComponentDisplayName(tc));
97     }
98     
99     public void setTopComponents(TopComponent[] tcs, TopComponent selected) {
100         //Cheaper to do the equality test here than later
101
if (!Arrays.equals(tcs, getTopComponents())) {
102             tabbedHandler.setTopComponents(tcs, selected);
103             updateTitle(WindowManagerImpl.getInstance().getTopComponentDisplayName(selected));
104         } else {
105             //[dafe] It is also used as selection modifier only, for example when
106
// clearing selection to null on sliding modes
107
setSelectedTopComponent(selected);
108         }
109     }
110     
111     protected abstract void updateTitle(String JavaDoc title);
112     
113     protected abstract void updateActive(boolean active);
114     
115     
116     public TopComponent getSelectedTopComponent() {
117         return tabbedHandler.getSelectedTopComponent();
118     }
119     
120     public void setActive(boolean active) {
121         updateActive(active);
122
123         TopComponent selected = tabbedHandler.getSelectedTopComponent();
124         updateTitle(selected == null
125             ? "" : WindowManagerImpl.getInstance().getTopComponentDisplayName(selected)); // NOI18N
126

127         tabbedHandler.setActive(active);
128     }
129     
130     public void focusSelectedTopComponent() {
131         // PENDING focus gets main window sometimes, investgate and refine (jdk1.4.1?).
132
final TopComponent selectedTopComponent = tabbedHandler.getSelectedTopComponent();
133
134         if (selectedTopComponent == null) {
135             return;
136         }
137
138         Window oldFocusedW = FocusManager.getCurrentManager().getFocusedWindow();
139         Window newFocusedW = SwingUtilities.getWindowAncestor(selectedTopComponent);
140         
141         if (newFocusedW != null && newFocusedW.equals(oldFocusedW)) {
142             // focus transfer inside one window, so requestFocusInWindow call is enough
143
if (!Utilities.isMac()) {
144                 selectedTopComponent.requestFocusInWindow();
145             } else {
146                 //#60235 - on macosx 1.5 there seems to be a bug with requesting focus.
147
// this piece of code seems to workaround most of the usecases in 60235
148
SwingUtilities.invokeLater(new Runnable JavaDoc() {
149                     public void run() {
150                         //#62947 another workaround for macosx 1.5 focus behaviour.
151
// when maximizing the mode, the old and new focused component is the same, but removed from
152
// awt hierarchy and added elsewhere.
153
// the DefautlkeyboardFocusmanager doen't do it's job then and locks the keyboard.
154
// hack it by make it believe the focus changed.
155
try {
156                             Field JavaDoc fld = KeyboardFocusManager.class.getDeclaredField("focusOwner");//NOI8N
157
fld.setAccessible(true);
158                             fld.set(KeyboardFocusManager.getCurrentKeyboardFocusManager(), null);
159                         } catch (IllegalArgumentException JavaDoc ex) {
160                             ex.printStackTrace();
161                         } catch (SecurityException JavaDoc ex) {
162                             ex.printStackTrace();
163                         } catch (NoSuchFieldException JavaDoc ex) {
164                             ex.printStackTrace();
165                         } catch (IllegalAccessException JavaDoc ex) {
166                             ex.printStackTrace();
167                         }
168                         //#62947 hack finished.
169
selectedTopComponent.requestFocusInWindow();
170                     }
171                 });
172             }
173         } else {
174             // focus transfer between different windows
175
selectedTopComponent.requestFocus();
176         }
177     }
178
179     
180     public TopComponent[] getTopComponents() {
181         return tabbedHandler.getTopComponents();
182     }
183
184     public void updateName(TopComponent tc) {
185         TopComponent selected = getSelectedTopComponent();
186         if(tc == selected) {
187             updateTitle(tc == null
188                 ? "" : WindowManagerImpl.getInstance().getTopComponentDisplayName(tc)); // NOI18N
189
}
190         
191         tabbedHandler.topComponentNameChanged(tc, kind);
192     }
193     
194     public void updateToolTip(TopComponent tc) {
195         tabbedHandler.topComponentToolTipChanged(tc);
196     }
197     
198     public void updateIcon(TopComponent tc) {
199         tabbedHandler.topComponentIconChanged(tc);
200     }
201
202     // XXX
203
protected int getKind() {
204         return kind;
205     }
206     
207     ////////////////////////
208
// Support for TopComponentDroppable
209
protected Shape getIndicationForLocation(Point location) {
210         return tabbedHandler.getIndicationForLocation(location,
211             windowDnDManager.getStartingTransfer(),
212             windowDnDManager.getStartingPoint(),
213             isAttachingPossible());
214     }
215     
216     protected Object JavaDoc getConstraintForLocation(Point location) {
217         return tabbedHandler.getConstraintForLocation(location, isAttachingPossible());
218     }
219     
220     protected abstract boolean isAttachingPossible();
221     
222     protected ModeView getDropModeView() {
223         return modeView;
224     }
225     
226     protected Component getDropComponent() {
227         return tabbedHandler.getComponent();
228     }
229     
230     protected abstract TopComponentDroppable getModeDroppable();
231     
232     protected boolean canDrop(TopComponent transfer) {
233         if(Constants.SWITCH_MODE_ADD_NO_RESTRICT
234           || WindowManagerImpl.getInstance().isTopComponentAllowedToMoveAnywhere(transfer)) {
235             return true;
236         }
237         
238         ModeImpl mode = (ModeImpl)WindowManagerImpl.getInstance().findMode(transfer);
239         int kind = mode != null ? mode.getKind() : Constants.MODE_KIND_EDITOR;
240         
241         boolean isNonEditor = kind == Constants.MODE_KIND_VIEW || kind == Constants.MODE_KIND_SLIDING;
242         boolean thisIsNonEditor = this.kind == Constants.MODE_KIND_VIEW || this.kind == Constants.MODE_KIND_SLIDING;
243         return isNonEditor == thisIsNonEditor;
244     }
245     // Support for TopComponentDroppable
246
////////////////////////
247

248 }
249
250
Popular Tags