1 11 package org.eclipse.pde.internal.core.text.build; 12 13 import java.io.PrintWriter ; 14 import java.util.ArrayList ; 15 import java.util.StringTokenizer ; 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 fName; 34 private ArrayList fTokens = new ArrayList (); 35 private String fLineDelimiter; 36 37 public BuildEntry(String 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"); } 50 } 51 52 55 public void addToken(String token) throws CoreException { 56 if (fTokens.contains(token)) 57 return; 58 if (fTokens.add(token)) 59 getModel().fireModelObjectChanged(this, getName(), null, token); 60 } 61 64 public String getName() { 65 return fName; 66 } 67 70 public String [] getTokens() { 71 return (String [])fTokens.toArray(new String [fTokens.size()]); 72 } 73 76 public boolean contains(String token) { 77 return fTokens.contains(token); 78 } 79 82 public void removeToken(String token) throws CoreException { 83 if (fTokens.remove(token)) 84 getModel().fireModelObjectChanged(this, getName(), token, null); 85 } 86 89 public void renameToken(String oldToken, String 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 100 public void setName(String name) { 101 String 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 118 public int getOffset() { 119 return fOffset; 120 } 121 124 public void setOffset(int offset) { 125 fOffset = offset; 126 } 127 128 131 public int getLength() { 132 return fLength; 133 } 134 135 138 public void setLength(int length) { 139 fLength = length; 140 } 141 144 public void write(String indent, PrintWriter writer) { 145 } 146 147 public IBuildModel getModel() { 148 return fModel; 149 } 150 151 public void processEntry(String value) { 152 StringTokenizer stok = new StringTokenizer (value, ","); while (stok.hasMoreTokens()) { 154 fTokens.add(stok.nextToken().trim()); 155 } 156 } 157 160 public String write() { 161 StringBuffer buffer = new StringBuffer (); 162 buffer.append(PropertiesUtil.createWritableName(fName)); 163 buffer.append(" = "); 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(",\\"); buffer.append(fLineDelimiter); 170 for (int j = 0; j < indentLength; j++) { 171 buffer.append(" "); } 173 } 174 } 175 buffer.append(fLineDelimiter); return buffer.toString(); 177 } 178 179 public void swap(int index1, int index2) { 180 Object obj1 = fTokens.get(index1); 181 Object obj2 = fTokens.set(index2, obj1); 182 fTokens.set(index1, obj2); 183 getModel().fireModelObjectChanged(this, getName(), 184 new Object [] {obj1, obj2}, new Object [] {obj2, obj1}); 185 } 186 187 } 188 | Popular Tags |