KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > ExtensionElementBodyTextDetails


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
49  * ExtensionElementBodyTextDetails
50  *
51  */

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     /**
70      *
71      */

72     public ExtensionElementBodyTextDetails(PDESection masterSection) {
73         super(masterSection);
74         fPluginElement = null;
75         fSchemaElement = null;
76         fTextBody = null;
77         fSectionElementDetails = null;
78     }
79
80     /* (non-Javadoc)
81      * @see org.eclipse.ui.forms.IDetailsPage#createContents(org.eclipse.swt.widgets.Composite)
82      */

83     public void createContents(Composite parent) {
84         // Get the toolkit
85
createUIToolkit();
86         // Configure the parents layout
87
configureParentLayout(parent);
88         // Create the UI
89
createUI(parent);
90         // Create the listeners
91
createListeners();
92     }
93
94     /**
95      *
96      */

97     private void createListeners() {
98         // Create the listeners for the body text field
99
createListenersTextBody();
100         // Create the listeners for the body text hyperlink
101
createListenersHyperlinkBody();
102         // Create the model listeners
103
createListenersModel();
104     }
105
106     /**
107      *
108      */

109     private void createListenersHyperlinkBody() {
110         // Listen to hyperlink clicks
111
fHyperlinkBody.addHyperlinkListener(new HyperlinkAdapter() {
112             public void linkActivated(HyperlinkEvent e) {
113                 handleHyperlinkBodyLinkActivated();
114             }
115         });
116         // Listen to mouse hovers
117
PDETextHover.addHoverListenerToControl(fInfoControlHover,
118                 fHyperlinkBody, this);
119     }
120
121     /**
122      *
123      */

124     private void handleHyperlinkBodyLinkActivated() {
125         boolean opened = false;
126         // Open the reference if this is not a reference model
127
if (isReferenceModel() == false) {
128             opened = openReference();
129         }
130         // If the reference was not opened, notify the user with a beep
131
if (opened == false) {
132             Display.getCurrent().beep();
133         }
134     }
135
136     /**
137      *
138      */

139     private boolean openReference() {
140         // Ensure a plugin element was specified
141
if (fPluginElement == null) {
142             return false;
143         }
144         // Create the link
145
TranslationHyperlink link = new TranslationHyperlink(
146                 null,
147                 fTextBody.getValue(),
148                 fPluginElement.getModel());
149         // Open the link
150
link.open();
151         
152         return link.getOpened();
153     }
154     
155     /**
156      *
157      */

158     private void createListenersModel() {
159         IPluginModelBase model = (IPluginModelBase)getPage().getModel();
160         model.addModelChangedListener(this);
161     }
162
163     /**
164      *
165      */

166     private void createListenersTextBody() {
167         // Listen for text input
168
fTextBody.setFormEntryListener(new FormEntryAdapter(this) {
169             public void textValueChanged(FormEntry entry) {
170                 handleTextBodyValueChanged();
171             }
172         });
173         // Listen to mouse hovers
174
PDETextHover.addHoverListenerToControl(fInfoControlHover, fTextBody
175                 .getText(), this);
176     }
177
178     /**
179      *
180      */

181     private void handleTextBodyValueChanged() {
182         // Plugin element data not defined, nothing to update
183
if (fPluginElement == null) {
184             return;
185         }
186         // Update the body text field with the new value from plugin element
187
// data
188
try {
189             fPluginElement.setText(fTextBody.getValue());
190         } catch (CoreException e) {
191             PDEPlugin.logException(e);
192         }
193     }
194     
195     /**
196      * @param parent
197      */

198     private void configureParentLayout(Composite parent) {
199         parent.setLayout(FormLayoutFactory.createDetailsGridLayout(false, 1));
200     }
201
202     /**
203      *
204      */

205     private void createUIToolkit() {
206         fToolkit = getManagedForm().getToolkit();
207     }
208
209     /**
210      * @param parent
211      */

