KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > cheatsheet > comp > details > CompCSEnclosingTextDetails


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.comp.details;
13
14 import org.eclipse.osgi.util.NLS;
15 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSConstants;
16 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSIntro;
17 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSOnCompletion;
18 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject;
19 import org.eclipse.pde.internal.core.util.PDETextHelper;
20 import org.eclipse.pde.internal.ui.PDEPlugin;
21 import org.eclipse.pde.internal.ui.PDEPluginImages;
22 import org.eclipse.pde.internal.ui.PDEUIMessages;
23 import org.eclipse.pde.internal.ui.editor.cheatsheet.CSAbstractSubDetails;
24 import org.eclipse.pde.internal.ui.editor.cheatsheet.CSSourceViewer;
25 import org.eclipse.pde.internal.ui.editor.cheatsheet.ICSMaster;
26 import org.eclipse.pde.internal.ui.editor.cheatsheet.comp.CompCSInputContext;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.custom.CTabFolder;
29 import org.eclipse.swt.custom.CTabItem;
30 import org.eclipse.swt.custom.StackLayout;
31 import org.eclipse.swt.dnd.Clipboard;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.graphics.Color;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.ui.forms.FormColors;
41 import org.eclipse.ui.forms.IFormColors;
42 import org.eclipse.ui.forms.widgets.ExpandableComposite;
43 import org.eclipse.ui.forms.widgets.Section;
44
45 /**
46  * CompCSEnclosingTextDetails
47  *
48  */

