KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > export > ExportAction


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.modules.tasklist.core.export;
21
22
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.awt.*;
26 import java.beans.PropertyChangeEvent JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import javax.swing.*;
29 import javax.swing.event.MenuEvent JavaDoc;
30 import javax.swing.event.MenuListener JavaDoc;
31
32 import org.openide.util.actions.SystemAction;
33 import org.openide.util.actions.Presenter.Menu;
34
35 import org.openide.awt.JMenuPlus;
36 import org.openide.util.HelpCtx;
37 import org.openide.util.NbBundle;
38 import org.openide.util.actions.CallableSystemAction;
39 import org.openide.DialogDisplayer;
40 import org.openide.WizardDescriptor;
41 import org.openide.awt.Mnemonics;
42 import org.openide.windows.TopComponent;
43 import org.openide.windows.WindowManager;
44
45 /**
46  * Export action which provides a pullright menu selecting the Export format
47  * to use.
48  *
49  * @author Tor Norbye
50  * @author Petr Kuzel, toolbar presenter
51  */

52 public final class ExportAction extends CallableSystemAction implements Menu {
53
54     static final long serialVersionUID = 1L;
55
56     /**
57      * Creates an instance
58      */

59     public ExportAction() {
60         WindowManager.getDefault().getRegistry().addPropertyChangeListener(
61             new PropertyChangeListener JavaDoc() {
62                 public void propertyChange(PropertyChangeEvent JavaDoc e) {
63                     update();
64                 }
65             }
66         );
67         update();
68     }
69     
70     /**
71      * Updates the enabled/disabled state of this action.
72      */

73     public void update() {
74         TopComponent tc = WindowManager.getDefault().getRegistry().getActivated();
75         setEnabled(tc instanceof ExportImportProvider);
76     }
77     
78     protected boolean asynchronous() {
79         return false;
80     }
81
82     public String JavaDoc getName() {
83         return NbBundle.getMessage(ExportAction.class, "LBL_Export"); // NOI18N
84
}
85
86     protected String JavaDoc iconResource() {
87         return "org/netbeans/modules/tasklist/core/exportAction.gif"; // NOI18N
88
}
89
90     public HelpCtx getHelpCtx() {
91         return HelpCtx.DEFAULT_HELP;
92         // If you will provide context help then use:
93
// return new HelpCtx (NewTodoItemAction.class);
94
}
95
96     /* Returns a Component that presents the Action, that implements this
97     * interface, in a ToolBar.
98     * @return the Component representation for the Action
99     */

100     public Component getToolbarPresenter() {
101         final Component original = super.getToolbarPresenter();
102         AbstractButton ab = (AbstractButton) original;
103         ab.addActionListener(new ActionListener JavaDoc() {
104             public void actionPerformed(ActionEvent JavaDoc e) {
105                 JPopupMenu menu = new JPopupMenu();
106
107                 // <<<< from menu presenter
108
TopComponent tc = WindowManager.
109                     getDefault().getRegistry().getActivated();
110                 assert tc instanceof ExportImportProvider;
111
112                 ExportImportFormat translators[] =
113                     ((ExportImportProvider) tc).getExportFormats();
114                 assert translators != null;
115
116                 for (int i = 0; i < translators.length; i++) {
117                     menu.add(createMenuItem(translators[i]));
118                 }
119                 // >>>> from menu presenter
120

121                 menu.show(original, original.getWidth(), 0);
122             }
123         });
124         return original;
125     }
126
127     /* Returns a submenu that will present this action in a Menu.
128     * @return the JMenuItem representation for this action
129     */

130     public JMenuItem getMenuPresenter() {
131         JMenu mainItem = new JMenuPlus();
132         Mnemonics.setLocalizedText(mainItem, getName());
133         mainItem.setIcon(SystemAction.get(
134             ExportAction.class).getIcon());
135         HelpCtx.setHelpIDString(mainItem,
136             ExportAction.class.getName());
137         mainItem.addMenuListener(new MainItemListener());
138         return mainItem;
139     }
140
141     /* Returns a submenu that will present this action in a PopupMenu.
142     * @return the JMenuItem representation for this action
143     */

144     public JMenuItem getPopupPresenter() {
145         JMenu mainItem = new JMenuPlus();
146         Mnemonics.setLocalizedText(mainItem, getName());
147         HelpCtx.setHelpIDString(mainItem,
148             ExportAction.class.getName());
149         mainItem.addMenuListener(new MainItemListener());
150         return mainItem;
151     }
152
153     public void performAction() {
154         // all functionality is accomplished by menu listeners
155
}
156
157     final private static String JavaDoc PROPNAME = "translator"; // NOI18N
158

159     private static JMenuItem createMenuItem(ExportImportFormat translator) {
160         JMenuItem curMenuItem = new JMenuItem();
161         Mnemonics.setLocalizedText(curMenuItem, translator.getName());
162         curMenuItem.putClientProperty(PROPNAME, translator);
163         curMenuItem.addActionListener(new ActionListener JavaDoc() {
164             public void actionPerformed(ActionEvent JavaDoc evt) {
165                 TopComponent tc = WindowManager.
166                     getDefault().getRegistry().getActivated();
167                 assert tc instanceof ExportImportProvider;
168                 
169                 JComponent jc = (JComponent) evt.getSource();
170                 ExportImportFormat translator =
171                         (ExportImportFormat) jc.getClientProperty(PROPNAME);
172                 WizardDescriptor wd = translator.getWizard();
173                 Dialog d = DialogDisplayer.getDefault().createDialog(wd);
174                 d.setVisible(true);
175                 if (wd.getValue() == WizardDescriptor.FINISH_OPTION) {
176                     translator.doExportImport((ExportImportProvider) tc, wd);
177                 }
178             }
179         });
180         return curMenuItem;
181     }
182
183     // innerclasses .......................................................
184

185     /** Listens to selecting of main item and expands it to the
186      * submenu of exiting and new modes
187      */

188     private static final class MainItemListener implements MenuListener JavaDoc {
189
190         /** Source of the events */
191         private JMenu menu;
192         
193         public MainItemListener () {}
194
195         public void menuCanceled(MenuEvent JavaDoc e) {
196         }
197
198         public void menuDeselected(MenuEvent JavaDoc e) {
199             JMenu menu = (JMenu) e.getSource();
200             menu.removeAll();
201         }
202
203         public void menuSelected(MenuEvent JavaDoc e) {
204             this.menu = (JMenu) e.getSource();
205
206             // Add the import filters to the menu
207

208             // CRAP. I have to add lookup for these suckers already
209
// since I can't include stuff in the submodules (usertasks)
210

211             // Also note: the specific tasks which can be imported depends
212
// on the window, right? (Well, the list really.)
213
// So perhaps I can just ask the list to provide it for me?
214
//
215

216             // missing common super interface for JPopupMenu and JMenu
217

218             TopComponent tc = WindowManager.
219                 getDefault().getRegistry().getActivated();
220             assert tc instanceof ExportImportProvider;
221             
222             ExportImportFormat[] translators =
223                 ((ExportImportProvider) tc).getExportFormats();
224             assert translators != null;
225             
226             for (int i = 0; i < translators.length; i++) {
227                 menu.add(createMenuItem(translators[i]));
228             }
229         }
230     }
231 }
232
Popular Tags