212     private void createUI(Composite parent) {
213         // Create the element details section
214
createUISectionElementDetails(parent);
215         // Create the client container for the section
216
Composite client = createUISectionContainer(fSectionElementDetails);
217         // Create the info hover control for the body text field and hyperlink
218
createUIInfoHoverControl(client);
219         // Create the body text label
220
createUIHyperlinkBody(client);
221         // Create the body text field
222
createUITextBody(client);
223         // Associate the client with the section
224
fToolkit.paintBordersFor(client);
225         fSectionElementDetails.setClient(client);
226         // Needed for keyboard paste operation to work
227
markDetailsPart(fSectionElementDetails);
228     }
229
230     /**
231      * @param client
232      */

233     private void createUIInfoHoverControl(Composite client) {
234         // Shared between the body text field and body text hyperlink / label
235
fInfoControlHover =
236             PDETextHover.getInformationControlCreator().createInformationControl(
237                     client.getShell());
238         fInfoControlHover.setSizeConstraints(300, 600);
239     }
240     
241     /**
242      * @param client
243      */

244     private void createUIHyperlinkBody(Composite client) {
245         fHyperlinkBody = fToolkit.createHyperlink(client,
246                 PDEUIMessages.ExtensionElementBodyTextDetails_labelBodyText,
247                 SWT.NULL);
248     }
249     
250     /**
251      * @return
252      */

253     private boolean isReferenceModel() {
254         // If the model has no underlying resource, then it is a reference
255
// model
256
if ((fPluginElement == null) ||
257                 (fPluginElement.getModel().getUnderlyingResource() == null)) {
258             return true;
259         }
260         return false;
261     }
262     
263     /**
264      * @param section
265      * @return
266      */

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     /**
274      * @param parent
275      */

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         // Align the master and details section headers (misalignment caused
289
// by section toolbar icons)
290
getPage().alignSectionHeaders(getMasterSection().getSection(),
291                 fSectionElementDetails);
292     }
293
294     /**
295      * @param parent
296      */

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     /* (non-Javadoc)
307      * @see org.eclipse.ui.forms.IPartSelectionListener#selectionChanged(org.eclipse.ui.forms.IFormPart, org.eclipse.jface.viewers.ISelection)
308      */

309     public void selectionChanged(IFormPart part, ISelection selection) {
310         // Get the structured selection
311
IStructuredSelection structured_selection =
312             (IStructuredSelection)selection;
313         // The selection from the master tree viewer is our plugin element data
314
if (structured_selection.size() == 1) {
315             fPluginElement = (IPluginElement)structured_selection.getFirstElement();
316         } else {
317             fPluginElement = null;
318         }
319         // Update the UI given the new plugin element data
320
updateUI();
321     }
322
323     /**
324      *
325      */

326     private void updateUI() {
327         // Update the section description
328
updateUISectionElementDetails();
329         // Update the body text field
330
updateUITextBody();
331     }
332
333     /**
334      *
335      */

336     private void updateUISectionElementDetails() {
337         // Set the general or specifc section description depending if whether
338
// the plugin element data is defined
339
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         // Re-layout the section to properly wrap the new section description
349
fSectionElementDetails.layout();
350     }
351
352     /**
353      *
354      */

