1 11 package org.eclipse.pde.internal.ui.editor.feature; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 16 import org.eclipse.core.resources.IContainer; 17 import org.eclipse.core.resources.IFile; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.jface.action.Action; 22 import org.eclipse.jface.action.IMenuManager; 23 import org.eclipse.jface.action.Separator; 24 import org.eclipse.jface.viewers.ISelection; 25 import org.eclipse.jface.viewers.IStructuredContentProvider; 26 import org.eclipse.jface.viewers.IStructuredSelection; 27 import org.eclipse.jface.viewers.StructuredSelection; 28 import org.eclipse.jface.viewers.TableViewer; 29 import org.eclipse.pde.core.IModel; 30 import org.eclipse.pde.core.IModelChangedEvent; 31 import org.eclipse.pde.internal.core.feature.FeatureData; 32 import org.eclipse.pde.internal.core.feature.FeaturePlugin; 33 import org.eclipse.pde.internal.core.ifeature.IFeature; 34 import org.eclipse.pde.internal.core.ifeature.IFeatureData; 35 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 36 import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin; 37 import org.eclipse.pde.internal.ui.PDEPlugin; 38 import org.eclipse.pde.internal.ui.PDEUIMessages; 39 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 40 import org.eclipse.pde.internal.ui.editor.ModelDataTransfer; 41 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 42 import org.eclipse.pde.internal.ui.editor.TableSection; 43 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider; 44 import org.eclipse.pde.internal.ui.parts.TablePart; 45 import org.eclipse.swt.SWT; 46 import org.eclipse.swt.custom.BusyIndicator; 47 import org.eclipse.swt.dnd.Clipboard; 48 import org.eclipse.swt.layout.GridData; 49 import org.eclipse.swt.widgets.Composite; 50 import org.eclipse.ui.actions.ActionFactory; 51 import org.eclipse.ui.dialogs.ResourceSelectionDialog; 52 import org.eclipse.ui.forms.widgets.FormToolkit; 53 import org.eclipse.ui.forms.widgets.Section; 54 55 public class DataSection extends TableSection { 56 private TableViewer fDataViewer; 57 58 private Action fNewAction; 59 60 private Action fOpenAction; 61 62 private Action fDeleteAction; 63 64 class PluginContentProvider extends DefaultContentProvider implements 65 IStructuredContentProvider { 66 public Object [] getElements(Object parent) { 67 if (parent instanceof IFeature) { 68 return ((IFeature) parent).getData(); 69 } 70 return new Object [0]; 71 } 72 } 73 74 public DataSection(PDEFormPage page, Composite parent) { 75 super(page, parent, Section.DESCRIPTION, new String [] { PDEUIMessages.FeatureEditor_DataSection_new }); 76 getSection().setText(PDEUIMessages.FeatureEditor_DataSection_title); 77 getSection().setDescription(PDEUIMessages.FeatureEditor_DataSection_desc); 78 getTablePart().setEditable(false); 79 } 80 81 public void commit(boolean onSave) { 82 super.commit(onSave); 83 } 84 85 public void createClient(Section section, FormToolkit toolkit) { 86 87 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 88 GridData data = new GridData(GridData.FILL_BOTH); 89 section.setLayoutData(data); 90 91 Composite container = createClientContainer(section, 2, toolkit); 92 93 createViewerPartControl(container, SWT.MULTI, 2, toolkit); 94 TablePart tablePart = getTablePart(); 95 fDataViewer = tablePart.getTableViewer(); 96 fDataViewer.setContentProvider(new PluginContentProvider()); 97 fDataViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider()); 98 toolkit.paintBordersFor(container); 99 makeActions(); 100 section.setClient(container); 101 initialize(); 102 } 103 104 protected void handleDoubleClick(IStructuredSelection selection) { 105 fOpenAction.run(); 106 } 107 108 protected void buttonSelected(int index) { 109 if (index == 0) 110 handleNew(); 111 } 112 113 public void dispose() { 114 IFeatureModel model = (IFeatureModel) getPage().getModel(); 115 if (model != null) 116 model.removeModelChangedListener(this); 117 super.dispose(); 118 } 119 120 public boolean setFormInput(Object object) { 121 if (object instanceof IFeatureData) { 122 fDataViewer.setSelection(new StructuredSelection(object), true); 123 return true; 124 } 125 return false; 126 } 127 128 protected void fillContextMenu(IMenuManager manager) { 129 manager.add(fOpenAction); 130 manager.add(new Separator()); 131 manager.add(fNewAction); 132 manager.add(fDeleteAction); 133 manager.add(new Separator()); 134 getPage().getPDEEditor().getContributor().contextMenuAboutToShow( 135 manager); 136 } 137 138 private void handleNew() { 139 final IFeatureModel model = (IFeatureModel) getPage().getModel(); 140 IResource resource = model.getUnderlyingResource(); 141 final IContainer folder = resource.getParent(); 142 143 BusyIndicator.showWhile(fDataViewer.getTable().getDisplay(), 144 new Runnable () { 145 public void run() { 146 ResourceSelectionDialog dialog = new ResourceSelectionDialog( 147 fDataViewer.getTable().getShell(), folder, null); 148 dialog.open(); 149 Object [] result = dialog.getResult(); 150 processNewResult(model, folder, result); 151 } 152 }); 153 } 154 155 private void processNewResult(IFeatureModel model, IContainer folder, 156 Object [] result) { 157 if (result == null || result.length == 0) 158 return; 159 IPath folderPath = folder.getProjectRelativePath(); 160 ArrayList entries = new ArrayList (); 161 for (int i = 0; i < result.length; i++) { 162 Object item = result[i]; 163 if (item instanceof IFile) { 164 IFile file = (IFile) item; 165 IPath filePath = file.getProjectRelativePath(); 166 int matching = filePath.matchingFirstSegments(folderPath); 167 IPath relativePath = filePath.removeFirstSegments(matching); 168 String path = relativePath.toString(); 169 if (canAdd(model, path)) 170 entries.add(path); 171 } 172 } 173 if (entries.size() > 0) { 174 try { 175 IFeatureData[] array = new IFeatureData[entries.size()]; 176 for (int i = 0; i < array.length; i++) { 177 IFeatureData data = model.getFactory().createData(); 178 String path = (String ) entries.get(i); 179 data.setId(path); 180 array[i] = data; 181 } 182 model.getFeature().addData(array); 183 fDataViewer.setSelection(new StructuredSelection(array[0])); 184 } catch (CoreException e) { 185 PDEPlugin.logException(e); 186 } 187 } 188 } 189 190 private boolean canAdd(IFeatureModel model, String path) { 191 if ("feature.xml".equals(path)) return false; 193 IFeatureData[] data = model.getFeature().getData(); 194 for (int i = 0; i < data.length; i++) { 195 if (path.equals(data[i].getId())) 196 return false; 197 } 198 return true; 199 200 } 201 202 private void handleSelectAll() { 203 IStructuredContentProvider provider = (IStructuredContentProvider) fDataViewer 204 .getContentProvider(); 205 Object [] elements = provider.getElements(fDataViewer.getInput()); 206 StructuredSelection ssel = new StructuredSelection(elements); 207 fDataViewer.setSelection(ssel); 208 } 209 210 private void handleDelete() { 211 IStructuredSelection ssel = (IStructuredSelection) fDataViewer 212 .getSelection(); 213 214 if (ssel.isEmpty()) 215 return; 216 IFeatureModel model = (IFeatureModel) getPage().getModel(); 217 if (!model.isEditable()) { 218 return; 219 } 220 IFeature feature = model.getFeature(); 221 222 try { 223 IFeatureData[] removed = new IFeatureData[ssel.size()]; 224 int i = 0; 225 for (Iterator iter = ssel.iterator(); iter.hasNext();) { 226 IFeatureData iobj = (IFeatureData) iter.next(); 227 removed[i++] = iobj; 228 } 229 feature.removeData(removed); 230 } catch (CoreException e) { 231 PDEPlugin.logException(e); 232 } 233 } 234 235 public boolean doGlobalAction(String actionId) { 236 if (actionId.equals(ActionFactory.DELETE.getId())) { 237 BusyIndicator.showWhile(fDataViewer.getTable().getDisplay(), 238 new Runnable () { 239 public void run() { 240 handleDelete(); 241 } 242 }); 243 return true; 244 } 245 if (actionId.equals(ActionFactory.SELECT_ALL.getId())) { 246 BusyIndicator.showWhile(fDataViewer.getTable().getDisplay(), 247 new Runnable () { 248 public void run() { 249 handleSelectAll(); 250 } 251 }); 252 return true; 253 } 254 if (actionId.equals(ActionFactory.CUT.getId())) { 255 handleDelete(); 258 return false; 259 } 260 if (actionId.equals(ActionFactory.PASTE.getId())) { 261 doPaste(); 262 return true; 263 } 264 return false; 265 } 266 267 protected void selectionChanged(IStructuredSelection selection) { 268 getPage().getPDEEditor().setSelection(selection); 269 } 270 271 public void initialize() { 272 IFeatureModel model = (IFeatureModel) getPage().getModel(); 273 refresh(); 274 getTablePart().setButtonEnabled(0, model.isEditable()); 275 model.addModelChangedListener(this); 276 } 277 278 public void modelChanged(IModelChangedEvent e) { 279 if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 280 markStale(); 281 return; 282 } 283 Object obj = e.getChangedObjects()[0]; 284 if (obj instanceof IFeatureData && !(obj instanceof IFeaturePlugin)) { 285 if (e.getChangeType() == IModelChangedEvent.CHANGE) { 286 fDataViewer.update(obj, null); 287 } else if (e.getChangeType() == IModelChangedEvent.INSERT) { 288 fDataViewer.add(e.getChangedObjects()); 289 } else if (e.getChangeType() == IModelChangedEvent.REMOVE) { 290 fDataViewer.remove(e.getChangedObjects()); 291 } 292 } 293 } 294 295 private void makeActions() { 296 IModel model = (IModel) getPage().getModel(); 297 fNewAction = new Action() { 298 public void run() { 299 handleNew(); 300 } 301 }; 302 fNewAction.setText(PDEUIMessages.Menus_new_label); 303 fNewAction.setEnabled(model.isEditable()); 304 305 fDeleteAction = new Action() { 306 public void run() { 307 BusyIndicator.showWhile(fDataViewer.getTable().getDisplay(), 308 new Runnable () { 309 public void run() { 310 handleDelete(); 311 } 312 }); 313 } 314 }; 315 fDeleteAction.setEnabled(model.isEditable()); 316 fDeleteAction.setText(PDEUIMessages.Actions_delete_label); 317 fOpenAction = new OpenReferenceAction(fDataViewer); 318 } 319 320 public void setFocus() { 321 if (fDataViewer != null) 322 fDataViewer.getTable().setFocus(); 323 } 324 325 public void refresh() { 326 IFeatureModel model = (IFeatureModel) getPage().getModel(); 327 IFeature feature = model.getFeature(); 328 fDataViewer.setInput(feature); 329 super.refresh(); 330 } 331 332 336 protected boolean canPaste(Object target, Object [] objects) { 337 for (int i = 0; i < objects.length; i++) { 338 if (objects[i] instanceof FeaturePlugin 339 || !(objects[i] instanceof FeatureData)) 340 return false; 341 } 342 return true; 343 } 344 345 348 protected void doPaste() { 349 Clipboard clipboard = getPage().getPDEEditor().getClipboard(); 350 ModelDataTransfer modelTransfer = ModelDataTransfer.getInstance(); 351 Object [] objects = (Object []) clipboard.getContents(modelTransfer); 352 if (objects != null) { 353 doPaste(null, objects); 354 } 355 } 356 357 361 protected void doPaste(Object target, Object [] objects) { 362 IFeatureModel model = (IFeatureModel) getPage().getModel(); 363 IFeature feature = model.getFeature(); 364 if (!model.isEditable()) { 365 return; 366 } 367 FeatureData[] fData = new FeatureData[objects.length]; 368 try { 369 for (int i = 0; i < objects.length; i++) { 370 if (objects[i] instanceof FeatureData 371 && !(objects[i] instanceof FeaturePlugin)) { 372 FeatureData fd = (FeatureData) objects[i]; 373 fd.setModel(model); 374 fd.setParent(feature); 375 fData[i] = fd; 376 } 377 } 378 feature.addData(fData); 379 } catch (CoreException e) { 380 PDEPlugin.logException(e); 381 } 382 } 383 384 void fireSelection() { 385 ISelection sel = fDataViewer.getSelection(); 386 if (!sel.isEmpty()) { 387 fDataViewer.setSelection(fDataViewer.getSelection()); 388 } else if (fDataViewer.getElementAt(0) != null) { 389 fDataViewer.setSelection(new StructuredSelection(fDataViewer 390 .getElementAt(0))); 391 } 392 } 393 } 394 | Popular Tags |