Lines 44-61
Link Here
|
44 |
|
44 |
|
45 |
package org.netbeans.modules.project.ui.actions; |
45 |
package org.netbeans.modules.project.ui.actions; |
46 |
|
46 |
|
47 |
import java.awt.Toolkit; |
|
|
48 |
import java.beans.PropertyChangeEvent; |
47 |
import java.beans.PropertyChangeEvent; |
49 |
import java.beans.PropertyChangeListener; |
48 |
import java.beans.PropertyChangeListener; |
50 |
import java.text.MessageFormat; |
49 |
import java.text.MessageFormat; |
51 |
import java.util.Arrays; |
50 |
import java.util.Arrays; |
|
|
51 |
import java.util.LinkedList; |
52 |
import javax.swing.Icon; |
52 |
import javax.swing.Icon; |
53 |
import org.netbeans.api.project.Project; |
53 |
import org.netbeans.api.project.Project; |
54 |
import org.netbeans.api.project.ProjectUtils; |
|
|
55 |
import org.netbeans.api.project.ui.OpenProjects; |
54 |
import org.netbeans.api.project.ui.OpenProjects; |
56 |
import org.netbeans.modules.project.ui.OpenProjectList; |
55 |
import org.netbeans.modules.project.ui.OpenProjectList; |
57 |
import static org.netbeans.modules.project.ui.actions.Bundle.*; |
56 |
import static org.netbeans.modules.project.ui.actions.Bundle.*; |
58 |
import org.netbeans.spi.project.ActionProvider; |
|
|
59 |
import org.netbeans.spi.project.ui.support.ProjectActionPerformer; |
57 |
import org.netbeans.spi.project.ui.support.ProjectActionPerformer; |
60 |
import org.openide.DialogDisplayer; |
58 |
import org.openide.DialogDisplayer; |
61 |
import org.openide.NotifyDescriptor; |
59 |
import org.openide.NotifyDescriptor; |
Lines 66-74
Link Here
|
66 |
import org.openide.util.NbBundle.Messages; |
64 |
import org.openide.util.NbBundle.Messages; |
67 |
import org.openide.util.WeakListeners; |
65 |
import org.openide.util.WeakListeners; |
68 |
|
66 |
|
69 |
/** Invokes command on the main project. |
67 |
/** |
70 |
* |
68 |
* Similar to {@link ProjectAction} but has a different selection model. |
71 |
* @author Pet Hrebejk |
69 |
* First uses the main project, if set. |
|
|
70 |
* Else uses the selected projects, if any. |
71 |
* Finally, if just one project is open, uses that. |
72 |
*/ |
72 |
*/ |
73 |
public class MainProjectAction extends LookupSensitiveAction implements PropertyChangeListener { |
73 |
public class MainProjectAction extends LookupSensitiveAction implements PropertyChangeListener { |
74 |
|
74 |
|
Lines 85-91
Link Here
|
85 |
} |
85 |
} |
86 |
|
86 |
|
87 |
@SuppressWarnings("LeakingThisInConstructor") |
87 |
@SuppressWarnings("LeakingThisInConstructor") |
88 |
public MainProjectAction(String command, ProjectActionPerformer performer, String name, Icon icon) { |
88 |
private MainProjectAction(String command, ProjectActionPerformer performer, String name, Icon icon) { |
89 |
|
89 |
|
90 |
super(icon, null, new Class<?>[] {Project.class, DataObject.class}); |
90 |
super(icon, null, new Class<?>[] {Project.class, DataObject.class}); |
91 |
this.command = command; |
91 |
this.command = command; |
Lines 117-170
Link Here
|
117 |
|
117 |
|
118 |
@Messages("MainProjectAction.no_main=Set a main project, or select one project or project file, or keep just one project open.") |
118 |
@Messages("MainProjectAction.no_main=Set a main project, or select one project or project file, or keep just one project open.") |
119 |
public @Override void actionPerformed(Lookup context) { |
119 |
public @Override void actionPerformed(Lookup context) { |
120 |
// first try to find main project |
120 |
Project mainProject = OpenProjectList.getDefault().getMainProject(); |
121 |
Project p = OpenProjectList.getDefault().getMainProject(); |
121 |
Project[] projects = selection(mainProject, context); |
122 |
|
|
|
123 |
// then try to find some selected project |
124 |
if (p == null) { |
125 |
Project[] projects = ActionsUtil.getProjectsFromLookup(context, command); |
126 |
if (projects.length == 1) { |
127 |
p = projects[0]; |
128 |
} |
129 |
} |
130 |
|
131 |
// then if there is only one project opened in IDE - use it |
132 |
if (p == null) { |
133 |
Project[] projects = OpenProjects.getDefault().getOpenProjects(); |
134 |
if (projects.length == 1) { |
135 |
p = projects[0]; |
136 |
} |
137 |
} |
138 |
|
122 |
|
139 |
// if no main project or no selected or more than one project opened, |
123 |
// if no main project or no selected or more than one project opened, |
140 |
// then show warning |
124 |
// then show warning |
141 |
if (p == null) { |
125 |
if (projects.length == 0) { |
142 |
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(MainProjectAction_no_main(), NotifyDescriptor.WARNING_MESSAGE)); |
126 |
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(MainProjectAction_no_main(), NotifyDescriptor.WARNING_MESSAGE)); |
143 |
return; |
127 |
return; |
144 |
} |
128 |
} |
145 |
|
129 |
|
146 |
if ( command != null ) { |
130 |
if (command != null && projects.length > 0) { |
147 |
ActionProvider ap = p.getLookup().lookup(ActionProvider.class); |
131 |
ProjectAction.runSequentially(new LinkedList<Project>(Arrays.asList(projects)), this, command); |
148 |
if (ap != null) { |
132 |
} else if (performer != null && projects.length == 1) { |
149 |
if (Arrays.asList(ap.getSupportedActions()).contains(command)) { |
133 |
performer.perform(projects[0]); |
150 |
ap.invokeAction(command, Lookup.EMPTY); |
|
|
151 |
} else { |
152 |
// #47160: was a supported command (e.g. on a freeform project) but was then removed. |
153 |
Toolkit.getDefaultToolkit().beep(); |
154 |
refreshView(null, false); |
155 |
} |
156 |
} |
157 |
} |
158 |
else { |
159 |
performer.perform( p ); |
160 |
} |
134 |
} |
161 |
} |
135 |
} |
162 |
|
136 |
|
163 |
|
|
|
164 |
// Private methods --------------------------------------------------------- |
165 |
|
166 |
// Implementation of PropertyChangeListener -------------------------------- |
167 |
|
168 |
public @Override void propertyChange( PropertyChangeEvent evt ) { |
137 |
public @Override void propertyChange( PropertyChangeEvent evt ) { |
169 |
if (OpenProjectList.PROPERTY_MAIN_PROJECT.equals(evt.getPropertyName()) || |
138 |
if (OpenProjectList.PROPERTY_MAIN_PROJECT.equals(evt.getPropertyName()) || |
170 |
OpenProjectList.PROPERTY_OPEN_PROJECTS.equals(evt.getPropertyName())) { |
139 |
OpenProjectList.PROPERTY_OPEN_PROJECTS.equals(evt.getPropertyName())) { |
Lines 172-221
Link Here
|
172 |
} |
141 |
} |
173 |
} |
142 |
} |
174 |
|
143 |
|
|
|
144 |
private Project[] selection(Project mainProject, Lookup context) { |
145 |
if (mainProject != null) { |
146 |
return new Project[] {mainProject}; |
147 |
} |
148 |
Lookup theContext = context; |
149 |
if (theContext == null) { |
150 |
theContext = LastActivatedWindowLookup.INSTANCE; |
151 |
} |
152 |
if (theContext != null) { |
153 |
Project[] projects = ActionsUtil.getProjectsFromLookup(theContext, command); |
154 |
if (projects.length > 0) { |
155 |
return projects; |
156 |
} |
157 |
} |
158 |
Project[] projects = OpenProjects.getDefault().getOpenProjects(); |
159 |
if (projects.length == 1) { |
160 |
return projects; |
161 |
} |
162 |
return new Project[0]; |
163 |
} |
164 |
|
175 |
private void refreshView(final Lookup context, boolean immediate) { |
165 |
private void refreshView(final Lookup context, boolean immediate) { |
176 |
Runnable r= new Runnable() { |
166 |
Runnable r= new Runnable() { |
177 |
public @Override void run() { |
167 |
public @Override void run() { |
178 |
|
168 |
|
179 |
Project p = OpenProjectList.getDefault().getMainProject(); |
169 |
Project mainProject = OpenProjectList.getDefault().getMainProject(); |
180 |
Lookup theContext = context; |
170 |
Project[] projects = selection(mainProject, context); |
181 |
|
171 |
|
182 |
if (p == null) { |
172 |
final String presenterName = getPresenterName(name, mainProject, projects); |
183 |
if (theContext == null) { |
|
|
184 |
theContext = LastActivatedWindowLookup.INSTANCE; |
185 |
} |
186 |
if (theContext != null) { |
187 |
Project[] projects = ActionsUtil.getProjectsFromLookup(theContext, command); |
188 |
if (projects.length == 1) { |
189 |
p = projects[0]; |
190 |
} |
191 |
} |
192 |
} |
193 |
|
194 |
if (p == null) { |
195 |
Project[] projects = OpenProjects.getDefault().getOpenProjects(); |
196 |
if (projects.length == 1) { |
197 |
p = projects[0]; |
198 |
} |
199 |
} |
200 |
|
201 |
Project mainProject = OpenProjectList.getDefault().getMainProject(); |
202 |
|
203 |
final String presenterName = getPresenterName(name, mainProject, p); |
204 |
final boolean enabled; |
173 |
final boolean enabled; |
205 |
|
174 |
|
206 |
if ( command == null ) { |
175 |
if ( command == null ) { |
207 |
enabled = performer.enable(p); |
176 |
enabled = projects.length == 1 && performer.enable(projects[0]); |
208 |
} |
177 |
} |
209 |
else { |
178 |
else if (projects.length == 0) { |
210 |
if ( p == null ) { |
179 |
enabled = false; |
211 |
enabled = false; |
180 |
} else { |
|
|
181 |
boolean e = true; |
182 |
for (Project p : projects) { |
183 |
if (!ActionsUtil.commandSupported(p, command, Lookup.EMPTY)) { |
184 |
e = false; |
185 |
break; |
186 |
} |
212 |
} |
187 |
} |
213 |
else if ( ActionsUtil.commandSupported ( p, command, Lookup.EMPTY ) ) { |
188 |
enabled = e; |
214 |
enabled = true; |
|
|
215 |
} |
216 |
else { |
217 |
enabled = false; |
218 |
} |
219 |
} |
189 |
} |
220 |
|
190 |
|
221 |
Mutex.EVENT.writeAccess(new Runnable() { |
191 |
Mutex.EVENT.writeAccess(new Runnable() { |
Lines 234-255
Link Here
|
234 |
} |
204 |
} |
235 |
} |
205 |
} |
236 |
|
206 |
|
237 |
private String getPresenterName(String name, Project mPrj, Project cPrj) { |
207 |
private String getPresenterName(String name, Project mPrj, Project[] cPrj) { |
238 |
String toReturn = ""; |
208 |
if (name == null) { |
239 |
Object[] formatterArgs; |
209 |
return ""; |
240 |
if (mPrj == null) { |
210 |
} else if (mPrj == null) { |
241 |
if (cPrj == null) { |
211 |
return ActionsUtil.formatProjectSensitiveName(name, cPrj); |
242 |
formatterArgs = new Object[] { 0 }; |
|
|
243 |
} else { |
244 |
formatterArgs = new Object[] { 1, ProjectUtils.getInformation(cPrj).getDisplayName() }; |
245 |
} |
246 |
} else { |
212 |
} else { |
247 |
formatterArgs = new Object[] { -1 }; |
213 |
return MessageFormat.format(name, -1); |
248 |
} |
214 |
} |
249 |
if (name != null) { |
|
|
250 |
toReturn = MessageFormat.format(name, formatterArgs); |
251 |
} |
252 |
return toReturn; |
253 |
} |
215 |
} |
254 |
|
216 |
|
255 |
@Override |
217 |
@Override |