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.comp.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.pde.internal.core.icheatsheet.comp.ICompCSModelFactory;
016: import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSOnCompletion;
017: import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject;
018: import org.eclipse.pde.internal.core.util.PDETextHelper;
019:
020: /**
021: * CompCSEnclosingTextModifyListener
022: *
023: */
024: public class CompCSConclusionTextListener implements IDocumentListener {
025:
026: private ICompCSTaskObject fDataTaskObject;
027:
028: private boolean fBlockEvents;
029:
030: /**
031: *
032: */
033: public CompCSConclusionTextListener() {
034: fDataTaskObject = null;
035: fBlockEvents = false;
036: }
037:
038: /**
039: * @param block
040: */
041: public void setBlockEvents(boolean block) {
042: fBlockEvents = block;
043: }
044:
045: /**
046: * @return
047: */
048: public boolean getBlockEvents() {
049: return fBlockEvents;
050: }
051:
052: /**
053: * @param object
054: */
055: public void setData(ICompCSTaskObject object) {
056: // Set data
057: fDataTaskObject = object;
058: }
059:
060: /* (non-Javadoc)
061: * @see org.eclipse.jface.text.IDocumentListener#documentAboutToBeChanged(org.eclipse.jface.text.DocumentEvent)
062: */
063: public void documentAboutToBeChanged(DocumentEvent e) {
064: // NO-OP
065: }
066:
067: /* (non-Javadoc)
068: * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
069: */
070: public void documentChanged(DocumentEvent event) {
071: // Check whether to handle this event
072: if (fBlockEvents) {
073: return;
074: }
075: // Ensure the task object is defined
076: if (fDataTaskObject == null) {
077: return;
078: }
079: // Get the text from the event
080: IDocument document = event.getDocument();
081: if (document == null) {
082: return;
083: }
084: String text = document.get().trim();
085: // Determine whether a conclusion was specified
086: boolean hasText = PDETextHelper.isDefined(text);
087: if (hasText) {
088: // A conclusion was specified, update accordingly
089: updateConclusionText(text);
090: } else {
091: // No conclusion was specified, remove any existing one
092: removeConclusionText(text);
093: }
094: }
095:
096: /**
097: * @param text
098: */
099: private void updateConclusionText(String text) {
100: if (fDataTaskObject.getFieldOnCompletion() == null) {
101: // Create a new conclusion
102: addConclusionText(text);
103: } else {
104: // Re-use the existing conclusion
105: modifyConclusionText(text);
106: }
107: }
108:
109: /**
110: * @param text
111: */
112: private void addConclusionText(String text) {
113: ICompCSModelFactory factory = fDataTaskObject.getModel()
114: .getFactory();
115: ICompCSOnCompletion conclusion = factory
116: .createCompCSOnCompletion(fDataTaskObject);
117: conclusion.setFieldContent(text);
118: fDataTaskObject.setFieldOnCompletion(conclusion);
119: }
120:
121: /**
122: * @param text
123: */
124: private void modifyConclusionText(String text) {
125: ICompCSOnCompletion conclusion = fDataTaskObject
126: .getFieldOnCompletion();
127: conclusion.setFieldContent(text);
128: }
129:
130: /**
131: * @param text
132: */
133: private void removeConclusionText(String text) {
134: ICompCSOnCompletion conclusion = fDataTaskObject
135: .getFieldOnCompletion();
136: if (conclusion != null) {
137: fDataTaskObject.setFieldOnCompletion(null);
138: }
139: }
140:
141: }
|