KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > actions > MainProjectAction


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.project.ui.actions;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Component JavaDoc;
25 import java.awt.Dialog JavaDoc;
26 import java.awt.Dimension JavaDoc;
27 import java.awt.Graphics JavaDoc;
28 import java.awt.Graphics2D JavaDoc;
29 import java.awt.RenderingHints JavaDoc;
30 import java.awt.Toolkit JavaDoc;
31 import java.awt.event.ActionEvent JavaDoc;
32 import java.awt.event.ActionListener JavaDoc;
33 import java.awt.event.MouseEvent JavaDoc;
34 import java.beans.PropertyChangeEvent JavaDoc;
35 import java.beans.PropertyChangeListener JavaDoc;
36 import java.util.Arrays JavaDoc;
37 import javax.swing.Icon JavaDoc;
38 import javax.swing.JButton JavaDoc;
39 import javax.swing.JMenuItem JavaDoc;
40 import javax.swing.JPanel JavaDoc;
41 import javax.swing.JPopupMenu JavaDoc;
42 import javax.swing.border.EmptyBorder JavaDoc;
43 import javax.swing.event.ChangeEvent JavaDoc;
44 import javax.swing.event.ChangeListener JavaDoc;
45 import org.netbeans.api.project.Project;
46 import org.netbeans.modules.project.ui.NoMainProjectWarning;
47 import org.netbeans.modules.project.ui.OpenProjectList;
48 import org.netbeans.spi.project.ActionProvider;
49 import org.netbeans.spi.project.ProjectConfiguration;
50 import org.netbeans.spi.project.ProjectConfigurationProvider;
51 import org.netbeans.spi.project.ui.support.ProjectActionPerformer;
52 import org.openide.DialogDescriptor;
53 import org.openide.DialogDisplayer;
54 import org.openide.awt.Actions;
55 import org.openide.awt.MouseUtils;
56 import org.openide.awt.ToolbarPool;
57 import org.openide.util.Lookup;
58 import org.openide.util.Mutex;
59 import org.openide.util.NbBundle;
60 import org.openide.util.WeakListeners;
61 import org.openide.util.actions.Presenter;
62 import org.openide.util.lookup.Lookups;
63
64 /** Invokes command on the main project.
65  *
66  * @author Pet Hrebejk
67  */

