Lines 53-58
Link Here
|
53 |
import java.util.List; |
53 |
import java.util.List; |
54 |
import java.util.Set; |
54 |
import java.util.Set; |
55 |
import java.util.TreeSet; |
55 |
import java.util.TreeSet; |
|
|
56 |
import org.netbeans.api.project.Project; |
56 |
import org.openide.awt.ActionID; |
57 |
import org.openide.awt.ActionID; |
57 |
import org.openide.awt.ActionReference; |
58 |
import org.openide.awt.ActionReference; |
58 |
import org.openide.awt.ActionReferences; |
59 |
import org.openide.awt.ActionReferences; |
Lines 65-79
Link Here
|
65 |
import org.openide.util.Lookup; |
66 |
import org.openide.util.Lookup; |
66 |
import org.openide.util.NbBundle.Messages; |
67 |
import org.openide.util.NbBundle.Messages; |
67 |
import org.openide.util.datatransfer.ExClipboard; |
68 |
import org.openide.util.datatransfer.ExClipboard; |
|
|
69 |
import org.openide.windows.TopComponent; |
68 |
|
70 |
|
69 |
/** |
71 |
/** |
70 |
* Copies the absolute path of selected {@link DataObject}s to the clipboard. |
72 |
* Copies the absolute path of selected {@link DataObject}s or {@link Project}s to the clipboard. |
71 |
* Action is shown in context menu of tab. It support DataShadows, so the path |
73 |
* Action is shown in context menu of tab. It support DataShadows, so the path |
72 |
* of items in the favorites window can be copied properly. It support items |
74 |
* of items in the favorites window can be copied properly. It support items |
73 |
* within JAR-files. |
75 |
* within JAR-files. |
74 |
* |
76 |
* |
75 |
* Note: It does not support selected Projects. |
|
|
76 |
* |
77 |
* @author markiewb |
77 |
* @author markiewb |
78 |
*/ |
78 |
*/ |
79 |
@ActionID( |
79 |
@ActionID( |
Lines 90-107
Link Here
|
90 |
"CTL_CopyPaths=Copy File Path(s)", |
90 |
"CTL_CopyPaths=Copy File Path(s)", |
91 |
"CTL_CopyPath=Copy File Path" |
91 |
"CTL_CopyPath=Copy File Path" |
92 |
}) |
92 |
}) |
93 |
public final class CopyPathToClipboardAction implements ActionListener, |
93 |
public class CopyPathToClipboardAction implements ActionListener, |
94 |
ClipboardOwner { |
94 |
ClipboardOwner { |
95 |
|
95 |
|
96 |
private final List<DataObject> context; |
|
|
97 |
|
98 |
public CopyPathToClipboardAction(List<DataObject> context) { |
99 |
this.context = context; |
100 |
} |
101 |
|
96 |
|
102 |
@Override |
97 |
@Override |
103 |
public void actionPerformed(ActionEvent ev) { |
98 |
public void actionPerformed(ActionEvent ev) { |
104 |
Collection<String> paths = getSelectedPaths(); |
99 |
|
|
|
100 |
Collection<String> paths=new TreeSet<String>(); |
101 |
paths.addAll(getSelectedPathsForDataObjects(getSelectedDataObjects())); |
102 |
paths.addAll(getSelectedPathsForProjects(getSelectedProjects())); |
103 |
|
105 |
StringBuilder sb = new StringBuilder(); |
104 |
StringBuilder sb = new StringBuilder(); |
106 |
int items = 0; |
105 |
int items = 0; |
107 |
for (String path : paths) { |
106 |
for (String path : paths) { |
Lines 120-130
Link Here
|
120 |
} |
119 |
} |
121 |
|
120 |
|
122 |
/** |
121 |
/** |
|
|
122 |
* Can be overridden in test. |
123 |
* @return selected {@link DataObject}s in current {@link TopComponent} |
124 |
*/ |
125 |
Collection<? extends DataObject> getSelectedDataObjects() { |
126 |
return TopComponent.getRegistry().getActivated().getLookup().lookupAll(DataObject.class); |
127 |
} |
128 |
|
129 |
/** |
130 |
* Can be overridden in test. |
131 |
* @return selected {@link Project}s in current {@link TopComponent} |
132 |
*/ |
133 |
Collection<? extends Project> getSelectedProjects() { |
134 |
return TopComponent.getRegistry().getActivated().getLookup().lookupAll(Project.class); |
135 |
} |
136 |
|
137 |
/** |
123 |
* Get paths of selected DataObjects. Prevent duplicates, see #219014. |
138 |
* Get paths of selected DataObjects. Prevent duplicates, see #219014. |
124 |
* |
139 |
* |
125 |
* @return Sorted collection of unique paths. |
140 |
* @return Sorted collection of unique paths. |
126 |
*/ |
141 |
*/ |
127 |
Collection<String> getSelectedPaths() { |
142 |
Collection<String> getSelectedPathsForDataObjects(Collection<? extends DataObject> context) { |
128 |
Set<String> paths = new TreeSet<String>(); |
143 |
Set<String> paths = new TreeSet<String>(); |
129 |
for (DataObject dataObject : context) { |
144 |
for (DataObject dataObject : context) { |
130 |
paths.add(getAbsolutePath(dataObject)); |
145 |
paths.add(getAbsolutePath(dataObject)); |
Lines 193-198
Link Here
|
193 |
} |
208 |
} |
194 |
return dataObject.getPrimaryFile(); |
209 |
return dataObject.getPrimaryFile(); |
195 |
} |
210 |
} |
|
|
211 |
|
212 |
/** |
213 |
* Get the project directory of the given project. |
214 |
* @param project |
215 |
* @return |
216 |
*/ |
217 |
private String getProjectDirectory(final Project project) { |
218 |
try { |
219 |
FileObject projectDirectory = project.getProjectDirectory(); |
220 |
return getNativePath(projectDirectory); |
221 |
} catch (Exception e) { |
222 |
//ignore the exception |
223 |
return null; |
224 |
} |
225 |
} |
226 |
/** |
227 |
* Get paths of selected projects. Prevent duplicates, see #219014. |
228 |
* |
229 |
* @return Sorted collection of unique paths. |
230 |
*/ |
231 |
Collection<String> getSelectedPathsForProjects(Collection<? extends Project> projects) { |
232 |
Set<String> paths = new TreeSet<String>(); |
233 |
for (Project project : projects) { |
234 |
String projectDir = getProjectDirectory(project); |
235 |
if (null != projectDir) { |
236 |
paths.add(projectDir); |
237 |
} |
238 |
} |
239 |
return paths; |
240 |
} |
196 |
|
241 |
|
197 |
/** |
242 |
/** |
198 |
* Sets the clipboard context in textual-format. |
243 |
* Sets the clipboard context in textual-format. |
Lines 202-208
Link Here
|
202 |
@Messages({ |
247 |
@Messages({ |
203 |
"# {0} - copied file path", |
248 |
"# {0} - copied file path", |
204 |
"CTL_Status_CopyToClipboardSingle=Copy to Clipboard: {0}", |
249 |
"CTL_Status_CopyToClipboardSingle=Copy to Clipboard: {0}", |
205 |
"# {0} - numer of copied paths", |
250 |
"# {0} - number of copied paths", |
206 |
"CTL_Status_CopyToClipboardMulti={0} paths were copied to clipboard" |
251 |
"CTL_Status_CopyToClipboardMulti={0} paths were copied to clipboard" |
207 |
}) |
252 |
}) |
208 |
private void setClipboardContents(String content, int items) { |
253 |
private void setClipboardContents(String content, int items) { |