KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > multiview > EditorsAction


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.multiview;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import javax.swing.AbstractAction JavaDoc;
26 import javax.swing.ButtonGroup JavaDoc;
27 import javax.swing.JComponent JavaDoc;
28 import javax.swing.JMenu JavaDoc;
29 import javax.swing.JMenuItem JavaDoc;
30 import javax.swing.JRadioButtonMenuItem JavaDoc;
31 import org.netbeans.core.api.multiview.MultiViewHandler;
32 import org.netbeans.core.api.multiview.MultiViewPerspective;
33 import org.netbeans.core.api.multiview.MultiViews;
34 import org.openide.awt.DynamicMenuContent;
35 import org.openide.awt.Mnemonics;
36 import org.openide.text.CloneableEditorSupport;
37 import org.openide.util.NbBundle;
38 import org.openide.util.actions.Presenter;
39 import org.openide.windows.Mode;
40 import org.openide.windows.TopComponent;
41 import org.openide.windows.WindowManager;
42
43 /**
44  * Action to show in main menu to switch active view.
45  * @author mkleint
46  */

47 public class EditorsAction extends AbstractAction JavaDoc
48                                 implements Presenter.Menu {
49                                     
50     public EditorsAction() {
51         super(NbBundle.getMessage(EditorsAction.class, "CTL_EditorsAction"));
52     }
53     
54     public void actionPerformed(ActionEvent JavaDoc ev) {
55         assert false;// no operation
56
}
57     
58     public JMenuItem JavaDoc getMenuPresenter() {
59         JMenu JavaDoc menu = new UpdatingMenu();
60         String JavaDoc label = NbBundle.getMessage(EditorsAction.class, "CTL_EditorsAction");
61         Mnemonics.setLocalizedText(menu, label);
62         return menu;
63     }
64     
65     private static final class UpdatingMenu extends JMenu JavaDoc implements DynamicMenuContent {
66         
67         public JComponent JavaDoc[] synchMenuPresenters(JComponent JavaDoc[] items) {
68             return getMenuPresenters();
69         }
70
71         public JComponent JavaDoc[] getMenuPresenters() {
72             Mode mode = WindowManager.getDefault().findMode(CloneableEditorSupport.EDITOR_MODE);
73             removeAll();
74             if (mode != null) {
75                 TopComponent tc = mode.getSelectedTopComponent();
76                 if (tc != null) {
77                     setEnabled(true);
78                     MultiViewHandler handler = MultiViews.findMultiViewHandler(tc);
79                     if (handler != null) {
80                         final WeakReference JavaDoc handlerRef = new WeakReference JavaDoc(handler);
81                         ButtonGroup JavaDoc group = new ButtonGroup JavaDoc();
82                         MultiViewPerspective[] pers = handler.getPerspectives();
83                         for (int i = 0; i < pers.length; i++) {
84                             MultiViewPerspective thisPers = pers[i];
85                             final WeakReference JavaDoc persRef = new WeakReference JavaDoc(thisPers);
86                             
87                             JRadioButtonMenuItem JavaDoc item = new JRadioButtonMenuItem JavaDoc(thisPers.getDisplayName());
88                             item.addActionListener(new ActionListener JavaDoc() {
89                                 public void actionPerformed(ActionEvent JavaDoc event) {
90                                     //#88626 prevent a memory leak
91
MultiViewHandler handler = (MultiViewHandler)handlerRef.get();
92                                     MultiViewPerspective thisPers = (MultiViewPerspective)persRef.get();
93                                     if (handler != null && thisPers != null) {
94                                         handler.requestActive(thisPers);
95                                     }
96                                 }
97                             });
98                             if (thisPers.getDisplayName().equals(handler.getSelectedPerspective().getDisplayName())) {
99                                 item.setSelected(true);
100                             }
101                             group.add(item);
102                             add(item);
103                         }
104                     } else { // handler == null
105
JRadioButtonMenuItem JavaDoc but = new JRadioButtonMenuItem JavaDoc(NbBundle.getMessage(EditorsAction.class, "EditorsAction.source"));
106                         but.setSelected(true);
107                         add(but);
108                     }
109                 } else { // tc == null
110
setEnabled(false);
111                 }
112             } else { // mode == null
113
setEnabled(false);
114             }
115             return new JComponent JavaDoc[] {this};
116         }
117         
118     }
119     
120 }
121
Popular Tags