1 11 package org.eclipse.pde.internal.core.text.bundle; 12 13 import java.io.PrintWriter ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.StringTokenizer ; 17 import java.util.TreeMap ; 18 19 import org.eclipse.jdt.core.IPackageFragment; 20 import org.eclipse.osgi.util.ManifestElement; 21 import org.eclipse.pde.core.IModelChangedEvent; 22 import org.eclipse.pde.internal.core.ibundle.IBundleModel; 23 import org.osgi.framework.Constants; 24 25 public class ExportPackageObject extends PackageObject { 26 27 private static final String INTERNAL = "x-internal"; private static final String FRIENDS = "x-friends"; 30 private static final long serialVersionUID = 1L; 31 32 private TreeMap fFriends = new TreeMap (); 33 34 public ExportPackageObject(ManifestHeader header, ManifestElement element, String versionAttribute) { 35 super(header, element, versionAttribute); 36 processFriends(); 37 } 38 39 public ExportPackageObject(ManifestHeader header, IPackageFragment fragment, String versionAttribute) { 40 super(header, fragment.getElementName(), null, versionAttribute); 41 } 42 43 public ExportPackageObject(ManifestHeader header, String id, String version, String versionAttribute) { 44 super(header, id, version, versionAttribute); 45 } 46 47 protected void processFriends() { 48 String [] friends = getDirectives(FRIENDS); 49 if (friends != null) { 50 for (int i = 0; i < friends.length; i++) { 51 fFriends.put(friends[i], new PackageFriend(this, friends[i])); 52 } 53 } 54 } 55 56 public boolean isInternal() { 57 return "true".equals(getDirective(INTERNAL)) || getDirective(FRIENDS) != null; } 59 60 public void removeInternalDirective() { 61 setDirective(INTERNAL, null); 62 ((CompositeManifestHeader)fHeader).update(true); 63 } 64 65 public void setInternal(boolean internal) { 66 boolean old = isInternal(); 67 if (!internal) { 68 setDirective(INTERNAL, null); 69 setDirective(FRIENDS, null); 70 } else { 71 if (fFriends.size() == 0) 72 setDirective(INTERNAL, "true"); else { 74 Iterator iter = fFriends.keySet().iterator(); 75 while (iter.hasNext()) 76 addDirective(FRIENDS, iter.next().toString()); 77 } 78 } 79 fHeader.update(); 80 firePropertyChanged(this, INTERNAL, Boolean.toString(old), Boolean.toString(internal)); 81 } 82 83 public PackageFriend[] getFriends() { 84 return (PackageFriend[])fFriends.values().toArray(new PackageFriend[fFriends.size()]); 85 } 86 87 public void addFriend(PackageFriend friend) { 88 fFriends.put(friend.getName(), friend); 89 addDirective(FRIENDS, friend.getName()); 90 setDirective(INTERNAL, null); 91 fHeader.update(); 92 fireStructureChanged(friend, IModelChangedEvent.INSERT); 93 } 94 95 public void removeFriend(PackageFriend friend) { 96 fFriends.remove(friend.getName()); 97 setDirective(FRIENDS, null); 98 if (fFriends.size() == 0) 99 setDirective(INTERNAL, "true"); else { 101 Iterator iter = fFriends.keySet().iterator(); 102 while (iter.hasNext()) 103 addDirective(FRIENDS, iter.next().toString()); 104 } 105 fHeader.update(); 106 fireStructureChanged(friend, IModelChangedEvent.REMOVE); 107 } 108 109 public boolean hasFriend(String name) { 110 return fFriends.containsKey(name); 111 } 112 113 public boolean hasSameVisibility(ExportPackageObject object) { 114 if (object.isInternal() != isInternal()) 115 return false; 116 117 if (fFriends.size() != object.fFriends.size()) 118 return false; 119 120 Iterator iter = fFriends.keySet().iterator(); 121 while (iter.hasNext()) { 122 if (!object.fFriends.containsKey(iter.next())) 123 return false; 124 } 125 return true; 126 } 127 128 public void setUsesDirective(String value) { 129 String oldValue = getUsesDirective(); 130 setDirective(Constants.USES_DIRECTIVE, value); 131 fHeader.update(); 132 firePropertyChanged(this, Constants.USES_DIRECTIVE, oldValue, value); 133 } 134 135 public String getUsesDirective() { 136 return getDirective(Constants.USES_DIRECTIVE); 137 } 138 139 protected void appendValuesToBuffer(StringBuffer sb, TreeMap table) { 140 if (table == null) 141 return; 142 Object usesValue = null; 143 if (table.containsKey(Constants.USES_DIRECTIVE)) 145 usesValue = table.remove(Constants.USES_DIRECTIVE); 146 super.appendValuesToBuffer(sb, table); 147 if (usesValue != null) { 148 table.put(Constants.USES_DIRECTIVE, usesValue); 149 formatUsesDirective(sb, usesValue); 150 } 151 } 152 153 private void formatUsesDirective(StringBuffer sb, Object usesValue) { 154 StringTokenizer tokenizer = null; 155 if (usesValue instanceof String ) 156 tokenizer = new StringTokenizer ((String )usesValue, ","); boolean newLine = (tokenizer != null) ? tokenizer.countTokens() > 3 : 158 ((ArrayList )usesValue).size() > 3; 159 String eol = getHeader().getLineLimiter(); 160 sb.append(';'); 161 if (newLine) 162 sb.append(eol).append(" "); sb.append(Constants.USES_DIRECTIVE); 164 sb.append(":=\""); if (tokenizer != null) 166 while (tokenizer.hasMoreTokens()) { 167 sb.append(tokenizer.nextToken()); 168 if (tokenizer.hasMoreTokens()) { 169 sb.append(','); 170 if (newLine) 171 sb.append(eol).append(" "); } 173 } 174 else { 175 ArrayList list = ((ArrayList )usesValue); 176 for (int i = 0; i < list.size(); i++) { 177 if (i != 0) { 178 sb.append(','); 179 if (newLine) 180 sb.append(eol).append(" "); } 182 sb.append(list.get(i)); 183 } 184 } 185 sb.append("\""); } 187 188 191 public void write(String indent, PrintWriter writer) { 192 writer.write(write()); 194 } 195 196 201 public void reconnect(IBundleModel model, ExportPackageHeader header, 202 String versionAttribute) { 203 super.reconnect(model, header, versionAttribute); 204 reconnectFriends(); 206 } 207 208 211 private void reconnectFriends() { 212 Iterator keys = fFriends.keySet().iterator(); 214 while (keys.hasNext()) { 216 String key = (String )keys.next(); 217 PackageFriend friend = (PackageFriend)fFriends.get(key); 218 friend.reconnect(this); 219 } 220 } 221 222 } 223 | Popular Tags |