1 11 package org.eclipse.pde.internal.ui.editor.product; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.jface.action.Action; 16 import org.eclipse.jface.action.IAction; 17 import org.eclipse.jface.action.IMenuManager; 18 import org.eclipse.jface.action.Separator; 19 import org.eclipse.jface.action.ToolBarManager; 20 import org.eclipse.jface.util.IPropertyChangeListener; 21 import org.eclipse.jface.util.PropertyChangeEvent; 22 import org.eclipse.jface.viewers.ISelection; 23 import org.eclipse.jface.viewers.IStructuredSelection; 24 import org.eclipse.jface.viewers.StructuredSelection; 25 import org.eclipse.jface.viewers.TableViewer; 26 import org.eclipse.jface.window.Window; 27 import org.eclipse.jface.wizard.WizardDialog; 28 import org.eclipse.pde.core.IModelChangedEvent; 29 import org.eclipse.pde.internal.core.FeatureModelManager; 30 import org.eclipse.pde.internal.core.PDECore; 31 import org.eclipse.pde.internal.core.ifeature.IFeature; 32 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 33 import org.eclipse.pde.internal.core.iproduct.IProduct; 34 import org.eclipse.pde.internal.core.iproduct.IProductFeature; 35 import org.eclipse.pde.internal.core.iproduct.IProductModel; 36 import org.eclipse.pde.internal.core.iproduct.IProductModelFactory; 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.PDEFormPage; 41 import org.eclipse.pde.internal.ui.editor.TableSection; 42 import org.eclipse.pde.internal.ui.editor.actions.SortAction; 43 import org.eclipse.pde.internal.ui.editor.feature.FeatureEditor; 44 import org.eclipse.pde.internal.ui.elements.DefaultTableProvider; 45 import org.eclipse.pde.internal.ui.parts.TablePart; 46 import org.eclipse.pde.internal.ui.util.SWTUtil; 47 import org.eclipse.pde.internal.ui.wizards.FeatureSelectionDialog; 48 import org.eclipse.pde.internal.ui.wizards.feature.NewFeatureProjectWizard; 49 import org.eclipse.swt.SWT; 50 import org.eclipse.swt.events.DisposeEvent; 51 import org.eclipse.swt.events.DisposeListener; 52 import org.eclipse.swt.graphics.Cursor; 53 import org.eclipse.swt.layout.GridData; 54 import org.eclipse.swt.widgets.Composite; 55 import org.eclipse.swt.widgets.Display; 56 import org.eclipse.swt.widgets.Table; 57 import org.eclipse.swt.widgets.TableItem; 58 import org.eclipse.swt.widgets.ToolBar; 59 import org.eclipse.ui.actions.ActionFactory; 60 import org.eclipse.ui.forms.widgets.FormToolkit; 61 import org.eclipse.ui.forms.widgets.Section; 62 63 public class FeatureSection extends TableSection implements IPropertyChangeListener { 64 65 private SortAction fSortAction; 66 67 class ContentProvider extends DefaultTableProvider { 68 public Object [] getElements(Object parent) { 69 return getProduct().getFeatures(); 70 } 71 } 72 73 private TableViewer fFeatureTable; 74 75 public FeatureSection(PDEFormPage formPage, Composite parent) { 76 super(formPage, parent, Section.DESCRIPTION, getButtonLabels()); 77 } 78 79 private static String [] getButtonLabels() { 80 String [] labels = new String [8]; 81 labels[0] = PDEUIMessages.Product_FeatureSection_add; 82 labels[1] = PDEUIMessages.Product_FeatureSection_remove; 83 labels[2] = PDEUIMessages.Product_PluginSection_removeAll; 84 labels[3] = PDEUIMessages.Product_FeatureSection_up; 85 labels[4] = PDEUIMessages.Product_FeatureSection_down; 86 labels[5] = null; 87 labels[6] = null; 88 labels[7] = PDEUIMessages.Product_FeatureSection_newFeature; 89 return labels; 90 } 91 92 93 96 protected void createClient(Section section, FormToolkit toolkit) { 97 98 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 99 GridData sectionData = new GridData(GridData.FILL_BOTH); 100 sectionData.verticalSpan = 2; 101 section.setLayoutData(sectionData); 102 103 Composite container = createClientContainer(section, 2, toolkit); 104 createViewerPartControl(container, SWT.MULTI, 2, toolkit); 105 container.setLayoutData(new GridData(GridData.FILL_BOTH)); 106 107 TablePart tablePart = getTablePart(); 108 fFeatureTable = tablePart.getTableViewer(); 109 fFeatureTable.setContentProvider(new ContentProvider()); 110 fFeatureTable.setLabelProvider(PDEPlugin.getDefault().getLabelProvider()); 111 fFeatureTable.setSorter(null); 112 GridData data = (GridData)tablePart.getControl().getLayoutData(); 113 data.minimumWidth = 200; 114 fFeatureTable.setInput(PDECore.getDefault().getFeatureModelManager()); 115 116 tablePart.setButtonEnabled(0, isEditable()); 117 118 120 tablePart.setButtonEnabled(3, isEditable()); 121 tablePart.setButtonEnabled(4, isEditable()); 122 tablePart.setButtonEnabled(7, isEditable()); 123 124 toolkit.paintBordersFor(container); 125 section.setClient(container); 126 127 section.setText(PDEUIMessages.Product_FeatureSection_title); 128 section.setDescription(PDEUIMessages.Product_FeatureSection_desc); 130 getModel().addModelChangedListener(this); 131 createSectionToolbar(section, toolkit); 132 } 133 134 138 private void createSectionToolbar(Section section, FormToolkit toolkit) { 139 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT); 140 ToolBar toolbar = toolBarManager.createControl(section); 141 final Cursor handCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND); 142 toolbar.setCursor(handCursor); 143 toolbar.addDisposeListener(new DisposeListener() { 145 public void widgetDisposed(DisposeEvent e) { 146 if ((handCursor != null) && 147 (handCursor.isDisposed() == false)) { 148 handCursor.dispose(); 149 } 150 } 151 }); 152 fSortAction = new SortAction(fFeatureTable, 154 PDEUIMessages.Product_FeatureSection_sortAlpha, null, null, this); 155 toolBarManager.add(fSortAction); 156 toolBarManager.update(true); 157 158 section.setTextClient(toolbar); 159 } 160 161 164 protected void buttonSelected(int index) { 165 switch (index) { 166 case 0: 167 handleAdd(); 168 break; 169 case 1: 170 handleDelete(); 171 break; 172 case 2: 173 handleRemoveAll(); 174 break; 175 case 3: 176 handleUp(); 177 break; 178 case 4: 179 handleDown(); 180 break; 181 case 7: 182 handleNewFeature(); 183 } 184 } 185 186 private void handleNewFeature() { 187 NewFeatureProjectWizard wizard = new NewFeatureProjectWizard(); 188 WizardDialog dialog = new WizardDialog(PDEPlugin.getActiveWorkbenchShell(), wizard); 189 dialog.create(); 190 SWTUtil.setDialogSize(dialog, 400, 500); 191 if (dialog.open() == Window.OK) { 192 addFeature(wizard.getFeatureId(), wizard.getFeatureVersion()); 193 } 194 } 195 196 private void addFeature(String id, String version) { 197 IProduct product = getProduct(); 198 IProductModelFactory factory = product.getModel().getFactory(); 199 IProductFeature feature = factory.createFeature(); 200 feature.setId(id); 201 feature.setVersion(version); 202 product.addFeatures(new IProductFeature[] {feature}); 203 } 204 205 206 private void handleRemoveAll() { 207 IProduct product = getProduct(); 208 product.removeFeatures(product.getFeatures()); 209 } 210 211 214 protected void handleDoubleClick(IStructuredSelection selection) { 215 handleOpen(selection); 216 } 217 218 221 public void dispose() { 222 IProductModel model = getModel(); 223 if (model != null) 224 model.removeModelChangedListener(this); 225 super.dispose(); 226 } 227 228 231 public boolean doGlobalAction(String actionId) { 232 if (actionId.equals(ActionFactory.DELETE.getId())) { 233 handleDelete(); 234 return true; 235 } 236 if (actionId.equals(ActionFactory.CUT.getId())) { 237 handleDelete(); 238 return false; 239 } 240 if (actionId.equals(ActionFactory.PASTE.getId())) { 241 doPaste(); 242 return true; 243 } 244 return false; 245 } 246 247 protected boolean canPaste(Object target, Object [] objects) { 248 for (int i = 0; i < objects.length; i++) { 249 if (objects[i] instanceof IProductFeature) 250 return true; 251 } 252 return false; 253 } 254 255 protected void doPaste(Object target, Object [] objects) { 256 IProductFeature[] features; 257 if (objects instanceof IProductFeature[]) 258 features = (IProductFeature[])objects; 259 else { 260 features = new IProductFeature[objects.length]; 261 for (int i = 0; i < objects.length; i++) 262 if (objects[i] instanceof IProductFeature) 263 features[i] = (IProductFeature)objects[i]; 264 } 265 getProduct().addFeatures(features); 266 } 267 268 269 private void handleDelete() { 270 IStructuredSelection ssel = (IStructuredSelection)fFeatureTable.getSelection(); 271 if (ssel.size() > 0) { 272 Object [] objects = ssel.toArray(); 273 IProductFeature[] features = new IProductFeature[objects.length]; 274 System.arraycopy(objects, 0, features, 0, objects.length); 275 getProduct().removeFeatures(features); 276 } 277 } 278 279 282 protected void fillContextMenu(IMenuManager manager) { 283 IStructuredSelection ssel = (IStructuredSelection)fFeatureTable.getSelection(); 284 if (ssel == null) 285 return; 286 287 Action openAction = new Action(PDEUIMessages.Product_FeatureSection_open) { 288 public void run() { 289 handleDoubleClick((IStructuredSelection)fFeatureTable.getSelection()); 290 } 291 }; 292 openAction.setEnabled(isEditable() && ssel.size() == 1); 293 manager.add(openAction); 294 295 manager.add(new Separator()); 296 297 Action removeAction = new Action(PDEUIMessages.Product_FeatureSection_remove) { 298 public void run() { 299 handleDelete(); 300 } 301 }; 302 removeAction.setEnabled(isEditable() && ssel.size() > 0); 303 manager.add(removeAction); 304 305 Action removeAll = new Action(PDEUIMessages.FeatureSection_removeAll) { 306 public void run() { 307 handleRemoveAll(); 308 } 309 }; 310 removeAll.setEnabled(isEditable()); 311 manager.add(removeAll); 312 313 manager.add(new Separator()); 314 315 getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager); 316 } 317 318 private void handleOpen(IStructuredSelection selection) { 319 if (!selection.isEmpty()) { 320 IProductFeature feature = (IProductFeature)selection.getFirstElement(); 321 FeatureModelManager manager = PDECore.getDefault().getFeatureModelManager(); 322 IFeatureModel model = manager.findFeatureModel(feature.getId(), feature.getVersion()); 323 FeatureEditor.openFeatureEditor(model); 324 } 325 } 326 327 private void handleAdd() { 328 FeatureSelectionDialog dialog = new FeatureSelectionDialog(PDEPlugin 329 .getActiveWorkbenchShell(), getAvailableChoices(), true); 330 if (dialog.open() == Window.OK) { 331 Object [] models = dialog.getResult(); 332 for (int i = 0; i < models.length; i++) { 333 IFeature feature = ((IFeatureModel)models[i]).getFeature(); 334 addFeature(feature.getId(), feature.getVersion()); 335 } 336 } 337 } 338 339 private IFeatureModel[] getAvailableChoices() { 340 IFeatureModel[] models = PDECore.getDefault().getFeatureModelManager().getModels(); 341 IProduct product = getProduct(); 342 ArrayList list = new ArrayList (); 343 for (int i = 0; i < models.length; i++) { 344 String id = models[i].getFeature().getId(); 345 if (id != null && !product.containsFeature(id)) { 346 list.add(models[i]); 347 } 348 } 349 return (IFeatureModel[])list.toArray(new IFeatureModel[list.size()]); 350 } 351 352 353 private IProduct getProduct() { 354 return getModel().getProduct(); 355 } 356 357 private IProductModel getModel() { 358 return (IProductModel) getPage().getPDEEditor().getAggregateModel(); 359 } 360 361 364 public void modelChanged(IModelChangedEvent e) { 365 Object [] objects = e.getChangedObjects(); 366 if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 368 handleModelEventWorldChanged(e); 369 return; 370 } else if (e.getChangeType() == IModelChangedEvent.INSERT) { 371 for (int i = 0; i < objects.length; i++) { 372 if (objects[i] instanceof IProductFeature) 373 fFeatureTable.add(objects[i]); 374 } 375 } else if (e.getChangeType() == IModelChangedEvent.REMOVE) { 376 377 Table table = fFeatureTable.getTable(); 378 int index = table.getSelectionIndex(); 379 380 for (int i = 0; i < objects.length; i++) { 381 if (objects[i] instanceof IProductFeature) 382 fFeatureTable.remove(objects[i]); 383 } 384 385 387 int count = table.getItemCount(); 388 389 if ( count == 0 ) { 390 } else if ( index < count ) { 392 table.setSelection( index ); 393 } else { 394 table.setSelection( count - 1 ); 395 } 396 397 } else if (e.getChangeType() == IModelChangedEvent.CHANGE) { 398 fFeatureTable.refresh(); 399 } 400 updateButtons(false, true); 401 } 402 403 406 private void handleModelEventWorldChanged(IModelChangedEvent event) { 407 if (fFeatureTable.getTable().isDisposed()) { 412 return; 413 } 414 fFeatureTable.setInput(PDECore.getDefault().getFeatureModelManager()); 416 refresh(); 418 } 419 420 423 public void refresh() { 424 fFeatureTable.refresh(); 425 updateButtons(true, true); 426 super.refresh(); 427 } 428 429 protected void selectionChanged(IStructuredSelection selection) { 430 getPage().getPDEEditor().setSelection(selection); 431 updateButtons(true, false); 432 } 433 434 public boolean setFormInput(Object input) { 435 if (input instanceof IProductFeature) { 436 fFeatureTable.setSelection(new StructuredSelection(input), true); 437 return true; 438 } 439 return super.setFormInput(input); 440 } 441 442 private void updateButtons(boolean updateRemove, boolean updateRemoveAll) { 443 TablePart tablePart = getTablePart(); 444 Table table = tablePart.getTableViewer().getTable(); 445 TableItem[] tableSelection = table.getSelection(); 446 boolean hasSelection = tableSelection.length > 0; 447 if (updateRemove) { 448 ISelection selection = getViewerSelection(); 449 tablePart.setButtonEnabled(1, 450 isEditable() && !selection.isEmpty() && selection instanceof IStructuredSelection && 451 ((IStructuredSelection)selection).getFirstElement() instanceof IProductFeature); 452 } 453 if (updateRemoveAll) 454 tablePart.setButtonEnabled(2, isEditable() && fFeatureTable.getTable().getItemCount() > 0); 455 456 boolean canMove = table.getItemCount() > 1 && tableSelection.length == 1 && !fSortAction.isChecked(); 458 tablePart.setButtonEnabled( 459 3, 460 canMove && isEditable() && hasSelection && table.getSelectionIndex() > 0); 461 tablePart.setButtonEnabled( 462 4, 463 canMove 464 && hasSelection && isEditable() 465 && table.getSelectionIndex() < table.getItemCount() - 1); 466 } 467 468 protected boolean createCount() { return true; } 469 470 private void handleUp() { 471 int index = getTablePart().getTableViewer().getTable().getSelectionIndex(); 472 if (index < 1) 473 return; 474 swap(index, index - 1); 475 } 476 477 private void handleDown() { 478 Table table = getTablePart().getTableViewer().getTable(); 479 int index = table.getSelectionIndex(); 480 if (index == table.getItemCount() - 1) 481 return; 482 swap(index, index + 1); 483 } 484 485 public void swap(int index1, int index2) { 486 Table table = getTablePart().getTableViewer().getTable(); 487 IProductFeature feature1 = ((IProductFeature)table.getItem(index1).getData()); 488 IProductFeature feature2 = ((IProductFeature)table.getItem(index2).getData()); 489 490 IProduct product = getProduct(); 491 product.swap(feature1, feature2); 492 } 493 494 public void propertyChange(PropertyChangeEvent event) { 495 if (fSortAction.equals(event.getSource()) && IAction.RESULT.equals(event.getProperty())) { 496 updateButtons(true, true); 497 } 498 } 499 } 500 | Popular Tags |