001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.util.ArrayList;
013: import java.util.Iterator;
014:
015: import org.eclipse.core.resources.IContainer;
016: import org.eclipse.core.resources.IFile;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.jface.action.Action;
021: import org.eclipse.jface.action.IMenuManager;
022: import org.eclipse.jface.action.Separator;
023: import org.eclipse.jface.viewers.ISelection;
024: import org.eclipse.jface.viewers.IStructuredContentProvider;
025: import org.eclipse.jface.viewers.IStructuredSelection;
026: import org.eclipse.jface.viewers.StructuredSelection;
027: import org.eclipse.jface.viewers.TableViewer;
028: import org.eclipse.pde.core.IModel;
029: import org.eclipse.pde.core.IModelChangedEvent;
030: import org.eclipse.pde.internal.core.feature.FeatureData;
031: import org.eclipse.pde.internal.core.feature.FeaturePlugin;
032: import org.eclipse.pde.internal.core.ifeature.IFeature;
033: import org.eclipse.pde.internal.core.ifeature.IFeatureData;
034: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
035: import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
036: import org.eclipse.pde.internal.ui.PDEPlugin;
037: import org.eclipse.pde.internal.ui.PDEUIMessages;
038: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
039: import org.eclipse.pde.internal.ui.editor.ModelDataTransfer;
040: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
041: import org.eclipse.pde.internal.ui.editor.TableSection;
042: import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
043: import org.eclipse.pde.internal.ui.parts.TablePart;
044: import org.eclipse.swt.SWT;
045: import org.eclipse.swt.custom.BusyIndicator;
046: import org.eclipse.swt.dnd.Clipboard;
047: import org.eclipse.swt.layout.GridData;
048: import org.eclipse.swt.widgets.Composite;
049: import org.eclipse.ui.actions.ActionFactory;
050: import org.eclipse.ui.dialogs.ResourceSelectionDialog;
051: import org.eclipse.ui.forms.widgets.FormToolkit;
052: import org.eclipse.ui.forms.widgets.Section;
053:
054: public class DataSection extends TableSection {
055: private TableViewer fDataViewer;
056:
057: private Action fNewAction;
058:
059: private Action fOpenAction;
060:
061: private Action fDeleteAction;
062:
063: class PluginContentProvider extends DefaultContentProvider
064: implements IStructuredContentProvider {
065: public Object[] getElements(Object parent) {
066: if (parent instanceof IFeature) {
067: return ((IFeature) parent).getData();
068: }
069: return new Object[0];
070: }
071: }
072:
073: public DataSection(PDEFormPage page, Composite parent) {
074: super (
075: page,
076: parent,
077: Section.DESCRIPTION,
078: new String[] { PDEUIMessages.FeatureEditor_DataSection_new });
079: getSection().setText(
080: PDEUIMessages.FeatureEditor_DataSection_title);
081: getSection().setDescription(
082: PDEUIMessages.FeatureEditor_DataSection_desc);
083: getTablePart().setEditable(false);
084: }
085:
086: public void commit(boolean onSave) {
087: super .commit(onSave);
088: }
089:
090: public void createClient(Section section, FormToolkit toolkit) {
091:
092: section.setLayout(FormLayoutFactory.createClearGridLayout(
093: false, 1));
094: GridData data = new GridData(GridData.FILL_BOTH);
095: section.setLayoutData(data);
096:
097: Composite container = createClientContainer(section, 2, toolkit);
098:
099: createViewerPartControl(container, SWT.MULTI, 2, toolkit);
100: TablePart tablePart = getTablePart();
101: fDataViewer = tablePart.getTableViewer();
102: fDataViewer.setContentProvider(new PluginContentProvider());
103: fDataViewer.setLabelProvider(PDEPlugin.getDefault()
104: .getLabelProvider());
105: toolkit.paintBordersFor(container);
106: makeActions();
107: section.setClient(container);
108: initialize();
109: }
110:
111: protected void handleDoubleClick(IStructuredSelection selection) {
112: fOpenAction.run();
113: }
114:
115: protected void buttonSelected(int index) {
116: if (index == 0)
117: handleNew();
118: }
119:
120: public void dispose() {
121: IFeatureModel model = (IFeatureModel) getPage().getModel();
122: if (model != null)
123: model.removeModelChangedListener(this );
124: super .dispose();
125: }
126:
127: public boolean setFormInput(Object object) {
128: if (object instanceof IFeatureData) {
129: fDataViewer.setSelection(new StructuredSelection(object),
130: true);
131: return true;
132: }
133: return false;
134: }
135:
136: protected void fillContextMenu(IMenuManager manager) {
137: manager.add(fOpenAction);
138: manager.add(new Separator());
139: manager.add(fNewAction);
140: manager.add(fDeleteAction);
141: manager.add(new Separator());
142: getPage().getPDEEditor().getContributor()
143: .contextMenuAboutToShow(manager);
144: }
145:
146: private void handleNew() {
147: final IFeatureModel model = (IFeatureModel) getPage()
148: .getModel();
149: IResource resource = model.getUnderlyingResource();
150: final IContainer folder = resource.getParent();
151:
152: BusyIndicator.showWhile(fDataViewer.getTable().getDisplay(),
153: new Runnable() {
154: public void run() {
155: ResourceSelectionDialog dialog = new ResourceSelectionDialog(
156: fDataViewer.getTable().getShell(),
157: folder, null);
158: dialog.open();
159: Object[] result = dialog.getResult();
160: processNewResult(model, folder, result);
161: }
162: });
163: }
164:
165: private void processNewResult(IFeatureModel model,
166: IContainer folder, Object[] result) {
167: if (result == null || result.length == 0)
168: return;
169: IPath folderPath = folder.getProjectRelativePath();
170: ArrayList entries = new ArrayList();
171: for (int i = 0; i < result.length; i++) {
172: Object item = result[i];
173: if (item instanceof IFile) {
174: IFile file = (IFile) item;
175: IPath filePath = file.getProjectRelativePath();
176: int matching = filePath
177: .matchingFirstSegments(folderPath);
178: IPath relativePath = filePath
179: .removeFirstSegments(matching);
180: String path = relativePath.toString();
181: if (canAdd(model, path))
182: entries.add(path);
183: }
184: }
185: if (entries.size() > 0) {
186: try {
187: IFeatureData[] array = new IFeatureData[entries.size()];
188: for (int i = 0; i < array.length; i++) {
189: IFeatureData data = model.getFactory().createData();
190: String path = (String) entries.get(i);
191: data.setId(path);
192: array[i] = data;
193: }
194: model.getFeature().addData(array);
195: fDataViewer.setSelection(new StructuredSelection(
196: array[0]));
197: } catch (CoreException e) {
198: PDEPlugin.logException(e);
199: }
200: }
201: }
202:
203: private boolean canAdd(IFeatureModel model, String path) {
204: if ("feature.xml".equals(path)) //$NON-NLS-1$
205: return false;
206: IFeatureData[] data = model.getFeature().getData();
207: for (int i = 0; i < data.length; i++) {
208: if (path.equals(data[i].getId()))
209: return false;
210: }
211: return true;
212:
213: }
214:
215: private void handleSelectAll() {
216: IStructuredContentProvider provider = (IStructuredContentProvider) fDataViewer
217: .getContentProvider();
218: Object[] elements = provider
219: .getElements(fDataViewer.getInput());
220: StructuredSelection ssel = new StructuredSelection(elements);
221: fDataViewer.setSelection(ssel);
222: }
223:
224: private void handleDelete() {
225: IStructuredSelection ssel = (IStructuredSelection) fDataViewer
226: .getSelection();
227:
228: if (ssel.isEmpty())
229: return;
230: IFeatureModel model = (IFeatureModel) getPage().getModel();
231: if (!model.isEditable()) {
232: return;
233: }
234: IFeature feature = model.getFeature();
235:
236: try {
237: IFeatureData[] removed = new IFeatureData[ssel.size()];
238: int i = 0;
239: for (Iterator iter = ssel.iterator(); iter.hasNext();) {
240: IFeatureData iobj = (IFeatureData) iter.next();
241: removed[i++] = iobj;
242: }
243: feature.removeData(removed);
244: } catch (CoreException e) {
245: PDEPlugin.logException(e);
246: }
247: }
248:
249: public boolean doGlobalAction(String actionId) {
250: if (actionId.equals(ActionFactory.DELETE.getId())) {
251: BusyIndicator.showWhile(
252: fDataViewer.getTable().getDisplay(),
253: new Runnable() {
254: public void run() {
255: handleDelete();
256: }
257: });
258: return true;
259: }
260: if (actionId.equals(ActionFactory.SELECT_ALL.getId())) {
261: BusyIndicator.showWhile(
262: fDataViewer.getTable().getDisplay(),
263: new Runnable() {
264: public void run() {
265: handleSelectAll();
266: }
267: });
268: return true;
269: }
270: if (actionId.equals(ActionFactory.CUT.getId())) {
271: // delete here and let the editor transfer
272: // the selection to the clipboard
273: handleDelete();
274: return false;
275: }
276: if (actionId.equals(ActionFactory.PASTE.getId())) {
277: doPaste();
278: return true;
279: }
280: return false;
281: }
282:
283: protected void selectionChanged(IStructuredSelection selection) {
284: getPage().getPDEEditor().setSelection(selection);
285: }
286:
287: public void initialize() {
288: IFeatureModel model = (IFeatureModel) getPage().getModel();
289: refresh();
290: getTablePart().setButtonEnabled(0, model.isEditable());
291: model.addModelChangedListener(this );
292: }
293:
294: public void modelChanged(IModelChangedEvent e) {
295: if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
296: markStale();
297: return;
298: }
299: Object obj = e.getChangedObjects()[0];
300: if (obj instanceof IFeatureData
301: && !(obj instanceof IFeaturePlugin)) {
302: if (e.getChangeType() == IModelChangedEvent.CHANGE) {
303: fDataViewer.update(obj, null);
304: } else if (e.getChangeType() == IModelChangedEvent.INSERT) {
305: fDataViewer.add(e.getChangedObjects());
306: } else if (e.getChangeType() == IModelChangedEvent.REMOVE) {
307: fDataViewer.remove(e.getChangedObjects());
308: }
309: }
310: }
311:
312: private void makeActions() {
313: IModel model = (IModel) getPage().getModel();
314: fNewAction = new Action() {
315: public void run() {
316: handleNew();
317: }
318: };
319: fNewAction.setText(PDEUIMessages.Menus_new_label);
320: fNewAction.setEnabled(model.isEditable());
321:
322: fDeleteAction = new Action() {
323: public void run() {
324: BusyIndicator.showWhile(fDataViewer.getTable()
325: .getDisplay(), new Runnable() {
326: public void run() {
327: handleDelete();
328: }
329: });
330: }
331: };
332: fDeleteAction.setEnabled(model.isEditable());
333: fDeleteAction.setText(PDEUIMessages.Actions_delete_label);
334: fOpenAction = new OpenReferenceAction(fDataViewer);
335: }
336:
337: public void setFocus() {
338: if (fDataViewer != null)
339: fDataViewer.getTable().setFocus();
340: }
341:
342: public void refresh() {
343: IFeatureModel model = (IFeatureModel) getPage().getModel();
344: IFeature feature = model.getFeature();
345: fDataViewer.setInput(feature);
346: super .refresh();
347: }
348:
349: /**
350: * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#canPaste(Object,
351: * Object[])
352: */
353: protected boolean canPaste(Object target, Object[] objects) {
354: for (int i = 0; i < objects.length; i++) {
355: if (objects[i] instanceof FeaturePlugin
356: || !(objects[i] instanceof FeatureData))
357: return false;
358: }
359: return true;
360: }
361:
362: /**
363: * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#doPaste()
364: */
365: protected void doPaste() {
366: Clipboard clipboard = getPage().getPDEEditor().getClipboard();
367: ModelDataTransfer modelTransfer = ModelDataTransfer
368: .getInstance();
369: Object[] objects = (Object[]) clipboard
370: .getContents(modelTransfer);
371: if (objects != null) {
372: doPaste(null, objects);
373: }
374: }
375:
376: /**
377: * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#doPaste(Object,
378: * Object[])
379: */
380: protected void doPaste(Object target, Object[] objects) {
381: IFeatureModel model = (IFeatureModel) getPage().getModel();
382: IFeature feature = model.getFeature();
383: if (!model.isEditable()) {
384: return;
385: }
386: FeatureData[] fData = new FeatureData[objects.length];
387: try {
388: for (int i = 0; i < objects.length; i++) {
389: if (objects[i] instanceof FeatureData
390: && !(objects[i] instanceof FeaturePlugin)) {
391: FeatureData fd = (FeatureData) objects[i];
392: fd.setModel(model);
393: fd.setParent(feature);
394: fData[i] = fd;
395: }
396: }
397: feature.addData(fData);
398: } catch (CoreException e) {
399: PDEPlugin.logException(e);
400: }
401: }
402:
403: void fireSelection() {
404: ISelection sel = fDataViewer.getSelection();
405: if (!sel.isEmpty()) {
406: fDataViewer.setSelection(fDataViewer.getSelection());
407: } else if (fDataViewer.getElementAt(0) != null) {
408: fDataViewer.setSelection(new StructuredSelection(
409: fDataViewer.getElementAt(0)));
410: }
411: }
412: }
|