KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 import org.netbeans.core.windows.Constants;
25 import org.netbeans.core.windows.ModeImpl;
26 import org.netbeans.core.windows.WindowManagerImpl;
27 import org.openide.util.NbBundle;
28 import org.openide.util.WeakListeners;
29 import org.openide.windows.TopComponent;
30
31 import javax.swing.*;
32 import java.beans.PropertyChangeEvent JavaDoc;
33 import java.beans.PropertyChangeListener JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36
37 /**
38  * @author Peter Zavadsky
39  */

40 public class SwitchToRecentDocumentAction extends AbstractAction
41 implements PropertyChangeListener JavaDoc {
42
43     public SwitchToRecentDocumentAction() {
44         putValue(Action.NAME, NbBundle.getMessage(SwitchToRecentDocumentAction.class, "CTL_SwitchToRecentDocumentAction"));
45         TopComponent.getRegistry().addPropertyChangeListener(
46             WeakListeners.propertyChange(this, TopComponent.getRegistry()));
47         updateEnabled();
48     }
49     
50     /** Perform the action. Sets/unsets maximzed mode. */
51     public void actionPerformed(java.awt.event.ActionEvent JavaDoc ev) {
52         WindowManagerImpl wm = WindowManagerImpl.getInstance();
53         TopComponent[] tcs = wm.getRecentViewList();
54         
55         if(tcs.length == 0) {
56             return;
57         }
58
59         for(int i = 0; i < tcs.length; i++) {
60             TopComponent tc = (TopComponent)tcs[i];
61             
62             ModeImpl mode = (ModeImpl)wm.findMode(tc);
63             if(mode == null) {
64                 continue;
65             }
66             
67             if(mode.getKind() == Constants.MODE_KIND_EDITOR) {
68                 // #37030 Unmaximize other mode if needed.
69
if(mode != wm.getCurrentMaximizedMode()) {
70                     wm.switchMaximizedMode(null);
71                 }
72                 tc.requestActive();
73                 break;
74             }
75         }
76     }
77
78     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
79         if(TopComponent.Registry.PROP_OPENED.equals(evt.getPropertyName())) {
80             updateEnabled();
81         }
82     }
83     
84     private void updateEnabled() {
85         for(Iterator JavaDoc it = WindowManagerImpl.getInstance().getModes().iterator(); it.hasNext(); ) {
86             ModeImpl mode = (ModeImpl)it.next();
87             if(mode.getKind() == Constants.MODE_KIND_EDITOR
88             && !mode.getOpenedTopComponents().isEmpty()) {
89                 setEnabled(true);
90                 return;
91             }
92         }
93         setEnabled(false);
94     }
95 }
96
97
Popular Tags