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.text.DocumentEvent;
013: import org.eclipse.jface.text.IDocument;
014: import org.eclipse.jface.text.IDocumentListener;
015: import org.eclipse.jface.viewers.ISelection;
016: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSIntro;
017: import org.eclipse.pde.internal.ui.PDEUIMessages;
018: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
019: import org.eclipse.pde.internal.ui.editor.cheatsheet.CSAbstractDetails;
020: import org.eclipse.pde.internal.ui.editor.cheatsheet.ICSMaster;
021: import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSInputContext;
022: import org.eclipse.pde.internal.ui.parts.PDESourceViewer;
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.dnd.Clipboard;
025: import org.eclipse.swt.graphics.Color;
026: import org.eclipse.swt.layout.GridData;
027: import org.eclipse.swt.widgets.Composite;
028: import org.eclipse.swt.widgets.Label;
029: import org.eclipse.ui.forms.IFormColors;
030: import org.eclipse.ui.forms.IFormPart;
031: import org.eclipse.ui.forms.IManagedForm;
032: import org.eclipse.ui.forms.widgets.ExpandableComposite;
033: import org.eclipse.ui.forms.widgets.Section;
034:
035: /**
036: * SimpleCSIntroDetails
037: *
038: */
039: public class SimpleCSIntroDetails extends CSAbstractDetails {
040:
041: private ISimpleCSIntro fIntro;
042:
043: private PDESourceViewer fContentViewer;
044:
045: private Section fMainSection;
046:
047: private SimpleCSHelpDetails fHelpSection;
048:
049: private boolean fBlockEvents;
050:
051: /**
052: * @param elementSection
053: */
054: public SimpleCSIntroDetails(ICSMaster elementSection) {
055: super (elementSection, SimpleCSInputContext.CONTEXT_ID);
056: fIntro = null;
057:
058: fContentViewer = null;
059: fMainSection = null;
060: fHelpSection = new SimpleCSHelpDetails(elementSection);
061: fBlockEvents = false;
062: }
063:
064: /**
065: * @param object
066: */
067: public void setData(ISimpleCSIntro object) {
068: // Set data
069: fIntro = object;
070: // Set data on help section
071: fHelpSection.setData(object);
072: }
073:
074: /* (non-Javadoc)
075: * @see org.eclipse.pde.internal.ui.editor.cheatsheet.CSAbstractDetails#selectionChanged(org.eclipse.ui.forms.IFormPart, org.eclipse.jface.viewers.ISelection)
076: */
077: public void selectionChanged(IFormPart part, ISelection selection) {
078: // Get the first selected object
079: Object object = getFirstSelectedObject(selection);
080: // Ensure we have the right type
081: if ((object == null)
082: || (object instanceof ISimpleCSIntro) == false) {
083: return;
084: }
085: // Set data
086: setData((ISimpleCSIntro) object);
087: // Update the UI given the new data
088: updateFields();
089: }
090:
091: /* (non-Javadoc)
092: * @see org.eclipse.ui.forms.AbstractFormPart#initialize(org.eclipse.ui.forms.IManagedForm)
093: */
094: public void initialize(IManagedForm form) {
095: super .initialize(form);
096: // Unfortunately this has to be explicitly called for sub detail
097: // sections through its main section parent; since, it never is
098: // registered directly.
099: // Initialize managed form for help section
100: fHelpSection.initialize(form);
101: }
102:
103: /* (non-Javadoc)
104: * @see org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSAbstractDetails#createDetails(org.eclipse.swt.widgets.Composite)
105: */
106: public void createDetails(Composite parent) {
107:
108: GridData data = null;
109:
110: // Create main section
111: fMainSection = getToolkit().createSection(parent,
112: Section.DESCRIPTION | ExpandableComposite.TITLE_BAR);
113: fMainSection.clientVerticalSpacing = FormLayoutFactory.SECTION_HEADER_VERTICAL_SPACING;
114: fMainSection.setText(PDEUIMessages.SimpleCSIntroDetails_2);
115: fMainSection
116: .setDescription(PDEUIMessages.SimpleCSIntroDetails_3);
117: fMainSection.setLayout(FormLayoutFactory.createClearGridLayout(
118: false, 1));
119: data = new GridData(GridData.FILL_HORIZONTAL);
120: fMainSection.setLayoutData(data);
121:
122: // Align the master and details section headers (misalignment caused
123: // by section toolbar icons)
124: getPage().alignSectionHeaders(getMasterSection().getSection(),
125: fMainSection);
126:
127: // Create container for main section
128: Composite mainSectionClient = getToolkit().createComposite(
129: fMainSection);
130: mainSectionClient.setLayout(FormLayoutFactory
131: .createSectionClientGridLayout(false, 2));
132:
133: // description: Content (Element)
134: createUIFieldContent(mainSectionClient);
135:
136: // Bind widgets
137: getToolkit().paintBordersFor(mainSectionClient);
138: fMainSection.setClient(mainSectionClient);
139: markDetailsPart(fMainSection);
140:
141: fHelpSection.createDetails(parent);
142: }
143:
144: /**
145: * @param parent
146: */
147: private void createUIFieldContent(Composite parent) {
148: GridData data = null;
149: // Create the label
150: Color foreground = getToolkit().getColors().getColor(
151: IFormColors.TITLE);
152: Label label = getToolkit().createLabel(parent,
153: PDEUIMessages.SimpleCSDescriptionDetails_0, SWT.WRAP);
154: label.setForeground(foreground);
155: int style = GridData.VERTICAL_ALIGN_BEGINNING
156: | GridData.HORIZONTAL_ALIGN_END;
157: data = new GridData(style);
158: label.setLayoutData(data);
159: // Create the source viewer
160: fContentViewer = new PDESourceViewer(getPage());
161: fContentViewer.createUI(parent, 90, 60);
162: // Needed to align vertically with form entry field and allow space
163: // for a possible field decoration
164: ((GridData) fContentViewer.getViewer().getTextWidget()
165: .getLayoutData()).horizontalIndent = FormLayoutFactory.CONTROL_HORIZONTAL_INDENT;
166: }
167:
168: /* (non-Javadoc)
169: * @see org.eclipse.pde.internal.ui.editor.PDEDetails#doGlobalAction(java.lang.String)
170: */
171: public boolean doGlobalAction(String actionId) {
172: return fContentViewer.doGlobalAction(actionId);
173: }
174:
175: /* (non-Javadoc)
176: * @see org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSAbstractDetails#hookListeners()
177: */
178: public void hookListeners() {
179: // description: Content (Element)
180: createUIListenersContentViewer();
181: fHelpSection.hookListeners();
182: }
183:
184: /**
185: *
186: */
187: private void createUIListenersContentViewer() {
188: fContentViewer.createUIListeners();
189: // Create document listener
190: fContentViewer.getDocument().addDocumentListener(
191: new IDocumentListener() {
192: public void documentAboutToBeChanged(
193: DocumentEvent event) {
194: // NO-OP
195: }
196:
197: public void documentChanged(DocumentEvent event) {
198: // Check whether to handle this event
199: if (fBlockEvents) {
200: return;
201: }
202: // Ensure data object is defined
203: if (fIntro == null) {
204: return;
205: }
206: // Get the text from the event
207: IDocument document = event.getDocument();
208: if (document == null) {
209: return;
210: }
211: // Get the text from the event
212: String text = document.get().trim();
213:
214: if (fIntro.getDescription() != null) {
215: fIntro.getDescription().setContent(text);
216: }
217: }
218: });
219: }
220:
221: /* (non-Javadoc)
222: * @see org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSAbstractDetails#updateFields()
223: */
224: public void updateFields() {
225: // Ensure data object is defined
226: if (fIntro == null) {
227: return;
228: }
229:
230: fHelpSection.updateFields();
231:
232: if (fIntro.getDescription() == null) {
233: return;
234: }
235:
236: // description: Content (Element)
237: fBlockEvents = true;
238: fContentViewer.getDocument().set(
239: fIntro.getDescription().getContent());
240: fBlockEvents = false;
241:
242: boolean editable = isEditableElement();
243: fContentViewer.getViewer().setEditable(editable);
244: }
245:
246: /* (non-Javadoc)
247: * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
248: */
249: public void dispose() {
250: // Set the context menu to null to prevent the editor context menu
251: // from being disposed along with the source viewer
252: if (fContentViewer != null) {
253: fContentViewer.unsetMenu();
254: fContentViewer = null;
255: }
256:
257: super .dispose();
258: }
259:
260: /* (non-Javadoc)
261: * @see org.eclipse.pde.internal.ui.editor.PDEDetails#canPaste(org.eclipse.swt.dnd.Clipboard)
262: */
263: public boolean canPaste(Clipboard clipboard) {
264: return fContentViewer.canPaste();
265: }
266:
267: /* (non-Javadoc)
268: * @see org.eclipse.ui.forms.AbstractFormPart#commit(boolean)
269: */
270: public void commit(boolean onSave) {
271: super .commit(onSave);
272: // Only required for form entries
273: // No need to call for sub details, because they contain no form entries
274: }
275:
276: }
|