KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > feature > URLDetailsSection


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.feature;
12
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.pde.core.IModelChangedEvent;
21 import org.eclipse.pde.internal.core.ifeature.IFeature;
22 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
23 import org.eclipse.pde.internal.core.ifeature.IFeatureURL;
24 import org.eclipse.pde.internal.core.ifeature.IFeatureURLElement;
25 import org.eclipse.pde.internal.ui.PDEPlugin;
26 import org.eclipse.pde.internal.ui.PDEUIMessages;
27 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
28 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
29 import org.eclipse.pde.internal.ui.editor.PDESection;
30 import org.eclipse.pde.internal.ui.parts.FormEntry;
31 import org.eclipse.swt.dnd.Clipboard;
32 import org.eclipse.swt.dnd.RTFTransfer;
33 import org.eclipse.swt.dnd.TextTransfer;
34 import org.eclipse.swt.dnd.Transfer;
35 import org.eclipse.swt.dnd.TransferData;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.ui.forms.IFormPart;
40 import org.eclipse.ui.forms.IPartSelectionListener;
41 import org.eclipse.ui.forms.widgets.ExpandableComposite;
42 import org.eclipse.ui.forms.widgets.FormToolkit;
43 import org.eclipse.ui.forms.widgets.Section;
44
45 public class URLDetailsSection extends PDESection implements
46         IPartSelectionListener {
47     private FormEntry fNameText;
48
49     private FormEntry fUrlText;
50
51     private IFeatureURLElement fInput;
52
53     public URLDetailsSection(PDEFormPage page, Composite parent) {
54         super(page, parent, Section.DESCRIPTION | ExpandableComposite.NO_TITLE,
55                 false);
56         getSection().setDescription(PDEUIMessages.FeatureEditor_URLDetailsSection_desc);
57         createClient(getSection(), page.getManagedForm().getToolkit());
58     }
59
60     public void commit(boolean onSave) {
61         fUrlText.commit();
62         fNameText.commit();
63         super.commit(onSave);
64     }
65
66     private void commitSiteUrl(String JavaDoc value) {
67         if (fInput == null) {
68             return;
69         }
70         try {
71             if (value.length() > 0) {
72                 URL JavaDoc siteUrl = new URL JavaDoc(value);
73                 fInput.setURL(siteUrl);
74             } else {
75                 fInput.setURL(null);
76             }
77         } catch (CoreException e) {
78             PDEPlugin.logException(e);
79         } catch (MalformedURLException JavaDoc e) {
80             PDEPlugin.logException(e);
81         }
82     }
83
84     private void commitSiteName(String JavaDoc value) {
85         if (fInput == null) {
86             return;
87         }
88         try {
89             fInput.setLabel(value);
90         } catch (CoreException e) {
91             PDEPlugin.logException(e);
92         }
93     }
94
95     public void createClient(Section section, FormToolkit toolkit) {
96         Composite container = toolkit.createComposite(section);
97         GridLayout layout = new GridLayout();
98         layout.numColumns = 2;
99         layout.verticalSpacing = 5;
100         layout.horizontalSpacing = 6;
101         container.setLayout(layout);
102
103         final IFeatureModel model = (IFeatureModel) getPage().getModel();
104         final IFeature feature = model.getFeature();
105
106         fUrlText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_URLDetailsSection_updateUrl, null, false);
107         fUrlText.setFormEntryListener(new FormEntryAdapter(this) {
108             public void textValueChanged(FormEntry text) {
109                 String JavaDoc url = text.getValue() != null ? text.getValue() : ""; //$NON-NLS-1$
110
if (url.length() > 0 && !verifySiteUrl(feature, url)) {
111                     warnBadUrl(url);
112                     setUrlText();
113                 } else {
114                     commitSiteUrl(url);
115                 }
116             }
117         });
118
119         fNameText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_URLDetailsSection_updateUrlLabel, null, false);
120         fNameText.setFormEntryListener(new FormEntryAdapter(this) {
121             public void textValueChanged(FormEntry text) {
122                 String JavaDoc name = text.getValue() != null ? text.getValue() : ""; //$NON-NLS-1$
123
commitSiteName(name);
124             }
125         });
126
127         GridData gd = (GridData) fUrlText.getText().getLayoutData();
128         gd.widthHint = 150;
129
130         toolkit.paintBordersFor(container);
131         section.setClient(container);
132         initialize();
133     }
134
135     private boolean verifySiteUrl(IFeature feature, String JavaDoc value) {
136         try {
137             new URL JavaDoc(value);
138         } catch (MalformedURLException JavaDoc e) {
139             return false;
140         }
141         return true;
142     }
143
144     private void warnBadUrl(String JavaDoc text) {
145         MessageDialog.openError(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.FeatureEditor_URLDetailsSection_badUrlTitle, PDEUIMessages.FeatureEditor_URLDetailsSection_badUrlMessage);
146     }
147
148     public void dispose() {
149         IFeatureModel model = (IFeatureModel) getPage().getModel();
150         if (model != null)
151             model.removeModelChangedListener(this);
152         super.dispose();
153     }
154
155     public void initialize() {
156         IFeatureModel model = (IFeatureModel) getPage().getModel();
157         refresh();
158         if (!model.isEditable()) {
159             fUrlText.getText().setEditable(false);
160             fNameText.getText().setEditable(false);
161         }
162         model.addModelChangedListener(this);
163     }
164
165     public void modelChanged(IModelChangedEvent e) {
166         if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
167             markStale();
168             return;
169         }
170         if (e.getChangeType() == IModelChangedEvent.CHANGE) {
171             Object JavaDoc objs[] = e.getChangedObjects();
172             if (objs.length > 0 && objs[0] instanceof IFeatureURL) {
173                 markStale();
174             }
175         }
176         Object JavaDoc objs[] = e.getChangedObjects();
177         if (objs.length > 0 && objs[0] instanceof IFeatureURLElement) {
178             markStale();
179         }
180     }
181
182     public void setFocus() {
183         if (fUrlText != null)
184             fUrlText.getText().setFocus();
185     }
186
187     public void refresh() {
188         update();
189         super.refresh();
190     }
191
192     private void setUrlText() {
193         String JavaDoc updateSiteUrl = ""; //$NON-NLS-1$
194
if (fInput != null && fInput.getURL() != null) {
195             updateSiteUrl = fInput.getURL().toExternalForm();
196         }
197         fUrlText.setValue(updateSiteUrl != null ? updateSiteUrl : "", //$NON-NLS-1$
198
true);
199
200     }
201
202     private void update() {
203         fUrlText.setEditable(fInput != null && fInput.getModel().isEditable());
204         fNameText.setEditable(fInput != null && fInput.getModel().isEditable());
205         setUrlText();
206         setNameText();
207     }
208
209     private void setNameText() {
210         String JavaDoc updateSiteLabel = ""; //$NON-NLS-1$
211
if (fInput != null) {
212             updateSiteLabel = fInput.getLabel();
213         }
214         fNameText
215                 .setValue(updateSiteLabel != null ? updateSiteLabel : "", true); //$NON-NLS-1$
216
}
217
218     public void cancelEdit() {
219         fNameText.cancelEdit();
220         fUrlText.cancelEdit();
221         super.cancelEdit();
222     }
223
224     /**
225      * @see org.eclipse.update.ui.forms.internal.FormSection#canPaste(Clipboard)
226      */

227     public boolean canPaste(Clipboard clipboard) {
228         TransferData[] types = clipboard.getAvailableTypes();
229         Transfer[] transfers = new Transfer[] { TextTransfer.getInstance(),
230                 RTFTransfer.getInstance() };
231         for (int i = 0; i < types.length; i++) {
232             for (int j = 0; j < transfers.length; j++) {
233                 if (transfers[j].isSupportedType(types[i]))
234                     return true;
235             }
236         }
237         return false;
238     }
239
240     public void selectionChanged(IFormPart part, ISelection selection) {
241         if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
242             Object JavaDoc o = ((IStructuredSelection) selection).getFirstElement();
243             if (o instanceof IFeatureURLElement
244                     && ((IFeatureURLElement) o).getElementType() == IFeatureURLElement.DISCOVERY) {
245                 fInput = (IFeatureURLElement) o;
246             } else {
247                 fInput = null;
248             }
249         } else
250             fInput = null;
251         update();
252     }
253 }
254
Popular Tags