001: /*******************************************************************************
002: * Copyright (c) 2000, 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.text.build;
011:
012: import java.io.PrintWriter;
013: import java.util.ArrayList;
014: import java.util.StringTokenizer;
015:
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.jface.text.IDocument;
018: import org.eclipse.jface.text.TextUtilities;
019: import org.eclipse.pde.core.build.IBuild;
020: import org.eclipse.pde.core.build.IBuildEntry;
021: import org.eclipse.pde.core.build.IBuildModel;
022: import org.eclipse.pde.internal.core.PDECore;
023: import org.eclipse.pde.internal.core.text.IDocumentKey;
024: import org.eclipse.pde.internal.core.text.IEditingModel;
025: import org.eclipse.pde.internal.core.util.PropertiesUtil;
026:
027: public class BuildEntry implements IBuildEntry, IDocumentKey {
028:
029: private int fLength = -1;
030: private int fOffset = -1;
031: private IBuildModel fModel;
032: private String fName;
033: private ArrayList fTokens = new ArrayList();
034: private String fLineDelimiter;
035:
036: public BuildEntry(String name, IBuildModel model) {
037: fName = name;
038: fModel = model;
039: setLineDelimiter();
040: }
041:
042: private void setLineDelimiter() {
043: if (fModel instanceof IEditingModel) {
044: IDocument document = ((IEditingModel) fModel).getDocument();
045: fLineDelimiter = TextUtilities
046: .getDefaultLineDelimiter(document);
047: } else {
048: fLineDelimiter = System.getProperty("line.separator"); //$NON-NLS-1$
049: }
050: }
051:
052: /* (non-Javadoc)
053: * @see org.eclipse.pde.core.build.IBuildEntry#addToken(java.lang.String)
054: */
055: public void addToken(String token) throws CoreException {
056: if (fTokens.contains(token))
057: return;
058: if (fTokens.add(token))
059: getModel().fireModelObjectChanged(this , getName(), null,
060: token);
061: }
062:
063: /* (non-Javadoc)
064: * @see org.eclipse.pde.internal.ui.model.IDocumentKey#getName()
065: */
066: public String getName() {
067: return fName;
068: }
069:
070: /* (non-Javadoc)
071: * @see org.eclipse.pde.core.build.IBuildEntry#getTokens()
072: */
073: public String[] getTokens() {
074: return (String[]) fTokens.toArray(new String[fTokens.size()]);
075: }
076:
077: /* (non-Javadoc)
078: * @see org.eclipse.pde.core.build.IBuildEntry#contains(java.lang.String)
079: */
080: public boolean contains(String token) {
081: return fTokens.contains(token);
082: }
083:
084: /* (non-Javadoc)
085: * @see org.eclipse.pde.core.build.IBuildEntry#removeToken(java.lang.String)
086: */
087: public void removeToken(String token) throws CoreException {
088: if (fTokens.remove(token))
089: getModel().fireModelObjectChanged(this , getName(), token,
090: null);
091: }
092:
093: /* (non-Javadoc)
094: * @see org.eclipse.pde.core.build.IBuildEntry#renameToken(java.lang.String, java.lang.String)
095: */
096: public void renameToken(String oldToken, String newToken)
097: throws CoreException {
098: int index = fTokens.indexOf(oldToken);
099: if (index != -1) {
100: fTokens.set(index, newToken);
101: getModel().fireModelObjectChanged(this , getName(),
102: oldToken, newToken);
103: }
104: }
105:
106: /* (non-Javadoc)
107: * @see org.eclipse.pde.internal.ui.model.IDocumentKey#setName(java.lang.String)
108: */
109: public void setName(String name) {
110: String oldName = fName;
111: if (getModel() != null) {
112: try {
113: IBuild build = getModel().getBuild();
114: build.remove(this );
115: fName = name;
116: build.add(this );
117: } catch (CoreException e) {
118: PDECore.logException(e);
119: }
120: getModel().fireModelObjectChanged(this , getName(), oldName,
121: name);
122: } else
123: fName = name;
124: }
125:
126: /* (non-Javadoc)
127: * @see org.eclipse.pde.internal.ui.model.IDocumentKey#getOffset()
128: */
129: public int getOffset() {
130: return fOffset;
131: }
132:
133: /* (non-Javadoc)
134: * @see org.eclipse.pde.internal.ui.model.IDocumentKey#setOffset(int)
135: */
136: public void setOffset(int offset) {
137: fOffset = offset;
138: }
139:
140: /* (non-Javadoc)
141: * @see org.eclipse.pde.internal.ui.model.IDocumentKey#getLength()
142: */
143: public int getLength() {
144: return fLength;
145: }
146:
147: /* (non-Javadoc)
148: * @see org.eclipse.pde.internal.ui.model.IDocumentKey#setLength(int)
149: */
150: public void setLength(int length) {
151: fLength = length;
152: }
153:
154: /* (non-Javadoc)
155: * @see org.eclipse.pde.core.IWritable#write(java.lang.String, java.io.PrintWriter)
156: */
157: public void write(String indent, PrintWriter writer) {
158: }
159:
160: public IBuildModel getModel() {
161: return fModel;
162: }
163:
164: public void processEntry(String value) {
165: StringTokenizer stok = new StringTokenizer(value, ","); //$NON-NLS-1$
166: while (stok.hasMoreTokens()) {
167: fTokens.add(stok.nextToken().trim());
168: }
169: }
170:
171: /* (non-Javadoc)
172: * @see org.eclipse.pde.internal.ui.model.IDocumentKey#write()
173: */
174: public String write() {
175: StringBuffer buffer = new StringBuffer();
176: buffer.append(PropertiesUtil.createWritableName(fName));
177: buffer.append(" = "); //$NON-NLS-1$
178: int indentLength = fName.length() + 3;
179: for (int i = 0; i < fTokens.size(); i++) {
180: buffer.append(PropertiesUtil.createEscapedValue(fTokens
181: .get(i).toString()));
182: if (i < fTokens.size() - 1) {
183: buffer.append(",\\"); //$NON-NLS-1$
184: buffer.append(fLineDelimiter);
185: for (int j = 0; j < indentLength; j++) {
186: buffer.append(" "); //$NON-NLS-1$
187: }
188: }
189: }
190: buffer.append(fLineDelimiter); //$NON-NLS-1$
191: return buffer.toString();
192: }
193:
194: public void swap(int index1, int index2) {
195: Object obj1 = fTokens.get(index1);
196: Object obj2 = fTokens.set(index2, obj1);
197: fTokens.set(index1, obj2);
198: getModel().fireModelObjectChanged(this , getName(),
199: new Object[] { obj1, obj2 },
200: new Object[] { obj2, obj1 });
201: }
202:
203: /**
204: * @param targetToken
205: * @return
206: */
207: public String getPreviousToken(String targetToken) {
208: // Ensure we have tokens
209: if (fTokens.size() <= 1) {
210: return null;
211: }
212: // Get the index of the target token
213: int targetIndex = fTokens.indexOf(targetToken);
214: // Validate index
215: if (targetIndex < 0) {
216: // Target token does not exist
217: return null;
218: } else if (targetIndex == 0) {
219: // Target token has no previous token
220: return null;
221: }
222: // 1 <= index < size()
223: // Get the previous token
224: String previousToken = (String) fTokens.get(targetIndex - 1);
225:
226: return previousToken;
227: }
228:
229: /**
230: * @param targetToken
231: * @return
232: */
233: public String getNextToken(String targetToken) {
234: // Ensure we have tokens
235: if (fTokens.size() <= 1) {
236: return null;
237: }
238: // Get the index of the target token
239: int targetIndex = fTokens.indexOf(targetToken);
240: // Get the index of the last token
241: int lastIndex = fTokens.size() - 1;
242: // Validate index
243: if (targetIndex < 0) {
244: // Target token does not exist
245: return null;
246: } else if (targetIndex >= lastIndex) {
247: // Target token has no next token
248: return null;
249: }
250: // 0 <= index < last token < size()
251: // Get the next token
252: String nextToken = (String) fTokens.get(targetIndex + 1);
253:
254: return nextToken;
255: }
256:
257: /**
258: * @param targetToken
259: * @return
260: */
261: public int getIndexOf(String targetToken) {
262: return fTokens.indexOf(targetToken);
263: }
264:
265: /**
266: * @param token
267: * @param position
268: */
269: public void addToken(String token, int position) {
270: // Validate position
271: if (position < 0) {
272: return;
273: } else if (position > fTokens.size()) {
274: return;
275: }
276: // Ensure no duplicates
277: if (fTokens.contains(token)) {
278: return;
279: }
280: // Add the token at the specified position
281: fTokens.add(position, token);
282: // Fire event
283: getModel().fireModelObjectChanged(this, getName(), null, token);
284: }
285:
286: }
|