KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javadocexport > RecentSettingsStore


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.internal.ui.javadocexport;
12
13 import java.io.File JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
20
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.core.resources.IWorkspaceRoot;
23 import org.eclipse.core.resources.ResourcesPlugin;
24 import org.eclipse.core.runtime.IPath;
25
26 import org.eclipse.jface.dialogs.IDialogSettings;
27
28 import org.eclipse.jdt.core.IJavaProject;
29 import org.eclipse.jdt.core.JavaCore;
30
31 import org.eclipse.jdt.ui.JavaUI;
32
33 public class RecentSettingsStore {
34     
35     private final String JavaDoc HREF= "href"; //$NON-NLS-1$
36
private final String JavaDoc DESTINATION= "destdir"; //$NON-NLS-1$
37
private final String JavaDoc ANTPATH= "antpath"; //$NON-NLS-1$
38

39     private final String JavaDoc SECTION_PROJECTS= "projects"; //$NON-NLS-1$
40

41     private final static char REF_SEPARATOR= ';';
42     
43     
44     //list of hrefs in string format
45
private Map JavaDoc fPerProjectSettings;
46     
47     /**
48      *
49      */

50     public RecentSettingsStore(IDialogSettings settings) {
51         fPerProjectSettings= new HashMap JavaDoc();
52         if (settings != null) {
53             load(settings);
54         }
55     }
56     
57     /**
58     * Method creates a list of data structes that contain
59     * The destination, antfile location and the list of library/project references for every
60     * project in the workspace.Defaults are created for new project.
61     */

62     private void load(IDialogSettings settings) {
63
64         IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
65         
66         IDialogSettings projectsSection= settings.getSection(SECTION_PROJECTS);
67         if (projectsSection != null) {
68             IDialogSettings[] sections= projectsSection.getSections();
69             for (int i= 0; i < sections.length; i++) {
70                 IDialogSettings curr= sections[i];
71                 String JavaDoc projectName= curr.getName();
72                 IProject project= root.getProject(projectName);
73                 //make sure project has not been removed
74
if (project.isAccessible()) {
75                     IJavaProject javaProject= JavaCore.create(project);
76                     if (!fPerProjectSettings.containsKey(javaProject)) {
77                         String JavaDoc hrefs= curr.get(HREF);
78                         if (hrefs == null) {
79                             hrefs= ""; //$NON-NLS-1$
80
}
81                         String JavaDoc destdir= curr.get(DESTINATION);
82                         if (destdir == null || destdir.length() == 0) {
83                             destdir= getDefaultDestination(javaProject);
84                         }
85                         String JavaDoc antpath= curr.get(ANTPATH);
86                         if (antpath == null || antpath.length() == 0) {
87                             antpath= getDefaultAntPath(javaProject);
88                         }
89                         ProjectData data= new ProjectData();
90                         data.setDestination(destdir);
91                         data.setAntpath(antpath);
92                         data.setHRefs(hrefs);
93                         if (!fPerProjectSettings.containsValue(javaProject))
94                             fPerProjectSettings.put(javaProject, data);
95                     }
96                 }
97             }
98         }
99         //finds projects in the workspace that have been added since the
100
//last time the wizard was run
101
IProject[] projects= root.getProjects();
102         for (int i= 0; i < projects.length; i++) {
103             IProject project= projects[i];
104             if (project.isAccessible()) {
105                 IJavaProject curr= JavaCore.create(project);
106                 if (!fPerProjectSettings.containsKey(curr)) {
107                     ProjectData data= new ProjectData();
108                     data.setDestination(getDefaultDestination(curr));
109                     data.setAntpath(getDefaultAntPath(curr));
110                     data.setHRefs(""); //$NON-NLS-1$
111
fPerProjectSettings.put(curr, data);
112                 }
113             }
114         }
115     }
116     
117     public void store(IDialogSettings settings) {
118         
119         IDialogSettings projectsSection= settings.addNewSection(SECTION_PROJECTS);
120
121         //Write all project information to DialogSettings.
122
Set JavaDoc keys= fPerProjectSettings.keySet();
123         for (Iterator JavaDoc iter= keys.iterator(); iter.hasNext();) {
124
125             IJavaProject curr= (IJavaProject) iter.next();
126
127             IDialogSettings proj= projectsSection.addNewSection(curr.getElementName());
128             if (!keys.contains(curr)) {
129                 proj.put(HREF, ""); //$NON-NLS-1$
130
proj.put(DESTINATION, ""); //$NON-NLS-1$
131
proj.put(ANTPATH, ""); //$NON-NLS-1$
132
} else {
133                 ProjectData data= (ProjectData) fPerProjectSettings.get(curr);
134                 proj.put(HREF, data.getHRefs());
135                 proj.put(DESTINATION, data.getDestination());
136                 proj.put(ANTPATH, data.getAntPath());
137             }
138             projectsSection.addSection(proj);
139         }
140     }
141
142     public void setProjectSettings(IJavaProject project, String JavaDoc destination, String JavaDoc antpath, String JavaDoc[] hrefs) {
143         ProjectData data= (ProjectData) fPerProjectSettings.get(project);
144         if (data == null) {
145             data= new ProjectData();
146         }
147         data.setDestination(destination);
148         data.setAntpath(antpath);
149         
150         StringBuffer JavaDoc refs= new StringBuffer JavaDoc();
151         for (int i= 0; i < hrefs.length; i++) {
152             if (i > 0) {
153                 refs.append(REF_SEPARATOR);
154             }
155             refs.append(hrefs[i]);
156             
157         }
158         data.setHRefs(refs.toString());
159     }
160     
161     public static String JavaDoc[] getRefTokens(String JavaDoc refs) {
162         StringTokenizer JavaDoc tok= new StringTokenizer JavaDoc(refs, String.valueOf(REF_SEPARATOR));
163         String JavaDoc[] res= new String JavaDoc[tok.countTokens()];
164         for (int i= 0; i < res.length; i++) {
165             res[i]= tok.nextToken();
166         }
167         return res;
168     }
169     
170     
171     
172     public String JavaDoc[] getHRefs(IJavaProject project) {
173         ProjectData data= (ProjectData) fPerProjectSettings.get(project);
174         if (data != null) {
175             String JavaDoc refs= data.getHRefs();
176             return getRefTokens(refs);
177         }
178         return new String JavaDoc[0];
179     }
180     
181     //for now if multiple projects are selected the destination
182
//feild will be empty,
183
public String JavaDoc getDestination(IJavaProject project) {
184
185         ProjectData data= (ProjectData) fPerProjectSettings.get(project);
186         if (data != null)
187             return data.getDestination();
188         else
189             return getDefaultDestination(project);
190     }
191     
192     public String JavaDoc getAntpath(IJavaProject project) {
193         ProjectData data= (ProjectData) fPerProjectSettings.get(project);
194         if (data != null)
195             return data.getAntPath();
196         else
197             return getDefaultAntPath(project);
198     }
199     
200     /// internal
201

202     
203     private String JavaDoc getDefaultAntPath(IJavaProject project) {
204         if (project != null) {
205             // The Javadoc.xml file can only be stored locally. So if
206
// the project isn't local then we can't provide a good
207
// default location.
208
IPath path= project.getProject().getLocation();
209             if (path != null)
210                 return path.append("javadoc.xml").toOSString(); //$NON-NLS-1$
211
}
212
213         return ""; //$NON-NLS-1$
214
}
215
216     private String JavaDoc getDefaultDestination(IJavaProject project) {
217         if (project != null) {
218             URL JavaDoc url= JavaUI.getProjectJavadocLocation(project);
219             //uses default if source is has http protocol
220
if (url == null || !url.getProtocol().equals("file")) { //$NON-NLS-1$
221
// Since Javadoc.exe is a local tool its output is local.
222
// So if the project isn't local then the default location
223
// can't be local to a project. So use #getLocation() to
224
// test this is fine here.
225
IPath path= project.getProject().getLocation();
226                 if (path != null)
227                     return path.append("doc").toOSString(); //$NON-NLS-1$
228
} else {
229                 //must do this to remove leading "/"
230
return (new File JavaDoc(url.getFile())).getPath();
231             }
232         }
233
234         return ""; //$NON-NLS-1$
235

236     }
237     
238     private static class ProjectData {
239
240         private String JavaDoc fHrefs;
241         private String JavaDoc fDestinationDir;
242         private String JavaDoc fAntPath;
243
244         public void setHRefs(String JavaDoc hrefs) {
245             if (hrefs == null)
246                 fHrefs= ""; //$NON-NLS-1$
247
else
248                 fHrefs= hrefs;
249         }
250
251         public void setDestination(String JavaDoc destination) {
252             if (destination == null)
253                 fDestinationDir= ""; //$NON-NLS-1$
254
else
255                 fDestinationDir= destination;
256         }
257
258         public void setAntpath(String JavaDoc antpath) {
259             if (antpath == null)
260                 fAntPath= ""; //$NON-NLS-1$
261
else
262                 fAntPath= antpath;
263         }
264
265         public String JavaDoc getHRefs() {
266             return fHrefs;
267         }
268
269         public String JavaDoc getDestination() {
270             return fDestinationDir;
271         }
272
273         public String JavaDoc getAntPath() {
274             return fAntPath;
275         }
276
277     }
278
279     
280 }
281
Popular Tags