KickJava   Java API By Example, From Geeks To Geeks.

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


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.windows.view.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Container JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.GridBagConstraints JavaDoc;
26 import java.awt.GridBagLayout JavaDoc;
27 import java.awt.LayoutManager JavaDoc;
28 import java.awt.Point JavaDoc;
29 import java.awt.Rectangle JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Set JavaDoc;
33 import javax.swing.JLayeredPane JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import javax.swing.SwingUtilities JavaDoc;
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 /** Implementation of compact mode desktop, containing split views as well
46  * as slide bars.
47  *
48  * @author Dafe Simonek
49  */

50 public final class DesktopImpl {
51
52     /** overall layered pane, contains desktop at regular layer and slided
53      * component on upper layers */

54     private JLayeredPane JavaDoc layeredPane;
55     /** panel which holds regular desktop - split view root and slide bars */
56     private JPanel JavaDoc desktop;
57     /** root of slit views */
58     private ViewElement splitRoot;
59     private ViewElement maximizedMode;
60     private Component JavaDoc splitRootComponent;
61     private Component JavaDoc viewComponent;
62     
63     /** slide bars. Lazy initialization, because slide bars are optional. */
64     private Set JavaDoc<SlidingView> slidingViews;
65     /** slide in operation in progress or null if no component is currently slided in */
66     private SlideOperation curSlideIn;
67
68     /** Minimal thick of slided component when system is trying to align
69      * slided component with editor area */

70     private static final int MIN_EDITOR_ALIGN_THICK = 200;
71     
72     /** Creates a new instance of DesktopImpl */
73     public DesktopImpl () {
74         // layered pane with absolute positioning, to enable overlapping
75
layeredPane = new JLayeredPane JavaDoc();
76         layeredPane.setLayout(new LayeredLayout());
77         // desktop represents regular layer of layeredPane
78
desktop = new JPanel JavaDoc();
79         desktop.setLayout(new GridBagLayout JavaDoc());
80         layeredPane.add(desktop);
81     }
82     
83     public Component JavaDoc getDesktopComponent () {
84         return layeredPane;
85     }
86
87     public Dimension JavaDoc 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 JavaDoc(width, height);
97     }
98     
99    public void setSplitRoot (ViewElement splitRoot) {
100  
101         this.splitRoot = splitRoot;
102         if (splitRoot != null) {
103 // System.out.println("desktopimpl: splitroot comp");
104
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 JavaDoc 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 JavaDoc constr = new GridBagConstraints JavaDoc();
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 JavaDoc<SlidingView> slidingViews = getSlidingViews();
149         if (slidingViews.contains(view)) {
150             return;
151         }
152         slidingViews.add(view);
153         GridBagConstraints JavaDoc constraint = new GridBagConstraints JavaDoc();
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         // #45033 fix - invalidate isn't enough, revalidate is correct
173
layeredPane.revalidate();
174     }
175     
176     public void removeSlidingView (SlidingView view) {
177         Set JavaDoc slidingViews = getSlidingViews();
178         if (!slidingViews.contains(view)) {
179             return;
180         }
181         slidingViews.remove(view);
182         desktop.remove(view.getComponent());
183         checkCurSlide();
184         // #45033 fix - invalidate isn't enough, revalidate is correct
185
layeredPane.revalidate();
186     }
187     
188     private void checkCurSlide() {
189         if (curSlideIn != null) {
190             SlidingView curView = null;
191             Component JavaDoc curSlideComp = curSlideIn.getComponent();
192             for (Iterator JavaDoc iter = slidingViews.iterator(); iter.hasNext(); ) {
193                 curView = (SlidingView)iter.next();
194                 if (curView.getTopComponents().contains(curSlideComp)) {
195                     return;
196                 }
197             }
198             // currently slided component not found in view data, so remove
199
layeredPane.remove(curSlideComp);
200         }
201     }
202     
203     public void performSlideIn(SlideOperation operation, Rectangle JavaDoc editorBounds) {
204         Rectangle JavaDoc 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 JavaDoc editorBounds) {
212         Rectangle JavaDoc 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 JavaDoc editorBounds) {
223         operation.setFinishBounds(computeLastButtonBounds(operation));
224         Rectangle JavaDoc screenStart = operation.getStartBounds();
225         operation.setStartBounds(convertRectFromScreen(layeredPane, screenStart));
226         
227         performSlide(operation);
228     }
229     
230     public void performSlideIntoDesktop(SlideOperation operation, Rectangle JavaDoc editorBounds) {
231         Rectangle JavaDoc screenStart = operation.getStartBounds();
232         operation.setStartBounds(convertRectFromScreen(layeredPane, screenStart));
233         Rectangle JavaDoc 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 JavaDoc side, Rectangle JavaDoc editorBounds ) {
244         Component JavaDoc tabbed = findTabbed( tc );
245         if( null != tabbed ) {
246             SlideOperation operation = SlideOperationFactory.createSlideResize( tabbed, side );
247             Rectangle JavaDoc slideInBounds = computeSlideInBounds(operation, editorBounds);
248             operation.setFinishBounds(slideInBounds);
249             performSlide(operation);
250         }
251     }
252     
253     private Component JavaDoc findTabbed( Component JavaDoc 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     /************** private stuff ***********/
263     
264     private void performSlide(SlideOperation operation) {
265         operation.run(layeredPane, Integer.valueOf(102));
266     }
267     
268     private Rectangle JavaDoc convertRectFromScreen (Component JavaDoc comp, Rectangle JavaDoc screenRect) {
269         // safety call to not crash on null bounds
270
if (screenRect == null) {
271             screenRect = new Rectangle JavaDoc(0, 0, 0, 0);
272         }
273         Point JavaDoc leftTop = screenRect.getLocation();
274         SwingUtilities.convertPointFromScreen(leftTop, comp);
275         
276         return new Rectangle JavaDoc(leftTop, screenRect.getSize());
277     }
278     
279     /** Updates slide operation by setting correct finish bounds of component
280      * which will component have after slide in. It should cover whole one side
281      * of desktop, but overlap editor area only if necessary.
282      */

283     private Rectangle JavaDoc computeSlideInBounds(SlideOperation operation, Rectangle JavaDoc editorBounds) {
284         Point JavaDoc editorLeftTop = editorBounds.getLocation();
285         SwingUtilities.convertPointFromScreen(editorLeftTop, layeredPane);
286         editorBounds = new Rectangle JavaDoc(editorLeftTop, editorBounds.getSize());
287         String JavaDoc side = operation.getSide();
288         SlidingView view = findView(side);
289         Rectangle JavaDoc splitRootRect = viewComponent.getBounds();
290         Rectangle JavaDoc result = new Rectangle JavaDoc();
291         Rectangle JavaDoc viewRect = view.getComponent().getBounds();
292         Dimension JavaDoc 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" ) ) ) // NOI18N
297
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                 // make sure we are not bigger than the current window..
309
result.width = splitRootRect.width;
310             }
311         } else if (Constants.RIGHT.equals(side)) {
312             int rightLimit = /*layeredPane.getBounds().x + */ 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                 // make sure we are not bigger than the current window..
317
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                 // make sure we are not bigger than the current window..
330
result.y = 0;
331             }
332             result.height = lowerLimit - result.y;
333             result.width = splitRootRect.width;
334         }
335         return result;
336     }
337     
338     private Rectangle JavaDoc computeThinBounds (SlideOperation operation, Rectangle JavaDoc slideInFinish) {
339         String JavaDoc side = operation.getSide();
340         Rectangle JavaDoc result = new Rectangle JavaDoc();
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     /** Returns bounds of last button in sliding view to which given
363      * operation belongs. Bounds are relative to desktop layered pane.
364      */

365     private Rectangle JavaDoc computeLastButtonBounds(SlideOperation operation) {
366         String JavaDoc side = operation.getSide();
367         SlidingView view = findView(side);
368         Rectangle JavaDoc screenRect = view.getTabBounds(view.getTopComponents().size() - 1);
369         Point JavaDoc 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 JavaDoc(leftTop, screenRect.getSize());
378     }
379     
380     private SlidingView findView (String JavaDoc side) {
381         SlidingView view;
382         for (Iterator JavaDoc 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 JavaDoc<SlidingView> getSlidingViews() {
392         if (slidingViews == null) {
393             slidingViews = new HashSet JavaDoc<SlidingView>(5);
394         }
395         return slidingViews;
396     }
397     
398     
399     /** Special layout manager for layered pane, just keeps desktop panel
400      * coreving whole layered pane and if sliding is in progress, it keeps
401      * slided component along right edge.
402      */

403     private final class LayeredLayout implements LayoutManager JavaDoc {
404         private Dimension JavaDoc lastSize;
405         public void layoutContainer(Container JavaDoc parent) {
406             Dimension JavaDoc size = parent.getSize();
407             desktop.setBounds(0, 0, size.width, size.height);
408             desktop.invalidate();
409             desktop.validate();
410             // keep right bounds of slide in progress
411
if ((curSlideIn != null) && curSlideIn.getComponent().isVisible()) {
412                 String JavaDoc side = curSlideIn.getSide();
413                 SlidingView curView = findView(side);
414                 // #43865, #49320 - sliding wiew or viewcomponent could be removed by closing
415
if (curView != null && viewComponent != null) {
416                     Component JavaDoc slidedComp = curSlideIn.getComponent();
417                     Rectangle JavaDoc result = slidedComp.getBounds();
418                     Rectangle JavaDoc viewRect = curView.getComponent().getBounds();
419                     Dimension JavaDoc viewPrefSize = curView.getComponent().getPreferredSize();
420                     Rectangle JavaDoc 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                                 // make sure we are not bigger than the current window..
428
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 JavaDoc minimumLayoutSize(Container JavaDoc parent) {
470             return desktop.getMinimumSize();
471         }
472         
473         public Dimension JavaDoc preferredLayoutSize(Container JavaDoc parent) {
474             return desktop.getPreferredSize();
475         }
476         
477         public void addLayoutComponent(String JavaDoc name, Component JavaDoc comp) {
478             // no op, slided components are added/removed via SlideOperation.run() calls.
479
}
480         
481         public void removeLayoutComponent(Component JavaDoc comp) {
482             // no op, slided components are added/removed via SlideOperation.run() calls.
483
}
484         
485     } // end of LayeredLayout
486

487 }
488
Popular Tags