KickJava   Java API By Example, From Geeks To Geeks.

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


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.ICompCSModelFactory;
18 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSOnCompletion;
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 CompCSConclusionTextListener implements IDocumentListener {
27
28     private ICompCSTaskObject fDataTaskObject;
29     
30     private boolean fBlockEvents;
31     
32     /**
33      *
34      */

35     public CompCSConclusionTextListener() {
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 a conclusion was specified
88
boolean hasText = PDETextHelper.isDefined(text);
89         if (hasText) {
90             // A conclusion was specified, update accordingly
91
updateConclusionText(text);
92         } else {
93             // No conclusion was specified, remove any existing one
94
removeConclusionText(text);
95         }
96     }
97     
98     /**
99      * @param text
100      */

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

114     private void addConclusionText(String JavaDoc text) {
115         ICompCSModelFactory factory =
116             fDataTaskObject.getModel().getFactory();
117         ICompCSOnCompletion conclusion =
118             factory.createCompCSOnCompletion(fDataTaskObject);
119         conclusion.setFieldContent(text);
120         fDataTaskObject.setFieldOnCompletion(conclusion);
121     }
122
123     /**
124      * @param text
125      */

126     private void modifyConclusionText(String JavaDoc text) {
127         ICompCSOnCompletion conclusion = fDataTaskObject.getFieldOnCompletion();
128         conclusion.setFieldContent(text);
129     }
130
131     /**
132      * @param text
133      */

134     private void removeConclusionText(String JavaDoc text) {
135         ICompCSOnCompletion conclusion = fDataTaskObject.getFieldOnCompletion();
136         if (conclusion != null) {
137             fDataTaskObject.setFieldOnCompletion(null);
138         }
139     }
140     
141 }
142
Popular Tags