KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > cheatsheet > simple > details > SimpleCSItemDetails


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.cheatsheet.simple.details;
13
14 import org.eclipse.jface.fieldassist.ControlDecoration;
15 import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
16 import org.eclipse.jface.text.DocumentEvent;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.IDocumentListener;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem;
21 import org.eclipse.pde.internal.ui.PDEUIMessages;
22 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
23 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
24 import org.eclipse.pde.internal.ui.editor.cheatsheet.CSAbstractDetails;
25 import org.eclipse.pde.internal.ui.editor.cheatsheet.CSSourceViewer;
26 import org.eclipse.pde.internal.ui.editor.cheatsheet.ICSMaster;
27 import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSInputContext;
28 import org.eclipse.pde.internal.ui.parts.FormEntry;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.dnd.Clipboard;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.graphics.Color;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.ui.forms.IFormColors;
39 import org.eclipse.ui.forms.IFormPart;
40 import org.eclipse.ui.forms.IManagedForm;
41 import org.eclipse.ui.forms.widgets.ExpandableComposite;
42 import org.eclipse.ui.forms.widgets.Section;
43
44 /**
45  * SimpleCSItemDetails
46  *
47  */

48 public class SimpleCSItemDetails extends CSAbstractDetails {
49
50     private ISimpleCSItem fItem;
51     
52     private FormEntry fTitle;
53     
54     private Button fSkip;
55     
56     private CSSourceViewer fContentViewer;
57
58     private Section fMainSection;
59
60     private SimpleCSHelpDetails fHelpSection;
61     
62     private SimpleCSCommandDetails fCommandSection;
63
64     private ControlDecoration fSkipInfoDecoration;
65     
66     private boolean fBlockEvents;
67     
68     /**
69      * @param section
70      */

71     public SimpleCSItemDetails(ICSMaster section) {
72         super(section, SimpleCSInputContext.CONTEXT_ID);
73         fItem = null;
74         
75         fTitle = null;
76         fSkip = null;
77         fSkipInfoDecoration = null;
78         fContentViewer = null;
79         fMainSection = null;
80         fBlockEvents = false;
81         
82         fHelpSection = new SimpleCSHelpDetails(section);
83         fCommandSection = new SimpleCSCommandDetails(section);
84     }
85     
86     /* (non-Javadoc)
87      * @see org.eclipse.ui.forms.AbstractFormPart#initialize(org.eclipse.ui.forms.IManagedForm)
88      */

89     public void initialize(IManagedForm form) {
90         super.initialize(form);
91         // Unfortunately this has to be explicitly called for sub detail
92
// sections through its main section parent; since, it never is
93
// registered directly.
94
// Initialize managed form for help section
95
fHelpSection.initialize(form);
96         // Initialized managed form for command section
97
fCommandSection.initialize(form);
98     }
99     
100     /* (non-Javadoc)
101      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSAbstractDetails#createDetails(org.eclipse.swt.widgets.Composite)
102      */

103     public void createDetails(Composite parent) {
104
105         Color foreground = getToolkit().getColors().getColor(IFormColors.TITLE);
106         GridData data = null;
107         
108         // Create main section
109
fMainSection = getToolkit().createSection(parent,
110                 Section.DESCRIPTION | ExpandableComposite.TITLE_BAR);
111         fMainSection.clientVerticalSpacing = FormLayoutFactory.SECTION_HEADER_VERTICAL_SPACING;
112         fMainSection.setText(PDEUIMessages.SimpleCSItemDetails_11);
113         fMainSection.setDescription(PDEUIMessages.SimpleCSItemDetails_12);
114         fMainSection.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
115         data = new GridData(GridData.FILL_HORIZONTAL);
116         fMainSection.setLayoutData(data);
117         
118         // Align the master and details section headers (misalignment caused
119
// by section toolbar icons)
120
getPage().alignSectionHeaders(getMasterSection().getSection(),
121                 fMainSection);
122         
123         // Create container for main section
124
Composite mainSectionClient = getToolkit().createComposite(fMainSection);
125         mainSectionClient.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
126
127         // Attribute: title
128
fTitle = new FormEntry(mainSectionClient, getToolkit(), PDEUIMessages.SimpleCSItemDetails_0, SWT.NONE);
129
130         // description: Content (Element)
131
createUIFieldContent(mainSectionClient);
132
133         // Attribute: skip
134
fSkip = getToolkit().createButton(mainSectionClient, PDEUIMessages.SimpleCSItemDetails_14, SWT.CHECK);
135         data = new GridData(GridData.FILL_HORIZONTAL);
136         data.horizontalSpan = 2;
137         fSkip.setLayoutData(data);
138         fSkip.setForeground(foreground);
139         createSkipInfoDecoration();
140         // Bind widgets
141
getToolkit().paintBordersFor(mainSectionClient);
142         fMainSection.setClient(mainSectionClient);
143         markDetailsPart(fMainSection);
144         
145         fCommandSection.createDetails(parent);
146         
147         fHelpSection.createDetails(parent);
148     }
149
150     /* (non-Javadoc)
151      * @see org.eclipse.pde.internal.ui.editor.PDEDetails#doGlobalAction(java.lang.String)
152      */

153     public boolean doGlobalAction(String JavaDoc actionId) {
154         return fContentViewer.doGlobalAction(actionId);
155     }
156     
157     /**
158      * @param parent
159      */

160     private void createUIFieldContent(Composite parent) {
161         GridData data = null;
162         // Create the label
163
Color foreground = getToolkit().getColors().getColor(IFormColors.TITLE);
164         Label label =
165             getToolkit().createLabel(
166                     parent,
167                     PDEUIMessages.SimpleCSDescriptionDetails_0,
168                     SWT.WRAP);
169         label.setForeground(foreground);
170         int style = GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_END;
171         data = new GridData(style);
172         label.setLayoutData(data);
173         // Create the source viewer
174
fContentViewer = new CSSourceViewer(getPage());
175         fContentViewer.createUI(parent, 90, 60);
176         // Needed to align vertically with form entry field and allow space
177
// for a possible field decoration
178
((GridData)fContentViewer.getViewer().getTextWidget().getLayoutData()).horizontalIndent =
179             FormLayoutFactory.CONTROL_HORIZONTAL_INDENT;
180     }
181     
182     /**
183      *
184      */

185     private void createSkipInfoDecoration() {
186         // Skip info decoration
187
int bits = SWT.TOP | SWT.LEFT;
188         fSkipInfoDecoration = new ControlDecoration(fSkip, bits);
189         fSkipInfoDecoration.setMarginWidth(1);
190         fSkipInfoDecoration.setDescriptionText(PDEUIMessages.SimpleCSItemDetails_msgFieldDisabledOptional);
191         updateSkipInfoDecoration(false);
192         fSkipInfoDecoration.setImage(
193             FieldDecorationRegistry.getDefault().getFieldDecoration(
194                 FieldDecorationRegistry.DEC_INFORMATION).getImage());
195     }
196     
197     /* (non-Javadoc)
198      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSAbstractDetails#hookListeners()
199      */

200     public void hookListeners() {
201         // description: Content (Element)
202
createUIListenersContentViewer();
203         // Attribute: title
204
fTitle.setFormEntryListener(new FormEntryAdapter(this) {
205             public void textValueChanged(FormEntry entry) {
206                 // Ensure data object is defined
207
if (fItem == null) {
208                     return;
209                 }
210                 fItem.setTitle(fTitle.getValue());
211             }
212         });
213         // Attribute: skip
214
fSkip.addSelectionListener(new SelectionAdapter() {
215             public void widgetSelected(SelectionEvent e) {
216                 // Ensure data object is defined
217
if (fItem == null) {
218                     return;
219                 }
220                 fItem.setSkip(fSkip.getSelection());
221                 getMasterSection().updateButtons();
222             }
223         });
224
225         fHelpSection.hookListeners();
226         
227         fCommandSection.hookListeners();
228     }
229     
230     /**
231      *
232      */

233     private void createUIListenersContentViewer() {
234         fContentViewer.createUIListeners();
235         // Create document listener
236
fContentViewer.getDocument().addDocumentListener(new IDocumentListener() {
237             public void documentAboutToBeChanged(DocumentEvent event) {
238                 // NO-OP
239
}
240             public void documentChanged(DocumentEvent event) {
241                 // Check whether to handle this event
242
if (fBlockEvents) {
243                     return;
244                 }
245                 // Ensure data object is defined
246
if (fItem == null) {
247                     return;
248                 }
249                 // Get the text from the event
250
IDocument document = event.getDocument();
251                 if (document == null) {
252                     return;
253                 }
254                 // Get the text from the event
255
String JavaDoc text = document.get().trim();
256                 
257                 if (fItem.getDescription() != null) {
258                     fItem.getDescription().setContent(text);
259                 }
260             }
261         });
262     }
263     
264     /* (non-Javadoc)
265      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSAbstractDetails#updateFields()
266      */

267     public void updateFields() {
268
269         boolean editable = isEditableElement();
270         // Ensure data object is defined
271
if (fItem == null) {
272             return;
273         }
274         // Attribute: title
275
fTitle.setValue(fItem.getTitle(), true);
276         fTitle.setEditable(editable);
277
278         // Attribute: skip
279
fSkip.setSelection(fItem.getSkip());
280         updateSkipEnablement();
281         // TODO: MP: SimpleCS: Revist all parameters and check we are simply looking for null - okay for non-String types
282
// TODO: MP: SimpleCS: Reevaluate write methods and make sure not writing empty string
283

284         fHelpSection.updateFields();
285
286         fCommandSection.updateFields();
287         
288         if (fItem.getDescription() == null) {
289             return;
290         }
291
292         // description: Content (Element)
293
fBlockEvents = true;
294         fContentViewer.getDocument().set(fItem.getDescription().getContent());
295         fBlockEvents = false;
296         fContentViewer.getViewer().setEditable(editable);
297     }
298
299     /* (non-Javadoc)
300      * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
301      */

302     public void dispose() {
303         // Set the context menu to null to prevent the editor context menu
304
// from being disposed along with the source viewer
305
if (fContentViewer != null) {
306             fContentViewer.unsetMenu();
307             fContentViewer = null;
308         }
309         
310         super.dispose();
311     }
312     
313     /* (non-Javadoc)
314      * @see org.eclipse.pde.internal.ui.editor.PDEDetails#canPaste(org.eclipse.swt.dnd.Clipboard)
315      */

316     public boolean canPaste(Clipboard clipboard) {
317         return fContentViewer.canPaste();
318     }
319     
320     /**
321      *
322      */

323     private void updateSkipEnablement() {
324         // Ensure data object is defined
325
if (fItem == null) {
326             return;
327         }
328         boolean editable = isEditableElement();
329         // Preserve cheat sheet validity
330
// Semantic Rule: Specifying whether an item can be skipped or not has
331
// no effect when subitems are present (because the item delegates the
332
// control to the subitem to skip).
333
if (fItem.hasSubItems()) {
334             editable = false;
335             updateSkipInfoDecoration(true);
336         } else {
337             updateSkipInfoDecoration(false);
338         }
339         fSkip.setEnabled(editable);
340     }
341     
342     /**
343      * @param show
344      */

345     private void updateSkipInfoDecoration(boolean show) {
346         if (show) {
347             fSkipInfoDecoration.show();
348         } else {
349             fSkipInfoDecoration.hide();
350         }
351         fSkipInfoDecoration.setShowHover(show);
352     }
353     
354     /* (non-Javadoc)
355      * @see org.eclipse.ui.forms.AbstractFormPart#commit(boolean)
356      */

357     public void commit(boolean onSave) {
358         super.commit(onSave);
359         // Only required for form entries
360
fTitle.commit();
361         // No need to call for sub details, because they contain no form entries
362
}
363     
364     /* (non-Javadoc)
365      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.CSAbstractDetails#selectionChanged(org.eclipse.ui.forms.IFormPart, org.eclipse.jface.viewers.ISelection)
366      */

367     public void selectionChanged(IFormPart part, ISelection selection) {
368         // Get the first selected object
369
Object JavaDoc object = getFirstSelectedObject(selection);
370         // Ensure we have the right type
371
if ((object == null) ||
372                 (object instanceof ISimpleCSItem) == false) {
373             return;
374         }
375         // Set data
376
setData((ISimpleCSItem)object);
377         // Update the UI given the new data
378
updateFields();
379     }
380     
381     /**
382      * @param object
383      */

384     public void setData(ISimpleCSItem object) {
385         // Set data
386
fItem = object;
387         // Set data on commands section
388
fCommandSection.setData(object);
389         // Set data on help section
390
fHelpSection.setData(object);
391     }
392 }
393
Popular Tags