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.core.cheatsheet.comp;
011:
012: import java.io.IOException;
013: import java.io.PrintWriter;
014: import java.util.ArrayList;
015: import java.util.List;
016:
017: import org.eclipse.pde.internal.core.XMLPrintHandler;
018: import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCS;
019: import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSModel;
020: import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSModelFactory;
021: import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject;
022: import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject;
023: import org.eclipse.pde.internal.core.util.PDETextHelper;
024: import org.w3c.dom.Element;
025: import org.w3c.dom.Text;
026:
027: /**
028: * CompCS
029: *
030: */
031: public class CompCS extends CompCSObject implements ICompCS {
032:
033: private String fFieldName;
034:
035: private ICompCSTaskObject fFieldTaskObject;
036:
037: /**
038: *
039: */
040: private static final long serialVersionUID = 1L;
041:
042: /**
043: * @param model
044: */
045: public CompCS(ICompCSModel model) {
046: super (model, null);
047: reset();
048: }
049:
050: /* (non-Javadoc)
051: * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSObject#getChildren()
052: */
053: public List getChildren() {
054: ArrayList list = new ArrayList();
055: // Add task / taskGroup
056: if (fFieldTaskObject != null) {
057: list.add(fFieldTaskObject);
058: }
059: return list;
060: }
061:
062: /* (non-Javadoc)
063: * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSObject#getName()
064: */
065: public String getName() {
066: return fFieldName;
067: }
068:
069: /* (non-Javadoc)
070: * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSObject#getType()
071: */
072: public int getType() {
073: return TYPE_COMPOSITE_CHEATSHEET;
074: }
075:
076: /* (non-Javadoc)
077: * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSObject#parseAttributes(org.w3c.dom.Element)
078: */
079: protected void parseAttributes(Element element) {
080: // Process name attribute
081: // Trim leading and trailing whitespace
082: fFieldName = element.getAttribute(ATTRIBUTE_NAME).trim();
083: }
084:
085: /* (non-Javadoc)
086: * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSObject#parseElement(org.w3c.dom.Element)
087: */
088: protected void parseElement(Element element) {
089: String name = element.getNodeName();
090: ICompCSModelFactory factory = getModel().getFactory();
091:
092: if (name.equals(ELEMENT_TASK)) {
093: // Process task element
094: fFieldTaskObject = factory.createCompCSTask(this );
095: fFieldTaskObject.parse(element);
096: } else if (name.equals(ELEMENT_TASKGROUP)) {
097: // Process taskGroup element
098: fFieldTaskObject = factory.createCompCSTaskGroup(this );
099: fFieldTaskObject.parse(element);
100: }
101: }
102:
103: /* (non-Javadoc)
104: * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSObject#reset()
105: */
106: public void reset() {
107: fFieldName = null;
108: fFieldTaskObject = null;
109: }
110:
111: /* (non-Javadoc)
112: * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSObject#write(java.lang.String, java.io.PrintWriter)
113: */
114: public void write(String indent, PrintWriter writer) {
115:
116: try {
117: // Print XML decl
118: XMLPrintHandler.printHead(writer, ATTRIBUTE_VALUE_ENCODING);
119: super .write(indent, writer);
120: } catch (IOException e) {
121: // Suppress
122: //e.printStackTrace();
123: }
124: }
125:
126: /* (non-Javadoc)
127: * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSObject#writeAttributes(java.lang.StringBuffer)
128: */
129: protected void writeAttributes(StringBuffer buffer) {
130: // Print name attribute
131: if ((fFieldName != null) && (fFieldName.length() > 0)) {
132: // Trim leading and trailing whitespace
133: // Encode characters
134: buffer.append(XMLPrintHandler.wrapAttribute(ATTRIBUTE_NAME,
135: PDETextHelper.translateWriteText(fFieldName.trim(),
136: DEFAULT_SUBSTITUTE_CHARS)));
137: }
138: }
139:
140: /* (non-Javadoc)
141: * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSObject#writeElements(java.lang.String, java.io.PrintWriter)
142: */
143: protected void writeElements(String indent, PrintWriter writer) {
144: String newIndent = indent + XMLPrintHandler.XML_INDENT;
145: // Print task / taskGroup element
146: if (fFieldTaskObject != null) {
147: fFieldTaskObject.write(newIndent, writer);
148: }
149: }
150:
151: /* (non-Javadoc)
152: * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCS#getFieldName()
153: */
154: public String getFieldName() {
155: return fFieldName;
156: }
157:
158: /* (non-Javadoc)
159: * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCS#getFieldTaskObject()
160: */
161: public ICompCSTaskObject getFieldTaskObject() {
162: return fFieldTaskObject;
163: }
164:
165: /* (non-Javadoc)
166: * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCS#setFieldName(java.lang.String)
167: */
168: public void setFieldName(String name) {
169: String old = fFieldName;
170: fFieldName = name;
171: if (isEditable()) {
172: firePropertyChanged(ATTRIBUTE_NAME, old, fFieldName);
173: }
174: }
175:
176: /* (non-Javadoc)
177: * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCS#setFieldTaskObject(org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject)
178: */
179: public void setFieldTaskObject(ICompCSTaskObject taskObject) {
180: ICompCSObject old = fFieldTaskObject;
181: fFieldTaskObject = taskObject;
182: if (isEditable()) {
183: fireStructureChanged(fFieldTaskObject, old);
184: }
185: }
186:
187: /* (non-Javadoc)
188: * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject#getElement()
189: */
190: public String getElement() {
191: return ELEMENT_COMPOSITE_CHEATSHEET;
192: }
193:
194: /* (non-Javadoc)
195: * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSObject#parseText(org.w3c.dom.Text)
196: */
197: protected void parseText(Text text) {
198: // NO-OP
199: }
200:
201: }
|