1 11 package org.eclipse.jdt.internal.ui.javadocexport; 12 13 import java.io.File ; 14 import java.net.URL ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 import java.util.Set ; 19 import java.util.StringTokenizer ; 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 HREF= "href"; private final String DESTINATION= "destdir"; private final String ANTPATH= "antpath"; 39 private final String SECTION_PROJECTS= "projects"; 41 private final static char REF_SEPARATOR= ';'; 42 43 44 private Map fPerProjectSettings; 46 47 50 public RecentSettingsStore(IDialogSettings settings) { 51 fPerProjectSettings= new HashMap (); 52 if (settings != null) { 53 load(settings); 54 } 55 } 56 57 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 projectName= curr.getName(); 72 IProject project= root.getProject(projectName); 73 if (project.isAccessible()) { 75 IJavaProject javaProject= JavaCore.create(project); 76 if (!fPerProjectSettings.containsKey(javaProject)) { 77 String hrefs= curr.get(HREF); 78 if (hrefs == null) { 79 hrefs= ""; } 81 String destdir= curr.get(DESTINATION); 82 if (destdir == null || destdir.length() == 0) { 83 destdir= getDefaultDestination(javaProject); 84 } 85 String 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 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(""); 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 Set keys= fPerProjectSettings.keySet(); 123 for (Iterator 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, ""); proj.put(DESTINATION, ""); proj.put(ANTPATH, ""); } 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 destination, String antpath, String [] 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 refs= new StringBuffer (); 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 [] getRefTokens(String refs) { 162 StringTokenizer tok= new StringTokenizer (refs, String.valueOf(REF_SEPARATOR)); 163 String [] res= new String [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 [] getHRefs(IJavaProject project) { 173 ProjectData data= (ProjectData) fPerProjectSettings.get(project); 174 if (data != null) { 175 String refs= data.getHRefs(); 176 return getRefTokens(refs); 177 } 178 return new String [0]; 179 } 180 181 public String 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 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 202 203 private String getDefaultAntPath(IJavaProject project) { 204 if (project != null) { 205 IPath path= project.getProject().getLocation(); 209 if (path != null) 210 return path.append("javadoc.xml").toOSString(); } 212 213 return ""; } 215 216 private String getDefaultDestination(IJavaProject project) { 217 if (project != null) { 218 URL url= JavaUI.getProjectJavadocLocation(project); 219 if (url == null || !url.getProtocol().equals("file")) { IPath path= project.getProject().getLocation(); 226 if (path != null) 227 return path.append("doc").toOSString(); } else { 229 return (new File (url.getFile())).getPath(); 231 } 232 } 233 234 return ""; 236 } 237 238 private static class ProjectData { 239 240 private String fHrefs; 241 private String fDestinationDir; 242 private String fAntPath; 243 244 public void setHRefs(String hrefs) { 245 if (hrefs == null) 246 fHrefs= ""; else 248 fHrefs= hrefs; 249 } 250 251 public void setDestination(String destination) { 252 if (destination == null) 253 fDestinationDir= ""; else 255 fDestinationDir= destination; 256 } 257 258 public void setAntpath(String antpath) { 259 if (antpath == null) 260 fAntPath= ""; else 262 fAntPath= antpath; 263 } 264 265 public String getHRefs() { 266 return fHrefs; 267 } 268 269 public String getDestination() { 270 return fDestinationDir; 271 } 272 273 public String getAntPath() { 274 return fAntPath; 275 } 276 277 } 278 279 280 } 281 | Popular Tags |