KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > ui > slides > CommandManager


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.slides;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Rectangle JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Point JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.awt.event.MouseEvent JavaDoc;
30 import java.util.Map JavaDoc;
31 import javax.swing.*;
32 import javax.swing.UIManager JavaDoc;
33 import javax.swing.border.Border JavaDoc;
34 import org.netbeans.core.windows.Constants;
35 import org.netbeans.swing.tabcontrol.*;
36 import org.netbeans.swing.tabcontrol.event.TabActionEvent;
37 import org.openide.util.Utilities;
38 import org.openide.windows.TopComponent;
39
40 /*
41  * Helper class to manage slide operations of asociated slide bar.
42  * Handles sliding logic that assures
43  * just one component is slided at the time.
44  * Uses TabbedContainer to represent and display slide component.
45  *
46  * @author Dafe Simonek
47  */

48 final class CommandManager implements ActionListener JavaDoc {
49    
50     /** Asociated slide bar */
51     private final SlideBar slideBar;
52     /** Local tabbed container used to display slided component */
53     private TabbedContainer slidedTabContainer;
54
55     /** Data of slide operation in progress */
56     private Component JavaDoc curSlidedComp;
57     private SlidingButton curSlideButton;
58     private int curSlideOrientation;
59     private int curSlidedIndex;
60     private ResizeGestureRecognizer recog;
61     
62     
63     public CommandManager(SlideBar slideBar) {
64         this.slideBar = slideBar;
65         recog = new ResizeGestureRecognizer(this);
66     }
67    
68     ResizeGestureRecognizer getResizer() {
69         return recog;
70     }
71     
72     public void slideResize(int delta) {
73         if (!isCompSlided()) {
74             return;
75         }
76         SlideOperation op = SlideOperationFactory.createSlideResize(getSlidedTabContainer(), curSlideOrientation);
77         Rectangle JavaDoc finish = getSlidedTabContainer().getBounds(null);
78         String JavaDoc side = orientation2Side(curSlideOrientation);
79         if (Constants.BOTTOM.equals(side)) {
80             finish.height = finish.height - delta;
81             finish.y = finish.y + delta;
82         }
83         if (Constants.RIGHT.equals(side)) {
84             finish.width = finish.width - delta;
85             finish.x = finish.x + delta;
86         }
87         if (Constants.LEFT.equals(side)) {
88             finish.width = finish.width + delta;
89         }
90         op.setFinishBounds(finish);
91         postEvent(new SlideBarActionEvent(slideBar, SlideBar.COMMAND_SLIDE_RESIZE, op));
92         
93     }
94     
95     public void slideIn(int tabIndex) {
96         SlideBarDataModel model = slideBar.getModel();
97         if (isCompSlided()) {
98             if (curSlidedComp != model.getTab(tabIndex).getComponent()) {
99                 // another component requests slide in, so slide out current first
100
slideOut(false, false);
101             }
102         }
103         
104         curSlidedIndex = tabIndex;
105         curSlidedComp = model.getTab(tabIndex).getComponent();
106         curSlideOrientation = model.getOrientation();
107         curSlideButton = slideBar.getButton(tabIndex);
108         TabbedContainer cont = updateSlidedTabContainer(tabIndex);
109         SlideOperation operation = SlideOperationFactory.createSlideIn(
110             cont, curSlideOrientation, true, true);
111         
112         curSlideButton.setSelected(true);
113
114         postEvent(new SlideBarActionEvent(slideBar, SlideBar.COMMAND_SLIDE_IN, operation));
115         recog.attachResizeRecognizer(orientation2Side(curSlideOrientation), cont);
116     }
117     
118     /** Fires slide out operation.
119      * @param requestsActivation true means restore focus to some other view after
120      * slide out, false means no additional reactivation
121      */

122     public void slideOut(boolean requestsActivation, boolean useEffect) {
123         if (!isCompSlided()) {
124             return;
125         }
126         
127         SlideOperation operation = SlideOperationFactory.createSlideOut(
128             getSlidedTabContainer(), curSlideOrientation, useEffect, requestsActivation);
129         
130         curSlideButton.setSelected(false);
131         
132         recog.detachResizeRecognizer(orientation2Side(curSlideOrientation), getSlidedTabContainer());
133         
134         curSlidedComp = null;
135         curSlideButton = null;
136         curSlideOrientation = -1;
137         curSlidedIndex = -1;
138
139         postEvent(new SlideBarActionEvent(slideBar, SlideBar.COMMAND_SLIDE_OUT, operation));
140     }
141     
142     
143     public void slideIntoDesktop(int tabIndex, boolean useEffect) {
144         SlideOperation operation = null;
145         if (isCompSlided()) {
146             operation = SlideOperationFactory.createSlideIntoDesktop(
147                 getSlidedTabContainer(), curSlideOrientation, useEffect);
148         }
149         recog.detachResizeRecognizer(orientation2Side(curSlideOrientation), getSlidedTabContainer());
150         postEvent(new SlideBarActionEvent(slideBar, SlideBar.COMMAND_DISABLE_AUTO_HIDE, operation, null, tabIndex));
151     }
152     
153     public void showPopup(MouseEvent JavaDoc mouseEvent, int tabIndex) {
154         postEvent(new SlideBarActionEvent(slideBar, SlideBar.COMMAND_POPUP_REQUEST, mouseEvent, tabIndex));
155     }
156     
157     protected static String JavaDoc orientation2Side (int orientation) {
158         String JavaDoc side = Constants.LEFT;
159         if (orientation == SlideBarDataModel.WEST) {
160             side = Constants.LEFT;
161         } else if (orientation == SlideBarDataModel.EAST) {
162             side = Constants.RIGHT;
163         } else if (orientation == SlideBarDataModel.SOUTH) {
164             side = Constants.BOTTOM;
165         }
166         return side;
167     }
168     
169     
170     /** Activates or deactivates asociated tabbed container used as
171      * sliding component.
172      */

173     public void setActive(boolean active) {
174         getSlidedTabContainer().setActive(active);
175     }
176     
177     /********* implementation of ActionListener **************/
178     
179     /** Reacts to actions from currently slided tabbed container, forwards
180      * received events to tabbed instance, which ensures that
181      * actions are handled in the same way as usual.
182      */

183     public void actionPerformed(ActionEvent JavaDoc e) {
184         if (TabbedContainer.COMMAND_POPUP_REQUEST.equals(e.getActionCommand())) {
185             TabActionEvent tae = (TabActionEvent) e;
186             if (curSlidedComp != null && curSlidedComp instanceof TopComponent) {
187                 TopComponent tc = (TopComponent)curSlidedComp;
188                 Action[] actions = slideBar.getTabbed().getPopupActions(tc.getActions(), curSlidedIndex);
189                 if (actions == null) {
190                     actions = tc.getActions();
191                 }
192                 
193                 showPopupMenu(
194                     Utilities.actionsToPopup(actions, tc.getLookup()), tae.getMouseEvent().getPoint(), tae.getMouseEvent().getComponent());
195                 
196             }
197         } else if (TabbedContainer.COMMAND_DISABLE_AUTO_HIDE.equals(e.getActionCommand())) {
198             slideIntoDesktop(curSlidedIndex, true);
199         } else if (TabbedContainer.COMMAND_MAXIMIZE.equals(e.getActionCommand())) {
200             //inform the window system that the slided window changes its maximized status
201
postEvent(new SlideBarActionEvent(slideBar, SlideBar.COMMAND_MAXIMIZE, null, null, curSlidedIndex));
202         } else {
203             // convert event - fix index, local tabbed container index isn't right in slide bar context
204
TabActionEvent tae = (TabActionEvent)e;
205             TabActionEvent newEvt = new TabActionEvent(
206                 tae.getSource(), tae.getActionCommand(), curSlidedIndex, tae.getMouseEvent());
207             
208             postEvent(newEvt);
209         }
210     }
211     
212     /************************** non-public stuff **********************/
213
214     private Rectangle JavaDoc getScreenCompRect(Component JavaDoc comp) {
215         Rectangle JavaDoc result = new Rectangle JavaDoc(comp.getLocationOnScreen(), comp.getSize());
216         
217         return result;
218     }
219     
220      private static final boolean NO_POPUP_PLACEMENT_HACK = Boolean.getBoolean("netbeans.popup.no_hack"); // NOI18N
221
// ##########################
222
// copied from TabbedHandler, maybe reuse..
223
//
224

225     /** Shows given popup on given coordinations and takes care about the
226      * situation when menu can exceed screen limits */

227     private static void showPopupMenu (JPopupMenu popup, Point JavaDoc p, Component JavaDoc comp) {
228         if (NO_POPUP_PLACEMENT_HACK) {
229             popup.show(comp, p.x, p.y);
230             return;
231         }
232
233         SwingUtilities.convertPointToScreen (p, comp);
234         Dimension JavaDoc popupSize = popup.getPreferredSize ();
235         Rectangle JavaDoc screenBounds = Utilities.getUsableScreenBounds(comp.getGraphicsConfiguration());
236
237         if (p.x + popupSize.width > screenBounds.x + screenBounds.width) {
238             p.x = screenBounds.x + screenBounds.width - popupSize.width;
239         }
240         if (p.y + popupSize.height > screenBounds.y + screenBounds.height) {
241             p.y = screenBounds.y + screenBounds.height - popupSize.height;
242         }
243
244         SwingUtilities.convertPointFromScreen (p, comp);
245         popup.show(comp, p.x, p.y);
246     }
247
248     
249     private TabbedContainer getSlidedTabContainer () {
250         if (slidedTabContainer == null) {
251             TabDataModel slidedCompModel = new DefaultTabDataModel();
252             slidedTabContainer = new TabbedContainer(slidedCompModel, TabbedContainer.TYPE_VIEW, slideBar);
253             slidedTabContainer.addActionListener(this);
254             Border JavaDoc b = (Border JavaDoc) UIManager.get ("floatingBorder"); //NOI18N
255
if (b != null) {
256                 slidedTabContainer.setBorder (b);
257             }
258             
259             registerEscHandler(slidedTabContainer);
260         }
261         return slidedTabContainer;
262     }
263     
264     private TabbedContainer updateSlidedTabContainer(int tabIndex) {
265         TabbedContainer container = getSlidedTabContainer();
266         TabDataModel containerModel = container.getModel();
267         SlideBarDataModel dataModel = slideBar.getModel();
268         // creating new TabData instead of just referencing
269
// to be able to compare and track changes between models of slide bar and
270
// slided tabbed container
271
TabData origTab = dataModel.getTab(tabIndex);
272         TabData newTab = new TabData(origTab.getUserObject(), origTab.getIcon(),
273                             origTab.getText(), origTab.getTooltip());
274         if (containerModel.size() == 0) {
275             containerModel.addTab(0, newTab);
276         } else {
277             containerModel.setTab(0, newTab);
278         }
279         container.getSelectionModel().setSelectedIndex(0);
280         return container;
281     }
282     
283     private void registerEscHandler (JComponent comp) {
284         comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "slideOut");
285         comp.getActionMap().put("slideOut", escapeAction);
286     }
287     
288
289 /***** dumping info about all registered Esc handlers, could be usable for
290  * debugging
291     
292     private void dumpEscHandlers (JComponent comp) {
293         InputMap map = null;
294         JComponent curChild = null;
295         Component[] children = comp.getComponents();
296         for (int i = 0; i < children.length; i++) {
297             if (children[i] instanceof JComponent) {
298                 curChild =(JComponent)children[i];
299                 dumpItem(curChild);
300                 dumpEscHandlers(curChild);
301             }
302         }
303     }
304     
305     private void dumpItem(JComponent comp) {
306         dumpInnerItem(comp.getInputMap(JComponent.WHEN_FOCUSED), comp.getActionMap(), comp);
307         dumpInnerItem(comp.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT), comp.getActionMap(), comp);
308         dumpInnerItem(comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW), comp.getActionMap(), comp);
309     }
310     
311     private void dumpInnerItem(InputMap map, ActionMap actionMap, JComponent comp) {
312         Object cmdKey = map.get(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0));
313         if (cmdKey != null) {
314             Action action = actionMap.get(cmdKey);
315             if (action.isEnabled()) {
316                 System.out.println("Enabled command found:");
317                 System.out.println("component: " + comp);
318                 System.out.println("command key: " + cmdKey);
319                 System.out.println("action: " + action);
320             } else {
321                 System.out.println("disabled command " + cmdKey);
322             }
323         }
324     }
325  
326  **********/

