001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.feature;
011:
012: import java.net.MalformedURLException;
013: import java.net.URL;
014:
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.jface.dialogs.MessageDialog;
017: import org.eclipse.jface.viewers.ISelection;
018: import org.eclipse.jface.viewers.IStructuredSelection;
019: import org.eclipse.pde.core.IModelChangedEvent;
020: import org.eclipse.pde.internal.core.ifeature.IFeature;
021: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
022: import org.eclipse.pde.internal.core.ifeature.IFeatureURL;
023: import org.eclipse.pde.internal.core.ifeature.IFeatureURLElement;
024: import org.eclipse.pde.internal.ui.PDEPlugin;
025: import org.eclipse.pde.internal.ui.PDEUIMessages;
026: import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
027: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
028: import org.eclipse.pde.internal.ui.editor.PDESection;
029: import org.eclipse.pde.internal.ui.parts.FormEntry;
030: import org.eclipse.swt.dnd.Clipboard;
031: import org.eclipse.swt.dnd.RTFTransfer;
032: import org.eclipse.swt.dnd.TextTransfer;
033: import org.eclipse.swt.dnd.Transfer;
034: import org.eclipse.swt.dnd.TransferData;
035: import org.eclipse.swt.layout.GridData;
036: import org.eclipse.swt.layout.GridLayout;
037: import org.eclipse.swt.widgets.Composite;
038: import org.eclipse.ui.forms.IFormPart;
039: import org.eclipse.ui.forms.IPartSelectionListener;
040: import org.eclipse.ui.forms.widgets.ExpandableComposite;
041: import org.eclipse.ui.forms.widgets.FormToolkit;
042: import org.eclipse.ui.forms.widgets.Section;
043:
044: public class URLDetailsSection extends PDESection implements
045: IPartSelectionListener {
046: private FormEntry fNameText;
047:
048: private FormEntry fUrlText;
049:
050: private IFeatureURLElement fInput;
051:
052: public URLDetailsSection(PDEFormPage page, Composite parent) {
053: super (page, parent, Section.DESCRIPTION
054: | ExpandableComposite.NO_TITLE, false);
055: getSection().setDescription(
056: PDEUIMessages.FeatureEditor_URLDetailsSection_desc);
057: createClient(getSection(), page.getManagedForm().getToolkit());
058: }
059:
060: public void commit(boolean onSave) {
061: fUrlText.commit();
062: fNameText.commit();
063: super .commit(onSave);
064: }
065:
066: private void commitSiteUrl(String value) {
067: if (fInput == null) {
068: return;
069: }
070: try {
071: if (value.length() > 0) {
072: URL siteUrl = new URL(value);
073: fInput.setURL(siteUrl);
074: } else {
075: fInput.setURL(null);
076: }
077: } catch (CoreException e) {
078: PDEPlugin.logException(e);
079: } catch (MalformedURLException e) {
080: PDEPlugin.logException(e);
081: }
082: }
083:
084: private void commitSiteName(String value) {
085: if (fInput == null) {
086: return;
087: }
088: try {
089: fInput.setLabel(value);
090: } catch (CoreException e) {
091: PDEPlugin.logException(e);
092: }
093: }
094:
095: public void createClient(Section section, FormToolkit toolkit) {
096: Composite container = toolkit.createComposite(section);
097: GridLayout layout = new GridLayout();
098: layout.numColumns = 2;
099: layout.verticalSpacing = 5;
100: layout.horizontalSpacing = 6;
101: container.setLayout(layout);
102:
103: final IFeatureModel model = (IFeatureModel) getPage()
104: .getModel();
105: final IFeature feature = model.getFeature();
106:
107: fUrlText = new FormEntry(
108: container,
109: toolkit,
110: PDEUIMessages.FeatureEditor_URLDetailsSection_updateUrl,
111: null, false);
112: fUrlText.setFormEntryListener(new FormEntryAdapter(this ) {
113: public void textValueChanged(FormEntry text) {
114: String url = text.getValue() != null ? text.getValue()
115: : ""; //$NON-NLS-1$
116: if (url.length() > 0 && !verifySiteUrl(feature, url)) {
117: warnBadUrl(url);
118: setUrlText();
119: } else {
120: commitSiteUrl(url);
121: }
122: }
123: });
124:
125: fNameText = new FormEntry(
126: container,
127: toolkit,
128: PDEUIMessages.FeatureEditor_URLDetailsSection_updateUrlLabel,
129: null, false);
130: fNameText.setFormEntryListener(new FormEntryAdapter(this ) {
131: public void textValueChanged(FormEntry text) {
132: String name = text.getValue() != null ? text.getValue()
133: : ""; //$NON-NLS-1$
134: commitSiteName(name);
135: }
136: });
137:
138: GridData gd = (GridData) fUrlText.getText().getLayoutData();
139: gd.widthHint = 150;
140:
141: toolkit.paintBordersFor(container);
142: section.setClient(container);
143: initialize();
144: }
145:
146: private boolean verifySiteUrl(IFeature feature, String value) {
147: try {
148: new URL(value);
149: } catch (MalformedURLException e) {
150: return false;
151: }
152: return true;
153: }
154:
155: private void warnBadUrl(String text) {
156: MessageDialog
157: .openError(
158: PDEPlugin.getActiveWorkbenchShell(),
159: PDEUIMessages.FeatureEditor_URLDetailsSection_badUrlTitle,
160: PDEUIMessages.FeatureEditor_URLDetailsSection_badUrlMessage);
161: }
162:
163: public void dispose() {
164: IFeatureModel model = (IFeatureModel) getPage().getModel();
165: if (model != null)
166: model.removeModelChangedListener(this );
167: super .dispose();
168: }
169:
170: public void initialize() {
171: IFeatureModel model = (IFeatureModel) getPage().getModel();
172: refresh();
173: if (!model.isEditable()) {
174: fUrlText.getText().setEditable(false);
175: fNameText.getText().setEditable(false);
176: }
177: model.addModelChangedListener(this );
178: }
179:
180: public void modelChanged(IModelChangedEvent e) {
181: if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
182: markStale();
183: return;
184: }
185: if (e.getChangeType() == IModelChangedEvent.CHANGE) {
186: Object objs[] = e.getChangedObjects();
187: if (objs.length > 0 && objs[0] instanceof IFeatureURL) {
188: markStale();
189: }
190: }
191: Object objs[] = e.getChangedObjects();
192: if (objs.length > 0 && objs[0] instanceof IFeatureURLElement) {
193: markStale();
194: }
195: }
196:
197: public void setFocus() {
198: if (fUrlText != null)
199: fUrlText.getText().setFocus();
200: }
201:
202: public void refresh() {
203: update();
204: super .refresh();
205: }
206:
207: private void setUrlText() {
208: String updateSiteUrl = ""; //$NON-NLS-1$
209: if (fInput != null && fInput.getURL() != null) {
210: updateSiteUrl = fInput.getURL().toExternalForm();
211: }
212: fUrlText.setValue(updateSiteUrl != null ? updateSiteUrl : "", //$NON-NLS-1$
213: true);
214:
215: }
216:
217: private void update() {
218: fUrlText.setEditable(fInput != null
219: && fInput.getModel().isEditable());
220: fNameText.setEditable(fInput != null
221: && fInput.getModel().isEditable());
222: setUrlText();
223: setNameText();
224: }
225:
226: private void setNameText() {
227: String updateSiteLabel = ""; //$NON-NLS-1$
228: if (fInput != null) {
229: updateSiteLabel = fInput.getLabel();
230: }
231: fNameText.setValue(updateSiteLabel != null ? updateSiteLabel
232: : "", true); //$NON-NLS-1$
233: }
234:
235: public void cancelEdit() {
236: fNameText.cancelEdit();
237: fUrlText.cancelEdit();
238: super .cancelEdit();
239: }
240:
241: /**
242: * @see org.eclipse.update.ui.forms.internal.FormSection#canPaste(Clipboard)
243: */
244: public boolean canPaste(Clipboard clipboard) {
245: TransferData[] types = clipboard.getAvailableTypes();
246: Transfer[] transfers = new Transfer[] {
247: TextTransfer.getInstance(), RTFTransfer.getInstance() };
248: for (int i = 0; i < types.length; i++) {
249: for (int j = 0; j < transfers.length; j++) {
250: if (transfers[j].isSupportedType(types[i]))
251: return true;
252: }
253: }
254: return false;
255: }
256:
257: public void selectionChanged(IFormPart part, ISelection selection) {
258: if (selection instanceof IStructuredSelection
259: && !selection.isEmpty()) {
260: Object o = ((IStructuredSelection) selection)
261: .getFirstElement();
262: if (o instanceof IFeatureURLElement
263: && ((IFeatureURLElement) o).getElementType() == IFeatureURLElement.DISCOVERY) {
264: fInput = (IFeatureURLElement) o;
265: } else {
266: fInput = null;
267: }
268: } else
269: fInput = null;
270: update();
271: }
272: }
|