KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > text > build > BuildEntry


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.pde.internal.core.text.build;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.TextUtilities;
20 import org.eclipse.pde.core.build.IBuild;
21 import org.eclipse.pde.core.build.IBuildEntry;
22 import org.eclipse.pde.core.build.IBuildModel;
23 import org.eclipse.pde.internal.core.PDECore;
24 import org.eclipse.pde.internal.core.text.IDocumentKey;
25 import org.eclipse.pde.internal.core.text.IEditingModel;
26 import org.eclipse.pde.internal.core.util.PropertiesUtil;
27
28 public class BuildEntry implements IBuildEntry, IDocumentKey {
29
30     private int fLength = -1;
31     private int fOffset = -1;
32     private IBuildModel fModel;
33     private String JavaDoc fName;
34     private ArrayList JavaDoc fTokens = new ArrayList JavaDoc();
35     private String JavaDoc fLineDelimiter;
36     
37     public BuildEntry(String JavaDoc name, IBuildModel model) {
38         fName = name;
39         fModel = model;
40         setLineDelimiter();
41     }
42     
43     private void setLineDelimiter() {
44         if (fModel instanceof IEditingModel) {
45             IDocument document = ((IEditingModel)fModel).getDocument();
46             fLineDelimiter = TextUtilities.getDefaultLineDelimiter(document);
47         } else {
48             fLineDelimiter = System.getProperty("line.separator"); //$NON-NLS-1$
49
}
50     }
51
52     /* (non-Javadoc)
53      * @see org.eclipse.pde.core.build.IBuildEntry#addToken(java.lang.String)
54      */

55     public void addToken(String JavaDoc token) throws CoreException {
56         if (fTokens.contains(token))
57             return;
58         if (fTokens.add(token))
59             getModel().fireModelObjectChanged(this, getName(), null, token);
60     }
61     /* (non-Javadoc)
62      * @see org.eclipse.pde.internal.ui.model.IDocumentKey#getName()
63      */

64     public String JavaDoc getName() {
65         return fName;
66     }
67     /* (non-Javadoc)
68      * @see org.eclipse.pde.core.build.IBuildEntry#getTokens()
69      */

70     public String JavaDoc[] getTokens() {
71         return (String JavaDoc[])fTokens.toArray(new String JavaDoc[fTokens.size()]);
72     }
73     /* (non-Javadoc)
74      * @see org.eclipse.pde.core.build.IBuildEntry#contains(java.lang.String)
75      */

76     public boolean contains(String JavaDoc token) {
77         return fTokens.contains(token);
78     }
79     /* (non-Javadoc)
80      * @see org.eclipse.pde.core.build.IBuildEntry#removeToken(java.lang.String)
81      */

82     public void removeToken(String JavaDoc token) throws CoreException {
83         if (fTokens.remove(token))
84             getModel().fireModelObjectChanged(this, getName(), token, null);
85     }
86     /* (non-Javadoc)
87      * @see org.eclipse.pde.core.build.IBuildEntry#renameToken(java.lang.String, java.lang.String)
88      */

89     public void renameToken(String JavaDoc oldToken, String JavaDoc newToken)
90             throws CoreException {
91         int index = fTokens.indexOf(oldToken);
92         if (index != -1) {
93             fTokens.set(index, newToken);
94             getModel().fireModelObjectChanged(this, getName(), oldToken, newToken);
95         }
96     }
97     /* (non-Javadoc)
98      * @see org.eclipse.pde.internal.ui.model.IDocumentKey#setName(java.lang.String)
99      */

100     public void setName(String JavaDoc name) {
101         String JavaDoc oldName = fName;
102         if (getModel() != null){
103             try {
104                 IBuild build = getModel().getBuild();
105                 build.remove(this);
106                 fName = name;
107                 build.add(this);
108             } catch (CoreException e) {
109                 PDECore.logException(e);
110             }
111             getModel().fireModelObjectChanged(this, getName(), oldName, name);
112         } else
113             fName = name;
114     }
115     /* (non-Javadoc)
116      * @see org.eclipse.pde.internal.ui.model.IDocumentKey#getOffset()
117      */

118     public int getOffset() {
119         return fOffset;
120     }
121     /* (non-Javadoc)
122      * @see org.eclipse.pde.internal.ui.model.IDocumentKey#setOffset(int)
123      */

124     public void setOffset(int offset) {
125         fOffset = offset;
126     }
127     
128     /* (non-Javadoc)
129      * @see org.eclipse.pde.internal.ui.model.IDocumentKey#getLength()
130      */

131     public int getLength() {
132         return fLength;
133     }
134     
135     /* (non-Javadoc)
136      * @see org.eclipse.pde.internal.ui.model.IDocumentKey#setLength(int)
137      */

138     public void setLength(int length) {
139         fLength = length;
140     }
141     /* (non-Javadoc)
142      * @see org.eclipse.pde.core.IWritable#write(java.lang.String, java.io.PrintWriter)
143      */

144     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
145     }
146     
147     public IBuildModel getModel() {
148         return fModel;
149     }
150     
151     public void processEntry(String JavaDoc value) {
152         StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(value, ","); //$NON-NLS-1$
153
while (stok.hasMoreTokens()) {
154             fTokens.add(stok.nextToken().trim());
155         }
156     }
157     /* (non-Javadoc)
158      * @see org.eclipse.pde.internal.ui.model.IDocumentKey#write()
159      */

160     public String JavaDoc write() {
161         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
162         buffer.append(PropertiesUtil.createWritableName(fName));
163         buffer.append(" = "); //$NON-NLS-1$
164
int indentLength = fName.length() + 3;
165         for (int i = 0; i < fTokens.size(); i++) {
166             buffer.append(PropertiesUtil.createEscapedValue(fTokens.get(i).toString()));
167             if (i < fTokens.size() - 1) {
168                 buffer.append(",\\"); //$NON-NLS-1$
169
buffer.append(fLineDelimiter);
170                 for (int j = 0; j < indentLength; j++) {
171                     buffer.append(" "); //$NON-NLS-1$
172
}
173             }
174         }
175         buffer.append(fLineDelimiter); //$NON-NLS-1$
176
return buffer.toString();
177     }
178
179     public void swap(int index1, int index2) {
180         Object JavaDoc obj1 = fTokens.get(index1);
181         Object JavaDoc obj2 = fTokens.set(index2, obj1);
182         fTokens.set(index1, obj2);
183         getModel().fireModelObjectChanged(this, getName(),
184                 new Object JavaDoc[] {obj1, obj2}, new Object JavaDoc[] {obj2, obj1});
185     }
186
187 }
188
Popular Tags