KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > ViewRequestor


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;
22
23
24 import org.netbeans.core.windows.view.View;
25 import org.netbeans.core.windows.view.ViewEvent;
26 import org.netbeans.core.windows.view.ViewFactory;
27
28 import javax.swing.*;
29 import java.awt.*;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import org.openide.windows.TopComponent;
34
35
36 /**
37  * Class responsible for communication between central and view parts.
38  * It sends requests to view. Also provides coallescing of more
39  * requests and is also responsible for scheduling the requests into AWT thread.
40  * This class is thread safe.
41  *
42  * @author Peter Zavadsky
43  */

44 class ViewRequestor {
45
46     /** Associated central. */
47     private final Central central;
48     /** View of window system. */
49     private final View view;
50     
51     /** List of requests to process. */
52     private final List JavaDoc<ViewRequest> requests = new ArrayList JavaDoc<ViewRequest>(10);
53     // PENDING
54
/** Instance of snapshot which is passed to view. Reflects the actual state of model.
55      * Manipulate it in AWT thread only. */

56     private WindowSystemSnapshot snapshot;
57     
58     private boolean reentryFlag = false;
59     
60     /** Debugging flag. */
61     private static final boolean DEBUG = Debug.isLoggable(ViewRequestor.class);
62     
63     
64     /** Creates a new instance of ViewRequestor */
65     public ViewRequestor(Central central) {
66         this.central = central;
67         this.view = ViewFactory.createWindowSystemView(central);
68     }
69
70     
71     // XXX PENDING Method requesting state of view... do not mix with the rest.
72
// It shoudn't exist any link in direction view -> central.
73
public boolean isDragInProgress() {
74         return view.isDragInProgress();
75     }
76     
77     // XXX PENDING Method requesting info of view... do not mix with the rest.
78
// It shouldn't exist any link in directtion view -> central, but due to old API.
79
public Frame getMainWindow() {
80         return view.getMainWindow();
81     }
82     
83     public Component getEditorAreaComponent() {
84         return view.getEditorAreaComponent();
85     }
86     
87     public String JavaDoc guessSlideSide(TopComponent tc) {
88         return view.guessSlideSide(tc);
89     }
90     
91     /** Schedules request into AWT. */
92     public void scheduleRequest(ViewRequest request) {
93         if(request.type == View.CHANGE_VISIBILITY_CHANGED) {
94             // Most important request, takes precedence to all others.
95
postVisibilityRequest(request);
96             return;
97         }
98
99         coallesceRequest(request);
100         
101         postRequest();
102     }
103
104     /** Coallesce request. */
105     private void coallesceRequest(ViewRequest request) {
106         Object JavaDoc source = request.source;
107         int type = request.type;
108         
109         boolean doCoallesce =
110             type == View.CHANGE_ACTIVE_MODE_CHANGED
111             || type == View.CHANGE_EDITOR_AREA_BOUNDS_CHANGED
112             || type == View.CHANGE_EDITOR_AREA_CONSTRAINTS_CHANGED
113             || type == View.CHANGE_EDITOR_AREA_STATE_CHANGED
114             || type == View.CHANGE_EDITOR_AREA_FRAME_STATE_CHANGED
115             || type == View.CHANGE_MAIN_WINDOW_BOUNDS_JOINED_CHANGED
116             || type == View.CHANGE_MAIN_WINDOW_BOUNDS_SEPARATED_CHANGED
117             || type == View.CHANGE_MAIN_WINDOW_FRAME_STATE_JOINED_CHANGED
118             || type == View.CHANGE_MAIN_WINDOW_FRAME_STATE_SEPARATED_CHANGED
119             || type == View.CHANGE_MAXIMIZED_MODE_CHANGED
120             || type == View.CHANGE_MODE_SELECTED_TOPCOMPONENT_CHANGED
121             || type == View.CHANGE_MODE_BOUNDS_CHANGED
122             || type == View.CHANGE_MODE_CONSTRAINTS_CHANGED
123             || type == View.CHANGE_MODE_FRAME_STATE_CHANGED
124             || type == View.CHANGE_TOOLBAR_CONFIGURATION_CHANGED
125             || type == View.CHANGE_TOPCOMPONENT_ICON_CHANGED
126             || type == View.CHANGE_TOPCOMPONENT_DISPLAY_NAME_CHANGED
127             || type == View.CHANGE_TOPCOMPONENT_DISPLAY_NAME_ANNOTATION_CHANGED
128             || type == View.CHANGE_TOPCOMPONENT_TOOLTIP_CHANGED
129             || type == View.CHANGE_TOPCOMPONENT_ACTIVATED
130             || type == View.CHANGE_DND_PERFORMED
131             || type == View.CHANGE_UI_UPDATE
132             || type == View.TOPCOMPONENT_CANCEL_REQUEST_ATTENTION
133             || type == View.TOPCOMPONENT_REQUEST_ATTENTION;
134             
135         synchronized(requests) {
136             Object JavaDoc oldValue = null;
137             if(doCoallesce) {
138                 for(Iterator JavaDoc it = requests.iterator(); it.hasNext(); ) {
139                     ViewRequest r = (ViewRequest)it.next();
140                     if(source == r.source && type == r.type) {
141                         // Remove the old request (it will be replaced by new one).
142
it.remove();
143                         oldValue = r.oldValue;
144                         break;
145                     }
146                 }
147             }
148
149             if(oldValue != null) {
150                 // Actually coallesce.
151
requests.add(new ViewRequest(
152                     request.source, request.type, oldValue, request.newValue));
153             } else {
154                 requests.add(request);
155             }
156         }
157     }
158     
159     private void postRequest() {
160         if(SwingUtilities.isEventDispatchThread()) {
161             processRequest();
162         } else {
163             SwingUtilities.invokeLater(new Runnable JavaDoc() {
164                 public void run() {
165                     processRequest();
166                 }
167             });
168         }
169     }
170     
171     private void postVisibilityRequest(final ViewRequest visibilityRequest) {
172         if(SwingUtilities.isEventDispatchThread()) {
173             processVisibilityRequest(visibilityRequest);
174         } else {
175             final long time = System.currentTimeMillis();
176             SwingUtilities.invokeLater(new Runnable JavaDoc() {
177                 public void run() {
178                     if(DEBUG) {
179                         debugLog("Rescheduling request into AWT took=" // NOI18N
180
+ (System.currentTimeMillis() - time) + " ms"); // NOI18N
181
}
182                     
183                     processVisibilityRequest(visibilityRequest);
184                 }
185             });
186         }
187     }
188     
189     
190     ///////////////////////////////////////////
191
// !! AWT thread only >>
192

193     private void processRequest() {
194         if (reentryFlag) {
195             return;
196         }
197         if(snapshot == null) {
198             // The system was made invisible.
199
return;
200         }
201         
202         ViewRequest[] rs;
203         synchronized(requests) {
204             if(requests.isEmpty()) {
205                 // No requests left.
206
return;
207             }
208             
209             rs = requests.toArray(new ViewRequest[0]);
210             requests.clear();
211         }
212         
213         List JavaDoc<ViewEvent> viewEvents = new ArrayList JavaDoc<ViewEvent>();
214         
215         updateSnapshot(rs);
216         for(int i = 0; i < rs.length; i++) {
217             ViewRequest r = rs[i];
218             if (DEBUG) {
219                 debugLog("Creating a view event for " + r);
220             }
221             viewEvents.add(getViewEvent(r));
222         }
223         dispatchRequest(viewEvents.toArray(new ViewEvent[0]), snapshot);
224
225     }
226     
227     private void processVisibilityRequest(ViewRequest visibilityRequest) {
228         if(((Boolean JavaDoc)visibilityRequest.newValue).booleanValue()) {
229             // Is visible, set snapshot
230
snapshot = central.createWindowSystemSnapshot();
231         } else {
232             // Is invisible, clear it.
233
snapshot = null;
234         }
235         
236         dispatchRequest(new ViewEvent[] { new ViewEvent(
237                 visibilityRequest.source,
238                 visibilityRequest.type,
239                 visibilityRequest.oldValue,
240                 visibilityRequest.newValue)},
241             snapshot);
242     }
243     
244     private void dispatchRequest(ViewEvent[] viewEvents, WindowSystemSnapshot snapshot) {
245         try {
246             reentryFlag = true;
247             view.changeGUI(viewEvents, snapshot);
248         } finally {
249             reentryFlag = false;
250             // check for events that appeared while processing..
251
processRequest();
252         }
253     }
254
255     private void updateSnapshot(ViewRequest[] requests) {
256         // PENDING Possibly optimization, update to snapshot only changed values
257
// ans create new structure snapshot only in case it affect strucure update.
258

259         long time = System.currentTimeMillis();
260
261         WindowSystemSnapshot currentSnapshot = central.createWindowSystemSnapshot();
262         snapshot.setMainWindowBoundsJoined(currentSnapshot.getMainWindowBoundsJoined());
263         snapshot.setMainWindowBoundsSeparated(currentSnapshot.getMainWindowBoundsSeparated());
264         snapshot.setMainWindowFrameStateJoined(currentSnapshot.getMainWindowFrameStateJoined());
265         snapshot.setMainWindowFrameStateSeparated(currentSnapshot.getMainWindowFrameStateSeparated());
266         snapshot.setEditorAreaState(currentSnapshot.getEditorAreaState());
267         snapshot.setEditorAreaFrameState(currentSnapshot.getEditorAreaFrameState());
268         snapshot.setEditorAreaBounds(currentSnapshot.getEditorAreaBounds());
269         snapshot.setActiveModeSnapshot(currentSnapshot.getActiveModeSnapshot());
270         snapshot.setMaximizedModeSnapshot(currentSnapshot.getMaximizedModeSnapshot());
271         snapshot.setModeStructureSnapshot(currentSnapshot.getModeStructureSnapshot());
272         snapshot.setToolbarConfigurationName(currentSnapshot.getToolbarConfigurationName());
273         snapshot.setProjectName(currentSnapshot.getProjectName());
274
275         if(DEBUG) {
276             debugLog("Updating winsys snapshot took=" // NOI18N
277
+ (System.currentTimeMillis() - time) + " ms"); // NOI18N
278
debugLog(snapshot.toString());
279         }
280     }
281     
282     // XXX PENDING Adjusts the source event to the view needs.
283
private ViewEvent getViewEvent(ViewRequest request) {
284         Object JavaDoc source = request.source;
285         int type = request.type;
286         Object JavaDoc oldValue = request.oldValue;
287         Object JavaDoc newValue = request.newValue;
288
289         /*if(type == View.CHANGE_ACTIVE_MODE_CHANGED) {
290         } else if(type == View.CHANGE_MAXIMIZED_MODE_CHANGED) {
291         } else if(type == View.CHANGE_EDITOR_AREA_BOUNDS_CHANGED) {
292         } else if(type == View.CHANGE_EDITOR_AREA_STATE_CHANGED) {
293         } else if(type == View.CHANGE_EDITOR_AREA_FRAME_STATE_CHANGED) {
294         } else if(type == View.CHANGE_MAIN_WINDOW_BOUNDS_JOINED_CHANGED) {
295         } else if(type == View.CHANGE_MAIN_WINDOW_BOUNDS_SEPARATED_CHANGED) {
296         } else if(type == View.CHANGE_MAIN_WINDOW_FRAME_STATE_JOINED_CHANGED) {
297         } else if(type == View.CHANGE_MAIN_WINDOW_FRAME_STATE_SEPARATED_CHANGED) {
298         } else if(type == View.CHANGE_TOOLBAR_CONFIGURATION_CHANGED) {
299         } else if(type == View.CHANGE_MODE_ADDED) {
300         } else if(type == View.CHANGE_MODE_REMOVED) {
301         } else if(type == View.CHANGE_SPLITS_CHANGED) {
302         } else*/
if(type == View.CHANGE_MODE_SELECTED_TOPCOMPONENT_CHANGED) {
303             return new ViewEvent(((ModeImpl)source).getName(), type, oldValue, newValue);
304 // } else if(type == View.CHANGE_MODE_CONSTRAINTS_CHANGED) {
305
} else if(type == View.CHANGE_MODE_TOPCOMPONENT_ADDED) {
306             return new ViewEvent(((ModeImpl)source).getName(), type, oldValue, newValue);
307         } else if(type == View.CHANGE_MODE_TOPCOMPONENT_REMOVED) {
308             return new ViewEvent(((ModeImpl)source).getName(), type, oldValue, newValue);
309         } else if(type == View.CHANGE_MODE_BOUNDS_CHANGED) {
310             return new ViewEvent(((ModeImpl)source).getName(), type, oldValue, newValue);
311         } else if(type == View.CHANGE_MODE_FRAME_STATE_CHANGED) {
312             return new ViewEvent(((ModeImpl)source).getName(), type, oldValue, newValue);
313         } else if(type == View.CHANGE_TOPCOMPONENT_ICON_CHANGED) {
314             return new ViewEvent(((ModeImpl)source).getName(), type, oldValue, newValue);
315         } else if(type == View.CHANGE_TOPCOMPONENT_DISPLAY_NAME_CHANGED) {
316             return new ViewEvent(((ModeImpl)source).getName(), type, oldValue, newValue);
317         } else if(type == View.CHANGE_TOPCOMPONENT_DISPLAY_NAME_ANNOTATION_CHANGED) {
318             return new ViewEvent(((ModeImpl)source).getName(), type, oldValue, newValue);
319         } else if(type == View.CHANGE_TOPCOMPONENT_TOOLTIP_CHANGED) {
320             return new ViewEvent(((ModeImpl)source).getName(), type, oldValue, newValue);
321         } else if(type == View.CHANGE_TOPCOMPONENT_ACTIVATED) {
322             return new ViewEvent(((ModeImpl)source).getName(), type, oldValue, newValue);
323         } else if(type == View.CHANGE_MODE_CLOSED) {
324             return new ViewEvent(((ModeImpl)source).getName(), type, oldValue, newValue);
325         }
326
327         return new ViewEvent(source, type, oldValue, newValue);
328     }
329     // !! AWT thread only <<
330
///////////////////////////////////////////
331

332     private static void debugLog(String JavaDoc message) {
333         Debug.log(ViewRequestor.class, message);
334     }
335
336 }
337
338
Popular Tags