| 1 11 12 package org.eclipse.pde.internal.ui.wizards.cheatsheet; 13 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.core.runtime.Path; 21 import org.eclipse.jface.dialogs.Dialog; 22 import org.eclipse.jface.window.Window; 23 import org.eclipse.jface.wizard.WizardPage; 24 import org.eclipse.pde.core.IModel; 25 import org.eclipse.pde.core.plugin.IPluginAttribute; 26 import org.eclipse.pde.core.plugin.IPluginElement; 27 import org.eclipse.pde.core.plugin.IPluginExtension; 28 import org.eclipse.pde.core.plugin.IPluginModelBase; 29 import org.eclipse.pde.core.plugin.IPluginObject; 30 import org.eclipse.pde.internal.core.ICoreConstants; 31 import org.eclipse.pde.internal.core.PDECore; 32 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSConstants; 33 import org.eclipse.pde.internal.core.plugin.AbbreviatedFragmentModel; 34 import org.eclipse.pde.internal.core.plugin.AbbreviatedPluginModel; 35 import org.eclipse.pde.internal.core.util.PDETextHelper; 36 import org.eclipse.pde.internal.ui.IHelpContextIds; 37 import org.eclipse.pde.internal.ui.PDEPlugin; 38 import org.eclipse.pde.internal.ui.PDEUIMessages; 39 import org.eclipse.pde.internal.ui.util.SWTUtil; 40 import org.eclipse.swt.SWT; 41 import org.eclipse.swt.events.ModifyEvent; 42 import org.eclipse.swt.events.ModifyListener; 43 import org.eclipse.swt.events.SelectionAdapter; 44 import org.eclipse.swt.events.SelectionEvent; 45 import org.eclipse.swt.layout.GridData; 46 import org.eclipse.swt.layout.GridLayout; 47 import org.eclipse.swt.widgets.Button; 48 import org.eclipse.swt.widgets.Combo; 49 import org.eclipse.swt.widgets.Composite; 50 import org.eclipse.swt.widgets.Group; 51 import org.eclipse.swt.widgets.Label; 52 import org.eclipse.swt.widgets.Text; 53 import org.eclipse.ui.PlatformUI; 54 55 59 public abstract class RegisterCSWizardPage extends WizardPage implements 60 IRegisterCSData { 61 62 public final static String F_PAGE_NAME = "register-cs"; 64 public final static String F_CS_EXTENSION_POINT_ID = 65 "org.eclipse.ui.cheatsheets.cheatSheetContent"; 67 public final static String F_CS_ELEMENT_CATEGORY = "category"; 69 public final static String F_CS_ELEMENT_CHEATSHEET = "cheatsheet"; 71 public final static String F_CS_ELEMENT_DESCRIPTION = "description"; 73 public final static String F_LOCALE_VARIABLE = "$nl$"; 75 private Combo fCategoryCombo; 76 77 private Button fCategoryButton; 78 79 private Text fDescriptionText; 80 81 protected IModel fCheatSheetModel; 82 83 private IPluginModelBase fPluginModelBase; 84 85 private IProject fPluginProject; 86 87 private boolean fPluginExists; 88 89 private String fDataCategoryName; 90 91 private String fDataDescription; 92 93 private String fDataCheatSheetID; 94 95 private IFile fPluginFile; 96 97 private boolean fIsFragment; 98 99 private CSCategoryTrackerUtil fCategoryTrackerUtil; 100 101 104 public RegisterCSWizardPage(IModel model) { 105 super(F_PAGE_NAME); 106 107 fCheatSheetModel = model; 108 initialize(); 109 } 110 111 114 private void initialize() { 115 116 setTitle(PDEUIMessages.CheatSheetFileWizardPage_1); 117 setDescription(PDEUIMessages.RegisterCSWizardPage_wizardPageDescription); 118 119 fCategoryCombo = null; 120 fCategoryButton = null; 121 fDescriptionText = null; 122 123 fCategoryTrackerUtil = new CSCategoryTrackerUtil(); 124 125 fDataCategoryName = null; 126 fDataDescription = null; 127 128 fPluginProject = fCheatSheetModel.getUnderlyingResource().getProject(); 130 131 fDataCheatSheetID = generateCheatSheetID(); 132 133 initializePluginModel(); 134 } 135 136 139 private void initializePluginModel() { 140 IPath pluginPath = new Path(ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR); 141 IPath fragmentPath = new Path(ICoreConstants.FRAGMENT_FILENAME_DESCRIPTOR); 142 if (fPluginProject.exists(pluginPath)) { 145 fPluginFile = fPluginProject.getFile(pluginPath.toPortableString()); 146 fPluginModelBase = 147 new AbbreviatedPluginModel(fPluginFile, F_CS_EXTENSION_POINT_ID); 148 fPluginExists = true; 149 fIsFragment = false; 150 } else if (fPluginProject.exists(fragmentPath)) { 151 fPluginFile = fPluginProject.getFile(fragmentPath.toPortableString()); 152 fPluginModelBase = 153 new AbbreviatedFragmentModel(fPluginFile, F_CS_EXTENSION_POINT_ID); 154 fPluginExists = true; 155 fIsFragment = true; 156 } else { 157 fPluginFile = fPluginProject.getFile(pluginPath.toPortableString()); 158 fPluginModelBase = null; 159 fPluginExists = false; 160 fIsFragment = false; 161 } 162 if (fPluginExists) { 164 try { 165 fPluginModelBase.load(); 166 } catch (CoreException e) { 167 PDECore.logException(e); 168 } 169 } 170 } 171 172 175 public String getDataDescription() { 176 return fDataDescription; 177 } 178 179 182 public String getDataCategoryName() { 183 return fDataCategoryName; 184 } 185 186 189 public int getDataCategoryType() { 190 String categoryID = getDataCategoryID(); 191 if (categoryID == null) { 192 return CSCategoryTrackerUtil.F_TYPE_NO_CATEGORY; 193 } 194 return fCategoryTrackerUtil.getCategoryType(categoryID); 195 } 196 197 200 public String getDataCategoryID() { 201 if (fDataCategoryName != null) { 202 return fCategoryTrackerUtil.getCategoryID(fDataCategoryName); 203 } 204 return null; 205 } 206 207 210 public boolean pluginExists() { 211 return fPluginExists; 212 } 213 214 217 public String getDataContentFile() { 218 String portablePath = 220 fCheatSheetModel.getUnderlyingResource().getFullPath().toPortableString(); 221 String result = portablePath.substring(1); 223 int index = result.indexOf('/'); 225 if (index == -1) { 226 return ""; } 228 result = result.substring(index); 229 result = F_LOCALE_VARIABLE + result; 231 232 return result; 233 } 234 235 238 public String getDataCheatSheetID() { 239 return fDataCheatSheetID; 240 } 241 242 245 public IFile getPluginFile() { 246 return fPluginFile; 247 } 248 249 252 public boolean isFragment() { 253 return fIsFragment; 254 } 255 256 259 public IProject getPluginProject() { 260 return fPluginProject; 261 } 262 263 266 public abstract String getDataCheatSheetName(); 267 268 271 public abstract boolean isCompositeCheatSheet(); 272 273 276 public void createControl(Composite parent) { 277 278 createUI(parent); 279 createUIListeners(); 280 281 updateUI(); 282 validateUI(); 283 284 } 285 286 289 private void createUI(Composite parent) { 290 Composite container = createUIContainer(parent); 292 createUILabel(container); 294 Group group = createUIGroup(container); 296 createUICategoryField(group); 298 createUIDescriptionField(group); 300 setControl(container); 303 Dialog.applyDialogFont(container); 305 PlatformUI.getWorkbench().getHelpSystem().setHelp(container, 308 IHelpContextIds.NEW_SCHEMA); 309 } 310 311 314 private void createUILabel(Composite container) { 315 Label label = new Label(container, SWT.WRAP); 316 label.setText(PDEUIMessages.RegisterCSWizardPage_labelInstructionText); 317 GridData data = new GridData(GridData.FILL_HORIZONTAL); 318 data.widthHint = 300; 319 label.setLayoutData(data); 320 } 321 322 326 private Group createUIGroup(Composite container) { 327 Group group = new Group(container, SWT.NONE); 328 GridLayout layout = new GridLayout(3, false); 329 layout.marginWidth = 6; 330 layout.marginHeight = 6; 331 group.setLayout(layout); 332 GridData data = new GridData(GridData.FILL_HORIZONTAL); 333 data.horizontalSpan = 3; 334 data.verticalIndent = 10; 335 group.setLayoutData(data); 336 group.setText(PDEUIMessages.RegisterCSWizardPage_groupRegistration); 337 return group; 338 } 339 340 344 private Composite createUIContainer(Composite parent) { 345 Composite container = new Composite(parent, SWT.NONE); 346 GridLayout layout = new GridLayout(); 347 layout.numColumns = 3; 348 container.setLayout(layout); 349 return container; 350 } 351 352 355 private void createUICategoryField(Composite parent) { 356 createUICategoryLabel(parent); 358 createUICategoryCombo(parent); 360 createUICategoryButton(parent); 362 } 363 364 367 private void createUICategoryLabel(Composite parent) { 368 Label label = new Label(parent, SWT.NONE); 369 label.setText(PDEUIMessages.RegisterCSWizardPage_labelCategory); 370 } 371 372 375 private void createUICategoryCombo(Composite parent) { 376 int style = SWT.READ_ONLY | SWT.BORDER; 377 fCategoryCombo = new Combo(parent, style); 378 GridData data = new GridData(GridData.FILL_HORIZONTAL); 379 fCategoryCombo.setLayoutData(data); 380 fCategoryCombo.add(PDEUIMessages.SimpleCSCommandDetails_6); 381 fCategoryCombo.setText(PDEUIMessages.SimpleCSCommandDetails_6); 382 } 383 384 387 private void createUICategoryButton(Composite parent) { 388 fCategoryButton = new Button(parent, SWT.PUSH); 389 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END); 390 data.widthHint = 50; 391 fCategoryButton.setLayoutData(data); 392 fCategoryButton.setText(PDEUIMessages.RegisterCSWizardPage_buttonNew); 393 fCategoryButton.setToolTipText(PDEUIMessages.RegisterCSWizardPage_toolTipNewCategory); 394 SWTUtil.setButtonDimensionHint(fCategoryButton); 395 } 396 397 400 private void createUIDescriptionField(Composite parent) { 401 createUIDescriptionLabel(parent); 403 createUIDescriptionText(parent); 405 } 406 407 410 private void createUIDescriptionLabel(Composite parent) { 411 Label label = new Label(parent, SWT.NONE); 412 label.setText(PDEUIMessages.RegisterCSWizardPage_labelDescription); 413 int style = GridData.VERTICAL_ALIGN_BEGINNING 414 | GridData.HORIZONTAL_ALIGN_END; 415 GridData data = new GridData(style); 416 label.setLayoutData(data); 417 } 418 419 422 private void createUIDescriptionText(Composite parent) { 423 int style = SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.BORDER; 424 fDescriptionText = new Text(parent, style); 425 GridData data = new GridData(GridData.FILL_HORIZONTAL); 426 data.heightHint = 60; 427 data.horizontalSpan = 2; 428 fDescriptionText.setLayoutData(data); 429 } 430 431 434 private void createUIListeners() { 435 createUIListenersCategoryButton(); 437 createUIListenersCategoryCombo(); 439 createUIListenersDescriptionText(); 441 } 442 443 446 private void createUIListenersCategoryButton() { 447 fCategoryButton.addSelectionListener(new SelectionAdapter() { 448 public void widgetSelected(SelectionEvent e) { 449 handleWidgetSelectedCategoryButton(); 450 } 451 }); 452 } 453 454 457 private void createUIListenersCategoryCombo() { 458 fCategoryCombo.addModifyListener(new ModifyListener() { 459 public void modifyText(ModifyEvent e) { 460 fDataCategoryName = fCategoryCombo.getText(); 461 } 462 }); 463 } 464 465 468 private void createUIListenersDescriptionText() { 469 fDescriptionText.addModifyListener(new ModifyListener() { 470 public void modifyText(ModifyEvent e) { 471 fDataDescription = fDescriptionText.getText(); 472 } 473 }); 474 } 475 476 479 private void handleWidgetSelectedCategoryButton() { 480 NewCategoryNameDialog dialog = new NewCategoryNameDialog( 482 PDEPlugin.getActiveWorkbenchShell()); 483 dialog.create(); 484 dialog.getShell().setText(PDEUIMessages.RegisterCSWizardPage_dialogTitleNewCategory); 485 486 if (dialog.open() == Window.OK) { 487 String newCategoryName = dialog.getNameText(); 488 489 if (PDETextHelper.isDefinedAfterTrim(newCategoryName)) { 490 String trimmedText = newCategoryName.trim(); 491 fCategoryCombo.add(trimmedText); 492 fCategoryCombo.setText(trimmedText); 493 fCategoryCombo.setFocus(); 494 String id = generateCategoryID(trimmedText); 495 fCategoryTrackerUtil.associate(id, trimmedText, 496 CSCategoryTrackerUtil.F_TYPE_NEW_CATEGORY); 497 } 498 } 499 500 } 501 502 505 private void updateUI() { 506 507 if (fPluginExists) { 508 IPluginExtension[] extensions = findExtensions(fPluginModelBase, 510 F_CS_EXTENSION_POINT_ID); 511 processCategoryElements(extensions); 513 processCheatSheetElements(extensions); 515 } 516 } 517 518 521 private void processCategoryElements(IPluginExtension[] extensions) { 522 for (int i = 0; i < extensions.length; i++) { 524 if (extensions[i].getChildCount() == 0) { 525 continue; 527 } 528 IPluginExtension extension = extensions[i]; 529 IPluginObject[] pluginObjects = extension.getChildren(); 530 for (int j = 0; j < pluginObjects.length; j++) { 532 if (pluginObjects[j] instanceof IPluginElement) { 533 IPluginElement element = (IPluginElement)pluginObjects[j]; 534 if (element.getName().equals(F_CS_ELEMENT_CATEGORY)) { 535 updateUICategoryComboElement(element); 538 } 539 } 540 } 541 } 542 } 543 544 547 private void processCheatSheetElements(IPluginExtension[] extensions) { 548 for (int i = 0; i < extensions.length; i++) { 552 if (extensions[i].getChildCount() == 0) { 553 continue; 555 } 556 IPluginExtension extension = extensions[i]; 557 IPluginObject[] pluginObjects = extension.getChildren(); 558 for (int j = 0; j < pluginObjects.length; j++) { 560 if (pluginObjects[j] instanceof IPluginElement) { 561 IPluginElement element = (IPluginElement)pluginObjects[j]; 562 if (element.getName().equals(F_CS_ELEMENT_CHEATSHEET)) { 563 processCheatSheetElement(element, fDataCheatSheetID); 565 } 566 567 } 568 } 569 } 570 } 571 572 576 private void updateUICategoryComboElement(IPluginElement parentElement) { 577 IPluginAttribute idAttribute = 579 parentElement.getAttribute(ICompCSConstants.ATTRIBUTE_ID); 580 IPluginAttribute nameAttribute = 582 parentElement.getAttribute(ICompCSConstants.ATTRIBUTE_NAME); 583 if ((nameAttribute != null) && 587 PDETextHelper.isDefined(nameAttribute.getValue()) && 588 (idAttribute != null) && 589 PDETextHelper.isDefined(idAttribute.getValue()) && 590 (fCategoryTrackerUtil.containsCategoryName(nameAttribute.getValue()) == false)) { 591 fCategoryCombo.add(nameAttribute.getValue()); 593 fCategoryTrackerUtil.associate(idAttribute.getValue(), 595 nameAttribute.getValue(), 596 CSCategoryTrackerUtil.F_TYPE_OLD_CATEGORY); 597 } 598 } 599 600 604 private void updateUICategoryComboAttribute(IPluginElement element) { 605 IPluginAttribute categoryAttribute = 607 element.getAttribute(F_CS_ELEMENT_CATEGORY); 608 if ((categoryAttribute != null) && 610 PDETextHelper.isDefined(categoryAttribute.getValue())) { 611 String id = categoryAttribute.getValue(); 612 if (fCategoryTrackerUtil.containsCategoryID(id)) { 614 String name = 616 fCategoryTrackerUtil.getCategoryName(id); 617 fCategoryCombo.setText(name); 618 } else { 619 fCategoryCombo.add(id); 623 fCategoryCombo.setText(id); 624 fCategoryTrackerUtil.associate(id, id, 625 CSCategoryTrackerUtil.F_TYPE_OLD_CATEGORY); 626 } 627 } 628 } 629 630 633 private void processCheatSheetElement(IPluginElement parentElement, 634 String generatedID) { 635 IPluginAttribute idAttribute = 637 parentElement.getAttribute(ICompCSConstants.ATTRIBUTE_ID); 638 639 if ((idAttribute != null) && 643 PDETextHelper.isDefined(idAttribute.getValue()) && 644 generatedID.equals(idAttribute.getValue())) { 645 if (parentElement.getChildCount() > 0) { 648 updateUIDescriptionText(parentElement); 650 } 651 updateUICategoryComboAttribute(parentElement); 652 } 653 } 654 655 658 private void updateUIDescriptionText(IPluginElement parentElement) { 659 IPluginObject pluginObject = parentElement.getChildren()[0]; 660 if (pluginObject instanceof IPluginElement) { 661 IPluginElement element = (IPluginElement)pluginObject; 662 if (element.getName().equals(F_CS_ELEMENT_DESCRIPTION) && 663 PDETextHelper.isDefinedAfterTrim(element.getText())) { 664 fDescriptionText.setText(element.getText().trim()); 666 } 667 } 668 } 669 670 673 private void validateUI() { 674 setPageComplete(true); 675 } 676 677 682 public IPluginExtension[] findExtensions(IPluginModelBase model, 683 String extensionPointID) { 684 IPluginExtension[] extensions = model.getPluginBase().getExtensions(); 685 686 ArrayList csExtensions = new ArrayList (); 687 for (int i = 0; i < extensions.length; i++) { 688 String point = extensions[i].getPoint(); 689 if (extensionPointID.equals(point)) { 690 csExtensions.add(extensions[i]); 691 } 692 } 693 return (IPluginExtension[]) csExtensions.toArray( 694 new IPluginExtension[csExtensions.size()]); 695 } 696 697 700 private String generateCheatSheetID() { 701 long uniqueID = hash(fCheatSheetModel.getUnderlyingResource().getFullPath().toPortableString()); 703 String result = fPluginProject.getName() + 708 '.' + 709 F_CS_ELEMENT_CHEATSHEET + 710 uniqueID; 711 return result; 712 } 713 714 717 private String generateCategoryID(String name) { 718 long uniqueID = hash(name); 720 String result = fPluginProject.getName() + 723 '.' + 724 F_CS_ELEMENT_CATEGORY + 725 uniqueID; 726 return result; 727 } 728 729 733 private long hash(String string) { 734 int b = 378551; 735 int a = 63689; 736 long hash = 0; 737 738 for(int i = 0; i < string.length(); i++) { 739 hash = hash * a + string.charAt(i); 740 a = a * b; 741 } 742 return (hash & 0x7FFFFFFF); 743 } 744 745 } 746 | Popular Tags |