1 19 20 package org.netbeans.core.windows.view.ui; 21 22 import java.awt.Component ; 23 import java.awt.Container ; 24 import java.awt.Dimension ; 25 import java.awt.GridBagConstraints ; 26 import java.awt.GridBagLayout ; 27 import java.awt.LayoutManager ; 28 import java.awt.Point ; 29 import java.awt.Rectangle ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.Set ; 33 import javax.swing.JLayeredPane ; 34 import javax.swing.JPanel ; 35 import javax.swing.SwingUtilities ; 36 import org.netbeans.core.windows.Constants; 37 import org.netbeans.core.windows.view.SlidingView; 38 import org.netbeans.core.windows.view.ViewElement; 39 import org.netbeans.core.windows.view.ui.slides.SlideOperation; 40 import org.netbeans.core.windows.view.ui.slides.SlideOperationFactory; 41 import org.netbeans.swing.tabcontrol.TabbedContainer; 42 import org.openide.windows.TopComponent; 43 44 45 50 public final class DesktopImpl { 51 52 54 private JLayeredPane layeredPane; 55 56 private JPanel desktop; 57 58 private ViewElement splitRoot; 59 private ViewElement maximizedMode; 60 private Component splitRootComponent; 61 private Component viewComponent; 62 63 64 private Set <SlidingView> slidingViews; 65 66 private SlideOperation curSlideIn; 67 68 70 private static final int MIN_EDITOR_ALIGN_THICK = 200; 71 72 73 public DesktopImpl () { 74 layeredPane = new JLayeredPane (); 76 layeredPane.setLayout(new LayeredLayout()); 77 desktop = new JPanel (); 79 desktop.setLayout(new GridBagLayout ()); 80 layeredPane.add(desktop); 81 } 82 83 public Component getDesktopComponent () { 84 return layeredPane; 85 } 86 87 public Dimension getInnerPaneDimension() { 88 int width = desktop.getSize().width; 89 int height = desktop.getSize().height; 90 SlidingView view = findView(Constants.LEFT); 91 width = (view != null ? width - view.getComponent().getSize().width : width); 92 view = findView(Constants.RIGHT); 93 width = (view != null ? width - view.getComponent().getSize().width : width); 94 view = findView(Constants.BOTTOM); 95 height = (view != null ? height - view.getComponent().getSize().height : height); 96 return new Dimension (width, height); 97 } 98 99 public void setSplitRoot (ViewElement splitRoot) { 100 101 this.splitRoot = splitRoot; 102 if (splitRoot != null) { 103 setViewComponent(splitRoot.getComponent()); 105 } else { 106 setViewComponent(null); 107 } 108 109 } 110 111 112 113 public void setMaximizedView(ViewElement component) { 114 115 maximizedMode = component; 116 if (component.getComponent() != viewComponent) { 117 setViewComponent(component.getComponent()); 118 119 } 120 } 121 122 private void setViewComponent( Component component) { 123 if (viewComponent == component) { 124 return; 125 } 126 if (viewComponent != null) { 127 desktop.remove(viewComponent); 128 } 129 viewComponent = component; 130 if (viewComponent != null) { 131 GridBagConstraints constr = new GridBagConstraints (); 132 constr.gridx = 1; 133 constr.gridy = 0; 134 constr.fill = constr.BOTH; 135 constr.weightx = 1; 136 constr.weighty = 1; 137 desktop.add(component, constr); 138 } 139 layeredPane.revalidate(); 140 layeredPane.repaint(); 141 } 142 143 public ViewElement getSplitRoot () { 144 return splitRoot; 145 } 146 147 public void addSlidingView (SlidingView view) { 148 Set <SlidingView> slidingViews = getSlidingViews(); 149 if (slidingViews.contains(view)) { 150 return; 151 } 152 slidingViews.add(view); 153 GridBagConstraints constraint = new GridBagConstraints (); 154 constraint.fill = GridBagConstraints.BOTH; 155 if (Constants.BOTTOM.equals(view.getSide())) { 156 constraint.gridx = 1; 157 constraint.gridy = 1; 158 constraint.anchor = GridBagConstraints.SOUTHWEST; 159 160 } else if (Constants.LEFT.equals(view.getSide())) { 161 constraint.gridx = 0; 162 constraint.gridy = 0; 163 constraint.gridheight = 2; 164 constraint.anchor = GridBagConstraints.NORTHWEST; 165 } else if (Constants.RIGHT.equals(view.getSide())) { 166 constraint.gridx = 2; 167 constraint.gridy = 0; 168 constraint.gridheight = 2; 169 constraint.anchor = GridBagConstraints.NORTHEAST; 170 } 171 desktop.add(view.getComponent(), constraint); 172 layeredPane.revalidate(); 174 } 175 176 public void removeSlidingView (SlidingView view) { 177 Set slidingViews = getSlidingViews(); 178 if (!slidingViews.contains(view)) { 179 return; 180 } 181 slidingViews.remove(view); 182 desktop.remove(view.getComponent()); 183 checkCurSlide(); 184 layeredPane.revalidate(); 186 } 187 188 private void checkCurSlide() { 189 if (curSlideIn != null) { 190 SlidingView curView = null; 191 Component curSlideComp = curSlideIn.getComponent(); 192 for (Iterator iter = slidingViews.iterator(); iter.hasNext(); ) { 193 curView = (SlidingView)iter.next(); 194 if (curView.getTopComponents().contains(curSlideComp)) { 195 return; 196 } 197 } 198 layeredPane.remove(curSlideComp); 200 } 201 } 202 203 public void performSlideIn(SlideOperation operation, Rectangle editorBounds) { 204 Rectangle slideInBounds = computeSlideInBounds(operation, editorBounds); 205 operation.setFinishBounds(slideInBounds); 206 operation.setStartBounds(computeThinBounds(operation, slideInBounds)); 207 performSlide(operation); 208 curSlideIn = operation; 209 } 210 211 public void performSlideOut(SlideOperation operation, Rectangle editorBounds) { 212 Rectangle slideOutBounds = operation.getComponent().getBounds(); 213 operation.setStartBounds(slideOutBounds); 214 operation.setFinishBounds(computeThinBounds(operation, slideOutBounds)); 215 216 curSlideIn = null; 217 performSlide(operation); 218 desktop.revalidate(); 219 desktop.repaint(); 220 } 221 222 public void performSlideIntoEdge(SlideOperation operation, Rectangle editorBounds) { 223 operation.setFinishBounds(computeLastButtonBounds(operation)); 224 Rectangle screenStart = operation.getStartBounds(); 225 operation.setStartBounds(convertRectFromScreen(layeredPane, screenStart)); 226 227 performSlide(operation); 228 } 229 230 public void performSlideIntoDesktop(SlideOperation operation, Rectangle editorBounds) { 231 Rectangle screenStart = operation.getStartBounds(); 232 operation.setStartBounds(convertRectFromScreen(layeredPane, screenStart)); 233 Rectangle screenFinish = operation.getStartBounds(); 234 operation.setStartBounds(convertRectFromScreen(layeredPane, screenFinish)); 235 236 performSlide(operation); 237 } 238 239 public void performSlideResize(SlideOperation operation) { 240 performSlide(operation); 241 } 242 243 public void performSlideToggleMaximize( TopComponent tc, String side, Rectangle editorBounds ) { 244 Component tabbed = findTabbed( tc ); 245 if( null != tabbed ) { 246 SlideOperation operation = SlideOperationFactory.createSlideResize( tabbed, side ); 247 Rectangle slideInBounds = computeSlideInBounds(operation, editorBounds); 248 operation.setFinishBounds(slideInBounds); 249 performSlide(operation); 250 } 251 } 252 253 private Component findTabbed( Component comp ) { 254 while( comp.getParent() != null ) { 255 if( comp.getParent() instanceof TabbedContainer ) { 256 return comp.getParent(); 257 } 258 comp = comp.getParent(); 259 } 260 return null; 261 } 262 263 264 private void performSlide(SlideOperation operation) { 265 operation.run(layeredPane, Integer.valueOf(102)); 266 } 267 268 private Rectangle convertRectFromScreen (Component comp, Rectangle screenRect) { 269 if (screenRect == null) { 271 screenRect = new Rectangle (0, 0, 0, 0); 272 } 273 Point leftTop = screenRect.getLocation(); 274 SwingUtilities.convertPointFromScreen(leftTop, comp); 275 276 return new Rectangle (leftTop, screenRect.getSize()); 277 } 278 279 283 private Rectangle computeSlideInBounds(SlideOperation operation, Rectangle editorBounds) { 284 Point editorLeftTop = editorBounds.getLocation(); 285 SwingUtilities.convertPointFromScreen(editorLeftTop, layeredPane); 286 editorBounds = new Rectangle (editorLeftTop, editorBounds.getSize()); 287 String side = operation.getSide(); 288 SlidingView view = findView(side); 289 Rectangle splitRootRect = viewComponent.getBounds(); 290 Rectangle result = new Rectangle (); 291 Rectangle viewRect = view.getComponent().getBounds(); 292 Dimension viewPreferred = view.getComponent().getPreferredSize(); 293 int minThick = MIN_EDITOR_ALIGN_THICK; 294 295 TopComponent tc = view.getSelectedTopComponent(); 296 if( null != tc && Boolean.TRUE.equals( tc.getClientProperty( "keepPreferredSizeWhenSlideIn" ) ) ) minThick = 20; 298 299 if (Constants.LEFT.equals(side)) { 300 result.x = viewRect.x + Math.max(viewRect.width, viewPreferred.width); 301 result.y = 0; 302 result.height = splitRootRect.height; 303 result.width = view.getSlideBounds().width; 304 if (result.width < minThick) { 305 result.width = splitRootRect.width / 3; 306 } 307 if (result.width > splitRootRect.width) { 308 result.width = splitRootRect.width; 310 } 311 } else if (Constants.RIGHT.equals(side)) { 312 int rightLimit = layeredPane.getBounds().width - Math.max(viewRect.width, viewPreferred.width); 313 result.x = (view.getSlideBounds().width < minThick) 314 ? rightLimit - splitRootRect.width / 3 : rightLimit - view.getSlideBounds().width; 315 if (result.x < 0) { 316 result.x = 0; 318 } 319 result.y = 0; 320 result.height = splitRootRect.height; 321 result.width = rightLimit - result.x; 322 323 } else if (Constants.BOTTOM.equals(side)) { 324 int lowerLimit = viewRect.y + viewRect.height - Math.max(viewRect.height, viewPreferred.height); 325 result.x = splitRootRect.x; 326 result.y = (view.getSlideBounds().height < minThick) 327 ? lowerLimit - splitRootRect.height / 3 : lowerLimit - view.getSlideBounds().height; 328 if (result.y < 0) { 329 result.y = 0; 331 } 332 result.height = lowerLimit - result.y; 333 result.width = splitRootRect.width; 334 } 335 return result; 336 } 337 338 private Rectangle computeThinBounds (SlideOperation operation, Rectangle slideInFinish) { 339 String side = operation.getSide(); 340 Rectangle result = new Rectangle (); 341 342 if (Constants.LEFT.equals(side)) { 343 result.x = slideInFinish.x; 344 result.y = slideInFinish.y; 345 result.height = slideInFinish.height; 346 result.width = 0; 347 } else if (Constants.RIGHT.equals(side)) { 348 result.x = slideInFinish.x + slideInFinish.width; 349 result.y = slideInFinish.y; 350 result.height = slideInFinish.height; 351 result.width = 0; 352 } else if (Constants.BOTTOM.equals(side)) { 353 result.x = slideInFinish.x; 354 result.y = slideInFinish.y + slideInFinish.height; 355 result.height = 0; 356 result.width = slideInFinish.width; 357 } 358 359 return result; 360 } 361 362 365 private Rectangle computeLastButtonBounds(SlideOperation operation) { 366 String side = operation.getSide(); 367 SlidingView view = findView(side); 368 Rectangle screenRect = view.getTabBounds(view.getTopComponents().size() - 1); 369 Point leftTop = screenRect.getLocation(); 370 371 if (Constants.BOTTOM.equals(side)) { 372 leftTop.y += desktop.getHeight() - view.getComponent().getPreferredSize().height; 373 } else if (Constants.RIGHT.equals(side)) { 374 leftTop.x += desktop.getWidth() - view.getComponent().getPreferredSize().width; 375 } 376 377 return new Rectangle (leftTop, screenRect.getSize()); 378 } 379 380 private SlidingView findView (String side) { 381 SlidingView view; 382 for (Iterator iter = getSlidingViews().iterator(); iter.hasNext(); ) { 383 view = (SlidingView)iter.next(); 384 if (side.equals(view.getSide())) { 385 return view; 386 } 387 } 388 return null; 389 } 390 391 private Set <SlidingView> getSlidingViews() { 392 if (slidingViews == null) { 393 slidingViews = new HashSet <SlidingView>(5); 394 } 395 return slidingViews; 396 } 397 398 399 403 private final class LayeredLayout implements LayoutManager { 404 private Dimension lastSize; 405 public void layoutContainer(Container parent) { 406 Dimension size = parent.getSize(); 407 desktop.setBounds(0, 0, size.width, size.height); 408 desktop.invalidate(); 409 desktop.validate(); 410 if ((curSlideIn != null) && curSlideIn.getComponent().isVisible()) { 412 String side = curSlideIn.getSide(); 413 SlidingView curView = findView(side); 414 if (curView != null && viewComponent != null) { 416 Component slidedComp = curSlideIn.getComponent(); 417 Rectangle result = slidedComp.getBounds(); 418 Rectangle viewRect = curView.getComponent().getBounds(); 419 Dimension viewPrefSize = curView.getComponent().getPreferredSize(); 420 Rectangle splitRootRect = viewComponent.getBounds(); 421 422 if (Constants.LEFT.equals(side)) { 423 result.height = splitRootRect.height; 424 if (lastSize != null && !lastSize.equals(size)) { 425 int wid = curView.getSlideBounds().width; 426 if (wid > (size.width - viewRect.width)) { 427 result.width = size.width - (size.width / 10); 429 } else { 430 result.width = wid; 431 } 432 } 433 } else if (Constants.RIGHT.equals(side)) { 434 result.height = splitRootRect.height; 435 if (lastSize != null && !lastSize.equals(size)) { 436 int avail = size.width - Math.max(viewRect.width, viewPrefSize.width); 437 int wid = curView.getSlideBounds().width; 438 if (avail - wid < (wid /10)) { 439 result.x = 0 + (wid / 10); 440 result.width = avail - (wid / 10); 441 } else { 442 result.x = avail - result.width; 443 result.width = wid; 444 } 445 } 446 } else if (Constants.BOTTOM.equals(side)) { 447 result.width = splitRootRect.width; 448 if (lastSize != null && !lastSize.equals(size)) { 449 int avail = size.height - Math.max(viewRect.height, viewPrefSize.height); 450 int hei = viewRect.height; 451 if (hei < curView.getSlideBounds().height) { 452 hei = curView.getSlideBounds().height; 453 } 454 if (avail - hei < (hei /10)) { 455 result.y = 0 + (hei / 10); 456 result.height = avail - (hei / 10); 457 } else { 458 result.y = avail - hei; 459 result.height = hei; 460 } 461 } 462 } 463 slidedComp.setBounds(result); 464 } 465 } 466 lastSize = size; 467 } 468 469 public Dimension minimumLayoutSize(Container parent) { 470 return desktop.getMinimumSize(); 471 } 472 473 public Dimension preferredLayoutSize(Container parent) { 474 return desktop.getPreferredSize(); 475 } 476 477 public void addLayoutComponent(String name, Component comp) { 478 } 480 481 public void removeLayoutComponent(Component comp) { 482 } 484 485 } 487 } 488 | Popular Tags |