001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.cheatsheet.simple.details;
011:
012: import org.eclipse.jface.fieldassist.ControlDecoration;
013: import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
014: import org.eclipse.jface.text.DocumentEvent;
015: import org.eclipse.jface.text.IDocument;
016: import org.eclipse.jface.text.IDocumentListener;
017: import org.eclipse.jface.viewers.ISelection;
018: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem;
019: import org.eclipse.pde.internal.ui.PDEUIMessages;
020: import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
021: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
022: import org.eclipse.pde.internal.ui.editor.cheatsheet.CSAbstractDetails;
023: import org.eclipse.pde.internal.ui.editor.cheatsheet.ICSMaster;
024: import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSInputContext;
025: import org.eclipse.pde.internal.ui.parts.PDESourceViewer;
026: import org.eclipse.pde.internal.ui.parts.FormEntry;
027: import org.eclipse.swt.SWT;
028: import org.eclipse.swt.dnd.Clipboard;
029: import org.eclipse.swt.events.SelectionAdapter;
030: import org.eclipse.swt.events.SelectionEvent;
031: import org.eclipse.swt.graphics.Color;
032: import org.eclipse.swt.layout.GridData;
033: import org.eclipse.swt.widgets.Button;
034: import org.eclipse.swt.widgets.Composite;
035: import org.eclipse.swt.widgets.Label;
036: import org.eclipse.ui.forms.IFormColors;
037: import org.eclipse.ui.forms.IFormPart;
038: import org.eclipse.ui.forms.IManagedForm;
039: import org.eclipse.ui.forms.widgets.ExpandableComposite;
040: import org.eclipse.ui.forms.widgets.Section;
041:
042: /**
043: * SimpleCSItemDetails
044: *
045: */
046: public class SimpleCSItemDetails extends CSAbstractDetails {
047:
048: private ISimpleCSItem fItem;
049:
050: private FormEntry fTitle;
051:
052: private Button fSkip;
053:
054: private PDESourceViewer fContentViewer;
055:
056: private Section fMainSection;
057:
058: private SimpleCSHelpDetails fHelpSection;
059:
060: private SimpleCSCommandDetails fCommandSection;
061:
062: private ControlDecoration fSkipInfoDecoration;
063:
064: private boolean fBlockEvents;
065:
066: /**
067: * @param section
068: */
069: public SimpleCSItemDetails(ICSMaster section) {
070: super (section, SimpleCSInputContext.CONTEXT_ID);
071: fItem = null;
072:
073: fTitle = null;
074: fSkip = null;
075: fSkipInfoDecoration = null;
076: fContentViewer = null;
077: fMainSection = null;
078: fBlockEvents = false;
079:
080: fHelpSection = new SimpleCSHelpDetails(section);
081: fCommandSection = new SimpleCSCommandDetails(section);
082: }
083:
084: /* (non-Javadoc)
085: * @see org.eclipse.ui.forms.AbstractFormPart#initialize(org.eclipse.ui.forms.IManagedForm)
086: */
087: public void initialize(IManagedForm form) {
088: super .initialize(form);
089: // Unfortunately this has to be explicitly called for sub detail
090: // sections through its main section parent; since, it never is
091: // registered directly.
092: // Initialize managed form for help section
093: fHelpSection.initialize(form);
094: // Initialized managed form for command section
095: fCommandSection.initialize(form);
096: }
097:
098: /* (non-Javadoc)
099: * @see org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSAbstractDetails#createDetails(org.eclipse.swt.widgets.Composite)
100: */
101: public void createDetails(Composite parent) {
102:
103: Color foreground = getToolkit().getColors().getColor(
104: IFormColors.TITLE);
105: GridData data = null;
106:
107: // Create main section
108: fMainSection = getToolkit().createSection(parent,
109: Section.DESCRIPTION | ExpandableComposite.TITLE_BAR);
110: fMainSection.clientVerticalSpacing = FormLayoutFactory.SECTION_HEADER_VERTICAL_SPACING;
111: fMainSection.setText(PDEUIMessages.SimpleCSItemDetails_11);
112: fMainSection
113: .setDescription(PDEUIMessages.SimpleCSItemDetails_12);
114: fMainSection.setLayout(FormLayoutFactory.createClearGridLayout(
115: false, 1));
116: data = new GridData(GridData.FILL_HORIZONTAL);
117: fMainSection.setLayoutData(data);
118:
119: // Align the master and details section headers (misalignment caused
120: // by section toolbar icons)
121: getPage().alignSectionHeaders(getMasterSection().getSection(),
122: fMainSection);
123:
124: // Create container for main section
125: Composite mainSectionClient = getToolkit().createComposite(
126: fMainSection);
127: mainSectionClient.setLayout(FormLayoutFactory
128: .createSectionClientGridLayout(false, 2));
129:
130: // Attribute: title
131: fTitle = new FormEntry(mainSectionClient, getToolkit(),
132: PDEUIMessages.SimpleCSItemDetails_0, SWT.NONE);
133:
134: // description: Content (Element)
135: createUIFieldContent(mainSectionClient);
136:
137: // Attribute: skip
138: fSkip = getToolkit().createButton(mainSectionClient,
139: PDEUIMessages.SimpleCSItemDetails_14, SWT.CHECK);
140: data = new GridData(GridData.FILL_HORIZONTAL);
141: data.horizontalSpan = 2;
142: fSkip.setLayoutData(data);
143: fSkip.setForeground(foreground);
144: createSkipInfoDecoration();
145: // Bind widgets
146: getToolkit().paintBordersFor(mainSectionClient);
147: fMainSection.setClient(mainSectionClient);
148: markDetailsPart(fMainSection);
149:
150: fCommandSection.createDetails(parent);
151:
152: fHelpSection.createDetails(parent);
153: }
154:
155: /* (non-Javadoc)
156: * @see org.eclipse.pde.internal.ui.editor.PDEDetails#doGlobalAction(java.lang.String)
157: */
158: public boolean doGlobalAction(String actionId) {
159: return fContentViewer.doGlobalAction(actionId);
160: }
161:
162: /**
163: * @param parent
164: */
165: private void createUIFieldContent(Composite parent) {
166: GridData data = null;
167: // Create the label
168: Color foreground = getToolkit().getColors().getColor(
169: IFormColors.TITLE);
170: Label label = getToolkit().createLabel(parent,
171: PDEUIMessages.SimpleCSDescriptionDetails_0, SWT.WRAP);
172: label.setForeground(foreground);
173: int style = GridData.VERTICAL_ALIGN_BEGINNING
174: | GridData.HORIZONTAL_ALIGN_END;
175: data = new GridData(style);
176: label.setLayoutData(data);
177: // Create the source viewer
178: fContentViewer = new PDESourceViewer(getPage());
179: fContentViewer.createUI(parent, 90, 60);
180: // Needed to align vertically with form entry field and allow space
181: // for a possible field decoration
182: ((GridData) fContentViewer.getViewer().getTextWidget()
183: .getLayoutData()).horizontalIndent = FormLayoutFactory.CONTROL_HORIZONTAL_INDENT;
184: }
185:
186: /**
187: *
188: */
189: private void createSkipInfoDecoration() {
190: // Skip info decoration
191: int bits = SWT.TOP | SWT.LEFT;
192: fSkipInfoDecoration = new ControlDecoration(fSkip, bits);
193: fSkipInfoDecoration.setMarginWidth(1);
194: fSkipInfoDecoration
195: .setDescriptionText(PDEUIMessages.SimpleCSItemDetails_msgFieldDisabledOptional);
196: updateSkipInfoDecoration(false);
197: fSkipInfoDecoration.setImage(FieldDecorationRegistry
198: .getDefault().getFieldDecoration(
199: FieldDecorationRegistry.DEC_INFORMATION)
200: .getImage());
201: }
202:
203: /* (non-Javadoc)
204: * @see org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSAbstractDetails#hookListeners()
205: */
206: public void hookListeners() {
207: // description: Content (Element)
208: createUIListenersContentViewer();
209: // Attribute: title
210: fTitle.setFormEntryListener(new FormEntryAdapter(this ) {
211: public void textValueChanged(FormEntry entry) {
212: // Ensure data object is defined
213: if (fItem == null) {
214: return;
215: }
216: fItem.setTitle(fTitle.getValue());
217: }
218: });
219: // Attribute: skip
220: fSkip.addSelectionListener(new SelectionAdapter() {
221: public void widgetSelected(SelectionEvent e) {
222: // Ensure data object is defined
223: if (fItem == null) {
224: return;
225: }
226: fItem.setSkip(fSkip.getSelection());
227: getMasterSection().updateButtons();
228: }
229: });
230:
231: fHelpSection.hookListeners();
232:
233: fCommandSection.hookListeners();
234: }
235:
236: /**
237: *
238: */
239: private void createUIListenersContentViewer() {
240: fContentViewer.createUIListeners();
241: // Create document listener
242: fContentViewer.getDocument().addDocumentListener(
243: new IDocumentListener() {
244: public void documentAboutToBeChanged(
245: DocumentEvent event) {
246: // NO-OP
247: }
248:
249: public void documentChanged(DocumentEvent event) {
250: // Check whether to handle this event
251: if (fBlockEvents) {
252: return;
253: }
254: // Ensure data object is defined
255: if (fItem == null) {
256: return;
257: }
258: // Get the text from the event
259: IDocument document = event.getDocument();
260: if (document == null) {
261: return;
262: }
263: // Get the text from the event
264: String text = document.get().trim();
265:
266: if (fItem.getDescription() != null) {
267: fItem.getDescription().setContent(text);
268: }
269: }
270: });
271: }
272:
273: /* (non-Javadoc)
274: * @see org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSAbstractDetails#updateFields()
275: */
276: public void updateFields() {
277:
278: boolean editable = isEditableElement();
279: // Ensure data object is defined
280: if (fItem == null) {
281: return;
282: }
283: // Attribute: title
284: fTitle.setValue(fItem.getTitle(), true);
285: fTitle.setEditable(editable);
286:
287: // Attribute: skip
288: fSkip.setSelection(fItem.getSkip());
289: updateSkipEnablement();
290: // TODO: MP: SimpleCS: Revist all parameters and check we are simply looking for null - okay for non-String types
291: // TODO: MP: SimpleCS: Reevaluate write methods and make sure not writing empty string
292:
293: fHelpSection.updateFields();
294:
295: fCommandSection.updateFields();
296:
297: if (fItem.getDescription() == null) {
298: return;
299: }
300:
301: // description: Content (Element)
302: fBlockEvents = true;
303: fContentViewer.getDocument().set(
304: fItem.getDescription().getContent());
305: fBlockEvents = false;
306: fContentViewer.getViewer().setEditable(editable);
307: }
308:
309: /* (non-Javadoc)
310: * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
311: */
312: public void dispose() {
313: // Set the context menu to null to prevent the editor context menu
314: // from being disposed along with the source viewer
315: if (fContentViewer != null) {
316: fContentViewer.unsetMenu();
317: fContentViewer = null;
318: }
319:
320: super .dispose();
321: }
322:
323: /* (non-Javadoc)
324: * @see org.eclipse.pde.internal.ui.editor.PDEDetails#canPaste(org.eclipse.swt.dnd.Clipboard)
325: */
326: public boolean canPaste(Clipboard clipboard) {
327: return fContentViewer.canPaste();
328: }
329:
330: /**
331: *
332: */
333: private void updateSkipEnablement() {
334: // Ensure data object is defined
335: if (fItem == null) {
336: return;
337: }
338: boolean editable = isEditableElement();
339: // Preserve cheat sheet validity
340: // Semantic Rule: Specifying whether an item can be skipped or not has
341: // no effect when subitems are present (because the item delegates the
342: // control to the subitem to skip).
343: if (fItem.hasSubItems()) {
344: editable = false;
345: updateSkipInfoDecoration(true);
346: } else {
347: updateSkipInfoDecoration(false);
348: }
349: fSkip.setEnabled(editable);
350: }
351:
352: /**
353: * @param show
354: */
355: private void updateSkipInfoDecoration(boolean show) {
356: if (show) {
357: fSkipInfoDecoration.show();
358: } else {
359: fSkipInfoDecoration.hide();
360: }
361: fSkipInfoDecoration.setShowHover(show);
362: }
363:
364: /* (non-Javadoc)
365: * @see org.eclipse.ui.forms.AbstractFormPart#commit(boolean)
366: */
367: public void commit(boolean onSave) {
368: super .commit(onSave);
369: // Only required for form entries
370: fTitle.commit();
371: // No need to call for sub details, because they contain no form entries
372: }
373:
374: /* (non-Javadoc)
375: * @see org.eclipse.pde.internal.ui.editor.cheatsheet.CSAbstractDetails#selectionChanged(org.eclipse.ui.forms.IFormPart, org.eclipse.jface.viewers.ISelection)
376: */
377: public void selectionChanged(IFormPart part, ISelection selection) {
378: // Get the first selected object
379: Object object = getFirstSelectedObject(selection);
380: // Ensure we have the right type
381: if ((object == null)
382: || (object instanceof ISimpleCSItem) == false) {
383: return;
384: }
385: // Set data
386: setData((ISimpleCSItem) object);
387: // Update the UI given the new data
388: updateFields();
389: }
390:
391: /**
392: * @param object
393: */
394: public void setData(ISimpleCSItem object) {
395: // Set data
396: fItem = object;
397: // Set data on commands section
398: fCommandSection.setData(object);
399: // Set data on help section
400: fHelpSection.setData(object);
401: }
402: }
|