1 11 12 package org.eclipse.pde.internal.ui.editor.plugin; 13 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.jface.text.IInformationControl; 16 import org.eclipse.jface.viewers.ISelection; 17 import org.eclipse.jface.viewers.IStructuredSelection; 18 import org.eclipse.osgi.util.NLS; 19 import org.eclipse.pde.core.IModelChangedEvent; 20 import org.eclipse.pde.core.plugin.IPluginElement; 21 import org.eclipse.pde.core.plugin.IPluginModelBase; 22 import org.eclipse.pde.internal.core.ischema.ISchemaElement; 23 import org.eclipse.pde.internal.ui.PDEPlugin; 24 import org.eclipse.pde.internal.ui.PDEUIMessages; 25 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter; 26 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 27 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 28 import org.eclipse.pde.internal.ui.editor.PDESection; 29 import org.eclipse.pde.internal.ui.editor.text.IControlHoverContentProvider; 30 import org.eclipse.pde.internal.ui.editor.text.PDETextHover; 31 import org.eclipse.pde.internal.ui.editor.text.TranslationHyperlink; 32 import org.eclipse.pde.internal.ui.parts.FormEntry; 33 import org.eclipse.swt.SWT; 34 import org.eclipse.swt.layout.GridData; 35 import org.eclipse.swt.widgets.Composite; 36 import org.eclipse.swt.widgets.Control; 37 import org.eclipse.swt.widgets.Display; 38 import org.eclipse.swt.widgets.Label; 39 import org.eclipse.swt.widgets.Text; 40 import org.eclipse.ui.forms.IFormPart; 41 import org.eclipse.ui.forms.events.HyperlinkAdapter; 42 import org.eclipse.ui.forms.events.HyperlinkEvent; 43 import org.eclipse.ui.forms.widgets.ExpandableComposite; 44 import org.eclipse.ui.forms.widgets.FormToolkit; 45 import org.eclipse.ui.forms.widgets.Hyperlink; 46 import org.eclipse.ui.forms.widgets.Section; 47 48 52 public class ExtensionElementBodyTextDetails extends AbstractPluginElementDetails implements 53 IControlHoverContentProvider { 54 55 private IPluginElement fPluginElement; 56 57 private ISchemaElement fSchemaElement; 58 59 private FormEntry fTextBody; 60 61 private Section fSectionElementDetails; 62 63 private FormToolkit fToolkit; 64 65 private Hyperlink fHyperlinkBody; 66 67 private IInformationControl fInfoControlHover; 68 69 72 public ExtensionElementBodyTextDetails(PDESection masterSection) { 73 super(masterSection); 74 fPluginElement = null; 75 fSchemaElement = null; 76 fTextBody = null; 77 fSectionElementDetails = null; 78 } 79 80 83 public void createContents(Composite parent) { 84 createUIToolkit(); 86 configureParentLayout(parent); 88 createUI(parent); 90 createListeners(); 92 } 93 94 97 private void createListeners() { 98 createListenersTextBody(); 100 createListenersHyperlinkBody(); 102 createListenersModel(); 104 } 105 106 109 private void createListenersHyperlinkBody() { 110 fHyperlinkBody.addHyperlinkListener(new HyperlinkAdapter() { 112 public void linkActivated(HyperlinkEvent e) { 113 handleHyperlinkBodyLinkActivated(); 114 } 115 }); 116 PDETextHover.addHoverListenerToControl(fInfoControlHover, 118 fHyperlinkBody, this); 119 } 120 121 124 private void handleHyperlinkBodyLinkActivated() { 125 boolean opened = false; 126 if (isReferenceModel() == false) { 128 opened = openReference(); 129 } 130 if (opened == false) { 132 Display.getCurrent().beep(); 133 } 134 } 135 136 139 private boolean openReference() { 140 if (fPluginElement == null) { 142 return false; 143 } 144 TranslationHyperlink link = new TranslationHyperlink( 146 null, 147 fTextBody.getValue(), 148 fPluginElement.getModel()); 149 link.open(); 151 152 return link.getOpened(); 153 } 154 155 158 private void createListenersModel() { 159 IPluginModelBase model = (IPluginModelBase)getPage().getModel(); 160 model.addModelChangedListener(this); 161 } 162 163 166 private void createListenersTextBody() { 167 fTextBody.setFormEntryListener(new FormEntryAdapter(this) { 169 public void textValueChanged(FormEntry entry) { 170 handleTextBodyValueChanged(); 171 } 172 }); 173 PDETextHover.addHoverListenerToControl(fInfoControlHover, fTextBody 175 .getText(), this); 176 } 177 178 181 private void handleTextBodyValueChanged() { 182 if (fPluginElement == null) { 184 return; 185 } 186 try { 189 fPluginElement.setText(fTextBody.getValue()); 190 } catch (CoreException e) { 191 PDEPlugin.logException(e); 192 } 193 } 194 195 198 private void configureParentLayout(Composite parent) { 199 parent.setLayout(FormLayoutFactory.createDetailsGridLayout(false, 1)); 200 } 201 202 205 private void createUIToolkit() { 206 fToolkit = getManagedForm().getToolkit(); 207 } 208 209 212 private void createUI(Composite parent) { 213 createUISectionElementDetails(parent); 215 Composite client = createUISectionContainer(fSectionElementDetails); 217 createUIInfoHoverControl(client); 219 createUIHyperlinkBody(client); 221 createUITextBody(client); 223 fToolkit.paintBordersFor(client); 225 fSectionElementDetails.setClient(client); 226 markDetailsPart(fSectionElementDetails); 228 } 229 230 233 private void createUIInfoHoverControl(Composite client) { 234 fInfoControlHover = 236 PDETextHover.getInformationControlCreator().createInformationControl( 237 client.getShell()); 238 fInfoControlHover.setSizeConstraints(300, 600); 239 } 240 241 244 private void createUIHyperlinkBody(Composite client) { 245 fHyperlinkBody = fToolkit.createHyperlink(client, 246 PDEUIMessages.ExtensionElementBodyTextDetails_labelBodyText, 247 SWT.NULL); 248 } 249 250 253 private boolean isReferenceModel() { 254 if ((fPluginElement == null) || 257 (fPluginElement.getModel().getUnderlyingResource() == null)) { 258 return true; 259 } 260 return false; 261 } 262 263 267 private Composite createUISectionContainer(Section section) { 268 Composite client = fToolkit.createComposite(section); 269 client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 1)); 270 return client; 271 } 272 273 276 private void createUISectionElementDetails(Composite parent) { 277 int section_style = Section.DESCRIPTION | ExpandableComposite.TITLE_BAR; 278 fSectionElementDetails = fToolkit.createSection(parent, 279 section_style); 280 fSectionElementDetails.clientVerticalSpacing = FormLayoutFactory.SECTION_HEADER_VERTICAL_SPACING; 281 fSectionElementDetails.setText(PDEUIMessages.ExtensionElementDetails_title); 282 fSectionElementDetails.setDescription(PDEUIMessages.ExtensionElementBodyTextDetails_sectionDescElementGeneral); 283 fSectionElementDetails.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 284 int layout_style = GridData.FILL_HORIZONTAL; 285 GridData data = new GridData(layout_style); 286 fSectionElementDetails.setLayoutData(data); 287 288 getPage().alignSectionHeaders(getMasterSection().getSection(), 291 fSectionElementDetails); 292 } 293 294 297 private void createUITextBody(Composite parent) { 298 int widget_style = SWT.MULTI | SWT.WRAP | SWT.V_SCROLL; 299 fTextBody = new FormEntry(parent, fToolkit, null, widget_style); 300 int layout_text_style = GridData.FILL_HORIZONTAL; 301 GridData data = new GridData(layout_text_style); 302 data.heightHint = 90; 303 fTextBody.getText().setLayoutData(data); 304 } 305 306 309 public void selectionChanged(IFormPart part, ISelection selection) { 310 IStructuredSelection structured_selection = 312 (IStructuredSelection)selection; 313 if (structured_selection.size() == 1) { 315 fPluginElement = (IPluginElement)structured_selection.getFirstElement(); 316 } else { 317 fPluginElement = null; 318 } 319 updateUI(); 321 } 322 323 326 private void updateUI() { 327 updateUISectionElementDetails(); 329 updateUITextBody(); 331 } 332 333 336 private void updateUISectionElementDetails() { 337 if (fPluginElement == null) { 340 fSectionElementDetails.setDescription( 341 PDEUIMessages.ExtensionElementBodyTextDetails_sectionDescElementGeneral); 342 } else { 343 fSectionElementDetails.setDescription( 344 NLS.bind( 345 PDEUIMessages.ExtensionElementBodyTextDetails_sectionDescElementSpecific, 346 fPluginElement.getName())); 347 } 348 fSectionElementDetails.layout(); 350 } 351 352 355 private void updateUITextBody() { 356 if (fPluginElement == null) { 359 fTextBody.setEditable(false); 360 fTextBody.setValue(null, true); 361 } else { 362 fTextBody.setEditable(isEditable()); 363 fTextBody.setValue(fPluginElement.getText(), true); 364 } 365 } 366 367 370 public void fireSaveNeeded() { 371 markDirty(); 372 getPage().getPDEEditor().fireSaveNeeded(getContextId(), false); 373 } 374 375 378 public String getContextId() { 379 return PluginInputContext.CONTEXT_ID; 380 } 381 382 385 public PDEFormPage getPage() { 386 return (PDEFormPage)getManagedForm().getContainer(); 387 } 388 389 392 public boolean isEditable() { 393 return getPage().getPDEEditor().getAggregateModel().isEditable(); 394 } 395 396 399 public void modelChanged(IModelChangedEvent event) { 400 if (event.getChangeType() == IModelChangedEvent.CHANGE) { 402 Object object = event.getChangedObjects()[0]; 403 if (object.equals(fPluginElement)) { 404 refresh(); 405 } 406 } 407 } 408 409 412 public void refresh() { 413 updateUI(); 414 super.refresh(); 415 } 416 417 420 public void cancelEdit() { 421 fTextBody.cancelEdit(); 422 super.cancelEdit(); 423 } 424 425 428 public void commit(boolean onSave) { 429 fTextBody.commit(); 430 super.commit(onSave); 431 } 432 433 436 public void dispose() { 437 IPluginModelBase model = (IPluginModelBase)getPage().getModel(); 438 if (model != null) { 440 model.removeModelChangedListener(this); 441 } 442 super.dispose(); 443 } 444 445 448 public void setFocus() { 449 fTextBody.getText().setFocus(); 450 } 451 452 455 public String getHoverContent(Control control) { 456 if ((control instanceof Hyperlink) || 459 (control instanceof Label)) { 460 return getHyperlinkDescription(); 461 } else if (control instanceof Text) { 462 return getTextDescription((Text)control); 463 } 464 465 return null; 466 } 467 468 471 private String getHyperlinkDescription() { 472 if (fSchemaElement == null) { 474 return null; 475 } 476 return fSchemaElement.getDescription(); 478 } 479 480 484 private String getTextDescription(Text text) { 485 if (fSchemaElement == null) { 487 return null; 488 } 489 String bodyText = text.getText(); 490 String translatedBodyText = null; 491 if ((bodyText.startsWith("%")) && fSchemaElement.hasTranslatableContent()) { 495 translatedBodyText = fPluginElement.getResourceString(bodyText); 496 if (bodyText.equals(translatedBodyText) == false) { 498 return translatedBodyText; 499 } 500 } 501 502 return null; 503 } 504 505 508 public void setSchemaElement(ISchemaElement schemaElement) { 509 fSchemaElement = schemaElement; 510 } 511 512 } 513 | Popular Tags |