Lines 1-171
Link Here
|
1 |
/* |
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2013 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2013 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.apisupport.project.ui.customizer; |
43 |
|
44 |
import java.awt.event.ActionEvent; |
45 |
import java.io.IOException; |
46 |
import java.util.ArrayList; |
47 |
import java.util.Collection; |
48 |
import java.util.Iterator; |
49 |
import java.util.SortedSet; |
50 |
import javax.swing.AbstractAction; |
51 |
import javax.swing.Action; |
52 |
import org.netbeans.api.project.FileOwnerQuery; |
53 |
import org.netbeans.api.project.Project; |
54 |
import org.netbeans.api.project.ProjectManager; |
55 |
import org.netbeans.modules.apisupport.project.ApisupportAntUtils; |
56 |
import org.netbeans.modules.apisupport.project.NbModuleProject; |
57 |
import org.openide.ErrorManager; |
58 |
import org.openide.awt.ActionID; |
59 |
import org.openide.awt.ActionReference; |
60 |
import org.openide.awt.ActionReferences; |
61 |
import org.openide.awt.ActionRegistration; |
62 |
import org.openide.awt.DynamicMenuContent; |
63 |
import org.openide.filesystems.FileObject; |
64 |
import org.openide.filesystems.FileUtil; |
65 |
import org.openide.util.ContextAwareAction; |
66 |
import org.openide.util.Lookup; |
67 |
import org.openide.util.Mutex; |
68 |
import org.openide.util.MutexException; |
69 |
import org.openide.util.NbBundle.Messages; |
70 |
|
71 |
/** |
72 |
* Submenu which permits the user to export or unexport appropriate package. |
73 |
* Must be locate in NetBeans Module Project. |
74 |
*/ |
75 |
@ActionID( |
76 |
category = "Project", |
77 |
id = "org.netbeans.modules.apisupport.project.ExportPackageAction") |
78 |
@ActionRegistration( |
79 |
displayName = "#CTL_ExportPackageAction", lazy = false) |
80 |
@ActionReferences({ |
81 |
@ActionReference(path = "Projects/package/Actions", position = 100), |
82 |
}) |
83 |
@Messages({"CTL_UnexportPackageAction=Unexport Package","CTL_ExportPackageAction=Export Package"}) |
84 |
public final class ExportPackageAction extends AbstractAction implements ContextAwareAction{ |
85 |
|
86 |
@Override |
87 |
public void actionPerformed(ActionEvent ev) { |
88 |
//well, since someone can assign a shortcut ti the action, the invokation is unvaiodable, make it noop |
89 |
//assert false : "Action should never be called without a context"; |
90 |
} |
91 |
|
92 |
@Override |
93 |
public Action createContextAwareInstance(Lookup actionContext) { |
94 |
Collection<FileObject> selectedPackages = (Collection<FileObject>) actionContext.lookupAll(FileObject.class); |
95 |
Project project = FileOwnerQuery.getOwner(selectedPackages.iterator().hasNext()?selectedPackages.iterator().next():null); |
96 |
NbModuleProject nbmProject = null; |
97 |
if((nbmProject = project.getLookup().lookup(NbModuleProject.class)) != null) |
98 |
{ |
99 |
Collection<String> packages = new ArrayList<String>(); |
100 |
SortedSet<String> availablePublicPackages = ApisupportAntUtils.scanProjectForPackageNames(FileUtil.toFile(nbmProject.getProjectDirectory()), false); |
101 |
final SingleModuleProperties properties = SingleModuleProperties.getInstance(nbmProject); |
102 |
String packageNameIter = ""; |
103 |
boolean export = false; |
104 |
for (Iterator<FileObject> it = selectedPackages.iterator(); it.hasNext();) { |
105 |
FileObject packageIter = it.next(); |
106 |
if(!availablePublicPackages.contains(packageNameIter = packageIter.getPath().substring(nbmProject.getSourceDirectory().getPath().length()+1).replace('/', '.'))) { |
107 |
continue; |
108 |
} |
109 |
packages.add(packageNameIter); |
110 |
if(!properties.getPublicPackagesModel().getSelectedPackages().contains(packageNameIter) && !export) { |
111 |
export = true; |
112 |
} |
113 |
} |
114 |
return new ContextAction(!packages.isEmpty(), nbmProject, packages, properties, export); |
115 |
} |
116 |
return new ContextAction(false); |
117 |
} |
118 |
|
119 |
/** |
120 |
* The particular instance of this action for a given package(s). |
121 |
*/ |
122 |
private static final class ContextAction extends AbstractAction { |
123 |
|
124 |
private NbModuleProject nbmProject; |
125 |
|
126 |
private final SingleModuleProperties properties; |
127 |
|
128 |
private Collection<String> packages; |
129 |
|
130 |
private boolean export; |
131 |
|
132 |
public ContextAction(boolean enabled) { |
133 |
this(enabled, null, null, null, true); |
134 |
} |
135 |
|
136 |
public ContextAction(boolean enabled, NbModuleProject nbmProject, Collection<String> packages, SingleModuleProperties properties, boolean export) { |
137 |
super(export?Bundle.CTL_ExportPackageAction():Bundle.CTL_UnexportPackageAction()); |
138 |
this.nbmProject = nbmProject; |
139 |
this.packages = packages; |
140 |
this.properties = properties; |
141 |
this.export = export; |
142 |
this.putValue(DynamicMenuContent.HIDE_WHEN_DISABLED, true); |
143 |
this.setEnabled(enabled); |
144 |
} |
145 |
|
146 |
@Override |
147 |
public void actionPerformed(ActionEvent evt) { |
148 |
CustomizerComponentFactory.PublicPackagesTableModel tableModel = properties.getPublicPackagesModel(); |
149 |
for(String packageIter:this.packages) { |
150 |
for(int i = 0; i < tableModel.getRowCount(); i++) { |
151 |
if(tableModel.getValueAt(i, 1).equals(packageIter)) { |
152 |
tableModel.setValueAt(this.export, i, 0); |
153 |
break; |
154 |
} |
155 |
} |
156 |
} |
157 |
try { |
158 |
ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() { |
159 |
@Override public Void run() throws IOException { |
160 |
properties.storeProperties(); |
161 |
ProjectManager.getDefault().saveProject(nbmProject); |
162 |
return null; |
163 |
} |
164 |
}); |
165 |
} catch (MutexException e) { |
166 |
ErrorManager.getDefault().notify((IOException)e.getException()); |
167 |
} |
168 |
} |
169 |
|
170 |
} |
171 |
} |