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: import java.util.Iterator;
015:
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.jface.action.Action;
018: import org.eclipse.jface.action.IMenuManager;
019: import org.eclipse.jface.action.Separator;
020: import org.eclipse.jface.viewers.ISelection;
021: import org.eclipse.jface.viewers.IStructuredContentProvider;
022: import org.eclipse.jface.viewers.IStructuredSelection;
023: import org.eclipse.jface.viewers.LabelProvider;
024: import org.eclipse.jface.viewers.StructuredSelection;
025: import org.eclipse.jface.viewers.TableViewer;
026: import org.eclipse.pde.core.IModel;
027: import org.eclipse.pde.core.IModelChangedEvent;
028: import org.eclipse.pde.internal.core.PDECore;
029: import org.eclipse.pde.internal.core.feature.FeatureURLElement;
030: import org.eclipse.pde.internal.core.ifeature.IFeature;
031: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
032: import org.eclipse.pde.internal.core.ifeature.IFeatureURL;
033: import org.eclipse.pde.internal.core.ifeature.IFeatureURLElement;
034: import org.eclipse.pde.internal.ui.PDELabelProvider;
035: import org.eclipse.pde.internal.ui.PDEPlugin;
036: import org.eclipse.pde.internal.ui.PDEPluginImages;
037: import org.eclipse.pde.internal.ui.PDEUIMessages;
038: import org.eclipse.pde.internal.ui.editor.ModelDataTransfer;
039: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
040: import org.eclipse.pde.internal.ui.editor.TableSection;
041: import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
042: import org.eclipse.pde.internal.ui.parts.TablePart;
043: import org.eclipse.swt.SWT;
044: import org.eclipse.swt.custom.BusyIndicator;
045: import org.eclipse.swt.dnd.Clipboard;
046: import org.eclipse.swt.graphics.Image;
047: import org.eclipse.swt.layout.GridLayout;
048: import org.eclipse.swt.widgets.Composite;
049: import org.eclipse.ui.actions.ActionFactory;
050: import org.eclipse.ui.forms.widgets.ExpandableComposite;
051: import org.eclipse.ui.forms.widgets.FormToolkit;
052: import org.eclipse.ui.forms.widgets.Section;
053:
054: public class URLSection extends TableSection {
055: private TableViewer fUrlViewer;
056:
057: private Action fNewAction;
058:
059: private Action fDeleteAction;
060:
061: private Image fUrlImage;
062:
063: class URLContentProvider extends DefaultContentProvider implements
064: IStructuredContentProvider {
065: public Object[] getElements(Object input) {
066: IFeature feature = (IFeature) input;
067: IFeatureURL featureUrl = feature.getURL();
068: if (featureUrl != null) {
069: return featureUrl.getDiscoveries();
070: }
071: return new Object[0];
072: }
073: }
074:
075: class URLLabelProvider extends LabelProvider {
076:
077: public Image getImage(Object obj) {
078: if (obj instanceof IFeatureURLElement) {
079: return fUrlImage;
080: }
081: return null;
082: }
083:
084: }
085:
086: public URLSection(PDEFormPage page, Composite parent) {
087: super (
088: page,
089: parent,
090: Section.DESCRIPTION | ExpandableComposite.NO_TITLE,
091: false,
092: new String[] { PDEUIMessages.FeatureEditor_URLSection_new });
093: PDELabelProvider provider = PDEPlugin.getDefault()
094: .getLabelProvider();
095: fUrlImage = provider.get(PDEPluginImages.DESC_LINK_OBJ);
096: createClient(getSection(), page.getManagedForm().getToolkit());
097:
098: getSection().setDescription(
099: PDEUIMessages.FeatureEditor_URLSection_desc);
100: }
101:
102: public void commit(boolean onSave) {
103: super .commit(onSave);
104: }
105:
106: public void createClient(Section section, FormToolkit toolkit) {
107: Composite container = createClientContainer(section, 2, toolkit);
108: GridLayout layout = (GridLayout) container.getLayout();
109: layout.verticalSpacing = 5;
110:
111: createViewerPartControl(container, SWT.SINGLE, 2, toolkit);
112: TablePart tablePart = getTablePart();
113: fUrlViewer = tablePart.getTableViewer();
114: fUrlViewer.setContentProvider(new URLContentProvider());
115: fUrlViewer.setLabelProvider(new URLLabelProvider());
116: toolkit.paintBordersFor(container);
117: makeActions();
118: section.setClient(container);
119: initialize();
120: }
121:
122: protected void buttonSelected(int index) {
123: if (index == 0)
124: handleNew();
125: }
126:
127: public void dispose() {
128: IFeatureModel model = (IFeatureModel) getPage().getModel();
129: if (model != null)
130: model.removeModelChangedListener(this );
131: super .dispose();
132: }
133:
134: protected void fillContextMenu(IMenuManager manager) {
135: IModel model = (IModel) getPage().getModel();
136: ISelection selection = fUrlViewer.getSelection();
137: Object object = ((IStructuredSelection) selection)
138: .getFirstElement();
139:
140: manager.add(fNewAction);
141: fNewAction.setEnabled(model.isEditable());
142:
143: if (object != null && object instanceof IFeatureURLElement) {
144: manager.add(fDeleteAction);
145: fDeleteAction.setEnabled(model.isEditable());
146: }
147:
148: manager.add(new Separator());
149: getPage().getPDEEditor().getContributor()
150: .contextMenuAboutToShow(manager);
151: }
152:
153: private void handleNew() {
154: IFeatureModel model = (IFeatureModel) getPage().getModel();
155: IFeature feature = model.getFeature();
156: IFeatureURL url = feature.getURL();
157:
158: if (url == null) {
159: url = model.getFactory().createURL();
160: try {
161: feature.setURL(url);
162: } catch (CoreException e) {
163: return;
164: }
165: }
166: try {
167: IFeatureURLElement element = model
168: .getFactory()
169: .createURLElement(url, IFeatureURLElement.DISCOVERY);
170: element
171: .setLabel(PDEUIMessages.FeatureEditor_URLSection_newDiscoverySite);
172: element.setURL(new URL(
173: PDEUIMessages.FeatureEditor_URLSection_newURL));
174: url.addDiscovery(element);
175: fUrlViewer.setSelection(new StructuredSelection(element));
176:
177: } catch (CoreException e) {
178: PDEPlugin.logException(e);
179: } catch (MalformedURLException e) {
180: PDEPlugin.logException(e);
181: }
182: }
183:
184: private void handleSelectAll() {
185: IStructuredContentProvider provider = (IStructuredContentProvider) fUrlViewer
186: .getContentProvider();
187: Object[] elements = provider.getElements(fUrlViewer.getInput());
188: StructuredSelection ssel = new StructuredSelection(elements);
189: fUrlViewer.setSelection(ssel);
190: }
191:
192: private void handleDelete() {
193: IStructuredSelection ssel = (IStructuredSelection) fUrlViewer
194: .getSelection();
195:
196: if (ssel.isEmpty())
197: return;
198: IFeatureModel model = (IFeatureModel) getPage().getModel();
199: if (!model.isEditable()) {
200: return;
201: }
202: IFeature feature = model.getFeature();
203:
204: IFeatureURL url = feature.getURL();
205: if (url == null) {
206: return;
207: }
208: for (Iterator iter = ssel.iterator(); iter.hasNext();) {
209: IFeatureURLElement urlElement = (IFeatureURLElement) iter
210: .next();
211: // IFeature feature = urlElement.getFeature();
212: try {
213: url.removeDiscovery(urlElement);
214: } catch (CoreException e) {
215: PDEPlugin.logException(e);
216: }
217: }
218: }
219:
220: public boolean doGlobalAction(String actionId) {
221: if (actionId.equals(ActionFactory.DELETE.getId())) {
222: BusyIndicator.showWhile(fUrlViewer.getTable().getDisplay(),
223: new Runnable() {
224: public void run() {
225: handleDelete();
226: }
227: });
228: return true;
229: }
230: if (actionId.equals(ActionFactory.CUT.getId())) {
231: // delete here and let the editor transfer
232: // the selection to the clipboard
233: handleDelete();
234: return false;
235: }
236: if (actionId.equals(ActionFactory.PASTE.getId())) {
237: doPaste();
238: return true;
239: }
240: if (actionId.equals(ActionFactory.SELECT_ALL.getId())) {
241: BusyIndicator.showWhile(fUrlViewer.getTable().getDisplay(),
242: new Runnable() {
243: public void run() {
244: handleSelectAll();
245: }
246: });
247: return true;
248: }
249: return false;
250: }
251:
252: protected void selectionChanged(IStructuredSelection selection) {
253: getPage().getPDEEditor().setSelection(selection);
254: }
255:
256: public void initialize() {
257: IFeatureModel model = (IFeatureModel) getPage().getModel();
258: refresh();
259: getTablePart().setButtonEnabled(0, model.isEditable());
260: model.addModelChangedListener(this );
261: }
262:
263: public void modelChanged(IModelChangedEvent e) {
264: if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
265: markStale();
266: return;
267: }
268: Object obj = e.getChangedObjects()[0];
269: if (obj instanceof IFeatureURL) {
270: markStale();
271: return;
272: }
273: if (obj instanceof IFeatureURLElement) {
274: markStale();
275: return;
276: // IFeatureURLElement element = (IFeatureURLElement) obj;
277: // if (element.getElementType() == IFeatureURLElement.DISCOVERY) {
278: // if (e.getChangeType() == IModelChangedEvent.INSERT) {
279: // fUrlViewer.add(element);
280: // fUrlViewer
281: // .setSelection(new StructuredSelection(element),
282: // true);
283: // } else if (e.getChangeType() == IModelChangedEvent.REMOVE) {
284: // fUrlViewer.remove(obj);
285: // } else {
286: // fUrlViewer.update(obj, null);
287: // }
288: // }
289: }
290: }
291:
292: private void makeActions() {
293: IModel model = (IModel) getPage().getModel();
294: fNewAction = new Action() {
295: public void run() {
296: handleNew();
297: }
298: };
299: fNewAction.setText(PDEUIMessages.Menus_new_label);
300: fNewAction.setEnabled(model.isEditable());
301:
302: fDeleteAction = new Action() {
303: public void run() {
304: BusyIndicator.showWhile(fUrlViewer.getTable()
305: .getDisplay(), new Runnable() {
306: public void run() {
307: handleDelete();
308: }
309: });
310: }
311: };
312: fDeleteAction.setText(PDEUIMessages.Actions_delete_label);
313: fDeleteAction.setEnabled(model.isEditable());
314: }
315:
316: public void setFocus() {
317: if (fUrlViewer != null)
318: fUrlViewer.getTable().setFocus();
319: }
320:
321: public void refresh() {
322: IFeatureModel model = (IFeatureModel) getPage().getModel();
323: IFeature feature = model.getFeature();
324: fUrlViewer.setInput(feature);
325: super .refresh();
326: }
327:
328: public boolean canPaste(Clipboard clipboard) {
329: ModelDataTransfer modelTransfer = ModelDataTransfer
330: .getInstance();
331: Object[] objects = (Object[]) clipboard
332: .getContents(modelTransfer);
333: if (objects != null && objects.length > 0) {
334: return canPaste(null, objects);
335: }
336: return false;
337: }
338:
339: /**
340: * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#canPaste(Object,
341: * Object[])
342: */
343: protected boolean canPaste(Object target, Object[] objects) {
344: for (int i = 0; i < objects.length; i++) {
345: if (!(objects[i] instanceof FeatureURLElement))
346: return false;
347: }
348: return true;
349: }
350:
351: protected void doPaste() {
352: Clipboard clipboard = getPage().getPDEEditor().getClipboard();
353: ModelDataTransfer modelTransfer = ModelDataTransfer
354: .getInstance();
355: Object[] objects = (Object[]) clipboard
356: .getContents(modelTransfer);
357: if (objects != null) {
358: doPaste(null, objects);
359: }
360: }
361:
362: /**
363: * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#doPaste(Object,
364: * Object[])
365: */
366: protected void doPaste(Object target, Object[] objects) {
367: IFeatureModel model = (IFeatureModel) getPage().getModel();
368: if (!model.isEditable()) {
369: return;
370: }
371: IFeature feature = model.getFeature();
372: for (int i = 0; i < objects.length; i++) {
373: if (objects[i] instanceof FeatureURLElement) {
374: FeatureURLElement element = (FeatureURLElement) objects[i];
375: element.setModel(model);
376: element.setParent(feature);
377: try {
378: feature.getURL().addDiscovery(element);
379: } catch (CoreException e) {
380: PDECore.logException(e);
381: }
382: }
383: }
384: }
385:
386: void fireSelection() {
387: fUrlViewer.setSelection(fUrlViewer.getSelection());
388: }
389: }
|