1 11 12 package org.eclipse.pde.internal.ui.editor.cheatsheet.comp.details; 13 14 import java.util.StringTokenizer ; 15 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.resources.IWorkspaceRoot; 19 import org.eclipse.core.runtime.Path; 20 import org.eclipse.jface.viewers.ISelection; 21 import org.eclipse.jface.viewers.StructuredSelection; 22 import org.eclipse.jface.window.Window; 23 import org.eclipse.jface.wizard.IWizardPage; 24 import org.eclipse.jface.wizard.WizardDialog; 25 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSConstants; 26 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSModelFactory; 27 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSParam; 28 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTask; 29 import org.eclipse.pde.internal.ui.PDEPlugin; 30 import org.eclipse.pde.internal.ui.PDEUIMessages; 31 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter; 32 import org.eclipse.pde.internal.ui.editor.cheatsheet.CSAbstractDetails; 33 import org.eclipse.pde.internal.ui.editor.cheatsheet.ICSMaster; 34 import org.eclipse.pde.internal.ui.editor.cheatsheet.comp.CompCSFileValidator; 35 import org.eclipse.pde.internal.ui.editor.cheatsheet.comp.CompCSInputContext; 36 import org.eclipse.pde.internal.ui.parts.FormEntry; 37 import org.eclipse.pde.internal.ui.util.FileExtensionFilter; 38 import org.eclipse.pde.internal.ui.wizards.cheatsheet.NewSimpleCSFileWizard; 39 import org.eclipse.pde.internal.ui.wizards.cheatsheet.SimpleCSFileWizardPage; 40 import org.eclipse.swt.SWT; 41 import org.eclipse.swt.events.SelectionAdapter; 42 import org.eclipse.swt.events.SelectionEvent; 43 import org.eclipse.swt.graphics.Color; 44 import org.eclipse.swt.layout.GridData; 45 import org.eclipse.swt.widgets.Button; 46 import org.eclipse.swt.widgets.Composite; 47 import org.eclipse.ui.PartInitException; 48 import org.eclipse.ui.PlatformUI; 49 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 50 import org.eclipse.ui.forms.IFormColors; 51 import org.eclipse.ui.forms.IFormPart; 52 import org.eclipse.ui.forms.IManagedForm; 53 import org.eclipse.ui.forms.events.HyperlinkEvent; 54 import org.eclipse.ui.forms.widgets.ExpandableComposite; 55 import org.eclipse.ui.forms.widgets.Section; 56 import org.eclipse.ui.ide.IDE; 57 import org.eclipse.ui.model.WorkbenchContentProvider; 58 import org.eclipse.ui.model.WorkbenchLabelProvider; 59 60 64 public class CompCSTaskDetails extends CSAbstractDetails { 65 66 private Section fDefinitionSection; 67 68 private FormEntry fNameEntry; 69 70 private FormEntry fPathEntry; 71 72 private Button fSkip; 73 74 private ICompCSTask fDataTask; 75 76 private CompCSEnclosingTextDetails fEnclosingTextSection; 77 78 private final static String F_PATH_SEPARATOR = "/"; 80 private final static String F_DOT_DOT = ".."; 82 85 public CompCSTaskDetails(ICSMaster section) { 86 super(section, CompCSInputContext.CONTEXT_ID); 87 fDataTask = null; 88 89 fNameEntry = null; 90 fPathEntry = null; 91 fSkip = null; 92 93 fDefinitionSection = null; 94 fEnclosingTextSection = 95 new CompCSEnclosingTextDetails(ICompCSConstants.TYPE_TASK, section); 96 } 97 98 101 public void setData(ICompCSTask object) { 102 fDataTask = object; 104 fEnclosingTextSection.setData(object); 106 } 107 108 111 public void initialize(IManagedForm form) { 112 super.initialize(form); 113 fEnclosingTextSection.initialize(form); 118 } 119 120 123 public void createDetails(Composite parent) { 124 125 int style = Section.DESCRIPTION | ExpandableComposite.TITLE_BAR; 127 fDefinitionSection = getPage().createUISection(parent, PDEUIMessages.SimpleCSDetails_3, 128 PDEUIMessages.CompCSTaskDetails_SectionDescription, style); 129 getPage().alignSectionHeaders(getMasterSection().getSection(), 132 fDefinitionSection); 133 Composite sectionClient = getPage().createUISectionContainer(fDefinitionSection, 3); 135 createUINameEntry(sectionClient); 137 createUIPathEntry(sectionClient); 139 createUISkipButton(sectionClient); 141 fEnclosingTextSection.createDetails(parent); 143 getManagedForm().getToolkit().paintBordersFor(sectionClient); 145 fDefinitionSection.setClient(sectionClient); 146 markDetailsPart(fDefinitionSection); 147 } 148 149 152 private void createUINameEntry(Composite parent) { 153 fNameEntry = new FormEntry(parent, getManagedForm().getToolkit(), 154 PDEUIMessages.CompCSTaskDetails_Name, SWT.NONE); 155 } 156 157 160 private void createUIPathEntry(Composite parent) { 161 fPathEntry = new FormEntry(parent, getManagedForm().getToolkit(), 162 PDEUIMessages.CompCSTaskDetails_Path, PDEUIMessages.GeneralInfoSection_browse, isEditable()); 163 } 164 165 168 private void createUISkipButton(Composite parent) { 169 Color foreground = getToolkit().getColors().getColor(IFormColors.TITLE); 170 fSkip = getToolkit().createButton(parent, PDEUIMessages.CompCSTaskDetails_SkipLabel, SWT.CHECK); 171 GridData data = new GridData(GridData.FILL_HORIZONTAL); 172 data.horizontalSpan = 3; 173 fSkip.setLayoutData(data); 174 fSkip.setForeground(foreground); 175 } 176 177 180 public void hookListeners() { 181 createListenersNameEntry(); 183 createListenersPathEntry(); 185 createListenersSkipButton(); 187 fEnclosingTextSection.hookListeners(); 189 } 190 191 194 private void createListenersNameEntry() { 195 fNameEntry.setFormEntryListener(new FormEntryAdapter(this) { 196 public void textValueChanged(FormEntry entry) { 197 if (fDataTask == null) { 199 return; 200 } 201 fDataTask.setFieldName(fNameEntry.getValue()); 202 } 203 }); 204 } 205 206 209 private void createListenersPathEntry() { 210 fPathEntry.setFormEntryListener(new FormEntryAdapter(this) { 211 public void browseButtonSelected(FormEntry entry) { 212 if (fDataTask == null) { 214 return; 215 } 216 handleButtonEventPathEntry(entry); 217 } 218 public void linkActivated(HyperlinkEvent e) { 219 if (fDataTask == null) { 221 return; 222 } 223 handleLinkEventPathEntry(convertPathRelativeToAbs(fPathEntry 224 .getValue(), fDataTask.getModel() 225 .getUnderlyingResource().getFullPath() 226 .toPortableString())); 227 } 228 public void textValueChanged(FormEntry entry) { 229 if (fDataTask == null) { 231 return; 232 } 233 handleTextEventPathEntry(entry.getValue()); 235 } 236 }); 237 } 238 239 242 private void handleButtonEventPathEntry(FormEntry entry) { 243 ElementTreeSelectionDialog dialog = 245 new ElementTreeSelectionDialog( 246 getManagedForm().getForm().getShell(), 247 new WorkbenchLabelProvider(), 248 new WorkbenchContentProvider()); 249 250 dialog.setValidator(new CompCSFileValidator()); 251 dialog.setAllowMultiple(false); 252 dialog.setTitle(PDEUIMessages.CompCSTaskDetails_simpleCSWizardTitle); 253 dialog.setMessage(PDEUIMessages.CompCSTaskDetails_simpleCSWizardDescription); 254 dialog.addFilter(new FileExtensionFilter("xml")); dialog.setInput(PDEPlugin.getWorkspace().getRoot().getProject( 256 fDataTask.getModel().getUnderlyingResource().getProject().getName())); 257 258 if (dialog.open() == Window.OK) { 259 IFile file = (IFile)dialog.getFirstResult(); 260 String newValue = convertPathAbsToRelative(file.getFullPath() 261 .toPortableString(), fDataTask.getModel() 262 .getUnderlyingResource().getFullPath().toPortableString()); 263 entry.setValue(newValue); 264 handleTextEventPathEntry(newValue); 265 } 266 } 267 268 272 private String extractFileName(String path) { 273 StringTokenizer tokenizer = new StringTokenizer (path, F_PATH_SEPARATOR); 274 while (tokenizer.countTokens() > 1) { 275 tokenizer.nextToken(); 276 } 277 return tokenizer.nextToken(); 278 } 279 280 284 private String convertPathAbsToRelative(String relativePath, String basePath) { 285 StringTokenizer convertPathTokenizer 286 = new StringTokenizer (relativePath, F_PATH_SEPARATOR); 287 StringTokenizer basePathTokenizer 288 = new StringTokenizer (basePath, F_PATH_SEPARATOR); 289 String convertPathToken = convertPathTokenizer.nextToken(); 291 String basePathToken = basePathTokenizer.nextToken(); 292 if (convertPathToken.equals(basePathToken) == false) { 295 return ""; } 297 while (basePathTokenizer.hasMoreTokens() && 299 convertPathTokenizer.hasMoreTokens()) { 300 301 if (basePathTokenizer.countTokens() == 1) { 302 return createRelativePath(0, null, convertPathTokenizer); 307 } else if (convertPathTokenizer.countTokens() == 1) { 308 return createRelativePath(basePathTokenizer.countTokens() - 1, 313 null, convertPathTokenizer); 314 } else { 315 convertPathToken = convertPathTokenizer.nextToken(); 317 basePathToken = basePathTokenizer.nextToken(); 318 if (convertPathToken.equals(basePathToken) == false) { 319 return createRelativePath(basePathTokenizer.countTokens(), 324 convertPathToken, convertPathTokenizer); 325 } 326 } 327 } 328 return ""; } 331 332 333 338 private String createRelativePath(int dotDotCount, String lastToken, 339 StringTokenizer tokenizer) { 340 StringBuffer relativePath = new StringBuffer (); 341 for (int i = 0; i < dotDotCount; i++) { 343 relativePath.append(F_DOT_DOT); 344 relativePath.append(F_PATH_SEPARATOR); 345 } 346 if (lastToken != null) { 348 relativePath.append(lastToken); 349 relativePath.append(F_PATH_SEPARATOR); 350 } 351 for (int i = 0; i < (tokenizer.countTokens() - 1); i++) { 353 relativePath.append(tokenizer.nextToken()); 354 relativePath.append(F_PATH_SEPARATOR); 355 } 356 relativePath.append(tokenizer.nextToken()); 358 359 return relativePath.toString(); 360 } 361 362 365 private void handleLinkEventPathEntry(String absolutePath) { 366 IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot(); 367 Path path = new Path(absolutePath); 368 if (path.isEmpty()) { 370 handleLinkWizardPathEntry(); 371 return; 372 } 373 IResource resource = root.findMember(path); 375 if ((resource != null) && 379 (resource instanceof IFile)) { 380 try { 381 IDE.openEditor(PDEPlugin.getActivePage(), (IFile) resource, true); 382 } catch (PartInitException e) { 383 } 385 } else { 386 handleLinkWizardPathEntry(); 387 } 388 } 389 390 393 private void handleLinkWizardPathEntry() { 394 NewSimpleCSFileWizard wizard = 395 new NewSimpleCSFileWizard(); 396 wizard.init(PlatformUI.getWorkbench(), new StructuredSelection( 399 fDataTask.getModel().getUnderlyingResource())); 400 WizardDialog dialog = 402 new WizardDialog(PDEPlugin.getActiveWorkbenchShell(), wizard); 403 dialog.create(); 404 IWizardPage wizardPage = wizard.getPage(SimpleCSFileWizardPage.F_PAGE_NAME); 406 if ((wizardPage instanceof SimpleCSFileWizardPage) == false) { 407 return; 408 } 409 SimpleCSFileWizardPage page = (SimpleCSFileWizardPage)wizardPage; 410 String initialValue = fPathEntry.getValue().trim(); 412 if (initialValue.length() > 0) { 413 page.setFileName(extractFileName(initialValue)); 415 } 416 page.setProjectName(fDataTask.getModel().getUnderlyingResource() 419 .getProject().getName()); 420 if (dialog.open() == Window.OK) { 422 String newValue = convertPathAbsToRelative(page 423 .getAbsoluteFileName(), fDataTask.getModel() 424 .getUnderlyingResource().getFullPath().toPortableString()); 425 fPathEntry.setValue(newValue, true); 426 handleTextEventPathEntry(newValue); 427 } 428 } 429 430 434 private String convertPathRelativeToAbs(String relativePath, String basePath) { 435 StringTokenizer convertPathTokenizer 436 = new StringTokenizer (relativePath, F_PATH_SEPARATOR); 437 StringTokenizer basePathTokenizer 438 = new StringTokenizer (basePath, F_PATH_SEPARATOR); 439 StringBuffer endPath = new StringBuffer (); 442 int dotDotCount = 0; 443 if (convertPathTokenizer.hasMoreTokens()) { 444 while (convertPathTokenizer.countTokens() > 1) { 445 String token = convertPathTokenizer.nextToken(); 446 if (token.equals(F_DOT_DOT)) { 447 dotDotCount++; 448 } else { 449 endPath.append(token); 450 endPath.append(F_PATH_SEPARATOR); 451 } 452 } 453 endPath.append(convertPathTokenizer.nextToken()); 455 } 456 int baseSegementCount = basePathTokenizer.countTokens() - dotDotCount - 458 1; 459 if (baseSegementCount < 0) { 461 return ""; } 463 StringBuffer startPath = new StringBuffer (F_PATH_SEPARATOR); 465 for (int i = 0; i < baseSegementCount; i++) { 466 startPath.append(basePathTokenizer.nextToken()); 467 startPath.append(F_PATH_SEPARATOR); 468 } 469 return startPath.toString() + endPath.toString(); 472 } 473 474 477 private void handleTextEventPathEntry(String newValue) { 478 if (fDataTask.hasFieldParams()) { 480 ICompCSParam parameter = 483 fDataTask.getFieldParam(ICompCSConstants.ATTRIBUTE_VALUE_PATH); 484 if (parameter != null) { 485 parameter.setFieldValue(newValue); 486 } else { 487 createTaskParamPathEntry(newValue); 490 } 491 } else { 492 createTaskParamPathEntry(newValue); 495 } 496 } 497 498 501 private void createTaskParamPathEntry(String newValue) { 502 ICompCSModelFactory factory = fDataTask.getModel().getFactory(); 503 ICompCSParam parameter = factory.createCompCSParam(fDataTask); 505 parameter.setFieldName(ICompCSConstants.ATTRIBUTE_VALUE_PATH); 507 parameter.setFieldValue(newValue); 508 fDataTask.addFieldParam(parameter); 510 } 511 512 515 private void createListenersSkipButton() { 516 fSkip.addSelectionListener(new SelectionAdapter() { 517 public void widgetSelected(SelectionEvent e) { 518 if (fDataTask == null) { 520 return; 521 } 522 fDataTask.setFieldSkip(fSkip.getSelection()); 523 } 524 }); 525 } 526 527 530 public void updateFields() { 531 if (fDataTask == null) { 533 return; 534 } 535 boolean editable = isEditableElement(); 536 updateNameEntry(editable); 538 updatePathEntry(editable); 540 updateSkipButton(editable); 542 fEnclosingTextSection.updateFields(); 544 } 545 546 549 private void updateNameEntry(boolean editable) { 550 fNameEntry.setValue(fDataTask.getFieldName(), true); 551 fNameEntry.setEditable(editable); 552 } 553 554 557 private void updatePathEntry(boolean editable) { 558 ICompCSParam parameter = 559 fDataTask.getFieldParam(ICompCSConstants.ATTRIBUTE_VALUE_PATH); 560 if (parameter != null) { 561 fPathEntry.setValue(parameter.getFieldValue(), true); 562 } else { 563 fPathEntry.setValue("", true); } 565 } 566 567 570 private void updateSkipButton(boolean editable) { 571 fSkip.setSelection(fDataTask.getFieldSkip()); 572 fSkip.setEnabled(editable); 573 } 574 575 578 public void commit(boolean onSave) { 579 super.commit(onSave); 580 fNameEntry.commit(); 582 fPathEntry.commit(); 583 } 585 586 589 public void selectionChanged(IFormPart part, ISelection selection) { 590 Object object = getFirstSelectedObject(selection); 592 if ((object == null) || 594 (object instanceof ICompCSTask) == false) { 595 return; 596 } 597 setData((ICompCSTask)object); 599 updateFields(); 601 } 602 603 606 public void dispose() { 607 if (fEnclosingTextSection != null) { 609 fEnclosingTextSection.dispose(); 610 fEnclosingTextSection = null; 611 } 612 super.dispose(); 613 } 614 615 } 616 | Popular Tags |