1 11 package org.eclipse.pde.internal.ui.editor.feature; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.jface.action.IMenuManager; 15 import org.eclipse.jface.resource.JFaceResources; 16 import org.eclipse.jface.text.Document; 17 import org.eclipse.jface.text.DocumentEvent; 18 import org.eclipse.jface.text.IDocument; 19 import org.eclipse.jface.text.IDocumentListener; 20 import org.eclipse.jface.text.IDocumentPartitioner; 21 import org.eclipse.jface.text.ITextOperationTarget; 22 import org.eclipse.jface.text.rules.FastPartitioner; 23 import org.eclipse.jface.text.source.SourceViewer; 24 import org.eclipse.jface.viewers.ISelection; 25 import org.eclipse.jface.viewers.ISelectionChangedListener; 26 import org.eclipse.jface.viewers.SelectionChangedEvent; 27 import org.eclipse.pde.core.IEditable; 28 import org.eclipse.pde.core.IModelChangedEvent; 29 import org.eclipse.pde.internal.core.ifeature.IFeature; 30 import org.eclipse.pde.internal.core.ifeature.IFeatureInfo; 31 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 32 import org.eclipse.pde.internal.core.ifeature.IFeatureURLElement; 33 import org.eclipse.pde.internal.ui.PDEPlugin; 34 import org.eclipse.pde.internal.ui.PDEPluginImages; 35 import org.eclipse.pde.internal.ui.PDEUIMessages; 36 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 37 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 38 import org.eclipse.pde.internal.ui.editor.PDESection; 39 import org.eclipse.pde.internal.ui.editor.text.IColorManager; 40 import org.eclipse.pde.internal.ui.editor.text.XMLConfiguration; 41 import org.eclipse.pde.internal.ui.editor.text.XMLPartitionScanner; 42 import org.eclipse.pde.internal.ui.elements.NamedElement; 43 import org.eclipse.pde.internal.ui.util.TextUtil; 44 import org.eclipse.swt.SWT; 45 import org.eclipse.swt.custom.CTabFolder; 46 import org.eclipse.swt.custom.CTabItem; 47 import org.eclipse.swt.custom.StackLayout; 48 import org.eclipse.swt.custom.StyledText; 49 import org.eclipse.swt.dnd.Clipboard; 50 import org.eclipse.swt.events.ModifyEvent; 51 import org.eclipse.swt.events.ModifyListener; 52 import org.eclipse.swt.events.SelectionAdapter; 53 import org.eclipse.swt.events.SelectionEvent; 54 import org.eclipse.swt.graphics.Color; 55 import org.eclipse.swt.layout.GridData; 56 import org.eclipse.swt.layout.GridLayout; 57 import org.eclipse.swt.widgets.Composite; 58 import org.eclipse.swt.widgets.Control; 59 import org.eclipse.swt.widgets.Label; 60 import org.eclipse.swt.widgets.Text; 61 import org.eclipse.ui.actions.ActionFactory; 62 import org.eclipse.ui.forms.IFormColors; 63 import org.eclipse.ui.forms.widgets.ExpandableComposite; 64 import org.eclipse.ui.forms.widgets.FormToolkit; 65 import org.eclipse.ui.forms.widgets.Section; 66 67 public class InfoSection extends PDESection { 68 private IDocument fDocument; 69 70 private IDocumentPartitioner fPartitioner; 71 72 private XMLConfiguration fSourceConfiguration; 73 74 private SourceViewer fSourceViewer; 75 76 private CTabFolder fTabFolder; 77 78 private Text fUrlText; 79 80 private Object fElement; 81 82 private int fElementIndex; 83 84 private boolean fIgnoreChange; 85 86 private Composite fNotebook; 87 88 private StackLayout fNotebookLayout; 89 90 private Control fInfoPage; 91 92 private Control fUrlsPage; 93 94 public InfoSection(PDEFormPage page, Composite parent, 95 IColorManager colorManager) { 96 super(page, parent, Section.DESCRIPTION | ExpandableComposite.NO_TITLE, 97 false); 98 String description = PDEUIMessages.FeatureEditor_InfoSection_desc; 99 getSection().setDescription(description); 100 fSourceConfiguration = new XMLConfiguration(colorManager); 101 fDocument = new Document(); 102 fPartitioner = new FastPartitioner(new XMLPartitionScanner(), 103 new String [] { XMLPartitionScanner.XML_TAG, 104 XMLPartitionScanner.XML_COMMENT }); 105 fPartitioner.connect(fDocument); 106 fDocument.setDocumentPartitioner(fPartitioner); 107 createClient(getSection(), page.getManagedForm().getToolkit()); 108 } 109 110 public void commit(boolean onSave) { 111 handleApply(); 112 super.commit(onSave); 113 } 114 115 public void createClient(Section section, FormToolkit toolkit) { 116 117 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 118 GridData data = new GridData(GridData.FILL_BOTH); 119 section.setLayoutData(data); 120 121 Composite container = toolkit.createComposite(section); 122 container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2)); 123 container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 124 125 GridData gd; 126 127 toolkit.createLabel(container, null); 128 fTabFolder = new CTabFolder(container, SWT.FLAT | SWT.TOP); 129 toolkit.adapt(fTabFolder, true, true); 130 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 131 fTabFolder.setLayoutData(gd); 132 gd.heightHint = 2; 133 toolkit.getColors().initializeSectionToolBarColors(); 134 Color selectedColor = toolkit.getColors().getColor(IFormColors.TB_BG); 135 fTabFolder.setSelectionBackground(new Color[] { selectedColor, 136 toolkit.getColors().getBackground() }, 137 new int[] { 100 }, true); 138 139 fTabFolder.addSelectionListener(new SelectionAdapter() { 140 public void widgetSelected(SelectionEvent e) { 141 updateTabSelection(); 142 } 143 }); 144 145 fNotebook = toolkit.createComposite(container); 146 gd = new GridData(GridData.FILL_BOTH); 147 gd.horizontalSpan = 2; 148 fNotebook.setLayoutData(gd); 149 fNotebookLayout = new StackLayout(); 150 fNotebook.setLayout(fNotebookLayout); 151 152 fInfoPage = createInfoPage(toolkit, fNotebook); 153 fUrlsPage = createUrlsPage(toolkit, fNotebook); 154 fNotebookLayout.topControl = fInfoPage; 155 156 createTabs(); 157 section.setClient(container); 158 initialize(); 159 if (fTabFolder.getItemCount() > 0) { 160 fTabFolder.setSelection(0); 161 updateTabSelection(); 162 } 163 } 164 165 169 private Control createInfoPage(FormToolkit toolkit, Composite parent) { 170 Composite page = toolkit.createComposite(parent); 171 GridLayout layout = new GridLayout(); 172 layout.numColumns = 2; 173 layout.marginWidth = 2; 174 layout.marginHeight = 5; 175 layout.verticalSpacing = 8; 176 page.setLayout(layout); 177 GridData gd; 178 Label label = toolkit.createLabel(page, PDEUIMessages.FeatureEditor_InfoSection_url); 179 label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE)); 180 fUrlText = toolkit.createText(page, null, SWT.SINGLE); 181 fUrlText.addModifyListener(new ModifyListener() { 182 public void modifyText(ModifyEvent e) { 183 infoModified(); 184 } 185 }); 186 gd = new GridData(GridData.FILL_HORIZONTAL); 187 fUrlText.setLayoutData(gd); 188 label = toolkit 189 .createLabel(page, PDEUIMessages.FeatureEditor_InfoSection_text); 190 label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE)); 191 gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING); 192 label.setLayoutData(gd); 193 int styles = SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL; 194 fSourceViewer = new SourceViewer(page, null, styles); 195 fSourceViewer.configure(fSourceConfiguration); 196 fSourceViewer.setDocument(fDocument); 197 fSourceViewer 198 .addSelectionChangedListener(new ISelectionChangedListener() { 199 public void selectionChanged(SelectionChangedEvent event) { 200 updateSelection(event.getSelection()); 201 } 202 }); 203 StyledText styledText = fSourceViewer.getTextWidget(); 204 styledText.setFont(JFaceResources.getTextFont()); 205 styledText.setMenu(getPage().getPDEEditor().getContextMenu()); 206 styledText 207 .setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER); 208 if (SWT.getPlatform().equals("motif") == false) toolkit.paintBordersFor(page); 211 Control[] children = page.getChildren(); 212 Control control = children[children.length - 1]; 213 gd = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL 214 | GridData.GRAB_VERTICAL); 215 gd.widthHint = 50; 216 gd.heightHint = 50; 217 control.setLayoutData(gd); 218 219 return page; 220 } 221 222 226 private Control createUrlsPage(FormToolkit toolkit, Composite parent) { 227 Composite page = toolkit.createComposite(parent); 228 GridLayout layout = new GridLayout(); 229 layout.numColumns = 2; 230 layout.makeColumnsEqualWidth = true; 231 layout.marginWidth = 2; 232 layout.marginHeight = 5; 233 layout.verticalSpacing = 8; 234 page.setLayout(layout); 235 236 URLSection urlSection = new URLSection(getPage(), page); 237 urlSection.getSection().setLayoutData( 238 new GridData(GridData.FILL_BOTH 239 | GridData.VERTICAL_ALIGN_BEGINNING)); 240 241 URLDetailsSection urlDetailsSection = new URLDetailsSection(getPage(), 242 page); 243 urlDetailsSection.getSection().setLayoutData( 244 new GridData(GridData.FILL_HORIZONTAL 245 | GridData.VERTICAL_ALIGN_BEGINNING)); 246 247 getManagedForm().addPart(urlSection); 248 getManagedForm().addPart(urlDetailsSection); 249 return page; 250 } 251 252 private void updateSelection(ISelection selection) { 253 getPage().getPDEEditor().setSelection(selection); 254 } 255 256 public boolean doGlobalAction(String actionId) { 257 if (actionId.equals(ActionFactory.CUT.getId())) { 258 fSourceViewer.doOperation(ITextOperationTarget.CUT); 259 return true; 260 } else if (actionId.equals(ActionFactory.COPY.getId())) { 261 fSourceViewer.doOperation(ITextOperationTarget.COPY); 262 return true; 263 } else if (actionId.equals(ActionFactory.PASTE.getId())) { 264 fSourceViewer.doOperation(ITextOperationTarget.PASTE); 265 return true; 266 } else if (actionId.equals(ActionFactory.DELETE.getId())) { 267 fSourceViewer.doOperation(ITextOperationTarget.DELETE); 268 return true; 269 } else if (actionId.equals(ActionFactory.UNDO.getId())) { 270 fSourceViewer.doOperation(ITextOperationTarget.UNDO); 271 return true; 272 } else if (actionId.equals(ActionFactory.REDO.getId())) { 273 fSourceViewer.doOperation(ITextOperationTarget.REDO); 274 return true; 275 } 276 return false; 277 } 278 279 public boolean setFormInput(Object input) { 280 if (input instanceof IFeatureInfo) { 281 IFeatureInfo info = (IFeatureInfo) input; 282 int index = info.getIndex(); 283 if (index != -1) 284 fTabFolder.setSelection(index); 285 updateEditorInput(input, false); 286 return true; 287 } 288 if (input instanceof IFeatureURLElement 289 || input instanceof NamedElement) { 290 fTabFolder.setSelection(3); 291 updateEditorInput(input, false); 292 return true; 293 } 294 return false; 295 } 296 297 private void handleApply() { 298 if (0 <= fElementIndex && fElementIndex < 3 && fElement != null) { 299 handleApply((IFeatureInfo) fElement, fTabFolder.getSelectionIndex()); 300 } else { 301 handleApply(null, fTabFolder.getSelectionIndex()); 302 } 303 } 304 305 private void handleApply(IFeatureInfo info, int index) { 306 if (index >= 3) 307 return; 308 String urlName = fUrlText.getText(); 309 String text = fDocument.get(); 310 applyInfoText(info, urlName, text, index); 311 updateTabImage(fTabFolder.getSelection()); 312 } 313 314 private void applyInfoText(IFeatureInfo targetInfo, String urlText, 315 String text, int index) { 316 String url = null; 317 318 if (urlText.length() > 0) { 319 url = urlText; 320 } 321 try { 322 IFeatureModel model = (IFeatureModel) getPage().getModel(); 323 IFeature feature = model.getFeature(); 324 325 if (targetInfo == null) 326 targetInfo = feature.getFeatureInfo(index); 327 328 if (targetInfo == null) { 329 targetInfo = model.getFactory().createInfo(index); 330 feature.setFeatureInfo(targetInfo, index); 331 } 332 targetInfo.setURL(url); 333 targetInfo.setDescription(text); 334 } catch (CoreException e) { 335 } 336 } 337 338 protected void fillContextMenu(IMenuManager manager) { 339 getPage().getPDEEditor().getContributor().contextMenuAboutToShow( 340 manager); 341 } 342 343 public void initialize() { 344 IFeatureModel featureModel = (IFeatureModel) getPage().getModel(); 345 fDocument.addDocumentListener(new IDocumentListener() { 346 public void documentChanged(DocumentEvent e) { 347 infoModified(); 348 } 349 350 public void documentAboutToBeChanged(DocumentEvent e) { 351 } 352 }); 353 fUrlText.setEditable(featureModel.isEditable()); 354 fSourceViewer.getTextWidget().setEditable(featureModel.isEditable()); 355 featureModel.addModelChangedListener(this); 356 updateEditorInput(featureModel.getFeature().getFeatureInfo(0), false); 357 } 358 359 362 public void dispose() { 363 if (fSourceConfiguration != null) { 365 fSourceConfiguration.dispose(); 366 } 367 IFeatureModel featureModel = (IFeatureModel) getPage().getModel(); 368 if (featureModel != null) 369 featureModel.removeModelChangedListener(this); 370 super.dispose(); 371 } 372 373 private void infoModified() { 374 IFeatureModel featureModel = (IFeatureModel) getPage().getModel(); 375 if (!fIgnoreChange && featureModel instanceof IEditable) { 376 markDirty(); 377 } 378 } 379 380 public void modelChanged(IModelChangedEvent e) { 381 if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 382 markStale(); 383 } 384 } 385 386 public void refresh() { 387 IFeatureModel model = (IFeatureModel) getPage().getModel(); 388 int index = fTabFolder.getSelectionIndex(); 389 if (index < 3) { 390 IFeatureInfo info = model.getFeature().getFeatureInfo(index); 391 fElement = null; 392 fElementIndex = -1; 393 updateEditorInput(info, false); 394 } 395 super.refresh(); 396 } 397 398 private void createTabs() { 399 IFeatureModel model = (IFeatureModel) getPage().getModel(); 400 IFeature feature = model.getFeature(); 401 addTab(PDEUIMessages.FeatureEditor_info_description, feature 402 .getFeatureInfo(0)); 403 addTab(PDEUIMessages.FeatureEditor_info_copyright, feature 404 .getFeatureInfo(1)); 405 addTab(PDEUIMessages.FeatureEditor_info_license, feature 406 .getFeatureInfo(2)); 407 addTab(PDEUIMessages.FeatureEditor_info_discoveryUrls, null); 408 } 409 410 private void addTab(String label, IFeatureInfo info) { 411 CTabItem item = new CTabItem(fTabFolder, SWT.NULL); 412 item.setText(label); 413 item.setData(info); 414 updateTabImage(item); 415 } 416 417 private void updateTabImage(CTabItem item) { 418 if (item == null) 419 return; 420 Object info = item.getData(); 421 if (info != null) { 422 item.setImage(PDEPlugin.getDefault().getLabelProvider().getImage( 423 info)); 424 } else { 425 item.setImage(PDEPlugin.getDefault().getLabelProvider().get( 426 PDEPluginImages.DESC_DOC_SECTION_OBJ)); 427 } 428 } 429 430 private void updateTabSelection() { 431 IFeatureModel model = (IFeatureModel) getPage().getModel(); 432 IFeature feature = model.getFeature(); 433 int index = fTabFolder.getSelectionIndex(); 434 if (index < 3) { 435 IFeatureInfo info = feature.getFeatureInfo(index); 436 updateEditorInput(info, true); 437 } 438 Control oldPage = fNotebookLayout.topControl; 439 if (index < 3) 440 fNotebookLayout.topControl = fInfoPage; 441 else 442 fNotebookLayout.topControl = fUrlsPage; 443 if (oldPage != fNotebookLayout.topControl) 444 fNotebook.layout(); 445 } 446 447 public void setFocus() { 448 fSourceViewer.getTextWidget().setFocus(); 449 updateSelection(fSourceViewer.getSelection()); 450 } 451 452 private void commitPrevious() { 453 IFeatureInfo previous = (IFeatureInfo) fElement; 454 handleApply(previous, fElementIndex); 455 } 456 457 public void updateEditorInput(Object input, boolean commitPrevious) { 458 if (isDirty() && commitPrevious ) { 462 commitPrevious(); 463 } 464 fIgnoreChange = true; 465 String text = ""; String url = null; 467 if (input instanceof IFeatureInfo) { 468 IFeatureInfo info = (IFeatureInfo) input; 469 text = info.getDescription(); 470 url = info.getURL(); 471 } 472 if (text == null) 473 text = ""; else 475 text = TextUtil.createMultiLine(text, 60, false); 476 fDocument.set(text); 477 if (url == null) 478 fUrlText.setText(""); else 480 fUrlText.setText(url.toString()); 481 fElement = input; 482 fElementIndex = fTabFolder.getSelectionIndex(); 483 484 Control oldPage = fNotebookLayout.topControl; 485 if (input instanceof IFeatureURLElement 486 || input instanceof NamedElement) { 487 fNotebookLayout.topControl = fUrlsPage; 488 } else { 489 fNotebookLayout.topControl = fInfoPage; 490 } 491 if (oldPage != fNotebookLayout.topControl) 492 fNotebook.layout(); 493 494 fIgnoreChange = false; 495 } 496 497 public boolean canPaste(Clipboard clipboard) { 498 return fSourceViewer.canDoOperation(ITextOperationTarget.PASTE); 499 } 500 } 501 | Popular Tags |