KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.DocumentEvent;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.IDocumentListener;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSIntro;
19 import org.eclipse.pde.internal.ui.PDEUIMessages;
20 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
21 import org.eclipse.pde.internal.ui.editor.cheatsheet.CSAbstractDetails;
22 import org.eclipse.pde.internal.ui.editor.cheatsheet.CSSourceViewer;
23 import org.eclipse.pde.internal.ui.editor.cheatsheet.ICSMaster;
24 import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSInputContext;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.dnd.Clipboard;
27 import org.eclipse.swt.graphics.Color;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.ui.forms.IFormColors;
32 import org.eclipse.ui.forms.IFormPart;
33 import org.eclipse.ui.forms.IManagedForm;
34 import org.eclipse.ui.forms.widgets.ExpandableComposite;
35 import org.eclipse.ui.forms.widgets.Section;
36
37 /**
38  * SimpleCSIntroDetails
39  *
40  */

41 public class SimpleCSIntroDetails extends CSAbstractDetails {
42
43     private ISimpleCSIntro fIntro;
44     
45     private CSSourceViewer fContentViewer;
46     
47     private Section fMainSection;
48     
49     private SimpleCSHelpDetails fHelpSection;
50     
51     private boolean fBlockEvents;
52     
53     /**
54      * @param elementSection
55      */

56     public SimpleCSIntroDetails(ICSMaster elementSection) {
57         super(elementSection, SimpleCSInputContext.CONTEXT_ID);
58         fIntro = null;
59         
60         fContentViewer = null;
61         fMainSection = null;
62         fHelpSection = new SimpleCSHelpDetails(elementSection);
63         fBlockEvents = false;
64     }
65
66     /**
67      * @param object
68      */

69     public void setData(ISimpleCSIntro object) {
70         // Set data
71
fIntro = object;
72         // Set data on help section
73
fHelpSection.setData(object);
74     }
75     
76     /* (non-Javadoc)
77      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.CSAbstractDetails#selectionChanged(org.eclipse.ui.forms.IFormPart, org.eclipse.jface.viewers.ISelection)
78      */

79     public void selectionChanged(IFormPart part, ISelection selection) {
80         // Get the first selected object
81
Object JavaDoc object = getFirstSelectedObject(selection);
82         // Ensure we have the right type
83
if ((object == null) ||
84                 (object instanceof ISimpleCSIntro) == false) {
85             return;
86         }
87         // Set data
88
setData((ISimpleCSIntro)object);
89         // Update the UI given the new data
90
updateFields();
91     }
92     
93     /* (non-Javadoc)
94      * @see org.eclipse.ui.forms.AbstractFormPart#initialize(org.eclipse.ui.forms.IManagedForm)
95      */

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

108     public void createDetails(Composite parent) {
109
110         GridData data = null;
111         
112         // Create main section
113
fMainSection = getToolkit().createSection(parent, Section.DESCRIPTION | ExpandableComposite.TITLE_BAR);
114         fMainSection.clientVerticalSpacing = FormLayoutFactory.SECTION_HEADER_VERTICAL_SPACING;
115         fMainSection.setText(PDEUIMessages.SimpleCSIntroDetails_2);
116         fMainSection.setDescription(PDEUIMessages.SimpleCSIntroDetails_3);
117         fMainSection.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
118         data = new GridData(GridData.FILL_HORIZONTAL);
119         fMainSection.setLayoutData(data);
120         
121         // Align the master and details section headers (misalignment caused
122
// by section toolbar icons)
123
getPage().alignSectionHeaders(getMasterSection().getSection(),
124                 fMainSection);
125         
126         // Create container for main section
127
Composite mainSectionClient = getToolkit().createComposite(fMainSection);
128         mainSectionClient.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
129     
130         // description: Content (Element)
131
createUIFieldContent(mainSectionClient);
132         
133         // Bind widgets
134
getToolkit().paintBordersFor(mainSectionClient);
135         fMainSection.setClient(mainSectionClient);
136         markDetailsPart(fMainSection);
137         
138         fHelpSection.createDetails(parent);
139     }
140
141     /**
142      * @param parent
143      */

144     private void createUIFieldContent(Composite parent) {
145         GridData data = null;
146         // Create the label
147
Color foreground = getToolkit().getColors().getColor(IFormColors.TITLE);
148         Label label =
149             getToolkit().createLabel(
150                     parent,
151                     PDEUIMessages.SimpleCSDescriptionDetails_0,
152                     SWT.WRAP);
153         label.setForeground(foreground);
154         int style = GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_END;
155         data = new GridData(style);
156         label.setLayoutData(data);
157         // Create the source viewer
158
fContentViewer = new CSSourceViewer(getPage());
159         fContentViewer.createUI(parent, 90, 60);
160         // Needed to align vertically with form entry field and allow space
161
// for a possible field decoration
162
((GridData)fContentViewer.getViewer().getTextWidget().getLayoutData()).horizontalIndent =
163             FormLayoutFactory.CONTROL_HORIZONTAL_INDENT;
164     }
165     
166     /* (non-Javadoc)
167      * @see org.eclipse.pde.internal.ui.editor.PDEDetails#doGlobalAction(java.lang.String)
168      */

169     public boolean doGlobalAction(String JavaDoc actionId) {
170         return fContentViewer.doGlobalAction(actionId);
171     }
172     
173     /* (non-Javadoc)
174      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSAbstractDetails#hookListeners()
175      */

176     public void hookListeners() {
177         // description: Content (Element)
178
createUIListenersContentViewer();
179         fHelpSection.hookListeners();
180     }
181
182     /**
183      *
184      */

185     private void createUIListenersContentViewer() {
186         fContentViewer.createUIListeners();
187         // Create document listener
188
fContentViewer.getDocument().addDocumentListener(new IDocumentListener() {
189             public void documentAboutToBeChanged(DocumentEvent event) {
190                 // NO-OP
191
}
192             public void documentChanged(DocumentEvent event) {
193                 // Check whether to handle this event
194
if (fBlockEvents) {
195                     return;
196                 }
197                 // Ensure data object is defined
198
if (fIntro == null) {
199                     return;
200                 }
201                 // Get the text from the event
202
IDocument document = event.getDocument();
203                 if (document == null) {
204                     return;
205                 }
206                 // Get the text from the event
207
String JavaDoc text = document.get().trim();
208                 
209                 if (fIntro.getDescription() != null) {
210                     fIntro.getDescription().setContent(text);
211                 }
212             }
213         });
214     }
215     
216     /* (non-Javadoc)
217      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSAbstractDetails#updateFields()
218      */

219     public void updateFields() {
220         // Ensure data object is defined
221
if (fIntro == null) {
222             return;
223         }
224         
225         fHelpSection.updateFields();
226         
227         if (fIntro.getDescription() == null) {
228             return;
229         }
230
231         // description: Content (Element)
232
fBlockEvents = true;
233         fContentViewer.getDocument().set(fIntro.getDescription().getContent());
234         fBlockEvents = false;
235         
236         boolean editable = isEditableElement();
237         fContentViewer.getViewer().setEditable(editable);
238     }
239     
240     /* (non-Javadoc)
241      * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
242      */

243     public void dispose() {
244         // Set the context menu to null to prevent the editor context menu
245
// from being disposed along with the source viewer
246
if (fContentViewer != null) {
247             fContentViewer.unsetMenu();
248             fContentViewer = null;
249         }
250         
251         super.dispose();
252     }
253     
254     /* (non-Javadoc)
255      * @see org.eclipse.pde.internal.ui.editor.PDEDetails#canPaste(org.eclipse.swt.dnd.Clipboard)
256      */

257     public boolean canPaste(Clipboard clipboard) {
258         return fContentViewer.canPaste();
259     }
260     
261     /* (non-Javadoc)
262      * @see org.eclipse.ui.forms.AbstractFormPart#commit(boolean)
263      */

264     public void commit(boolean onSave) {
265         super.commit(onSave);
266         // Only required for form entries
267
// No need to call for sub details, because they contain no form entries
268
}
269     
270 }
271
Popular Tags