1 11 17 package org.eclipse.pde.internal.ui.editor; 18 19 import java.util.ResourceBundle ; 20 21 import org.eclipse.pde.core.IBaseModel; 22 import org.eclipse.pde.internal.core.text.AbstractEditingModel; 23 import org.eclipse.pde.internal.core.text.IDocumentNode; 24 import org.eclipse.pde.internal.core.text.IDocumentRange; 25 import org.eclipse.pde.internal.core.text.IEditingModel; 26 import org.eclipse.pde.internal.ui.IHelpContextIds; 27 import org.eclipse.pde.internal.ui.PDEPlugin; 28 import org.eclipse.pde.internal.ui.PDEPluginImages; 29 import org.eclipse.pde.internal.ui.PDEUIMessages; 30 import org.eclipse.pde.internal.ui.editor.actions.FormatAction; 31 import org.eclipse.pde.internal.ui.editor.actions.HyperlinkAction; 32 import org.eclipse.pde.internal.ui.editor.actions.PDEActionConstants; 33 import org.eclipse.pde.internal.ui.editor.context.InputContext; 34 import org.eclipse.pde.internal.ui.editor.outline.IOutlineContentCreator; 35 import org.eclipse.pde.internal.ui.editor.outline.IOutlineSelectionHandler; 36 import org.eclipse.pde.internal.ui.editor.plugin.ExtensionHyperLink; 37 import org.eclipse.pde.internal.ui.editor.text.PDESelectAnnotationRulerAction; 38 39 import org.eclipse.swt.SWT; 40 import org.eclipse.swt.widgets.Composite; 41 import org.eclipse.swt.widgets.Control; 42 43 import org.eclipse.core.runtime.CoreException; 44 45 import org.eclipse.core.resources.IMarker; 46 47 import org.eclipse.jface.action.IAction; 48 import org.eclipse.jface.action.IMenuManager; 49 import org.eclipse.jface.preference.IPreferenceStore; 50 import org.eclipse.jface.viewers.ILabelProvider; 51 import org.eclipse.jface.viewers.IPostSelectionProvider; 52 import org.eclipse.jface.viewers.ISelection; 53 import org.eclipse.jface.viewers.ISelectionChangedListener; 54 import org.eclipse.jface.viewers.ISelectionProvider; 55 import org.eclipse.jface.viewers.IStructuredSelection; 56 import org.eclipse.jface.viewers.ITreeContentProvider; 57 import org.eclipse.jface.viewers.SelectionChangedEvent; 58 import org.eclipse.jface.viewers.StructuredSelection; 59 import org.eclipse.jface.viewers.ViewerComparator; 60 61 import org.eclipse.jface.text.IDocument; 62 import org.eclipse.jface.text.ITextSelection; 63 import org.eclipse.jface.text.source.ISourceViewer; 64 65 import org.eclipse.ui.editors.text.EditorsUI; 66 import org.eclipse.ui.editors.text.TextEditor; 67 68 import org.eclipse.ui.PlatformUI; 69 import org.eclipse.ui.forms.IManagedForm; 70 import org.eclipse.ui.forms.editor.FormEditor; 71 import org.eclipse.ui.forms.editor.IFormPage; 72 import org.eclipse.ui.ide.IDE; 73 import org.eclipse.ui.ide.IGotoMarker; 74 import org.eclipse.ui.texteditor.ChainedPreferenceStore; 75 import org.eclipse.ui.texteditor.ContentAssistAction; 76 import org.eclipse.ui.texteditor.DefaultRangeIndicator; 77 import org.eclipse.ui.texteditor.ITextEditorActionConstants; 78 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; 79 import org.eclipse.ui.texteditor.ResourceAction; 80 import org.eclipse.ui.texteditor.TextOperationAction; 81 82 public abstract class PDESourcePage extends TextEditor implements IFormPage, 83 IGotoMarker, ISelectionChangedListener, IOutlineContentCreator, 84 IOutlineSelectionHandler { 85 86 private static String RES_BUNDLE_LOCATION = "org.eclipse.pde.internal.ui.editor.text.ConstructedPDEEditorMessages"; private static ResourceBundle fgBundleForConstructedKeys = ResourceBundle.getBundle(RES_BUNDLE_LOCATION); 88 public static ResourceBundle getBundleForConstructedKeys() { 89 return fgBundleForConstructedKeys; 90 } 91 92 95 protected void initializeKeyBindingScopes() { 96 setKeyBindingScopes(new String [] { "org.eclipse.pde.ui.pdeEditorContext" }); } 98 99 104 private class PDESourcePageChangedListener implements 105 ISelectionChangedListener { 106 107 115 public void install(ISelectionProvider selectionProvider) { 116 if (selectionProvider != null) { 117 if (selectionProvider instanceof IPostSelectionProvider) { 118 IPostSelectionProvider provider = (IPostSelectionProvider) selectionProvider; 119 provider.addPostSelectionChangedListener(this); 120 } else { 121 selectionProvider.addSelectionChangedListener(this); 122 } 123 } 124 } 125 126 129 public void selectionChanged(SelectionChangedEvent event) { 130 handleSelectionChangedSourcePage(event); 131 } 132 133 139 public void uninstall(ISelectionProvider selectionProvider) { 140 if (selectionProvider != null) { 141 if (selectionProvider instanceof IPostSelectionProvider) { 142 IPostSelectionProvider provider = (IPostSelectionProvider) selectionProvider; 143 provider.removePostSelectionChangedListener(this); 144 } else { 145 selectionProvider.removeSelectionChangedListener(this); 146 } 147 } 148 } 149 150 } 151 152 157 private PDESourcePageChangedListener fEditorSelectionChangedListener; 158 private PDEFormEditor fEditor; 159 private Control fControl; 160 private int fIndex; 161 private String fId; 162 private InputContext fInputContext; 163 private ISortableContentOutlinePage fOutlinePage; 164 private ISelectionChangedListener fOutlineSelectionChangedListener; 165 protected Object fSelection; 166 167 public PDESourcePage(PDEFormEditor editor, String id, String title) { 168 fId = id; 169 initialize(editor); 170 IPreferenceStore[] stores = new IPreferenceStore[2]; 171 stores[0] = PDEPlugin.getDefault().getPreferenceStore(); 172 stores[1] = EditorsUI.getPreferenceStore(); 173 setPreferenceStore(new ChainedPreferenceStore(stores)); 174 setRangeIndicator(new DefaultRangeIndicator()); 175 if (isSelectionListener()) 176 getEditor().getSite().getSelectionProvider().addSelectionChangedListener(this); 177 } 178 179 182 public void initialize(FormEditor editor) { 183 fEditor = (PDEFormEditor)editor; 184 } 185 186 public void dispose() { 187 if (fEditorSelectionChangedListener != null) { 188 fEditorSelectionChangedListener.uninstall(getSelectionProvider()); 189 fEditorSelectionChangedListener= null; 190 } 191 if (fOutlinePage != null) { 192 fOutlinePage.dispose(); 193 fOutlinePage = null; 194 } 195 if (isSelectionListener()) 196 getEditor().getSite().getSelectionProvider().removeSelectionChangedListener(this); 197 super.dispose(); 198 } 199 200 203 public abstract ILabelProvider createOutlineLabelProvider(); 204 205 208 public abstract ITreeContentProvider createOutlineContentProvider(); 209 210 213 public abstract ViewerComparator createOutlineComparator(); 214 215 218 public void updateSelection(SelectionChangedEvent event) { 219 ISelection sel = event.getSelection(); 220 if (sel instanceof IStructuredSelection) { 221 IStructuredSelection structuredSelection = (IStructuredSelection) sel; 222 updateSelection(structuredSelection.getFirstElement()); 223 } 224 } 225 226 229 public abstract void updateSelection(Object object); 230 231 234 public ViewerComparator createDefaultOutlineComparator() { 235 return null; 236 } 237 238 protected ISortableContentOutlinePage createOutlinePage() { 239 SourceOutlinePage sourceOutlinePage= 240 new SourceOutlinePage( 241 (IEditingModel) getInputContext().getModel(), 242 createOutlineLabelProvider(), createOutlineContentProvider(), 243 createDefaultOutlineComparator(), createOutlineComparator()); 244 fOutlinePage = sourceOutlinePage; 245 fOutlineSelectionChangedListener = new ISelectionChangedListener() { 246 public void selectionChanged(SelectionChangedEvent event) { 247 updateSelection(event); 248 } 249 }; 250 fOutlinePage.addSelectionChangedListener(fOutlineSelectionChangedListener); 251 getSelectionProvider().addSelectionChangedListener(sourceOutlinePage); 252 fEditorSelectionChangedListener= new PDESourcePageChangedListener(); 253 fEditorSelectionChangedListener.install(getSelectionProvider()); 254 return fOutlinePage; 255 } 256 257 260 public ISortableContentOutlinePage getContentOutline() { 261 if (fOutlinePage == null) 262 fOutlinePage = createOutlinePage(); 263 return fOutlinePage; 264 } 265 266 269 public FormEditor getEditor() { 270 return fEditor; 271 } 272 273 276 public IManagedForm getManagedForm() { 277 return null; 279 } 280 281 protected void firePropertyChange(int type) { 282 if (type == PROP_DIRTY) { 283 fEditor.fireSaveNeeded(getEditorInput(), true); 284 } else 285 super.firePropertyChange(type); 286 } 287 288 291 public void setActive(boolean active) { 292 fInputContext.setSourceEditingMode(active); 293 } 294 295 public boolean canLeaveThePage() { 296 return true; 297 } 298 299 302 public boolean isActive() { 303 return this.equals(fEditor.getActivePageInstance()); 304 } 305 306 public void createPartControl(Composite parent) { 307 super.createPartControl(parent); 308 Control[] children = parent.getChildren(); 309 fControl = children[children.length - 1]; 310 311 PlatformUI.getWorkbench().getHelpSystem().setHelp(fControl, IHelpContextIds.MANIFEST_SOURCE_PAGE); 312 } 313 314 317 public Control getPartControl() { 318 return fControl; 319 } 320 321 324 public String getId() { 325 return fId; 326 } 327 328 331 public int getIndex() { 332 return fIndex; 333 } 334 335 338 public void setIndex(int index) { 339 fIndex = index; 340 } 341 342 345 public boolean isEditor() { 346 return true; 347 } 348 349 352 public InputContext getInputContext() { 353 return fInputContext; 354 } 355 356 359 public void setInputContext(InputContext inputContext) { 360 fInputContext = inputContext; 361 setDocumentProvider(inputContext.getDocumentProvider()); 362 } 363 364 367 public boolean selectReveal(Object object) { 368 if (object instanceof IMarker) { 369 IDE.gotoMarker(this, (IMarker)object); 370 return true; 371 } 372 return false; 373 } 374 375 public IDocumentRange getRangeElement(int offset, boolean searchChildren) { 376 return null; 377 } 378 379 public void setHighlightRange(IDocumentRange range, boolean moveCursor) { 380 int offset = range.getOffset(); 381 if (offset == -1) { 382 resetHighlightRange(); 383 return; 384 } 385 386 ISourceViewer sourceViewer = getSourceViewer(); 387 if (sourceViewer == null) 388 return; 389 390 IDocument document = sourceViewer.getDocument(); 391 if (document == null) 392 return; 393 394 int length = range.getLength(); 395 setHighlightRange(offset, length == -1 ? 1 : length, moveCursor); 396 } 397 398 public void setSelectedRange(IDocumentRange range, boolean fullNodeSelection) { 399 ISourceViewer sourceViewer = getSourceViewer(); 400 if (sourceViewer == null) 401 return; 402 403 IDocument document = sourceViewer.getDocument(); 404 if (document == null) 405 return; 406 407 int offset; 408 int length; 409 if (range instanceof IDocumentNode && !fullNodeSelection) { 410 length = ((IDocumentNode)range).getXMLTagName().length(); 411 offset = range.getOffset() + 1; 412 } else { 413 length = range.getLength(); 414 offset = range.getOffset(); 415 } 416 sourceViewer.setSelectedRange(offset, length); 417 } 418 419 public int getOrientation() { 420 return SWT.LEFT_TO_RIGHT; 421 } 422 423 protected void createActions() { 424 super.createActions(); 425 PDESelectAnnotationRulerAction action = new PDESelectAnnotationRulerAction( 426 getBundleForConstructedKeys(), "PDESelectAnnotationRulerAction.", this, getVerticalRuler()); setAction(ITextEditorActionConstants.RULER_CLICK, action); 428 PDEFormEditorContributor contributor = fEditor == null ? null : fEditor.getContributor(); 429 if (contributor instanceof PDEFormTextEditorContributor) { 430 PDEFormTextEditorContributor textContributor = (PDEFormTextEditorContributor)contributor; 431 setAction(PDEActionConstants.OPEN, textContributor.getHyperlinkAction()); 432 setAction(PDEActionConstants.FORMAT, textContributor.getFormatAction()); 433 if (textContributor.supportsContentAssist()) 434 createContentAssistAction(); 435 } 436 437 createQuickOutlineAction(); 439 } 440 441 444 private void createQuickOutlineAction() { 445 ResourceAction action = new TextOperationAction( 447 getBundleForConstructedKeys(), "QuickOutline.", this, PDEProjectionViewer.QUICK_OUTLINE, true); 449 action.setActionDefinitionId(PDEActionConstants.COMMAND_ID_QUICK_OUTLINE); 450 action.setText(PDEUIMessages.PDESourcePage_actionTextQuickOutline); 451 action.setId(PDEActionConstants.COMMAND_ID_QUICK_OUTLINE); 452 action.setImageDescriptor(PDEPluginImages.DESC_OVERVIEW_OBJ); 453 setAction(PDEActionConstants.COMMAND_ID_QUICK_OUTLINE, action); 454 } 455 456 private void createContentAssistAction() { 457 IAction contentAssist = new ContentAssistAction( 458 getBundleForConstructedKeys(), "ContentAssistProposal.", this); contentAssist.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS); 460 setAction("ContentAssist", contentAssist); markAsStateDependentAction("ContentAssist", true); } 463 464 public final void selectionChanged(SelectionChangedEvent event) { 465 if (event.getSource() == getSelectionProvider()) 466 return; 467 ISelection sel = event.getSelection(); 468 if (sel instanceof ITextSelection) 469 return; 470 if (sel instanceof IStructuredSelection) 471 fSelection = ((IStructuredSelection)sel).getFirstElement(); 472 else 473 fSelection = null; 474 } 475 476 481 protected IDocumentRange findRange() { 482 return null; 483 } 484 485 public void updateTextSelection() { 486 IDocumentRange range = findRange(); 487 if (range == null) 488 return; 489 IBaseModel model = getInputContext().getModel(); 490 if (!(model instanceof AbstractEditingModel)) 491 return; 492 493 if (range.getOffset() == -1 || isDirty()) { 494 try { 495 ((AbstractEditingModel)model).adjustOffsets(((AbstractEditingModel)model).getDocument()); 496 } catch (CoreException e) { 497 } 498 range = findRange(); 499 } 500 setHighlightRange(range, true); 501 setSelectedRange(range, false); 502 } 503 504 508 protected boolean isSelectionListener() { 509 return false; 510 } 511 512 public ISourceViewer getViewer() { 513 return getSourceViewer(); 514 } 515 516 protected void editorContextMenuAboutToShow(IMenuManager menu) { 517 PDEFormEditorContributor contributor = fEditor == null ? null : fEditor.getContributor(); 518 if (contributor instanceof PDEFormTextEditorContributor) { 519 PDEFormTextEditorContributor textContributor = (PDEFormTextEditorContributor)contributor; 520 HyperlinkAction action = textContributor.getHyperlinkAction(); 521 if ((action != null) && 522 action.isEnabled() && 523 ((action.getHyperLink() instanceof ExtensionHyperLink) == false)) { 524 menu.add(action); 529 } 530 FormatAction formatManifestAction = textContributor.getFormatAction(); 531 if (formatManifestAction != null && formatManifestAction.isEnabled()) 532 menu.add(formatManifestAction); 533 } 534 super.editorContextMenuAboutToShow(menu); 535 } 536 537 540 public Object getSelection() { 541 return fSelection; 542 } 543 544 546 549 public Object getOutlineInput() { 550 return getInputContext().getModel(); 551 } 552 553 556 protected void updateOutlinePageSelection(Object rangeElement) { 557 if (PDEPlugin.getDefault().getPreferenceStore().getBoolean( 560 "ToggleLinkWithEditorAction.isChecked")) { if ((fOutlinePage instanceof SourceOutlinePage) == false) { 563 return; 564 } 565 SourceOutlinePage outlinePage = (SourceOutlinePage)fOutlinePage; 566 outlinePage.removeAllSelectionChangedListeners(); 569 if (rangeElement != null) { 570 outlinePage.setSelection(new StructuredSelection(rangeElement)); 571 } else { 572 outlinePage.setSelection(StructuredSelection.EMPTY); 573 } 574 outlinePage.addAllSelectionChangedListeners(); 575 } 576 } 577 578 581 protected void handleSelectionChangedSourcePage(SelectionChangedEvent event) { 582 ISelection selection = event.getSelection(); 583 if (!selection.isEmpty() && selection instanceof ITextSelection) { 584 synchronizeOutlinePage(((ITextSelection) selection).getOffset()); 585 } 586 } 587 588 591 protected void updateHighlightRange(IDocumentRange rangeElement) { 592 if (rangeElement != null) { 593 setHighlightRange(rangeElement, false); 594 } else { 595 resetHighlightRange(); 596 } 597 } 598 599 602 protected void synchronizeOutlinePage(int offset) { 603 IDocumentRange rangeElement = getRangeElement(offset, false); 604 updateHighlightRange(rangeElement); 605 updateOutlinePageSelection(rangeElement); 606 } 607 608 613 public void synchronizeOutlinePage() { 614 int current_offset = getSourceViewer().getSelectedRange().x; 616 synchronizeOutlinePage(current_offset); 617 } 618 619 } 620 | Popular Tags |