327     
328     /** @return true if some component is currently slided, it means visible
329      * over another components in desktop, false otherwise
330      */

331     boolean isCompSlided() {
332         return curSlidedComp != null;
333     }
334     
335     /* #return Component that is slided into desktop or null if no component is
336      * slided currently.
337      */

338     Component JavaDoc getSlidedComp() {
339         if (!isCompSlided()) {
340             return null;
341         }
342         return slidedTabContainer;
343     }
344
345     /** Synchronizes its state with current state of data model.
346      * Removes currently slided component if it is no longer present in the model,
347      * also keeps text up to date.
348      */

349     void syncWithModel() {
350         if (curSlidedComp == null) {
351             return;
352         }
353         
354         if (!slideBar.containsComp(curSlidedComp)) {
355             // TBD - here should be closeSlide operation, which means
356
// just remove from desktop
357
slideOut(false, false);
358         } else {
359             // keep title text up to date
360
SlideBarDataModel model = slideBar.getModel();
361             // #46319 - during close, curSlidedIndex may become out of sync,
362
// in which case do nothing
363
if (curSlidedIndex < model.size()) {
364                 String JavaDoc freshText = model.getTab(curSlidedIndex).getText();
365                 TabDataModel slidedModel = getSlidedTabContainer().getModel();
366                 String JavaDoc slidedText = slidedModel.getTab(0).getText();
367                 if (slidedText == null || !slidedText.equals(freshText)) {
368                     slidedModel.setText(0, freshText);
369                     slideBar.repaint();
370                 }
371             }
372         }
373     }
374
375     /** Actually performs sliding related event by sending it to the
376      * winsys through Tabbed instance
377      */

378     private void postEvent(ActionEvent JavaDoc evt) {
379         ((TabbedSlideAdapter)slideBar.getTabbed()).postActionEvent(evt);
380     }
381     
382     private final Action escapeAction = new EscapeAction();
383     
384     private final class EscapeAction extends javax.swing.AbstractAction JavaDoc {
385         public void actionPerformed(ActionEvent JavaDoc e) {
386             slideOut(true, true);
387         }
388     } // end of EscapeAction
389

390     
391 }
392
Popular Tags