1 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 ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import org.openide.windows.TopComponent; 34 35 36 44 class ViewRequestor { 45 46 47 private final Central central; 48 49 private final View view; 50 51 52 private final List <ViewRequest> requests = new ArrayList <ViewRequest>(10); 53 56 private WindowSystemSnapshot snapshot; 57 58 private boolean reentryFlag = false; 59 60 61 private static final boolean DEBUG = Debug.isLoggable(ViewRequestor.class); 62 63 64 65 public ViewRequestor(Central central) { 66 this.central = central; 67 this.view = ViewFactory.createWindowSystemView(central); 68 } 69 70 71 public boolean isDragInProgress() { 74 return view.isDragInProgress(); 75 } 76 77 public Frame getMainWindow() { 80 return view.getMainWindow(); 81 } 82 83 public Component getEditorAreaComponent() { 84 return view.getEditorAreaComponent(); 85 } 86 87 public String guessSlideSide(TopComponent tc) { 88 return view.guessSlideSide(tc); 89 } 90 91 92 public void scheduleRequest(ViewRequest request) { 93 if(request.type == View.CHANGE_VISIBILITY_CHANGED) { 94 postVisibilityRequest(request); 96 return; 97 } 98 99 coallesceRequest(request); 100 101 postRequest(); 102 } 103 104 105 private void coallesceRequest(ViewRequest request) { 106 Object 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 oldValue = null; 137 if(doCoallesce) { 138 for(Iterator it = requests.iterator(); it.hasNext(); ) { 139 ViewRequest r = (ViewRequest)it.next(); 140 if(source == r.source && type == r.type) { 141 it.remove(); 143 oldValue = r.oldValue; 144 break; 145 } 146 } 147 } 148 149 if(oldValue != null) { 150 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 () { 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 () { 177 public void run() { 178 if(DEBUG) { 179 debugLog("Rescheduling request into AWT took=" + (System.currentTimeMillis() - time) + " ms"); } 182 183 processVisibilityRequest(visibilityRequest); 184 } 185 }); 186 } 187 } 188 189 190 193 private void processRequest() { 194 if (reentryFlag) { 195 return; 196 } 197 if(snapshot == null) { 198 return; 200 } 201 202 ViewRequest[] rs; 203 synchronized(requests) { 204 if(requests.isEmpty()) { 205 return; 207 } 208 209 rs = requests.toArray(new ViewRequest[0]); 210 requests.clear(); 211 } 212 213 List <ViewEvent> viewEvents = new ArrayList <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 )visibilityRequest.newValue).booleanValue()) { 229 snapshot = central.createWindowSystemSnapshot(); 231 } else { 232 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 processRequest(); 252 } 253 } 254 255 private void updateSnapshot(ViewRequest[] requests) { 256 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=" + (System.currentTimeMillis() - time) + " ms"); debugLog(snapshot.toString()); 279 } 280 } 281 282 private ViewEvent getViewEvent(ViewRequest request) { 284 Object source = request.source; 285 int type = request.type; 286 Object oldValue = request.oldValue; 287 Object newValue = request.newValue; 288 289 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_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 332 private static void debugLog(String message) { 333 Debug.log(ViewRequestor.class, message); 334 } 335 336 } 337 338 | Popular Tags |