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