KickJava   Java API By Example, From Geeks To Geeks.

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


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.jface.text.DocumentEvent;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.IDocumentListener;
17 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSIntro;
18 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSModelFactory;
19 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject;
20 import org.eclipse.pde.internal.core.util.PDETextHelper;
21
22 /**
23  * CompCSEnclosingTextModifyListener
24  *
25  */

26 public class CompCSIntroductionTextListener implements IDocumentListener {
27
28     private ICompCSTaskObject fDataTaskObject;
29     
30     private boolean fBlockEvents;
31     
32     /**
33      *
34      */

35     public CompCSIntroductionTextListener() {
36         fDataTaskObject = null;
37         fBlockEvents = false;
38     }
39
40     /**
41      * @param block
42      */

43     public void setBlockEvents(boolean block) {
44         fBlockEvents = block;
45     }
46     
47     /**
48      * @return
49      */

50     public boolean getBlockEvents() {
51         return fBlockEvents;
52     }
53     
54     /**
55      * @param object
56      */

57     public void setData(ICompCSTaskObject object) {
58         // Set data
59
fDataTaskObject = object;
60     }
61     
62     /* (non-Javadoc)
63      * @see org.eclipse.jface.text.IDocumentListener#documentAboutToBeChanged(org.eclipse.jface.text.DocumentEvent)
64      */

65     public void documentAboutToBeChanged(DocumentEvent e) {
66         // NO-OP
67
}
68     
69     /* (non-Javadoc)
70      * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
71      */

72     public void documentChanged(DocumentEvent event) {
73         // Check whether to handle this event
74
if (fBlockEvents) {
75             return;
76         }
77         // Ensure the task object is defined
78
if (fDataTaskObject == null) {
79             return;
80         }
81         // Get the text from the event
82
IDocument document = event.getDocument();
83         if (document == null) {
84             return;
85         }
86         String JavaDoc text = document.get().trim();
87         // Determine whether an introduction was specified
88
boolean hasText = PDETextHelper.isDefined(text);
89         if (hasText) {
90             // An introduction was specified, update accordingly
91
updateIntroductionText(text);
92         } else {
93             // No introduction was specified, remove any existing one
94
removeIntroductionText(text);
95         }
96     }
97     
98     /**
99      * @param text
100      */

101     private void updateIntroductionText(String JavaDoc text) {
102         if (fDataTaskObject.getFieldIntro() == null) {
103             // Create a new introduction
104
addIntroductionText(text);
105         } else {
106             // Re-use the existing introduction
107
modifyIntroductionText(text);
108         }
109     }
110
111     /**
112      * @param text
113      */

114     private void addIntroductionText(String JavaDoc text) {
115         ICompCSModelFactory factory =
116             fDataTaskObject.getModel().getFactory();
117         ICompCSIntro intro = factory.createCompCSIntro(fDataTaskObject);
118         intro.setFieldContent(text);
119         fDataTaskObject.setFieldIntro(intro);
120     }
121
122     /**
123      * @param text
124      */

125     private void modifyIntroductionText(String JavaDoc text) {
126         ICompCSIntro intro = fDataTaskObject.getFieldIntro();
127         intro.setFieldContent(text);
128     }
129
130     /**
131      * @param text
132      */

133     private void removeIntroductionText(String JavaDoc text) {
134         ICompCSIntro intro = fDataTaskObject.getFieldIntro();
135         if (intro != null) {
136             fDataTaskObject.setFieldIntro(null);
137         }
138     }
139     
140 }
141
Popular Tags