KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > actions > RecentViewListAction


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.actions;
22
23 import java.awt.Frame JavaDoc;
24 import java.awt.Image JavaDoc;
25 import java.awt.KeyboardFocusManager JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.InputEvent JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.beans.PropertyChangeEvent JavaDoc;
30 import java.beans.PropertyChangeListener JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import javax.swing.AbstractAction JavaDoc;
35 import javax.swing.ImageIcon JavaDoc;
36 import javax.swing.KeyStroke JavaDoc;
37 import org.netbeans.core.windows.Constants;
38 import org.netbeans.core.windows.ModeImpl;
39 import org.netbeans.core.windows.WindowManagerImpl;
40 import org.netbeans.core.windows.view.ui.KeyboardPopupSwitcher;
41 import org.netbeans.swing.popupswitcher.SwitcherTableItem;
42 import org.openide.util.NbBundle;
43 import org.openide.util.Utilities;
44 import org.openide.util.WeakListeners;
45 import org.openide.windows.TopComponent;
46 import org.openide.windows.WindowManager;
47
48 /**
49  * Invokes Recent View List
50  *
51  * @author Marek Slama
52  */

53 public final class RecentViewListAction extends AbstractAction JavaDoc
54         implements PropertyChangeListener JavaDoc {
55     
56     /** Creates a new instance of RecentViewListAction */
57     public RecentViewListAction() {
58         putValue(NAME, NbBundle.getMessage(RecentViewListAction.class, "CTL_RecentViewListAction"));
59         TopComponent.getRegistry().addPropertyChangeListener(
60                 WeakListeners.propertyChange(this, TopComponent.getRegistry()));
61         updateEnabled();
62     }
63     
64     public void actionPerformed(ActionEvent JavaDoc evt) {
65         TopComponent[] documents = getRecentDocuments();
66         
67         if (documents.length < 2) {
68             return;
69         }
70         
71         if(!"immediately".equals(evt.getActionCommand()) && // NOI18N
72
!(evt.getSource() instanceof javax.swing.JMenuItem JavaDoc)) {
73             // #46800: fetch key directly from action command
74
KeyStroke JavaDoc keyStroke = Utilities.stringToKey(evt.getActionCommand());
75             
76             if(keyStroke != null) {
77                 int triggerKey = keyStroke.getKeyCode();
78                 int reverseKey = KeyEvent.VK_SHIFT;
79                 int releaseKey = 0;
80                 
81                 int modifiers = keyStroke.getModifiers();
82                 if((InputEvent.CTRL_MASK & modifiers) != 0) {
83                     releaseKey = KeyEvent.VK_CONTROL;
84                 } else if((InputEvent.ALT_MASK & modifiers) != 0) {
85                     releaseKey = KeyEvent.VK_ALT;
86                 } else if((InputEvent.META_MASK & modifiers) != 0) {
87                     releaseKey = InputEvent.META_MASK;
88                 }
89                 
90                 if(releaseKey != 0) {
91                     if (!KeyboardPopupSwitcher.isShown()) {
92                         Frame JavaDoc owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow()
93                         instanceof Frame JavaDoc ?
94                             (Frame JavaDoc) KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow()
95                             : WindowManager.getDefault().getMainWindow();
96                         KeyboardPopupSwitcher.selectItem(
97                                 createSwitcherItems(documents),
98                                 releaseKey, triggerKey);
99                     }
100                     return;
101                 }
102             }
103         }
104         
105         TopComponent tc = documents[1];
106         // #37226 Unmaximized the other mode if needed.
107
WindowManagerImpl wm = WindowManagerImpl.getInstance();
108         ModeImpl mode = (ModeImpl) wm.findMode(tc);
109         if(mode != null && mode != wm.getCurrentMaximizedMode()) {
110             wm.switchMaximizedMode(null);
111         }
112         
113         tc.requestActive();
114     }
115     
116     private SwitcherTableItem[] createSwitcherItems(TopComponent[] tcs) {
117         SwitcherTableItem[] items = new SwitcherTableItem[tcs.length];
118         for (int i = 0; i < tcs.length; i++) {
119             TopComponent tc = tcs[i];
120             String JavaDoc name = tc.getDisplayName();
121             if (name == null || name.trim().length() == 0) {
122                 name = tc.getName();
123             }
124             String JavaDoc htmlName = tc.getHtmlDisplayName();
125             if (htmlName == null) {
126                 htmlName = name;
127             }
128             Image JavaDoc image = tc.getIcon();
129             String JavaDoc description = tc.getToolTipText();
130             ImageIcon JavaDoc imageIcon = (image != null ? new ImageIcon JavaDoc(image) : null);
131             items[i] = new SwitcherTableItem(
132                     new ActivatableTC(tc),
133                     name,
134                     htmlName,
135                     imageIcon,
136                     false,
137                     description != null ? description : name);
138         }
139         return items;
140     }
141     
142     private class ActivatableTC implements SwitcherTableItem.Activatable {
143         private TopComponent tc;
144         private ActivatableTC(TopComponent tc) {
145             this.tc = tc;
146         }
147         public void activate() {
148             if (tc != null) {
149                 tc.requestActive();
150             }
151         }
152     }
153     
154     
155     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
156         if(TopComponent.Registry.PROP_OPENED.equals(evt.getPropertyName())) {
157             updateEnabled();
158         }
159     }
160     
161     /** Only here for fix #41477:, called from layer.xml:
162      * For KDE on unixes, Ctrl+TAB is occupied by OS,
163      * so we also register Ctrl+BACk_QUOTE as recent view list action shortcut.
164      * For other OS's, Ctrl+TAB is the only default, because we create link
165      * not pointing to anything by returning null
166      */

167     public static String JavaDoc getStringRep4Unixes() {
168         if (Utilities.isUnix()) {
169             return "Actions/Window/org-netbeans-core-windows-actions-RecentViewListAction.instance"; //NOI18N
170
}
171         return null;
172     }
173     
174     /**
175      * Update enable state of this action.
176      */

177     private void updateEnabled() {
178         setEnabled(isMoreThanOneDocOpened());
179     }
180     
181     private boolean isMoreThanOneDocOpened() {
182         for(Iterator JavaDoc it = WindowManagerImpl.getInstance().getModes().iterator(); it.hasNext(); ) {
183             ModeImpl mode = (ModeImpl)it.next(); {
184                 if (mode.getKind() == Constants.MODE_KIND_EDITOR)
185                     return (mode.getOpenedTopComponents().size() > 1);
186             }
187         }
188         return false;
189     }
190     
191     private TopComponent[] getRecentDocuments() {
192         WindowManagerImpl wm = WindowManagerImpl.getInstance();
193         TopComponent[] documents = wm.getRecentViewList();
194         
195         List JavaDoc<TopComponent> docsList = new ArrayList JavaDoc<TopComponent>();
196         for (int i = 0; i < documents.length; i++) {
197             TopComponent tc = documents[i];
198             if (tc == null) {
199                 continue;
200             }
201             ModeImpl mode = (ModeImpl)wm.findMode(tc);
202             if (mode == null) {
203                 continue;
204             }
205             
206             if (mode.getKind() == Constants.MODE_KIND_EDITOR) {
207                 docsList.add(tc);
208             }
209         }
210         return docsList.toArray(new TopComponent[0]);
211     }
212 }
213
214
Popular Tags