68 public class MainProjectAction extends BasicAction implements Presenter.Toolbar, PropertyChangeListener JavaDoc {
69     
70     private String JavaDoc command;
71     private ProjectActionPerformer performer;
72     private String JavaDoc name;
73         
74     public MainProjectAction(ProjectActionPerformer performer, String JavaDoc name, Icon JavaDoc icon) {
75         this( null, performer, name, icon );
76     }
77     
78     public MainProjectAction(String JavaDoc command, String JavaDoc name, Icon JavaDoc icon) {
79         this( command, null, name, icon );
80     }
81     
82     public MainProjectAction(String JavaDoc command, ProjectActionPerformer performer, String JavaDoc name, Icon JavaDoc icon) {
83             
84         this.command = command;
85         this.performer = performer;
86         this.name = name;
87         
88         setDisplayName( name );
89         if ( icon != null ) {
90             setSmallIcon( icon );
91         }
92         
93         refreshView();
94         // Start listening on open projects list to correctly enable the action
95
OpenProjectList.getDefault().addPropertyChangeListener( WeakListeners.propertyChange( this, OpenProjectList.getDefault() ) );
96         // XXX #47160: listen to changes in supported commands on current project, when that becomes possible
97
}
98
99     public void actionPerformed( ActionEvent JavaDoc e ) {
100     
101         Project p = OpenProjectList.getDefault().getMainProject();
102         
103         // if no main project than show warning and allow choose a main project
104
if (p == null) {
105             // show warning, if cancel then return
106
if (showNoMainProjectWarning (OpenProjectList.getDefault().getOpenProjects (), name)) {
107                 return ;
108             }
109             p = OpenProjectList.getDefault().getMainProject();
110         }
111
112         if ( command != null ) {
113             ActionProvider ap = (ActionProvider)p.getLookup().lookup( ActionProvider.class );
114             if (Arrays.asList(ap.getSupportedActions()).contains(command)) {
115                 ap.invokeAction(command, Lookup.EMPTY);
116             } else {
117                 // #47160: was a supported command (e.g. on a freeform project) but was then removed.
118
Toolkit.getDefaultToolkit().beep();
119                 refreshView();
120             }
121         }
122         else {
123             performer.perform( p );
124         }
125     }
126         
127        
128     // Private methods ---------------------------------------------------------
129

130     // Implementation of PropertyChangeListener --------------------------------
131

132     public void propertyChange( PropertyChangeEvent JavaDoc evt ) {
133         if ( evt.getPropertyName() == OpenProjectList.PROPERTY_MAIN_PROJECT ||
134              evt.getPropertyName() == OpenProjectList.PROPERTY_OPEN_PROJECTS ) {
135             Mutex.EVENT.readAccess(new Runnable JavaDoc() {
136                 public void run() {
137                     refreshView();
138                 }
139             });
140         }
141     }
142     
143     private void refreshView() {
144         
145         Project p = OpenProjectList.getDefault().getMainProject();
146         boolean noOpenProject = OpenProjectList.getDefault ().getOpenProjects ().length == 0;
147         
148         if ( command == null ) {
149             setEnabled( performer.enable( p ) );
150         }
151         else {
152             if ( p == null ) {
153                 setEnabled( !noOpenProject );
154             }
155             else if ( ActionsUtil.commandSupported ( p, command, Lookup.EMPTY ) ) {
156                 setEnabled( !noOpenProject );
157             }
158             else {
159                 setEnabled( false );
160             }
161         }
162     }
163     
164     private boolean showNoMainProjectWarning(Project[] projects, String JavaDoc action) {
165         boolean canceled;
166         final JButton JavaDoc okButton = new JButton JavaDoc (NbBundle.getMessage (NoMainProjectWarning.class, "LBL_NoMainClassWarning_ChooseMainProject_OK")); // NOI18N
167
okButton.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage (NoMainProjectWarning.class, "AD_NoMainClassWarning_ChooseMainProject_OK"));
168         
169         // no main project set => warning
170
final NoMainProjectWarning panel = new NoMainProjectWarning (projects);
171
172         Object JavaDoc[] options = new Object JavaDoc[] {
173             okButton,
174             DialogDescriptor.CANCEL_OPTION
175         };
176         
177         panel.addChangeListener (new ChangeListener JavaDoc () {
178            public void stateChanged (ChangeEvent JavaDoc e) {
179                if (e.getSource () instanceof MouseEvent JavaDoc && MouseUtils.isDoubleClick (((MouseEvent JavaDoc)e.getSource ()))) {
180                    // click button and the finish dialog with selected class
181
if (panel.getSelectedProject () != null) {
182                        okButton.doClick ();
183                    }
184                } else {
185                    okButton.setEnabled (panel.getSelectedProject () != null);
186                }
187            }
188         });
189         
190         okButton.setEnabled (panel.getSelectedProject () != null);
191         
192         DialogDescriptor desc = new DialogDescriptor (panel,
193                 action == null ?
194                     NbBundle.getMessage(NoMainProjectWarning.class, "CTL_NoMainProjectWarning_Title") :
195                     Actions.cutAmpersand(action),
196             true, options, options[0], DialogDescriptor.DEFAULT_ALIGN, null, null);
197         desc.setMessageType (DialogDescriptor.INFORMATION_MESSAGE);
198         Dialog JavaDoc dlg = DialogDisplayer.getDefault ().createDialog (desc);
199         dlg.setVisible (true);
200         if (desc.getValue() != options[0]) {
201             canceled = true;
202         } else {
203             Project mainProject = panel.getSelectedProject ();
204             OpenProjectList.getDefault ().setMainProject (mainProject);
205             canceled = false;
206         }
207         dlg.dispose();
208
209         return canceled;
210     }
211
212     private static final boolean SHOW_CONFIG_DROPDOWN = Boolean.getBoolean("org.netbeans.modules.project.ui.actions.MainProjectAction.SHOW_CONFIG_DROPDOWN"); // NOI18N
213

214     /**
215      * @see org.openide.awt.Toolbar.Folder#createInstance
216      * @see org.openide.awt.Actions.ButtonBridge#updateButtonIcon
217      */

