Fix NPE for advanced media view
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / UriWithLabelElement.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.element;
12
13 import java.net.MalformedURLException;
14 import java.net.URI;
15
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.jface.resource.JFaceResources;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.SelectionAdapter;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.ui.PartInitException;
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.forms.widgets.TableWrapLayout;
29
30 import eu.etaxonomy.taxeditor.Messages;
31 import eu.etaxonomy.taxeditor.model.ImageResources;
32 import eu.etaxonomy.taxeditor.model.MessagingUtils;
33 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
34
35 /**
36 * @author n.hoffmann
37 * @created Dec 20, 2010
38 * @version 1.0
39 */
40 public class UriWithLabelElement extends TextWithLabelElement {
41
42 private final Label labelException;
43 private final Button btnOpenBrowser;
44
45 protected UriWithLabelElement(CdmFormFactory formFactory,
46 ICdmFormElement parentElement, String labelString,
47 URI initialUri, Integer textHeight, int style) {
48 super(formFactory, parentElement, false);
49
50 //label
51 initLabel(formFactory, labelString, false, getLayoutComposite());
52
53 //composite(uri + button)
54 Composite textAndButton = formFactory.createComposite(getLayoutComposite(), style);
55 addControl(textAndButton);
56 textAndButton.setLayout(LayoutConstants.LAYOUT(2, false));
57 textAndButton.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
58
59 //uri text
60 initText(formFactory, null, textHeight, null, false, style, textAndButton);
61
62 //button
63 btnOpenBrowser = formFactory.createButton(textAndButton, "", SWT.NONE); //$NON-NLS-1$
64 btnOpenBrowser.setImage(ImageResources.getImage(ImageResources.WEB));
65 btnOpenBrowser.setToolTipText(Messages.UriWithLabelElement_OPEN_EXTERNAL_BROWSER);
66 btnOpenBrowser.addSelectionListener(new SelectionAdapter() {
67 @Override
68 public void widgetSelected(SelectionEvent e) {
69 String errorTitle = Messages.UriWithLabelElement_INVALID_URL;
70 String errorText = Messages.UriWithLabelElement_COULD_NOT_OPEN_BROWSER;
71
72 URI uri = getUri();
73 if(uri!=null){
74 try {
75 PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(uri.toURL());
76 } catch (PartInitException pie) {
77 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, pie));
78 } catch (MalformedURLException mue) {
79 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, mue));
80 } catch (IllegalArgumentException iae) {
81 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, iae));
82 }
83 }
84 else{
85 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, null));
86 }
87 }
88 });
89 btnOpenBrowser.setLayoutData(LayoutConstants.RIGHT());
90
91 labelException = formFactory.createLabel(getLayoutComposite(), "", SWT.WRAP); //$NON-NLS-1$
92 int numColumns = AbstractFormSection.DEFAULT_NUM_COLUMNS;
93 if(getLayoutComposite().getLayout() instanceof TableWrapLayout){
94 numColumns = ((TableWrapLayout)getLayoutComposite().getLayout()).numColumns;
95 }
96 labelException.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(numColumns, 1));
97 addControl(labelException);
98 setUri(initialUri);
99 }
100
101 public void setUri(URI uri) {
102 if(uri != null){
103 super.setText(uri.toString());
104 }
105 }
106
107 public URI getUri(){
108 try {
109 labelException.setFont(JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT));
110 labelException.setForeground(getPersistentBackground());
111 labelException.setText(""); //$NON-NLS-1$
112 return new URI(super.getText());
113 } catch (Exception e) {
114 labelException.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
115 labelException.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
116 labelException.setText(Messages.UriWithLabelElement_URL_NOT_SAVED+e.getMessage());
117 return null;
118 }
119 }
120
121 }