]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/editors/BundleDetailPage.java
Fix distribution editor after further testing with maven produced releases
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / editors / BundleDetailPage.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.client.ui.dist.editors;
17
18 import java.net.URL;
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import javax.jcr.Node;
23 import javax.jcr.RepositoryException;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.argeo.ArgeoException;
28 import org.argeo.eclipse.ui.utils.CommandUtils;
29 import org.argeo.slc.SlcException;
30 import org.argeo.slc.client.ui.dist.DistConstants;
31 import org.argeo.slc.client.ui.dist.utils.AbstractHyperlinkListener;
32 import org.argeo.slc.client.ui.specific.OpenJcrFile;
33 import org.argeo.slc.client.ui.specific.OpenJcrFileCmdId;
34 import org.argeo.slc.jcr.SlcNames;
35 import org.argeo.slc.repo.RepoConstants;
36 import org.argeo.slc.repo.RepoUtils;
37 import org.eclipse.jface.dialogs.IMessageProvider;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.layout.RowLayout;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Label;
44 import org.eclipse.swt.widgets.Text;
45 import org.eclipse.ui.PlatformUI;
46 import org.eclipse.ui.browser.IWebBrowser;
47 import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
48 import org.eclipse.ui.forms.IManagedForm;
49 import org.eclipse.ui.forms.editor.FormEditor;
50 import org.eclipse.ui.forms.editor.FormPage;
51 import org.eclipse.ui.forms.events.HyperlinkEvent;
52 import org.eclipse.ui.forms.widgets.FormToolkit;
53 import org.eclipse.ui.forms.widgets.Hyperlink;
54 import org.eclipse.ui.forms.widgets.ScrolledForm;
55 import org.eclipse.ui.forms.widgets.Section;
56
57 /**
58 * Show the details for a given bundle.
59 */
60 public class BundleDetailPage extends FormPage implements SlcNames {
61 private final static Log log = LogFactory.getLog(BundleDetailPage.class);
62
63 final static String PAGE_ID = "BundleDetailPage";
64
65 // Business Objects
66 private Node bundle;
67
68 // This page widgets
69 private FormToolkit tk;
70
71 public BundleDetailPage(FormEditor formEditor, String title, Node bundle) {
72 super(formEditor, PAGE_ID, title);
73 this.bundle = bundle;
74 }
75
76 @Override
77 protected void createFormContent(IManagedForm managedForm) {
78 // General settings for this page
79 ScrolledForm form = managedForm.getForm();
80 tk = managedForm.getToolkit();
81 Composite body = form.getBody();
82
83 GridLayout layout = new GridLayout(1, false);
84 layout.marginWidth = 5;
85 layout.marginRight = 15;
86 layout.verticalSpacing = 0;
87 body.setLayout(layout);
88 try {
89 form.setText(bundle.hasProperty(SlcNames.SLC_SYMBOLIC_NAME) ? bundle
90 .getProperty(SlcNames.SLC_SYMBOLIC_NAME).getString() : "");
91 form.setMessage(bundle
92 .hasProperty(DistConstants.SLC_BUNDLE_DESCRIPTION) ? bundle
93 .getProperty(DistConstants.SLC_BUNDLE_DESCRIPTION)
94 .getString() : "", IMessageProvider.NONE);
95 } catch (RepositoryException re) {
96 throw new SlcException("Unable to get bundle name for node "
97 + bundle, re);
98 }
99
100 // Main layout
101 Composite header = tk.createComposite(body);
102 header.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
103 populateHeaderPart(header);
104
105 Composite mavenSnipet = tk.createComposite(body);
106 mavenSnipet.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
107 populateMavenSnippetPart(mavenSnipet);
108 }
109
110 private void populateHeaderPart(Composite parent) {
111 GridLayout layout = new GridLayout(6, false);
112 // layout.marginWidth = layout.horizontalSpacing = layout.marginHeight =
113 // 0;
114 layout.horizontalSpacing = 10;
115 parent.setLayout(layout);
116 try {
117 // 1st Line: Category, name version
118 createLT(parent, "Category",
119 bundle.hasProperty(SlcNames.SLC_GROUP_ID) ? bundle
120 .getProperty(SlcNames.SLC_GROUP_ID).getString()
121 : "");
122 createLT(parent, "Name",
123 bundle.hasProperty(SlcNames.SLC_ARTIFACT_ID) ? bundle
124 .getProperty(SlcNames.SLC_ARTIFACT_ID).getString()
125 : "");
126 createLT(parent, "Version",
127 bundle.hasProperty(SlcNames.SLC_ARTIFACT_VERSION) ? bundle
128 .getProperty(SlcNames.SLC_ARTIFACT_VERSION)
129 .getString() : "");
130
131 // 3rd Line: Vendor, licence, sources
132 createLT(
133 parent,
134 "Vendor",
135 bundle.hasProperty(DistConstants.SLC_BUNDLE_VENDOR) ? bundle
136 .getProperty(DistConstants.SLC_BUNDLE_VENDOR)
137 .getString() : "N/A");
138
139 createLicencesLink(parent, "Licence",
140 DistConstants.SLC_BUNDLE_LICENCE);
141 addSourceLink(parent);
142
143 // 2nd Line: The Jar itself and the Manifest
144 createJarLink(parent);
145 createManifestLink(parent);
146
147 // Last line
148 createPomLink(parent);
149
150 } catch (RepositoryException re) {
151 throw new SlcException("Unable to get bundle name for node "
152 + bundle, re);
153 }
154
155 }
156
157 private void populateMavenSnippetPart(Composite parent) {
158 GridLayout layout = new GridLayout(1, false);
159 layout.marginWidth = layout.horizontalSpacing = layout.horizontalSpacing = layout.marginHeight = 0;
160 parent.setLayout(layout);
161
162 Section section = tk.createSection(parent, Section.TITLE_BAR
163 | Section.DESCRIPTION);
164 section.setText("Maven");
165 section.setDescription("Add the below tag to your Artifact pom dependencies");
166 section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
167 Text snippetTxt = createMavenSnippet(section);
168 section.setClient(snippetTxt);
169 }
170
171 // /////////////////////
172 // HELPERS
173
174 private Text createLT(Composite parent, String labelValue, String textValue) {
175 Label label = tk.createLabel(parent, labelValue, SWT.RIGHT);
176 // label.setFont(EclipseUiUtils.getBoldFont(parent));
177 label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
178 // Add a trailing space to workaround a display glitch in RAP 1.3
179 Text text = new Text(parent, SWT.LEFT);
180 text.setText(textValue + " ");
181 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
182 text.setEditable(false);
183 return text;
184 }
185
186 private void createLicencesLink(Composite parent, String label,
187 String jcrPropName) throws RepositoryException {
188 tk.createLabel(parent, label, SWT.NONE);
189 if (bundle.hasProperty(jcrPropName)) {
190
191 String licenceLinkVal = bundle.getProperty(jcrPropName).getString();
192 String[] licenceVals = licenceLinkVal.split(", ");
193
194 Composite body = tk.createComposite(parent);
195 body.setLayout(new RowLayout(SWT.WRAP));
196
197 for (String value : licenceVals) {
198 final Hyperlink link = tk
199 .createHyperlink(body, value, SWT.NONE);
200 link.addHyperlinkListener(new AbstractHyperlinkListener() {
201 @Override
202 public void linkActivated(HyperlinkEvent e) {
203 try {
204 IWorkbenchBrowserSupport browserSupport = PlatformUI
205 .getWorkbench().getBrowserSupport();
206 IWebBrowser browser = browserSupport
207 .createBrowser(
208 IWorkbenchBrowserSupport.LOCATION_BAR
209 | IWorkbenchBrowserSupport.NAVIGATION_BAR,
210 "SLC Distribution browser",
211 "SLC Distribution browser",
212 "A tool tip");
213 browser.openURL(new URL(link.getText()));
214 } catch (Exception ex) {
215 throw new SlcException("error opening browser", ex); //$NON-NLS-1$
216 }
217 }
218 });
219 }
220 } else
221 tk.createLabel(parent, "N/A", SWT.NONE);
222 }
223
224 private void createJarLink(Composite parent) throws RepositoryException {
225 Label label = tk.createLabel(parent, "Jar", SWT.RIGHT);
226 label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
227
228 Composite body = tk.createComposite(parent);
229 RowLayout rl = new RowLayout(SWT.HORIZONTAL);
230 rl.spacing = 6;
231 body.setLayout(rl);
232 body.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
233
234 Hyperlink jarLink = tk
235 .createHyperlink(body, bundle.getName(), SWT.NONE);
236 jarLink.addHyperlinkListener(new OpenFileLinkListener(bundle.getPath()));
237
238 // Corresponding check sums
239
240 String name = bundle.getName() + ".md5";
241 if (bundle.getParent().hasNode(name)) {
242 Node md5 = bundle.getParent().getNode(name);
243 Hyperlink md5Link = tk.createHyperlink(body, "MD5", SWT.NONE);
244 md5Link.addHyperlinkListener(new OpenFileLinkListener(md5.getPath()));
245 }
246
247 name = bundle.getName() + ".sha1";
248 if (bundle.getParent().hasNode(name)) {
249 Node sha1 = bundle.getParent().getNode(name);
250 Hyperlink sha1Link = tk.createHyperlink(body, "SHA1", SWT.NONE);
251 sha1Link.addHyperlinkListener(new OpenFileLinkListener(sha1
252 .getPath()));
253 }
254 }
255
256 private void createPomLink(Composite parent) throws RepositoryException {
257 Label label = tk.createLabel(parent, "Pom", SWT.RIGHT);
258 label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
259
260 String name = bundle.getName().substring(0,
261 bundle.getName().length() - "jar".length())
262 + "pom";
263
264 if (bundle.getParent().hasNode(name)) {
265 Node pom = bundle.getParent().getNode(name);
266
267 Composite body = tk.createComposite(parent);
268 RowLayout rl = new RowLayout(SWT.HORIZONTAL);
269 rl.spacing = 6;
270 body.setLayout(rl);
271 body.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
272 3, 1));
273
274 Hyperlink pomLink = tk.createHyperlink(body, "pom.xml", SWT.NONE);
275 pomLink.addHyperlinkListener(new OpenFileLinkListener(pom.getPath()));
276
277 // Corresponding check sums
278
279 name = pom.getName() + ".md5";
280 if (pom.getParent().hasNode(name)) {
281 Node md5 = pom.getParent().getNode(name);
282 Hyperlink md5Link = tk.createHyperlink(body, "MD5", SWT.NONE);
283 md5Link.addHyperlinkListener(new OpenFileLinkListener(md5
284 .getPath()));
285 }
286
287 name = pom.getName() + ".sha1";
288 if (pom.getParent().hasNode(name)) {
289 Node sha1 = pom.getParent().getNode(name);
290 Hyperlink sha1Link = tk.createHyperlink(body, "SHA1", SWT.NONE);
291 sha1Link.addHyperlinkListener(new OpenFileLinkListener(sha1
292 .getPath()));
293 }
294 } else
295 tk.createLabel(parent, "N/A", SWT.NONE);
296 }
297
298 private void createManifestLink(Composite parent)
299 throws RepositoryException {
300 tk.createLabel(parent, "Manifest", SWT.NONE);
301 Hyperlink link = tk.createHyperlink(parent, "MANIFEST.MF", SWT.NONE);
302 // link.addHyperlinkListener(new
303 // OpenFileLinkListener(bundle.getPath()));
304 }
305
306 private void createHyperlink(Composite parent, String label,
307 String jcrPropName) throws RepositoryException {
308 tk.createLabel(parent, label, SWT.NONE);
309 if (bundle.hasProperty(jcrPropName)) {
310 final Hyperlink link = tk.createHyperlink(parent, bundle
311 .getProperty(jcrPropName).getString(), SWT.NONE);
312 link.addHyperlinkListener(new AbstractHyperlinkListener() {
313 @Override
314 public void linkActivated(HyperlinkEvent e) {
315 try {
316 IWorkbenchBrowserSupport browserSupport = PlatformUI
317 .getWorkbench().getBrowserSupport();
318 IWebBrowser browser = browserSupport
319 .createBrowser(
320 IWorkbenchBrowserSupport.LOCATION_BAR
321 | IWorkbenchBrowserSupport.NAVIGATION_BAR,
322 "SLC Distribution browser",
323 "SLC Distribution browser",
324 "A tool tip");
325 browser.openURL(new URL(link.getText()));
326 } catch (Exception ex) {
327 throw new SlcException("error opening browser", ex); //$NON-NLS-1$
328 }
329 }
330 });
331 } else
332 tk.createLabel(parent, "N/A", SWT.NONE);
333 }
334
335 // helper to check if sources are available
336 private void addSourceLink(Composite parent) {
337 try {
338 String srcPath = RepoUtils.relatedPdeSourcePath(
339 RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH, bundle);
340 if (!bundle.getSession().nodeExists(srcPath)) {
341 createLT(parent, "Sources", "N/A");
342 } else {
343 final Node sourcesNode = bundle.getSession().getNode(srcPath);
344
345 String srcName = null;
346 if (sourcesNode.hasProperty(SlcNames.SLC_SYMBOLIC_NAME))
347 srcName = sourcesNode.getProperty(
348 SlcNames.SLC_SYMBOLIC_NAME).getString();
349 else
350 srcName = sourcesNode.getName();
351 Label label = tk.createLabel(parent, "Sources", SWT.RIGHT);
352 label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
353 false));
354 final Hyperlink link = tk.createHyperlink(parent, srcName,
355 SWT.NONE);
356 link.addHyperlinkListener(new OpenFileLinkListener(sourcesNode
357 .getPath()));
358
359 // {
360 // @Override
361 // public void linkActivated(HyperlinkEvent e) {
362 // try {
363 // ModuleEditorInput editorInput = (ModuleEditorInput)
364 // getEditorInput();
365 // Map<String, String> params = new HashMap<String, String>();
366 // params.put(OpenJcrFile.PARAM_REPO_NODE_PATH,
367 // editorInput.getRepoNodePath());
368 // params.put(OpenJcrFile.PARAM_REPO_URI,
369 // editorInput.getUri());
370 // params.put(OpenJcrFile.PARAM_WORKSPACE_NAME,
371 // editorInput.getWorkspaceName());
372 // params.put(OpenJcrFile.PARAM_FILE_PATH,
373 // );
374 // CommandUtils.callCommand(OpenJcrFile.ID, params);
375 // } catch (Exception ex) {
376 // throw new SlcException("error opening browser", ex); //$NON-NLS-1$
377 // }
378 // }
379 // });
380
381 }
382 } catch (RepositoryException e) {
383 throw new SlcException("Unable to configure sources link for "
384 + bundle, e);
385 }
386 }
387
388 private class OpenFileLinkListener extends AbstractHyperlinkListener {
389 final private String path;
390
391 public OpenFileLinkListener(String path) {
392 this.path = path;
393 }
394
395 @Override
396 public void linkActivated(HyperlinkEvent e) {
397 try {
398 ModuleEditorInput editorInput = (ModuleEditorInput) getEditorInput();
399 Map<String, String> params = new HashMap<String, String>();
400 params.put(OpenJcrFile.PARAM_REPO_NODE_PATH,
401 editorInput.getRepoNodePath());
402 params.put(OpenJcrFile.PARAM_REPO_URI, editorInput.getUri());
403 params.put(OpenJcrFile.PARAM_WORKSPACE_NAME,
404 editorInput.getWorkspaceName());
405 params.put(OpenJcrFile.PARAM_FILE_PATH, path);
406
407 String cmdId = (new OpenJcrFileCmdId()).getCmdId();
408 if (log.isTraceEnabled())
409 log.debug("Retrieved openFile Cmd ID: " + cmdId);
410 CommandUtils.callCommand(cmdId, params);
411 } catch (Exception ex) {
412 throw new SlcException("error opening browser", ex); //$NON-NLS-1$
413 }
414 }
415 }
416
417 /** Creates a text area with corresponding maven snippet */
418 private Text createMavenSnippet(Composite parent) {
419 Text mavenSnippet = new Text(parent, SWT.MULTI | SWT.WRAP);
420 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
421 gd.grabExcessHorizontalSpace = true;
422 gd.heightHint = 100;
423 mavenSnippet.setLayoutData(gd);
424 mavenSnippet.setText(generateXmlSnippet());
425 return mavenSnippet;
426 }
427
428 private String generateXmlSnippet() {
429 try {
430 StringBuffer sb = new StringBuffer();
431 sb.append("<dependency>\n");
432 sb.append("\t<groupId>");
433 sb.append(bundle.getProperty(SLC_GROUP_ID).getString());
434 sb.append("</groupId>\n");
435 sb.append("\t<artifactId>");
436 sb.append(bundle.getProperty(SLC_ARTIFACT_ID).getString());
437 sb.append("</artifactId>\n");
438 sb.append("\t<version>");
439 sb.append(bundle.getProperty(SLC_ARTIFACT_VERSION).getString());
440 sb.append("</version>\n");
441 sb.append("</dependency>");
442 return sb.toString();
443 } catch (RepositoryException re) {
444 throw new ArgeoException(
445 "unexpected error while generating maven snippet");
446 }
447 }
448 }