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;
011:
012: import org.eclipse.jface.action.IMenuListener;
013: import org.eclipse.jface.action.IMenuManager;
014: import org.eclipse.jface.action.MenuManager;
015: import org.eclipse.jface.viewers.ISelection;
016: import org.eclipse.jface.viewers.IStructuredSelection;
017: import org.eclipse.jface.viewers.StructuredViewer;
018: import org.eclipse.pde.internal.ui.parts.StructuredViewerPart;
019: import org.eclipse.swt.dnd.Clipboard;
020: import org.eclipse.swt.dnd.DND;
021: import org.eclipse.swt.dnd.TextTransfer;
022: import org.eclipse.swt.dnd.Transfer;
023: import org.eclipse.swt.widgets.Composite;
024: import org.eclipse.swt.widgets.Control;
025: import org.eclipse.swt.widgets.Menu;
026: import org.eclipse.ui.forms.widgets.FormToolkit;
027:
028: public abstract class StructuredViewerSection extends PDESection
029: implements IPDEDragParticipant, IPDEDropParticipant {
030:
031: protected StructuredViewerPart fViewerPart;
032:
033: private boolean fDoSelection;
034:
035: private PDEDragAdapter fDragAdapter;
036:
037: private PDEDropAdapter fDropAdapter;
038:
039: /**
040: * Constructor for StructuredViewerSection.
041: * @param formPage
042: */
043: public StructuredViewerSection(PDEFormPage formPage,
044: Composite parent, int style, String[] buttonLabels) {
045: this (formPage, parent, style, true, buttonLabels);
046: }
047:
048: /**
049: * Constructor for StructuredViewerSection.
050: * @param formPage
051: */
052: public StructuredViewerSection(PDEFormPage formPage,
053: Composite parent, int style, boolean titleBar,
054: String[] buttonLabels) {
055: super (formPage, parent, style, titleBar);
056: fViewerPart = createViewerPart(buttonLabels);
057: fViewerPart.setMinimumSize(50, 50);
058: FormToolkit toolkit = formPage.getManagedForm().getToolkit();
059: createClient(getSection(), toolkit);
060: fDoSelection = true;
061: }
062:
063: protected void createViewerPartControl(Composite parent, int style,
064: int span, FormToolkit toolkit) {
065: fViewerPart.createControl(parent, style, span, toolkit);
066: MenuManager popupMenuManager = new MenuManager();
067: IMenuListener listener = new IMenuListener() {
068: public void menuAboutToShow(IMenuManager mng) {
069: fillContextMenu(mng);
070: }
071: };
072: popupMenuManager.addMenuListener(listener);
073: popupMenuManager.setRemoveAllWhenShown(true);
074: Control control = fViewerPart.getControl();
075: Menu menu = popupMenuManager.createContextMenu(control);
076: control.setMenu(menu);
077: // Initialize drag and drop
078: if (isDragAndDropEnabled()) {
079: initializeDragAndDrop();
080: } else {
081: fDragAdapter = null;
082: fDropAdapter = null;
083: }
084: }
085:
086: protected Composite createClientContainer(Composite parent,
087: int span, FormToolkit toolkit) {
088: Composite container = toolkit.createComposite(parent);
089: container.setLayout(FormLayoutFactory
090: .createSectionClientGridLayout(false, span));
091: return container;
092: }
093:
094: protected abstract StructuredViewerPart createViewerPart(
095: String[] buttonLabels);
096:
097: protected void fillContextMenu(IMenuManager manager) {
098: }
099:
100: protected void buttonSelected(int index) {
101: }
102:
103: protected void doPaste() {
104: ISelection selection = getViewerSelection();
105: IStructuredSelection ssel = (IStructuredSelection) selection;
106: if (ssel.size() > 1)
107: return;
108:
109: Object target = ssel.getFirstElement();
110:
111: Clipboard clipboard = getPage().getPDEEditor().getClipboard();
112: ModelDataTransfer modelTransfer = ModelDataTransfer
113: .getInstance();
114: Object[] objects = (Object[]) clipboard
115: .getContents(modelTransfer);
116: if (objects != null) {
117: doPaste(target, objects);
118: }
119: }
120:
121: /* (non-Javadoc)
122: * @see org.eclipse.pde.internal.ui.editor.PDESection#canPaste(org.eclipse.swt.dnd.Clipboard)
123: */
124: public boolean canPaste(Clipboard clipboard) {
125: // TODO: MP: CCP: Checking clipboard data done incorrectly. See Bug 37223
126: ISelection selection = getViewerSelection();
127: IStructuredSelection ssel = (IStructuredSelection) selection;
128: if (ssel.size() > 1)
129: return false;
130:
131: Object target = ssel.getFirstElement();
132: ModelDataTransfer modelTransfer = ModelDataTransfer
133: .getInstance();
134: Object[] objects = (Object[]) clipboard
135: .getContents(modelTransfer);
136: if (objects != null && objects.length > 0) {
137: return canPaste(target, objects);
138: }
139: return false;
140: }
141:
142: protected ISelection getViewerSelection() {
143: return fViewerPart.getViewer().getSelection();
144: }
145:
146: /**
147: * @param targetObject
148: * @param sourceObjects
149: */
150: protected void doPaste(Object targetObject, Object[] sourceObjects) {
151: // NO-OP
152: // Children will override to provide fuctionality
153: }
154:
155: /**
156: * @param targetObject
157: * @param sourceObjects
158: * @return
159: */
160: protected boolean canPaste(Object targetObject,
161: Object[] sourceObjects) {
162: return false;
163: }
164:
165: public void setFocus() {
166: fViewerPart.getControl().setFocus();
167: }
168:
169: public StructuredViewerPart getStructuredViewerPart() {
170: return this .fViewerPart;
171: }
172:
173: /**
174: * <p>Given the index of TreeViewer item and the size of the array of its immediate
175: * siblings, gets the index of the desired new selection as follows:
176: * <ul><li>if this is the only item, return -1 (meaning select the parent)</li>
177: * <li>if this is the last item, return the index of the predecessor</li>
178: * <li>otherwise, return the index of the successor</li></p>
179: *
180: * @param thisIndex
181: * the item's index
182: * @param length
183: * the array length
184: * @return
185: * new selection index or -1 for parent
186: */
187: protected int getNewSelectionIndex(int this Index, int length) {
188: if (this Index == length - 1)
189: return this Index - 1;
190: return this Index + 1;
191: }
192:
193: protected int getArrayIndex(Object[] array, Object object) {
194: for (int i = 0; i < array.length; i++) {
195: if (array[i].equals(object))
196: return i;
197: }
198: return -1;
199: }
200:
201: /* (non-Javadoc)
202: * @see org.eclipse.pde.internal.ui.editor.IPDEDragParticipant#canDragCopy(java.lang.Object[])
203: */
204: public boolean canDragCopy(Object[] sourceObjects) {
205: return false;
206: }
207:
208: /* (non-Javadoc)
209: * @see org.eclipse.pde.internal.ui.editor.IPDEDragParticipant#canDragLink(java.lang.Object[])
210: */
211: public boolean canDragLink(Object[] sourceObjects) {
212: return false;
213: }
214:
215: /* (non-Javadoc)
216: * @see org.eclipse.pde.internal.ui.editor.IPDEDragParticipant#canDragMove(java.lang.Object[])
217: */
218: public boolean canDragMove(Object[] sourceObjects) {
219: return false;
220: }
221:
222: /* (non-Javadoc)
223: * @see org.eclipse.pde.internal.ui.editor.IPDEDropParticipant#canDropCopy(java.lang.Object, java.lang.Object[], int)
224: */
225: public boolean canDropCopy(Object targetObject,
226: Object[] sourceObjects, int targetLocation) {
227: return false;
228: }
229:
230: /* (non-Javadoc)
231: * @see org.eclipse.pde.internal.ui.editor.IPDEDropParticipant#canDropLink(java.lang.Object, java.lang.Object[], int)
232: */
233: public boolean canDropLink(Object targetObject,
234: Object[] sourceObjects, int targetLocation) {
235: return false;
236: }
237:
238: /* (non-Javadoc)
239: * @see org.eclipse.pde.internal.ui.editor.IPDEDropParticipant#canDropMove(java.lang.Object, java.lang.Object[], int)
240: */
241: public boolean canDropMove(Object targetObject,
242: Object[] sourceObjects, int targetLocation) {
243: return false;
244: }
245:
246: /* (non-Javadoc)
247: * @see org.eclipse.pde.internal.ui.editor.IPDEDropParticipant#doDropCopy(java.lang.Object, java.lang.Object[], int)
248: */
249: public void doDropCopy(Object targetObject, Object[] sourceObjects,
250: int targetLocation) {
251: // NO-OP
252: // Sub-classes to override
253: }
254:
255: /* (non-Javadoc)
256: * @see org.eclipse.pde.internal.ui.editor.IPDEDropParticipant#doDropLink(java.lang.Object, java.lang.Object[], int)
257: */
258: public void doDropLink(Object targetObject, Object[] sourceObjects,
259: int targetLocation) {
260: // NO-OP
261: // Sub-classes to override
262: }
263:
264: /* (non-Javadoc)
265: * @see org.eclipse.pde.internal.ui.editor.IPDEDropParticipant#doDropMove(java.lang.Object, java.lang.Object[], int)
266: */
267: public void doDropMove(Object targetObject, Object[] sourceObjects,
268: int targetLocation) {
269: // NO-OP
270: // Sub-classes to override
271: }
272:
273: /* (non-Javadoc)
274: * @see org.eclipse.pde.internal.ui.editor.IPDEDragParticipant#doDragRemove(java.lang.Object[])
275: */
276: public void doDragRemove(Object[] sourceObjects) {
277: // NO-OP
278: // Sub-classes to override
279: }
280:
281: /* (non-Javadoc)
282: * @see org.eclipse.pde.internal.ui.editor.IPDEDragParticipant#getSupportedDNDOperations()
283: */
284: public int getSupportedDNDOperations() {
285: return DND.DROP_MOVE;
286: }
287:
288: /**
289: *
290: */
291: protected void initializeDragAndDrop() {
292: // Ensure the model is editable and we have a viewer part
293: if (isEditable() == false) {
294: return;
295: } else if (fViewerPart == null) {
296: return;
297: }
298: StructuredViewer viewer = fViewerPart.getViewer();
299: // Ensure we have a viewer
300: if (viewer == null) {
301: return;
302: }
303: // Create drag adapter
304: fDragAdapter = new PDEDragAdapter(this );
305: // Create drop adapter
306: fDropAdapter = new PDEDropAdapter(viewer, this , fDragAdapter);
307: // Add drag support to viewer
308: int dragOperations = getSupportedDNDOperations();
309: viewer.addDragSupport(dragOperations, getDragTransfers(),
310: fDragAdapter);
311: // Add drop support to viewer
312: int dropOperations = dragOperations | DND.DROP_DEFAULT;
313: viewer.addDropSupport(dropOperations, getDropTransfers(),
314: fDropAdapter);
315: }
316:
317: /**
318: * @return The original source objects (dragged)
319: */
320: protected Object[] getDragSourceObjects() {
321: // Verify DND is enabled
322: if (isDragAndDropEnabled() == false) {
323: return null;
324: } else if (fDragAdapter == null) {
325: return null;
326: }
327: return fDragAdapter.getSourceObjects();
328: }
329:
330: /**
331: * @return
332: */
333: protected Transfer[] getDragTransfers() {
334: return new Transfer[] { ModelDataTransfer.getInstance(),
335: TextTransfer.getInstance() };
336: }
337:
338: /**
339: * @return
340: */
341: protected Transfer[] getDropTransfers() {
342: return getDragTransfers();
343: }
344:
345: /**
346: * @return
347: */
348: protected boolean isDragAndDropEnabled() {
349: return false;
350: }
351:
352: /**
353: * @param select
354: */
355: protected void doSelect(boolean select) {
356: fDoSelection = select;
357: }
358:
359: /**
360: * @return
361: */
362: protected boolean canSelect() {
363: return fDoSelection;
364: }
365:
366: }
|