1 11 package org.eclipse.pde.internal.ui.editor; 12 13 import org.eclipse.jface.action.IMenuListener; 14 import org.eclipse.jface.action.IMenuManager; 15 import org.eclipse.jface.action.MenuManager; 16 import org.eclipse.jface.viewers.ISelection; 17 import org.eclipse.jface.viewers.IStructuredSelection; 18 import org.eclipse.pde.internal.ui.parts.StructuredViewerPart; 19 import org.eclipse.swt.dnd.Clipboard; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Control; 22 import org.eclipse.swt.widgets.Menu; 23 import org.eclipse.ui.forms.widgets.FormToolkit; 24 25 public abstract class StructuredViewerSection extends PDESection { 26 protected StructuredViewerPart fViewerPart; 27 28 32 public StructuredViewerSection(PDEFormPage formPage, Composite parent, int style, String [] buttonLabels) { 33 this(formPage, parent, style, true, buttonLabels); 34 } 35 36 40 public StructuredViewerSection(PDEFormPage formPage, Composite parent, int style, boolean titleBar, String [] buttonLabels) { 41 super(formPage, parent, style, titleBar); 42 fViewerPart = createViewerPart(buttonLabels); 43 fViewerPart.setMinimumSize(50, 50); 44 FormToolkit toolkit = formPage.getManagedForm().getToolkit(); 45 createClient(getSection(), toolkit); 46 } 47 48 protected void createViewerPartControl(Composite parent, int style, int span, FormToolkit toolkit) { 49 fViewerPart.createControl(parent, style, span, toolkit); 50 MenuManager popupMenuManager = new MenuManager(); 51 IMenuListener listener = new IMenuListener() { 52 public void menuAboutToShow(IMenuManager mng) { 53 fillContextMenu(mng); 54 } 55 }; 56 popupMenuManager.addMenuListener(listener); 57 popupMenuManager.setRemoveAllWhenShown(true); 58 Control control = fViewerPart.getControl(); 59 Menu menu = popupMenuManager.createContextMenu(control); 60 control.setMenu(menu); 61 } 62 63 protected Composite createClientContainer(Composite parent, int span, FormToolkit toolkit) { 64 Composite container = toolkit.createComposite(parent); 65 container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, span)); 66 return container; 67 } 68 69 protected abstract StructuredViewerPart createViewerPart(String [] buttonLabels); 70 71 protected void fillContextMenu(IMenuManager manager) { 72 } 73 74 protected void buttonSelected(int index) { 75 } 76 77 protected void doPaste() { 78 ISelection selection = getViewerSelection(); 79 IStructuredSelection ssel = (IStructuredSelection)selection; 80 if (ssel.size()>1) return; 81 82 Object target = ssel.getFirstElement(); 83 84 Clipboard clipboard = getPage().getPDEEditor().getClipboard(); 85 ModelDataTransfer modelTransfer = ModelDataTransfer.getInstance(); 86 Object [] objects = (Object [])clipboard.getContents(modelTransfer); 87 if (objects!=null) { 88 doPaste(target, objects); 89 } 90 } 91 92 95 public boolean canPaste(Clipboard clipboard) { 96 ISelection selection = getViewerSelection(); 98 IStructuredSelection ssel = (IStructuredSelection)selection; 99 if (ssel.size()>1) return false; 100 101 Object target = ssel.getFirstElement(); 102 ModelDataTransfer modelTransfer = ModelDataTransfer.getInstance(); 103 Object [] objects = (Object [])clipboard.getContents(modelTransfer); 104 if (objects!=null && objects.length>0) { 105 return canPaste(target, objects); 106 } 107 return false; 108 } 109 110 protected ISelection getViewerSelection() { 111 return fViewerPart.getViewer().getSelection(); 112 } 113 114 118 protected void doPaste(Object targetObject, Object [] sourceObjects) { 119 } 122 123 128 protected boolean canPaste(Object targetObject, Object [] sourceObjects) { 129 return false; 130 } 131 132 public void setFocus() { 133 fViewerPart.getControl().setFocus(); 134 } 135 136 public StructuredViewerPart getStructuredViewerPart() { 137 return this.fViewerPart; 138 } 139 } 140 | Popular Tags |