1 11 12 package org.eclipse.pde.internal.ui.editor.cheatsheet.comp; 13 14 import java.io.IOException ; 15 import java.io.PrintWriter ; 16 import java.io.StringWriter ; 17 import java.net.URL ; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.jface.action.IMenuManager; 21 import org.eclipse.jface.action.MenuManager; 22 import org.eclipse.jface.action.Separator; 23 import org.eclipse.jface.action.ToolBarManager; 24 import org.eclipse.jface.viewers.ISelection; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 import org.eclipse.jface.viewers.StructuredSelection; 27 import org.eclipse.jface.viewers.TreeViewer; 28 import org.eclipse.pde.core.IModelChangedEvent; 29 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCS; 30 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSConstants; 31 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSModel; 32 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject; 33 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTask; 34 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskGroup; 35 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject; 36 import org.eclipse.pde.internal.ui.PDEPlugin; 37 import org.eclipse.pde.internal.ui.PDEUIMessages; 38 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 39 import org.eclipse.pde.internal.ui.editor.TreeSection; 40 import org.eclipse.pde.internal.ui.editor.actions.CollapseAction; 41 import org.eclipse.pde.internal.ui.editor.cheatsheet.ICSDetails; 42 import org.eclipse.pde.internal.ui.editor.cheatsheet.ICSMaster; 43 import org.eclipse.pde.internal.ui.editor.cheatsheet.comp.actions.CompCSAddGroupAction; 44 import org.eclipse.pde.internal.ui.editor.cheatsheet.comp.actions.CompCSAddTaskAction; 45 import org.eclipse.pde.internal.ui.editor.cheatsheet.comp.actions.CompCSRemoveTaskObjectAction; 46 import org.eclipse.pde.internal.ui.parts.TreePart; 47 import org.eclipse.swt.SWT; 48 import org.eclipse.swt.events.DisposeEvent; 49 import org.eclipse.swt.events.DisposeListener; 50 import org.eclipse.swt.graphics.Cursor; 51 import org.eclipse.swt.widgets.Composite; 52 import org.eclipse.swt.widgets.Display; 53 import org.eclipse.swt.widgets.ToolBar; 54 import org.eclipse.ui.IEditorInput; 55 import org.eclipse.ui.IFileEditorInput; 56 import org.eclipse.ui.IStorageEditorInput; 57 import org.eclipse.ui.actions.ActionFactory; 58 import org.eclipse.ui.cheatsheets.OpenCheatSheetAction; 59 import org.eclipse.ui.forms.IDetailsPage; 60 import org.eclipse.ui.forms.widgets.FormToolkit; 61 import org.eclipse.ui.forms.widgets.Section; 62 63 67 public class CompCSMasterTreeSection extends TreeSection implements 68 ICSMaster { 69 70 private static final int F_BUTTON_ADD_TASK = 0; 71 72 private static final int F_BUTTON_ADD_GROUP = 1; 73 74 private static final int F_BUTTON_REMOVE = 2; 75 76 private static final int F_BUTTON_UP = 3; 77 78 private static final int F_BUTTON_DOWN = 4; 79 80 private static final int F_BUTTON_PREVIEW = 5; 81 82 private static final int F_UP_FLAG = -1; 83 84 private static final int F_DOWN_FLAG = 1; 85 86 private TreeViewer fTreeViewer; 87 88 private ICompCSModel fModel; 89 90 private CollapseAction fCollapseAction; 91 92 private CompCSRemoveTaskObjectAction fRemoveTaskObjectAction; 93 94 private CompCSAddGroupAction fAddGroupAction; 95 96 private CompCSAddTaskAction fAddTaskAction; 97 98 private CompCSGroupValidator fGroupValidator; 99 100 106 public CompCSMasterTreeSection(PDEFormPage formPage, Composite parent) { 107 super(formPage, parent, Section.DESCRIPTION, new String [] { 108 PDEUIMessages.CompCSMasterTreeSection_addTask, 109 PDEUIMessages.CompCSMasterTreeSection_addGroup, 110 PDEUIMessages.SimpleCSElementSection_7, 111 PDEUIMessages.SimpleCSElementSection_1, 112 PDEUIMessages.SimpleCSElementSection_2, 113 PDEUIMessages.SimpleCSElementSection_3}); 114 115 fAddGroupAction = new CompCSAddGroupAction(); 117 fAddTaskAction = new CompCSAddTaskAction(); 118 fRemoveTaskObjectAction = new CompCSRemoveTaskObjectAction(); 119 fCollapseAction = null; 120 } 121 122 125 protected void createClient(Section section, FormToolkit toolkit) { 126 fModel = (ICompCSModel)getPage().getModel(); 128 129 Composite container = createClientContainer(section, 2, toolkit); 130 createTree(container, toolkit); 131 toolkit.paintBordersFor(container); 132 section.setText(PDEUIMessages.SimpleCSElementSection_4); 133 section.setDescription(PDEUIMessages.SimpleCSElementSection_5); 134 section.setClient(container); 135 initializeTreeViewer(); 136 createSectionToolbar(section, toolkit); 137 } 138 139 143 private void createTree(Composite container, FormToolkit toolkit) { 144 TreePart treePart = getTreePart(); 145 createViewerPartControl(container, SWT.SINGLE, 2, toolkit); 146 fTreeViewer = treePart.getTreeViewer(); 147 fTreeViewer.setContentProvider(new CompCSContentProvider()); 148 fTreeViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider()); 149 PDEPlugin.getDefault().getLabelProvider().connect(this); 150 createTreeListeners(); 151 } 153 154 157 private void createTreeListeners() { 158 fTreeViewer.addPostSelectionChangedListener( 161 getPage().getPDEEditor().new PDEFormEditorChangeListener()); 162 } 163 164 167 public ISelection getSelection() { 168 return fTreeViewer.getSelection(); 169 } 170 171 174 private void initializeTreeViewer() { 175 176 if (fModel == null) { 177 return; 178 } 179 fTreeViewer.setInput(fModel); 180 ICompCS cheatsheet = fModel.getCompCS(); 181 182 fGroupValidator = new CompCSGroupValidator(cheatsheet, 185 getManagedForm().getForm().getForm(), 186 PDEUIMessages.SimpleCSElementSection_4); 187 188 boolean addFlag = false; 192 if (cheatsheet.getFieldTaskObject() == null) { 193 addFlag = fModel.isEditable(); 194 } 195 getTreePart().setButtonEnabled(F_BUTTON_ADD_TASK, addFlag); 196 getTreePart().setButtonEnabled(F_BUTTON_ADD_GROUP, addFlag); 197 198 getTreePart().setButtonEnabled(F_BUTTON_REMOVE, false); 200 getTreePart().setButtonEnabled(F_BUTTON_UP, false); 202 getTreePart().setButtonEnabled(F_BUTTON_DOWN, false); 204 205 updatePreviewButton(fGroupValidator.validate()); 210 211 fTreeViewer.setSelection(new StructuredSelection(cheatsheet), true); 213 fTreeViewer.expandToLevel(2); 214 } 215 216 220 private void createSectionToolbar(Section section, FormToolkit toolkit) { 221 222 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT); 223 ToolBar toolbar = toolBarManager.createControl(section); 224 final Cursor handCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND); 225 toolbar.setCursor(handCursor); 226 toolbar.addDisposeListener(new DisposeListener() { 228 public void widgetDisposed(DisposeEvent e) { 229 if ((handCursor != null) && 230 (handCursor.isDisposed() == false)) { 231 handCursor.dispose(); 232 } 233 } 234 }); 235 fCollapseAction = new CollapseAction(fTreeViewer, 237 PDEUIMessages.ExtensionsPage_collapseAll, 238 1, 239 fModel.getCompCS()); 240 toolBarManager.add(fCollapseAction); 241 242 toolBarManager.update(true); 243 244 section.setTextClient(toolbar); 245 } 246 247 250 protected void buttonSelected(int index) { 251 switch (index) { 252 case F_BUTTON_ADD_TASK: 253 handleAddTaskAction(); 254 break; 255 case F_BUTTON_ADD_GROUP: 256 handleAddGroupAction(); 257 break; 258 case F_BUTTON_REMOVE: 259 handleDeleteAction(); 260 break; 261 case F_BUTTON_UP: 262 handleMoveTaskObjectAction(F_UP_FLAG); 263 break; 264 case F_BUTTON_DOWN: 265 handleMoveTaskObjectAction(F_DOWN_FLAG); 266 break; 267 case F_BUTTON_PREVIEW: 268 handlePreviewAction(); 269 break; 270 } 271 } 272 273 276 protected void selectionChanged(IStructuredSelection selection) { 277 updateButtons(); 278 } 279 280 283 public void updateButtons() { 284 if (!fModel.isEditable()) { 285 return; 286 } 287 Object object = ((IStructuredSelection) fTreeViewer.getSelection()).getFirstElement(); 288 ICompCSObject csObject = (ICompCSObject)object; 289 boolean canAddTask = false; 290 boolean canAddGroup = false; 291 boolean canRemove = false; 292 boolean canMoveUp = false; 293 boolean canMoveDown = false; 294 295 if (csObject != null) { 296 ICompCSObject parent = csObject.getParent(); 297 if ((csObject.getType() == ICompCSConstants.TYPE_TASK) || 298 (csObject.getType() == ICompCSConstants.TYPE_TASKGROUP)) { 299 300 if ((parent.getType() == ICompCSConstants.TYPE_COMPOSITE_CHEATSHEET) && 301 (csObject.getType() == ICompCSConstants.TYPE_TASKGROUP)) { 302 canAddTask = true; 303 canAddGroup = true; 304 } else if (parent.getType() == ICompCSConstants.TYPE_TASKGROUP) { 305 ICompCSTaskGroup taskGroup = (ICompCSTaskGroup)parent; 306 ICompCSTaskObject taskObject = (ICompCSTaskObject)csObject; 307 if (taskGroup.isFirstFieldTaskObject(taskObject) == false) { 308 canMoveUp = true; 309 } 310 if (taskGroup.isLastFieldTaskObject(taskObject) == false) { 311 canMoveDown = true; 312 } 313 canRemove = canRemoveTaskObject(taskGroup); 314 canAddTask = true; 315 canAddGroup = true; 316 } 317 } 318 } 319 320 getTreePart().setButtonEnabled(F_BUTTON_ADD_TASK, canAddTask); 321 getTreePart().setButtonEnabled(F_BUTTON_ADD_GROUP, canAddGroup); 322 getTreePart().setButtonEnabled(F_BUTTON_REMOVE, canRemove); 323 getTreePart().setButtonEnabled(F_BUTTON_UP, canMoveUp); 324 getTreePart().setButtonEnabled(F_BUTTON_DOWN, canMoveDown); 325 } 326 327 330 private void handleAddTaskAction() { 331 332 ISelection sel = fTreeViewer.getSelection(); 333 Object object = ((IStructuredSelection) sel).getFirstElement(); 334 if (object == null) { 335 return; 336 } 337 if (object instanceof ICompCSTaskGroup) { 338 fAddTaskAction.setParentObject((ICompCSObject)object); 339 fAddTaskAction.run(); 340 } else if (object instanceof ICompCSTask) { 341 fAddTaskAction.setParentObject(((ICompCSObject)object).getParent()); 342 fAddTaskAction.run(); 343 } 344 } 345 346 349 private void updatePreviewButton(boolean flag) { 350 getTreePart().setButtonEnabled(F_BUTTON_PREVIEW, flag); 351 } 352 353 356 private void handleAddGroupAction() { 357 358 ISelection sel = fTreeViewer.getSelection(); 359 Object object = ((IStructuredSelection) sel).getFirstElement(); 360 if (object == null) { 361 return; 362 } 363 if (object instanceof ICompCSTaskGroup) { 364 fAddGroupAction.setParentObject((ICompCSObject)object); 365 fAddGroupAction.run(); 366 } else if (object instanceof ICompCSTask) { 367 fAddGroupAction.setParentObject(((ICompCSObject)object).getParent()); 368 fAddGroupAction.run(); 369 } 370 } 371 372 375 private void handleMoveTaskObjectAction(int positionFlag) { 376 377 ISelection sel = fTreeViewer.getSelection(); 378 Object object = ((IStructuredSelection) sel).getFirstElement(); 379 if (object == null) { 380 return; 381 } else if (object instanceof ICompCSTaskObject) { 382 ICompCSTaskObject taskObject = (ICompCSTaskObject)object; 383 ICompCSTaskGroup parent = null; 384 if (taskObject.getParent().getType() == ICompCSConstants.TYPE_TASKGROUP) { 386 parent = (ICompCSTaskGroup)taskObject.getParent(); 387 } else { 388 return; 389 } 390 parent.moveFieldTaskObject(taskObject, positionFlag); 392 } 393 } 394 395 398 private void handlePreviewAction() { 399 IEditorInput input = getPage().getEditorInput(); 403 URL url = null; 404 try { 405 if (input instanceof IFileEditorInput) { 406 IFileEditorInput fileInput = (IFileEditorInput)input; 407 url = fileInput.getFile().getLocationURI().toURL(); 408 } else if (input instanceof IStorageEditorInput) { 409 IStorageEditorInput storageInput = (IStorageEditorInput)input; 415 url = storageInput.getStorage().getFullPath().toFile().toURL(); 416 } else { 417 url = null; 419 } 420 421 StringWriter swriter = new StringWriter (); 423 PrintWriter writer = new PrintWriter (swriter); 424 fModel.getCompCS().write("", writer); writer.flush(); 426 swriter.close(); 427 OpenCheatSheetAction openAction = new OpenCheatSheetAction( 429 input.getName(), 430 input.getName(), 431 swriter.toString(), 432 url); 433 openAction.run(); 434 } catch (IOException e) { 435 PDEPlugin.logException(e); 436 } catch (CoreException e) { 437 PDEPlugin.logException(e); 438 } 439 } 440 441 444 public void modelChanged(IModelChangedEvent event) { 445 if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 447 handleModelEventWorldChanged(event); 448 } else if (event.getChangeType() == IModelChangedEvent.INSERT) { 449 handleModelInsertType(event); 450 } else if (event.getChangeType() == IModelChangedEvent.REMOVE) { 451 handleModelRemoveType(event); 452 } else if (event.getChangeType() == IModelChangedEvent.CHANGE) { 453 handleModelChangeType(event); 454 } 455 456 updatePreviewButton(fGroupValidator.validate()); 461 } 462 463 466 private void handleModelEventWorldChanged(IModelChangedEvent event) { 467 468 Object [] objects = event.getChangedObjects(); 469 ICompCSObject object = (ICompCSObject) objects[0]; 470 if (object == null) { 471 return; 473 } else if (object.getType() == ICompCSConstants.TYPE_COMPOSITE_CHEATSHEET) { 474 CompCSPage page = (CompCSPage)getPage(); 476 IDetailsPage previousDetailsPage = 478 page.getBlock().getDetailsPart().getCurrentPage(); 479 fModel = ((ICompCS)object).getModel(); 482 fTreeViewer.setInput(fModel); 485 initializeTreeViewer(); 487 IDetailsPage currentDetailsPage = 489 page.getBlock().getDetailsPart().getCurrentPage(); 490 if (currentDetailsPage.equals(previousDetailsPage) && 495 currentDetailsPage instanceof ICSDetails) { 496 ((ICSDetails)currentDetailsPage).updateFields(); 497 } 498 } 499 500 } 501 502 505 private void handleModelInsertType(IModelChangedEvent event) { 506 Object [] objects = event.getChangedObjects(); 508 ICompCSObject object = (ICompCSObject) objects[0]; 509 if (object == null) { 510 } else if (object.getType() == ICompCSConstants.TYPE_TASK) { 512 handleTaskObjectInsert(object); 513 } else if (object.getType() == ICompCSConstants.TYPE_TASKGROUP) { 514 handleTaskObjectInsert(object); 515 fGroupValidator.addGroup((ICompCSTaskGroup) object); 517 } 518 } 519 520 523 private void handleTaskObjectInsert(ICompCSObject object) { 524 fTreeViewer.refresh(object.getParent()); 527 fTreeViewer.setSelection(new StructuredSelection(object), true); 529 } 530 531 534 private void handleModelRemoveType(IModelChangedEvent event) { 535 Object [] objects = event.getChangedObjects(); 537 ICompCSObject object = (ICompCSObject) objects[0]; 538 if (object == null) { 539 } else if (object.getType() == ICompCSConstants.TYPE_TASK) { 541 handleTaskObjectRemove(object); 542 } else if (object.getType() == ICompCSConstants.TYPE_TASKGROUP) { 543 handleTaskObjectRemove(object); 544 fGroupValidator.removeGroup((ICompCSTaskGroup) object); 546 } 547 } 548 549 552 private void handleTaskObjectRemove(ICompCSObject object) { 553 fTreeViewer.remove(object); 555 ICompCSObject csObject = fRemoveTaskObjectAction.getObjectToSelect(); 557 if (csObject == null) { 558 csObject = object.getParent(); 559 } 560 fTreeViewer.setSelection(new StructuredSelection(csObject), true); 561 } 562 563 566 private void handleModelChangeType(IModelChangedEvent event) { 567 Object [] objects = event.getChangedObjects(); 569 ICompCSObject object = (ICompCSObject) objects[0]; 570 if (object == null) { 571 } else if (object.getType() == ICompCSConstants.TYPE_TASK) { 573 fTreeViewer.update(object, null); 575 } else if (object.getType() == ICompCSConstants.TYPE_TASKGROUP) { 576 fTreeViewer.update(object, null); 578 } else if (object.getType() == ICompCSConstants.TYPE_COMPOSITE_CHEATSHEET) { 579 fTreeViewer.update(object, null); 581 } 582 } 583 584 589 public void fireSelection() { 590 fTreeViewer.setSelection(fTreeViewer.getSelection()); 591 } 592 593 596 protected void fillContextMenu(IMenuManager manager) { 597 ISelection selection = fTreeViewer.getSelection(); 599 Object object = ((IStructuredSelection) selection).getFirstElement(); 600 ICompCSObject csObject = (ICompCSObject)object; 603 MenuManager submenu = new MenuManager(PDEUIMessages.Menus_new_label); 605 manager.add(submenu); 607 if ((csObject == null) || 608 (csObject.getType() == ICompCSConstants.TYPE_COMPOSITE_CHEATSHEET)) { 609 } else if (csObject.getType() == ICompCSConstants.TYPE_TASK) { 611 fillContextMenuRemoveAction(manager, (ICompCSTaskObject)csObject); 613 } else if (csObject.getType() == ICompCSConstants.TYPE_TASKGROUP) { 614 ICompCSTaskGroup group = (ICompCSTaskGroup)csObject; 615 fAddTaskAction.setParentObject(group); 618 fAddTaskAction.setEnabled(fModel.isEditable()); 619 submenu.add(fAddTaskAction); 620 fAddGroupAction.setParentObject(group); 623 fAddGroupAction.setEnabled(fModel.isEditable()); 624 submenu.add(fAddGroupAction); 625 fillContextMenuRemoveAction(manager, (ICompCSTaskObject)csObject); 627 } 628 } 633 634 638 private void fillContextMenuRemoveAction(IMenuManager manager, 639 ICompCSTaskObject taskObject) { 640 manager.add(new Separator()); 643 fRemoveTaskObjectAction.setTaskObject(taskObject); 645 manager.add(fRemoveTaskObjectAction); 646 ICompCSObject parent = taskObject.getParent(); 647 if (canRemoveTaskObject(parent) == false) { 648 fRemoveTaskObjectAction.setEnabled(false); 649 } else { 650 fRemoveTaskObjectAction.setEnabled(fModel.isEditable()); 651 } 652 } 653 654 657 public boolean doGlobalAction(String actionId) { 658 if (actionId.equals(ActionFactory.DELETE.getId())) { 659 handleDeleteAction(); 660 return true; 661 } 662 return false; 663 } 664 665 668 private void handleDeleteAction() { 669 670 ISelection sel = fTreeViewer.getSelection(); 671 Object object = ((IStructuredSelection) sel).getFirstElement(); 672 if (object != null) { 673 if (object instanceof ICompCSTaskObject) { 674 ICompCSTaskObject taskObject = (ICompCSTaskObject)object; 675 ICompCSObject parent = taskObject.getParent(); 676 if (canRemoveTaskObject(parent) == false) { 677 Display.getCurrent().beep(); 680 } else { 681 fRemoveTaskObjectAction.setTaskObject(taskObject); 682 fRemoveTaskObjectAction.run(); 683 } 684 } else if (object instanceof ICompCS) { 685 Display.getCurrent().beep(); 690 } 691 } 692 } 693 694 698 private boolean canRemoveTaskObject(ICompCSObject parent) { 699 if (parent.getType() == ICompCSConstants.TYPE_COMPOSITE_CHEATSHEET) { 700 return false; 708 } 709 return true; 710 } 711 712 715 public boolean setFormInput(Object object) { 716 if (object instanceof ICompCSObject) { 720 fTreeViewer.setSelection(new StructuredSelection(object), true); 722 ISelection selection = fTreeViewer.getSelection(); 724 if ((selection != null) && 725 (selection.isEmpty() == false)) { 726 return true; 727 } 728 } 729 return false; 730 } 731 732 } 733 | Popular Tags |