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