218     private static final String JavaDoc PREFERRED_ICON_SIZE = "PreferredIconSize"; // NOI18N
219
public Component JavaDoc getToolbarPresenter() {
220         final JButton JavaDoc main = new JButton JavaDoc();
221         Actions.connect(main, this);
222         if (!SHOW_CONFIG_DROPDOWN) {
223             return main;
224         }
225         final JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc());
226         panel.addPropertyChangeListener(PREFERRED_ICON_SIZE, new PropertyChangeListener JavaDoc() {
227             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
228                 main.putClientProperty(PREFERRED_ICON_SIZE, panel.getClientProperty(PREFERRED_ICON_SIZE));
229             }
230         });
231         panel.setBorder(new EmptyBorder JavaDoc(0, 0, 0, 0));
232         panel.add(main, BorderLayout.CENTER);
233         JButton JavaDoc configs = new ConfigButton(main.getPreferredSize().height);
234         panel.add(configs, BorderLayout.LINE_END);
235         return panel;
236     }
237
238     private static final class ArrowIcon implements Icon JavaDoc {
239         private static final int SIZE = 6;
240         private static final int HEIGHT = ToolbarPool.getDefault().getPreferredIconSize();
241         private static final int PAD = 3;
242         public int getIconWidth() {
243             return SIZE * 2 - 1 + PAD * 2;
244         }
245         public int getIconHeight() {
246             return Math.max(SIZE + PAD * 2, HEIGHT);
247         }
248         public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
249             g.setColor(Color.BLACK);
250             int offx = PAD;
251             int offy = (g.getClipBounds().height - SIZE) / 2;
252             ((Graphics2D JavaDoc) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
253             g.fillPolygon(new int[] {offx, offx + SIZE * 2 - 2, offx + SIZE - 1}, new int[] {offy, offy, offy + SIZE - 1}, 3);
254         }
255     }
256
257     private final class ConfigButton extends JButton JavaDoc implements PropertyChangeListener JavaDoc, ActionListener JavaDoc {
258
259         private final PropertyChangeListener JavaDoc pcl = WeakListeners.propertyChange(this, null);
260         private JPopupMenu JavaDoc menu;
261
262         public ConfigButton(int height) {
263             super(new ArrowIcon());
264             OpenProjectList.getDefault().addPropertyChangeListener(pcl);
265             propertyChange(null);
266             addActionListener(this);
267             setPreferredSize(new Dimension JavaDoc(getIcon().getIconWidth(), height));
268             setFocusPainted(false);
269         }
270
271         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
272             String JavaDoc prop = evt != null ? evt.getPropertyName() : null;
273             if (prop == null || prop.equals(OpenProjectList.PROPERTY_MAIN_PROJECT) || prop.equals(ProjectConfigurationProvider.PROP_CONFIGURATIONS)) {
274                 Mutex.EVENT.readAccess(new Runnable JavaDoc() {
275                     public void run() {
276                         boolean v = false;
277                         Project p = OpenProjectList.getDefault().getMainProject();
278                         if (p != null) {
279                             ActionProvider ap = p.getLookup().lookup(ActionProvider.class);
280                             if (ap != null) {
281                                 if (Arrays.asList(ap.getSupportedActions()).contains(command)) {
282                                     ProjectConfigurationProvider<?> pcp = p.getLookup().lookup(ProjectConfigurationProvider.class);
283                                     if (pcp != null) {
284                                         pcp.removePropertyChangeListener(pcl);
285                                         pcp.addPropertyChangeListener(pcl);
286                                         v = pcp.configurationsAffectAction(command) &&
287                                                 // Only show it if there are multiple configs to run.
288
pcp.getConfigurations().size() > 1;
289                                     }
290                                 }
291                             }
292                         }
293                         setVisible(v);
294                     }
295                 });
296             }
297         }
298
299         public void actionPerformed(ActionEvent JavaDoc e) {
300             if (menu != null) {
301                 menu.setVisible(false);
302                 menu = null;
303                 return;
304             }
305             final Project p = OpenProjectList.getDefault().getMainProject();
306             ProjectConfigurationProvider<?> pcp = p.getLookup().lookup(ProjectConfigurationProvider.class);
307             menu = new JPopupMenu JavaDoc();
308             for (final ProjectConfiguration config : pcp.getConfigurations()) {
309                 JMenuItem JavaDoc item = new JMenuItem JavaDoc(config.getDisplayName());
310                 menu.add(item);
311                 item.addActionListener(new ActionListener JavaDoc() {
312                     public void actionPerformed(ActionEvent JavaDoc e) {
313                         menu = null;
314                         p.getLookup().lookup(ActionProvider.class).invokeAction(command, Lookups.singleton(config));
315                     }
316                 });
317             }
318             menu.show(this, 0, getSize().height);
319         }
320
321     }
322
323 }
324
Popular Tags