1 11 package org.eclipse.pde.internal.core.text.bundle; 12 13 import java.util.ArrayList ; 14 import java.util.Map ; 15 import java.util.TreeMap ; 16 import java.util.Vector ; 17 18 import org.eclipse.osgi.util.ManifestElement; 19 import org.eclipse.pde.core.IModelChangedEvent; 20 import org.eclipse.pde.internal.core.bundle.BundleObject; 21 import org.eclipse.pde.internal.core.ibundle.IBundle; 22 import org.osgi.framework.BundleException; 23 24 public class CompositeManifestHeader extends ManifestHeader { 25 26 private static final PDEManifestElement[] NO_ELEMENTS = new PDEManifestElement[0]; 27 28 private static final long serialVersionUID = 1L; 29 30 private boolean fSort; 31 32 protected ArrayList fManifestElements; 33 34 protected Map fElementMap; 35 36 public CompositeManifestHeader(String name, String value, IBundle bundle, String lineDelimiter) { 37 this(name, value, bundle, lineDelimiter, false); 38 } 39 40 public CompositeManifestHeader(String name, String value, IBundle bundle, String lineDelimiter, boolean sort) { 41 fName = name; 42 fBundle = bundle; 43 fLineDelimiter = lineDelimiter; 44 setModel(fBundle.getModel()); 45 fSort = sort; 46 fValue = value; 47 processValue(value); 48 } 49 50 protected void processValue(String value) { 51 try { 52 ManifestElement[] elements = ManifestElement.parseHeader(fName, value); 53 for (int i = 0; i < elements.length; i++) 54 addManifestElement(createElement(elements[i]), false); 55 } catch (BundleException e) { 56 } 57 } 58 59 protected PDEManifestElement createElement(ManifestElement element) { 60 return new PDEManifestElement(this, element); 61 } 62 63 public void update() { 64 update(false); 66 } 67 68 public void update(boolean notify) { 69 StringBuffer sb = new StringBuffer (); 70 PDEManifestElement[] elements = getElements(); 71 for (int i = 0; i < elements.length; i++) { 72 if (sb.length() > 0) { 73 sb.append(","); sb.append(fLineDelimiter); 75 sb.append(" "); } 77 sb.append(elements[i].write()); 78 } 79 String old = fValue; 80 fValue = sb.toString(); 81 if (notify) 82 firePropertyChanged(this, fName, old, fValue); 83 } 84 85 protected void addManifestElement(String value) { 86 addManifestElement(new PDEManifestElement(this, value)); 87 } 88 89 protected void addManifestElement(PDEManifestElement element) { 90 addManifestElement(element, true); 91 } 92 93 protected void addManifestElements(PDEManifestElement[] elements) { 94 for (int i = 0; i < elements.length; i++) 95 addManifestElement(elements[i], false); 96 update(false); 97 fireStructureChanged(elements, IModelChangedEvent.INSERT); 98 } 99 100 protected void addManifestElement(PDEManifestElement element, boolean update) { 101 element.setModel(getModel()); 102 element.setHeader(this); 103 if (fSort) { 104 if (fElementMap == null) 105 fElementMap = new TreeMap (); 106 fElementMap.put(element.getValue(), element); 107 } else { 108 if (fManifestElements == null) 109 fManifestElements = new ArrayList (1); 110 fManifestElements.add(element); 111 } 112 if (update) { 113 update(false); 114 fireStructureChanged(element, IModelChangedEvent.INSERT); 115 } 116 } 117 118 protected Object removeManifestElement(PDEManifestElement element) { 119 return removeManifestElement(element.getValue()); 120 } 121 122 protected Object removeManifestElement(String name) { 123 Object object = null; 124 if (fSort) { 125 if (fElementMap != null) { 126 object = fElementMap.remove(name); 127 } 128 } else if (fManifestElements != null){ 129 for (int i = 0; i < fManifestElements.size(); i++) { 130 PDEManifestElement element = (PDEManifestElement)fManifestElements.get(i); 131 if (name.equals(element.getValue())) 132 object = fManifestElements.remove(i); 133 } 134 } 135 update(false); 136 if (object instanceof BundleObject) 137 fireStructureChanged((BundleObject)object, IModelChangedEvent.REMOVE); 138 return object; 139 } 140 141 public PDEManifestElement[] getElements() { 142 if (fSort && fElementMap != null) 143 return (PDEManifestElement[])fElementMap.values().toArray(new PDEManifestElement[fElementMap.size()]); 144 145 if (fManifestElements != null) 146 return (PDEManifestElement[])fManifestElements.toArray(new PDEManifestElement[fManifestElements.size()]); 147 148 return NO_ELEMENTS; 149 } 150 151 public boolean isEmpty() { 152 if (fSort) 153 return fElementMap == null || fElementMap.size() == 0; 154 return fManifestElements == null || fManifestElements.size() == 0; 155 } 156 157 public boolean hasElement(String name) { 158 if (fSort && fElementMap != null) 159 return fElementMap.containsKey(name); 160 161 if (fManifestElements != null) { 162 for (int i = 0; i < fManifestElements.size(); i++) { 163 PDEManifestElement element = (PDEManifestElement)fManifestElements.get(i); 164 if (name.equals(element.getValue())) 165 return true; 166 } 167 } 168 return false; 169 } 170 171 public Vector getElementNames() { 172 PDEManifestElement[] elements = getElements(); 173 Vector vector = new Vector (elements.length); 174 for (int i = 0; i < elements.length; i++) { 175 vector.add(elements[i].getValue()); 176 } 177 return vector; 178 } 179 180 public void swap(int index1, int index2) { 181 if (fSort || fManifestElements == null) 182 return; 183 int size = fManifestElements.size(); 184 if (index1 >= 0 && index2 >= 0 && size > Math.max(index1, index2)) { 185 Object object1 = fManifestElements.get(index1); 186 Object object2 = fManifestElements.get(index2); 187 fManifestElements.set(index1, object2); 188 fManifestElements.set(index2, object1); 189 update(true); 190 } 191 } 192 193 protected PDEManifestElement getElementAt(int index) { 194 if (fManifestElements != null && fManifestElements.size() > index) 195 return (PDEManifestElement)fManifestElements.get(index); 196 return null; 197 } 198 199 200 201 } 202 | Popular Tags |