KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > text > bundle > ExportPackageObject


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.bundle;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17 import java.util.TreeMap JavaDoc;
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 JavaDoc INTERNAL = "x-internal"; //$NON-NLS-1$
28
private static final String JavaDoc FRIENDS = "x-friends"; //$NON-NLS-1$
29

30     private static final long serialVersionUID = 1L;
31     
32     private TreeMap JavaDoc fFriends = new TreeMap JavaDoc();
33      
34     public ExportPackageObject(ManifestHeader header, ManifestElement element, String JavaDoc versionAttribute) {
35         super(header, element, versionAttribute);
36         processFriends();
37     }
38     
39     public ExportPackageObject(ManifestHeader header, IPackageFragment fragment, String JavaDoc versionAttribute) {
40         super(header, fragment.getElementName(), null, versionAttribute);
41     }
42     
43     public ExportPackageObject(ManifestHeader header, String JavaDoc id, String JavaDoc version, String JavaDoc versionAttribute) {
44         super(header, id, version, versionAttribute);
45     }
46     
47     protected void processFriends() {
48         String JavaDoc[] 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; //$NON-NLS-1$
58
}
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"); //$NON-NLS-1$
73
else {
74                 Iterator JavaDoc 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"); //$NON-NLS-1$
100
else {
101             Iterator JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc value) {
129         String JavaDoc oldValue = getUsesDirective();
130         setDirective(Constants.USES_DIRECTIVE, value);
131         fHeader.update();
132         firePropertyChanged(this, Constants.USES_DIRECTIVE, oldValue, value);
133     }
134     
135     public String JavaDoc getUsesDirective() {
136         return getDirective(Constants.USES_DIRECTIVE);
137     }
138     
139     protected void appendValuesToBuffer(StringBuffer JavaDoc sb, TreeMap JavaDoc table) {
140         if (table == null)
141             return;
142         Object JavaDoc usesValue = null;
143         // remove the Uses directive, we will make sure to put it at the end
144
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 JavaDoc sb, Object JavaDoc usesValue) {
154         StringTokenizer JavaDoc tokenizer = null;
155         if (usesValue instanceof String JavaDoc)
156             tokenizer = new StringTokenizer JavaDoc((String JavaDoc)usesValue, ","); //$NON-NLS-1$
157
boolean newLine = (tokenizer != null) ? tokenizer.countTokens() > 3 :
158             ((ArrayList JavaDoc)usesValue).size() > 3;
159         String JavaDoc eol = getHeader().getLineLimiter();
160         sb.append(';');
161         if (newLine)
162             sb.append(eol).append(" "); //$NON-NLS-1$
163
sb.append(Constants.USES_DIRECTIVE);
164         sb.append(":=\""); //$NON-NLS-1$
165
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(" "); //$NON-NLS-1$
172
}
173             }
174         else {
175             ArrayList JavaDoc list = ((ArrayList JavaDoc)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(" "); //$NON-NLS-1$
181
}
182                 sb.append(list.get(i));
183             }
184         }
185         sb.append("\""); //$NON-NLS-1$
186
}
187
188     /* (non-Javadoc)
189      * @see org.eclipse.pde.internal.core.bundle.BundleObject#write(java.lang.String, java.io.PrintWriter)
190      */

191     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
192         // Used for text transfers for copy, cut, paste operations
193
writer.write(write());
194     }
195     
196     /**
197      * @param model
198      * @param header
199      * @param versionAttribute
200      */

201     public void reconnect(IBundleModel model, ExportPackageHeader header,
202             String JavaDoc versionAttribute) {
203         super.reconnect(model, header, versionAttribute);
204         // Non-Transient Field: Friends
205
reconnectFriends();
206     }
207
208     /**
209      *
210      */

211     private void reconnectFriends() {
212         // Get all the friends
213
Iterator JavaDoc keys = fFriends.keySet().iterator();
214         // Fill in appropriate transient field values for all friends
215
while (keys.hasNext()) {
216             String JavaDoc key = (String JavaDoc)keys.next();
217             PackageFriend friend = (PackageFriend)fFriends.get(key);
218             friend.reconnect(this);
219         }
220     }
221     
222 }
223
Popular Tags