355     private void updateUITextBody() {
356         // Set the new body text value from the new plugin element data if
357
// defined
358
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     /* (non-Javadoc)
368      * @see org.eclipse.pde.internal.ui.editor.IContextPart#fireSaveNeeded()
369      */

370     public void fireSaveNeeded() {
371         markDirty();
372         getPage().getPDEEditor().fireSaveNeeded(getContextId(), false);
373     }
374
375     /* (non-Javadoc)
376      * @see org.eclipse.pde.internal.ui.editor.IContextPart#getContextId()
377      */

378     public String JavaDoc getContextId() {
379         return PluginInputContext.CONTEXT_ID;
380     }
381
382     /* (non-Javadoc)
383      * @see org.eclipse.pde.internal.ui.editor.IContextPart#getPage()
384      */

385     public PDEFormPage getPage() {
386         return (PDEFormPage)getManagedForm().getContainer();
387     }
388
389     /* (non-Javadoc)
390      * @see org.eclipse.pde.internal.ui.editor.IContextPart#isEditable()
391      */

392     public boolean isEditable() {
393         return getPage().getPDEEditor().getAggregateModel().isEditable();
394     }
395
396     /* (non-Javadoc)
397      * @see org.eclipse.pde.core.IModelChangedListener#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
398      */

399     public void modelChanged(IModelChangedEvent event) {
400         // Refresh the UI if the plugin element data changed
401
if (event.getChangeType() == IModelChangedEvent.CHANGE) {
402             Object JavaDoc object = event.getChangedObjects()[0];
403             if (object.equals(fPluginElement)) {
404                 refresh();
405             }
406         }
407     }
408
409     /* (non-Javadoc)
410      * @see org.eclipse.ui.forms.AbstractFormPart#refresh()
411      */

412     public void refresh() {
413         updateUI();
414         super.refresh();
415     }
416     
417     /* (non-Javadoc)
418      * @see org.eclipse.pde.internal.ui.editor.PDEDetails#cancelEdit()
419      */

420     public void cancelEdit() {
421         fTextBody.cancelEdit();
422         super.cancelEdit();
423     }
424     
425     /* (non-Javadoc)
426      * @see org.eclipse.ui.forms.AbstractFormPart#commit(boolean)
427      */

428     public void commit(boolean onSave) {
429         fTextBody.commit();
430         super.commit(onSave);
431     }
432     
433     /* (non-Javadoc)
434      * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
435      */

436     public void dispose() {
437         IPluginModelBase model = (IPluginModelBase)getPage().getModel();
438         // Remove the model listener
439
if (model != null) {
440             model.removeModelChangedListener(this);
441         }
442         super.dispose();
443     }
444     
445     /* (non-Javadoc)
446      * @see org.eclipse.ui.forms.AbstractFormPart#setFocus()
447      */

448     public void setFocus() {
449         fTextBody.getText().setFocus();
450     }
451
452     /* (non-Javadoc)
453      * @see org.eclipse.pde.internal.ui.editor.text.TextHoverDescriptionProvider#getDescription(org.eclipse.swt.widgets.Control)
454      */

455     public String JavaDoc getHoverContent(Control control) {
456         // Retrieve either the hyperlink, label or text description as the
457
// hover content
458
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     /**
469      * @return
470      */

471     private String JavaDoc getHyperlinkDescription() {
472         // Ensure there is an associated schema
473
if (fSchemaElement == null) {
474             return null;
475         }
476         // Return the associated element description
477
return fSchemaElement.getDescription();
478     }
479
480     /**
481      * @param text
482      * @return
483      */

484     private String JavaDoc getTextDescription(Text text) {
485         // Ensure there is an associated schema
486
if (fSchemaElement == null) {
487             return null;
488         }
489         String JavaDoc bodyText = text.getText();
490         String JavaDoc translatedBodyText = null;
491         // If the text represents a translated string key, retrieve its
492
// associated value
493
if ((bodyText.startsWith("%")) && //$NON-NLS-1$
494
fSchemaElement.hasTranslatableContent()) {
495             translatedBodyText = fPluginElement.getResourceString(bodyText);
496             // If the value does not equal the key, a value was found
497
if (bodyText.equals(translatedBodyText) == false) {
498                 return translatedBodyText;
499             }
500         }
501
502         return null;
503     }
504     
505     /**
506      * @param schemaElement
507      */

508     public void setSchemaElement(ISchemaElement schemaElement) {
509         fSchemaElement = schemaElement;
510     }
511     
512 }
513
Popular Tags