Link Here
|
39 |
* |
39 |
* |
40 |
* Portions Copyrighted 2008 Sun Microsystems, Inc. |
40 |
* Portions Copyrighted 2008 Sun Microsystems, Inc. |
41 |
*/ |
41 |
*/ |
42 |
|
|
|
43 |
package org.netbeans.modules.maven.customizer; |
42 |
package org.netbeans.modules.maven.customizer; |
|
|
43 |
|
44 |
import java.io.ByteArrayInputStream; |
45 |
import java.io.File; |
46 |
import java.io.IOException; |
47 |
import java.io.OutputStream; |
44 |
import javax.swing.JComponent; |
48 |
import javax.swing.JComponent; |
|
|
49 |
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; |
45 |
import org.netbeans.api.project.Project; |
50 |
import org.netbeans.api.project.Project; |
|
|
51 |
import org.netbeans.modules.maven.api.Constants; |
52 |
import org.netbeans.modules.maven.api.FileUtilities; |
53 |
import org.netbeans.modules.maven.api.PluginPropertyUtils; |
46 |
import org.netbeans.modules.maven.api.customizer.ModelHandle2; |
54 |
import org.netbeans.modules.maven.api.customizer.ModelHandle2; |
47 |
import org.netbeans.spi.project.ui.support.ProjectCustomizer; |
55 |
import org.netbeans.spi.project.ui.support.ProjectCustomizer; |
48 |
import org.netbeans.spi.project.ui.support.ProjectCustomizer.Category; |
56 |
import org.netbeans.spi.project.ui.support.ProjectCustomizer.Category; |
49 |
import org.openide.util.Lookup; |
57 |
import org.openide.util.Lookup; |
50 |
import static org.netbeans.modules.maven.customizer.Bundle.*; |
58 |
import static org.netbeans.modules.maven.customizer.Bundle.*; |
|
|
59 |
import org.netbeans.modules.maven.model.ModelOperation; |
60 |
import org.netbeans.modules.maven.model.pom.POMModel; |
61 |
import org.netbeans.spi.project.AuxiliaryProperties; |
62 |
import org.netbeans.spi.project.support.ant.ui.CustomizerUtilities; |
63 |
import org.openide.filesystems.FileObject; |
64 |
import org.openide.filesystems.FileUtil; |
65 |
import org.openide.util.Exceptions; |
51 |
import org.openide.util.NbBundle.Messages; |
66 |
import org.openide.util.NbBundle.Messages; |
52 |
|
67 |
|
53 |
/** |
68 |
/** |
54 |
* |
69 |
* |
55 |
* @author Milos Kleint |
70 |
* @author Milos Kleint |
56 |
*/ |
71 |
*/ |
57 |
@ProjectCustomizer.CompositeCategoryProvider.Registration(projectType="org-netbeans-modules-maven", position=100) |
72 |
@ProjectCustomizer.CompositeCategoryProvider.Registration(projectType = "org-netbeans-modules-maven", position = 620) |
58 |
public class BasicPanelProvider implements ProjectCustomizer.CompositeCategoryProvider { |
73 |
public class LicenseHeaderPanelProvider implements ProjectCustomizer.CompositeCategoryProvider { |
59 |
|
74 |
|
60 |
@Override |
75 |
@Override |
61 |
@Messages("TIT_Basic=General") |
76 |
@Messages("TIT_Headers=License Headers") |
62 |
public Category createCategory(Lookup context) { |
77 |
public Category createCategory(Lookup context) { |
63 |
return ProjectCustomizer.Category.create( |
78 |
return ProjectCustomizer.Category.create( |
64 |
ModelHandle2.PANEL_BASIC, |
79 |
ModelHandle2.PANEL_HEADERS, |
65 |
TIT_Basic(), |
80 |
TIT_Headers(), |
66 |
null); |
81 |
null); |
67 |
} |
82 |
} |
68 |
|
83 |
|
Link Here
|
70 |
public JComponent createComponent(Category category, Lookup context) { |
85 |
public JComponent createComponent(Category category, Lookup context) { |
71 |
ModelHandle2 handle = context.lookup(ModelHandle2.class); |
86 |
ModelHandle2 handle = context.lookup(ModelHandle2.class); |
72 |
Project prj = context.lookup(Project.class); |
87 |
Project prj = context.lookup(Project.class); |
73 |
return new BasicInfoPanel(handle, category, prj); |
88 |
return CustomizerUtilities.createLicenseHeaderCustomizerPanel(category, new Impl(handle, prj, prj.getLookup().lookup(AuxiliaryProperties.class))); |
74 |
} |
89 |
} |
75 |
|
90 |
|
|
|
91 |
private final class Impl implements CustomizerUtilities.LicensePanelContentHandler { |
92 |
|
93 |
final ModelHandle2 handle; |
94 |
final AuxiliaryProperties props; |
95 |
private final Project project; |
96 |
private final ModelOperation<POMModel> operation; |
97 |
private String licenseContent; |
98 |
private String licensePath; |
99 |
|
100 |
public Impl(final ModelHandle2 handle, Project prj, AuxiliaryProperties props) { |
101 |
this.handle = handle; |
102 |
this.props = props; |
103 |
this.project = prj; |
104 |
operation = new ModelOperation<POMModel>() { |
105 |
@Override |
106 |
public void performOperation(POMModel model) { |
107 |
if (licenseContent == null) { |
108 |
return; |
76 |
} |
109 |
} |
|
|
110 |
try { |
111 |
String eval = PluginPropertyUtils.createEvaluator(project).evaluate(licensePath).toString(); |
112 |
|
113 |
File file = FileUtilities.resolveFilePath(handle.getProject().getBasedir(), eval); |
114 |
FileObject fo; |
115 |
if (!file.exists()) { |
116 |
fo = FileUtil.createData(file); |
117 |
} else { |
118 |
fo = FileUtil.toFileObject(file); |
119 |
} |
120 |
OutputStream out = fo.getOutputStream(); |
121 |
try { |
122 |
FileUtil.copy(new ByteArrayInputStream(licenseContent.getBytes()), out); |
123 |
} finally { |
124 |
out.close(); |
125 |
} |
126 |
} catch (IOException ex) { |
127 |
Exceptions.printStackTrace(ex); |
128 |
} catch (ExpressionEvaluationException ex) { |
129 |
Exceptions.printStackTrace(ex); |
130 |
} |
131 |
} |
132 |
}; |
133 |
licensePath = getProjectLicenseLocation(); |
134 |
} |
135 |
|
136 |
@Override |
137 |
public String getProjectLicenseLocation() { |
138 |
return props.get(Constants.HINT_LICENSE_PATH, true); |
139 |
} |
140 |
|
141 |
@Override |
142 |
public String getGlobalLicenseName() { |
143 |
return props.get(Constants.HINT_LICENSE, true); |
144 |
} |
145 |
|
146 |
@Override |
147 |
public FileObject resolveProjectLocation(String path) { |
148 |
try { |
149 |
String eval = PluginPropertyUtils.createEvaluator(project).evaluate(path).toString(); |
150 |
return FileUtil.toFileObject(FileUtilities.resolveFilePath(handle.getProject().getBasedir(), eval)); |
151 |
} catch (ExpressionEvaluationException ex) { |
152 |
Exceptions.printStackTrace(ex); |
153 |
} |
154 |
return null; |
155 |
} |
156 |
|
157 |
@Override |
158 |
public void setProjectLicenseLocation(String newLocation) { |
159 |
licensePath = newLocation; |
160 |
handle.setRawAuxiliaryProperty(Constants.HINT_LICENSE_PATH, newLocation, true); |
161 |
} |
162 |
|
163 |
@Override |
164 |
public void setGlobalLicenseName(String newName) { |
165 |
handle.setRawAuxiliaryProperty(Constants.HINT_LICENSE, newName, true); |
166 |
} |
167 |
|
168 |
@Override |
169 |
public String getDefaultProjectLicenseLocation() { |
170 |
return "${project.basedir}/licenseheader.txt"; |
171 |
} |
172 |
|
173 |
@Override |
174 |
public void setProjectLicenseContent(String text) { |
175 |
//TODO a fake model operation that is only meant to provide us with way to save |
176 |
// the license file at the end of the customizer. |
177 |
if (text == null) { |
178 |
handle.removePOMModification(operation); |
179 |
licenseContent = null; |
180 |
} else { |
181 |
handle.addPOMModification(operation); |
182 |
licenseContent = text; |
183 |
} |
184 |
} |
185 |
} |
186 |
} |