49 public class CompCSEnclosingTextDetails extends CSAbstractSubDetails {
50
51     private ICompCSTaskObject fDataTaskObject;
52     
53     private Section fEnclosingTextSection;
54     
55     private CSSourceViewer fIntroductionViewer;
56
57     private CSSourceViewer fConclusionViewer;
58     
59     private CTabFolder fTabFolder;
60     
61     private final static int F_INTRODUCTION_TAB = 0;
62
63     private final static int F_CONCLUSION_TAB = 1;
64
65     private final static int F_NO_TAB = -1;
66     
67     private Composite fNotebookComposite;
68     
69     private StackLayout fNotebookLayout;
70     
71     private Composite fIntroductionComposite;
72     
73     private Composite fConclusionComposite;
74     
75     private String JavaDoc fTaskObjectLabelName;
76     
77     private CompCSIntroductionTextListener fIntroductionListener;
78     
79     private CompCSConclusionTextListener fConclusionListener;
80     
81     /**
82      *
83      */

84     public CompCSEnclosingTextDetails(int type,
85             ICSMaster section) {
86         super(section, CompCSInputContext.CONTEXT_ID);
87         
88         fDataTaskObject = null;
89         
90         fEnclosingTextSection = null;
91         
92         fIntroductionViewer = null;
93         fConclusionViewer = null;
94         
95         fTabFolder = null;
96         fNotebookComposite = null;
97         fNotebookLayout = null;
98         
99         fIntroductionComposite = null;
100         fConclusionComposite = null;
101         
102         fIntroductionListener = new CompCSIntroductionTextListener();
103         fConclusionListener = new CompCSConclusionTextListener();
104         
105         defineTaskObjectLabelName(type);
106     }
107
108     /**
109      * @param object
110      */

111     public void setData(ICompCSTaskObject object) {
112         // Set data
113
fDataTaskObject = object;
114         // Set data on introduction text listener
115
fIntroductionListener.setData(object);
116         // Set data on conclusion text listener
117
fConclusionListener.setData(object);
118     }
119
120     /* (non-Javadoc)
121      * @see org.eclipse.ui.forms.AbstractFormPart#commit(boolean)
122      */

123     public void commit(boolean onSave) {
124         super.commit(onSave);
125         // Only required for form entries
126
}
127     
128     /**
129      *
130      */

131     private void defineTaskObjectLabelName(int type) {
132         if (type == ICompCSConstants.TYPE_TASK) {
133             fTaskObjectLabelName = PDEUIMessages.CompCSDependenciesDetails_task;
134         } else {
135             fTaskObjectLabelName = PDEUIMessages.CompCSDependenciesDetails_group;
136         }
137     }
138     
139     /* (non-Javadoc)
140      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.ICSDetails#createDetails(org.eclipse.swt.widgets.Composite)
141      */

142     public void createDetails(Composite parent) {
143         // Create the main section
144
int style = Section.DESCRIPTION | ExpandableComposite.TITLE_BAR;
145         String JavaDoc description = NLS.bind(
146                 PDEUIMessages.CompCSEnclosingTextDetails_SectionDescription,
147                 fTaskObjectLabelName);
148         fEnclosingTextSection = getPage().createUISection(parent,
149                 PDEUIMessages.CompCSEnclosingTextDetails_EnclosingText,
150                 description, style);
151         // Configure the section
152
// The source viewers get clipped when the label above it wraps.
153
// Prevent this by making the section fill vertically in addition to
154
// horizontally
155
GridData data = new GridData(GridData.FILL_BOTH);
156         fEnclosingTextSection.setLayoutData(data);
157         // Create the container for the main section
158
Composite sectionClient = getPage().createUISectionContainer(
159                 fEnclosingTextSection, 1);
160         // Create the tab folder
161
createUITabFolder(sectionClient);
162         // Create the introduction folder tab
163
createUIIntroductionTab();
164         // Create the conclusion folder tab
165
createUIConclusionTab();
166         // Create the notebook composite
167
createUINotebookComposite(sectionClient);
168         // Create the introduction text
169
createUIIntroductionViewer();
170         // Create the conclusion text
171
createUIConclusionViewer();
172         // Bind widgets
173
getToolkit().paintBordersFor(sectionClient);
174         fEnclosingTextSection.setClient(sectionClient);
175         // Mark as a details part to enable cut, copy, paste, etc.
176
markDetailsPart(fEnclosingTextSection);
177     }
178
179     /**
180      * @param parent
181      */

182     private void createUITabFolder(Composite parent) {
183         fTabFolder = new CTabFolder(parent, SWT.FLAT | SWT.TOP);
184         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
185         data.heightHint = 2;
186         fTabFolder.setLayoutData(data);
187         
188         getToolkit().adapt(fTabFolder, true, true);
189         
190         FormColors colors = getToolkit().getColors();
191         colors.initializeSectionToolBarColors();
192         Color selectedColor = colors.getColor(IFormColors.TB_BG);
193         fTabFolder.setSelectionBackground(new Color[] { selectedColor,
194                 colors.getBackground() },
195                 new int[] { 100 }, true);
196     }
197
198     /**
199      *
200      */

201     private void createUIIntroductionTab() {
202         CTabItem item = new CTabItem(fTabFolder, SWT.NULL);
203         item.setText(PDEUIMessages.CompCSEnclosingTextDetails_Introduction);
204         item.setImage(PDEPlugin.getDefault().getLabelProvider().get(
205                 PDEPluginImages.DESC_CSINTRO_OBJ));
206     }
207
208     /**
209      *
210      */

211     private void createUIConclusionTab() {
212         CTabItem item = new CTabItem(fTabFolder, SWT.NULL);
213         item.setText(PDEUIMessages.CompCSEnclosingTextDetails_Conclusion);
214         item.setImage(PDEPlugin.getDefault().getLabelProvider().get(
215                 PDEPluginImages.DESC_CSCONCLUSION_OBJ));
216     }
217     
218     /**
219      * @param parent
220      */

221     private void createUINotebookComposite(Composite parent) {
222         fNotebookComposite = getToolkit().createComposite(parent);
223         GridData data = new GridData(GridData.FILL_HORIZONTAL);
224         fNotebookComposite.setLayoutData(data);
225         fNotebookLayout = new StackLayout();
226         fNotebookComposite.setLayout(fNotebookLayout);
227     }
228     
229     /**
230      *
231      */

232     private void createUIIntroductionViewer() {
233         // Create composite
234
fIntroductionComposite = createUIContainer(fNotebookComposite, 1);
235         // Create label
236
String JavaDoc description = NLS.bind(
237                 PDEUIMessages.CompCSEnclosingTextDetails_IntroductionDescription,
238                 fTaskObjectLabelName);
239         final Label label = getToolkit().createLabel(
240                 fIntroductionComposite, description, SWT.WRAP);
241         GridData data = new GridData(GridData.FILL_HORIZONTAL);
242         label.setLayoutData(data);
243         // Create the source viewer
244
fIntroductionViewer = new CSSourceViewer(getPage());
245         fIntroductionViewer.createUI(fIntroductionComposite, 60, 60);
246         // Note: Must paint border for parent composite; otherwise, the border
247
// goes missing on the text widget when using the Windows XP Classic
248
// theme
249
getToolkit().paintBordersFor(fIntroductionComposite);
250     }
251     
252     /* (non-Javadoc)
253      * @see org.eclipse.pde.internal.ui.editor.PDEDetails#doGlobalAction(java.lang.String)
254      */

255     public boolean doGlobalAction(String JavaDoc actionId) {
256         // Determine which tab is selected
257
int index = fTabFolder.getSelectionIndex();
258         // Do the global action on the source viewer on that tab
259
CSSourceViewer viewer = null;
260         
261         if (index == F_INTRODUCTION_TAB) {
262             viewer = fIntroductionViewer;
263         } else if (index == F_CONCLUSION_TAB) {
264             viewer = fConclusionViewer;
265         }
266         
267         return viewer.doGlobalAction(actionId);
268     }
269     
270     /**
271      *
272      */

273     private void createUIConclusionViewer() {
274         // Create composite
275
fConclusionComposite = createUIContainer(fNotebookComposite, 1);
276         // Create label
277
String JavaDoc description = NLS.bind(
278                 PDEUIMessages.CompCSEnclosingTextDetails_ConclusionDescription,
279                 fTaskObjectLabelName);
280         final Label label = getToolkit().createLabel(
281                 fConclusionComposite, description, SWT.WRAP);
282         GridData data = new GridData(GridData.FILL_HORIZONTAL);
283         label.setLayoutData(data);
284         // Create the source viewer
285
fConclusionViewer = new CSSourceViewer(getPage());
286         fConclusionViewer.createUI(fConclusionComposite, 60, 60);
287         // Note: Must paint border for parent composite; otherwise, the border
288
// goes missing on the text widget when using the Windows XP Classic
289
// theme
290
getToolkit().paintBordersFor(fConclusionComposite);
291     }
292
293     /**
294      * @param parent
295      * @param columns
296      * @return
297      */

298     private Composite createUIContainer(Composite parent, int columns) {
299         Composite container = getToolkit().createComposite(parent);
300         GridLayout layout = new GridLayout();
301         layout.marginWidth = 2;
302         layout.marginHeight = 2;
303         layout.numColumns = columns;
304         container.setLayout(layout);
305         return container;
306     }
307     
308     /* (non-Javadoc)
309      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.ICSDetails#hookListeners()
310      */

311     public void hookListeners() {
312         // Create the listeners for the introduction text
313
createListenersIntroductionViewer();
314         // Create the listeners for the conclusion text
315
createListenersConclusionViewer();
316         // Create the listeners for the tab folder
317
createListenersTabFolder();
318     }
319
320     /**
321      *
322      */

323     private void createListenersIntroductionViewer() {
324         fIntroductionViewer.createUIListeners();
325         // Create document listener
326
fIntroductionViewer.getDocument().addDocumentListener(fIntroductionListener);
327     }
328     
329     /**
330      *
331      */

332     private void createListenersConclusionViewer() {
333         fConclusionViewer.createUIListeners();
334         // Create document listener
335
fConclusionViewer.getDocument().addDocumentListener(fConclusionListener);
336     }
337
338     /**
339      *
340      */

341     private void createListenersTabFolder() {
342         fTabFolder.addSelectionListener(new SelectionAdapter() {
343             public void widgetSelected(SelectionEvent e) {
344                 updateTabFolder();
345             }
346         });
347     }
348
349     /**
350      *
351      */

352     private void updateTabFolder() {
353
354         int index = fTabFolder.getSelectionIndex();
355         Control oldControl = fNotebookLayout.topControl;
356         
357         if (index == F_NO_TAB) {
358             // Select the introduction contents by default
359
fNotebookLayout.topControl = fIntroductionComposite;
360             // Select the introduction tab by default to match
361
// Does not trigger selection adapter (only user UI selections do)
362
fTabFolder.setSelection(F_INTRODUCTION_TAB);
363         } else if (index == F_INTRODUCTION_TAB) {
364             fNotebookLayout.topControl = fIntroductionComposite;
365         } else if (index == F_CONCLUSION_TAB) {
366             fNotebookLayout.topControl = fConclusionComposite;
367         }
368
369         if (oldControl != fNotebookLayout.topControl) {
370             fNotebookComposite.layout();
371         }
372         
373     }
374     
375     /* (non-Javadoc)
376      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.ICSDetails#updateFields()
377      */

378     public void updateFields() {
379         // Ensure data object is defined
380
if (fDataTaskObject == null) {
381             return;
382         }
383         boolean editable = isEditableElement();
384         // Update tab folder
385
updateTabFolder();
386         // Update introduction text
387
updateIntroductionViewer(editable);
388         // Update conclusion text
389
updateConclusionViewer(editable);
390     }
391
392     /**
393      * @param editable
394      */

395     private void updateIntroductionViewer(boolean editable) {
396         ICompCSIntro intro = fDataTaskObject.getFieldIntro();
397         
398         // Block listener from handling this update
399
fIntroductionListener.setBlockEvents(true);
400         if ((intro != null) &&
401                 PDETextHelper.isDefined(intro.getFieldContent())) {
402             fIntroductionViewer.getDocument().set(intro.getFieldContent());
403         } else {
404             fIntroductionViewer.getDocument().set(""); //$NON-NLS-1$
405
}
406         // Unblock for user updates
407
fIntroductionListener.setBlockEvents(false);
408         
409         fIntroductionViewer.getViewer().setEditable(editable);
410     }
411
412     /**
413      * @param editable
414      */

415     private void updateConclusionViewer(boolean editable) {
416         ICompCSOnCompletion conclusion = fDataTaskObject.getFieldOnCompletion();
417         
418         // Block listener from handling this update
419
fConclusionListener.setBlockEvents(true);
420         if ((conclusion != null) &&
421                 PDETextHelper.isDefined(conclusion.getFieldContent())) {
422             fConclusionViewer.getDocument().set(conclusion.getFieldContent());
423         } else {
424             fConclusionViewer.getDocument().set(""); //$NON-NLS-1$
425
}
426         // Unblock for user updates
427
fConclusionListener.setBlockEvents(false);
428         
429         fConclusionViewer.getViewer().setEditable(editable);
430     }
431
432     /* (non-Javadoc)
433      * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
434      */

435     public void dispose() {
436         // Set the context menu to null to prevent the editor context menu
437
// from being disposed along with the source viewer
438
if (fIntroductionViewer != null) {
439             fIntroductionViewer.unsetMenu();
440             fIntroductionViewer = null;
441         }
442         if (fConclusionViewer != null) {
443             fConclusionViewer.unsetMenu();
444             fConclusionViewer = null;
445         }
446         super.dispose();
447     }
448     
449     /* (non-Javadoc)
450      * @see org.eclipse.pde.internal.ui.editor.PDEDetails#canPaste(org.eclipse.swt.dnd.Clipboard)
451      */

452     public boolean canPaste(Clipboard clipboard) {
453         // Determine which tab is selected
454
int index = fTabFolder.getSelectionIndex();
455         // Check if the source viewer on that tab can paste
456
CSSourceViewer viewer = null;
457         
458         if (index == F_INTRODUCTION_TAB) {
459             viewer = fIntroductionViewer;
460         } else if (index == F_CONCLUSION_TAB) {
461             viewer = fConclusionViewer;
462         }
463         
464         return viewer.canPaste();
465     }
466 }
467
Popular Tags