001: /**
002: * <copyright></copyright> $Id: ProjectRegistryImpl.java 23333 2006-12-08 19:40:41Z jeichar $
003: */package net.refractions.udig.project.internal.impl;
004:
005: import java.io.File;
006: import java.io.IOException;
007: import java.util.Collection;
008: import java.util.Iterator;
009: import java.util.List;
010:
011: import net.refractions.udig.project.IProject;
012: import net.refractions.udig.project.internal.Messages;
013: import net.refractions.udig.project.internal.Project;
014: import net.refractions.udig.project.internal.ProjectFactory;
015: import net.refractions.udig.project.internal.ProjectPackage;
016: import net.refractions.udig.project.internal.ProjectPlugin;
017: import net.refractions.udig.project.internal.ProjectRegistry;
018: import net.refractions.udig.ui.PlatformGIS;
019:
020: import org.eclipse.core.runtime.IPath;
021: import org.eclipse.core.runtime.Platform;
022: import org.eclipse.emf.common.notify.Notification;
023: import org.eclipse.emf.common.notify.NotificationChain;
024: import org.eclipse.emf.common.notify.impl.AdapterImpl;
025: import org.eclipse.emf.common.util.URI;
026: import org.eclipse.emf.ecore.EClass;
027: import org.eclipse.emf.ecore.EStructuralFeature;
028: import org.eclipse.emf.ecore.InternalEObject;
029: import org.eclipse.emf.ecore.impl.ENotificationImpl;
030: import org.eclipse.emf.ecore.impl.EObjectImpl;
031: import org.eclipse.emf.ecore.resource.Resource;
032: import org.eclipse.emf.ecore.resource.ResourceSet;
033: import org.eclipse.emf.ecore.resource.Resource.Internal;
034: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
035: import org.eclipse.emf.ecore.util.EObjectResolvingEList;
036:
037: /**
038: * TODO Purpose of net.refractions.udig.project.internal.impl
039: * <p>
040: * </p>
041: *
042: * @author Jesse
043: * @since 1.0.0
044: * @generated
045: */
046: public class ProjectRegistryImpl extends EObjectImpl implements
047: ProjectRegistry {
048: /**
049: * <!-- begin-user-doc --> <!-- end-user-doc -->
050: * @generated
051: */
052: public static final String copyright = "uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004, Refractions Research Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; version 2.1 of the License. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details."; //$NON-NLS-1$
053:
054: /**
055: * The cached value of the '{@link #getProjects() <em>Projects</em>}' reference list. <!--
056: * begin-user-doc --> <!-- end-user-doc -->
057: *
058: * @see #getProjects()
059: * @generated NOT
060: * @ordered
061: */
062: protected List projects = null;
063:
064: /**
065: * The cached value of the '{@link #getCurrentProject() <em>Current Project</em>}' reference.
066: * <!-- begin-user-doc --> <!-- end-user-doc -->
067: * @see #getCurrentProject()
068: * @generated
069: * @ordered
070: */
071: protected Project currentProject = null;
072:
073: /**
074: * <!-- begin-user-doc --> <!-- end-user-doc -->
075: * @generated
076: */
077: protected ProjectRegistryImpl() {
078: super ();
079: }
080:
081: /**
082: * <!-- begin-user-doc --> <!-- end-user-doc -->
083: * @generated
084: */
085: protected EClass eStaticClass() {
086: return ProjectPackage.eINSTANCE.getProjectRegistry();
087: }
088:
089: /**
090: * <!-- begin-user-doc --> <!-- end-user-doc -->
091: *
092: * @generated NOT
093: */
094: @SuppressWarnings("unchecked")//$NON-NLS-1$
095: public List<Project> getProjects() {
096: if (projects == null) {
097: projects = new EObjectResolvingEList(Project.class, this ,
098: ProjectPackage.PROJECT_REGISTRY__PROJECTS);
099: }
100:
101: for (Iterator<Project> iter = projects.iterator(); iter
102: .hasNext();) {
103: if (iter.next().eResource() == null)
104: iter.remove();
105: }
106: return projects;
107: }
108:
109: /**
110: * @see net.refractions.udig.project.internal.ProjectRegistry#getDefaultProject()
111: */
112: public Project getDefaultProject() {
113: String projectName = Messages.ProjectRegistry_defaultName;
114: String path = new File(Platform.getLocation().toString()
115: + File.separatorChar + projectName).getAbsolutePath();
116: currentProject = getProject(path);
117: return currentProject;
118: }
119:
120: /**
121: * @see net.refractions.udig.project.internal.ProjectRegistry#getCurrentProject()
122: * @uml.property name="currentProject"
123: */
124: public Project getCurrentProject() {
125: Project p = getCurrentProjectGen();
126: if (p == null && getProjects().size() > 0)
127: p = getProjects().get(0);
128: if (p == null)
129: p = getDefaultProject();
130: return p;
131: }
132:
133: /**
134: * <!-- begin-user-doc --> <!-- end-user-doc -->
135: * @generated
136: */
137: public Project getCurrentProjectGen() {
138: if (currentProject != null && currentProject.eIsProxy()) {
139: Project oldCurrentProject = currentProject;
140: currentProject = (Project) eResolveProxy((InternalEObject) currentProject);
141: if (currentProject != oldCurrentProject) {
142: if (eNotificationRequired())
143: eNotify(new ENotificationImpl(
144: this ,
145: Notification.RESOLVE,
146: ProjectPackage.PROJECT_REGISTRY__CURRENT_PROJECT,
147: oldCurrentProject, currentProject));
148: }
149: }
150: return currentProject;
151: }
152:
153: /**
154: * <!-- begin-user-doc --> <!-- end-user-doc -->
155: * @generated
156: */
157: public Project basicGetCurrentProject() {
158: return currentProject;
159: }
160:
161: /**
162: * <!-- begin-user-doc --> <!-- end-user-doc -->
163: * @generated
164: */
165: public void setCurrentProject(Project newCurrentProject) {
166: Project oldCurrentProject = currentProject;
167: currentProject = newCurrentProject;
168: if (eNotificationRequired())
169: eNotify(new ENotificationImpl(this , Notification.SET,
170: ProjectPackage.PROJECT_REGISTRY__CURRENT_PROJECT,
171: oldCurrentProject, currentProject));
172: }
173:
174: @Override
175: public NotificationChain eSetResource(Internal resource,
176: NotificationChain notifications) {
177: if (resource == null
178: || resource.getResourceSet() != resourceSet
179: || resourceSet == null)
180: throw new AssertionError();
181: return super .eSetResource(resource, notifications);
182: }
183:
184: /**
185: * <!-- begin-user-doc --> <!-- end-user-doc -->
186: *
187: * @generated NOT
188: */
189: public Project getProject(URI uri) {
190: // There is a sporatic bug here. Remove this try/catch when it is fixed.
191: try {
192: if (findProject(uri) != null)
193: return findProject(uri);
194:
195: URI resourceURI = URI.createURI(uri.toString());
196: final ProjectRegistry registry = getProjectRegistry();
197: Resource resource2 = registry.eResource();
198: if (resource2 == null) {
199: System.out.println("null"); //$NON-NLS-1$
200: throw new Error(Messages.ProjectRegistryImpl_load_error);
201: }
202: ResourceSet resourceSet2 = resource2.getResourceSet();
203: Resource resource = resourceSet2
204: .createResource(resourceURI);
205: try {
206: resource.load(null);
207: } catch (IOException e1) {
208: // resource doesn't exist. That is ok.
209: }
210: Project tmpProject;
211: if (resource.getContents().isEmpty()) {
212: tmpProject = createProject(uri, resource);
213: } else {
214: tmpProject = (Project) resource.getContents().get(0);
215: }
216:
217: final Project newProject = tmpProject;
218:
219: PlatformGIS.syncInDisplayThread(new Runnable() {
220:
221: public void run() {
222: try {
223: setCurrentProject(newProject);
224: registry.getProjects().add(newProject);
225: } catch (Exception e) {
226: e.printStackTrace();
227: }
228: }
229: });
230:
231: return newProject;
232: } catch (IllegalArgumentException e) {
233: e.printStackTrace();
234: // System.err.println("GetProject URI: " +uri);
235: }
236: return null; // remove this too
237: }
238:
239: private Project findProject(URI uri) {
240:
241: for (Iterator iter = getProjects().iterator(); iter.hasNext();) {
242: Project project = (Project) iter.next();
243: if (isURIEqual(project.eResource().getURI(), uri))
244: return project;
245: }
246: return null;
247: }
248:
249: public static boolean isURIEqual(URI uri, URI uri2) {
250: if (uri.equals(uri2))
251: return true;
252:
253: String uriString = uri.toString();
254: String uri2String = uri2.toString();
255: uriString = replaceBackSlashes(uriString);
256: uri2String = replaceBackSlashes(uri2String);
257:
258: uriString = uriString.replace("://", ":/"); //$NON-NLS-1$//$NON-NLS-2$
259: uri2String = uri2String.replace("://", ":/"); //$NON-NLS-1$//$NON-NLS-2$
260: return uriString.equals(uri2String);
261: }
262:
263: private static String replaceBackSlashes(String uriString) {
264: StringBuffer buffer = new StringBuffer(uriString.length());
265: for (int i = 0; i < uriString.length(); i++) {
266: char charAt = uriString.charAt(i);
267: if (charAt == '\\')
268: buffer.append('/');
269: else {
270: buffer.append(charAt);
271: }
272: }
273: return buffer.toString();
274: }
275:
276: /**
277: *
278: * @param query
279: * @param query2
280: * @param result
281: * @return
282: */
283: private static boolean compareStrings(String query, String query2) {
284: if (query == null) {
285: if (query2 != null)
286: return false;
287: } else {
288: if (!query.equals(query2))
289: return false;
290: }
291: return true;
292: }
293:
294: @SuppressWarnings("unchecked")//$NON-NLS-1$
295: private Project createProject(URI uri, Resource resource) {
296: Project tmpProject = ProjectFactory.eINSTANCE.createProject();
297: String path = uri.toFileString();
298: int start = path.indexOf(File.separator) != -1 ? path
299: .lastIndexOf(File.separator) : 0;
300: int end = path.indexOf('.') != -1 ? path.lastIndexOf('.')
301: : path.length() - 1;
302: tmpProject.setName(path.substring(start + 1, end));
303: resource.getContents().add(tmpProject);
304: resource.setModified(true);
305:
306: return tmpProject;
307: }
308:
309: public Project getProject(String projectPath) {
310: // HACK
311: if (!projectPath.startsWith("file://")) { //$NON-NLS-1$
312: int index = 0;
313: for (int i = 0; i < projectPath.length(); i++) {
314: if (projectPath.charAt(i) == File.separatorChar)
315: index++;
316: else
317: break;
318: }
319: projectPath = "file://" + projectPath.substring(index); //$NON-NLS-1$
320: }
321: // DONE HACK
322: final String path = projectPath + File.separatorChar
323: + "project.uprj"; //$NON-NLS-1$
324: final URI uri = URI.createURI(path);
325: Project project = getProject(uri);
326: return project;
327: }
328:
329: /**
330: * <!-- begin-user-doc --> <!-- end-user-doc -->
331: * @generated
332: */
333: public Object eGet(EStructuralFeature eFeature, boolean resolve) {
334: switch (eDerivedStructuralFeatureID(eFeature)) {
335: case ProjectPackage.PROJECT_REGISTRY__CURRENT_PROJECT:
336: if (resolve)
337: return getCurrentProject();
338: return basicGetCurrentProject();
339: case ProjectPackage.PROJECT_REGISTRY__PROJECTS:
340: return getProjects();
341: }
342: return eDynamicGet(eFeature, resolve);
343: }
344:
345: /**
346: * <!-- begin-user-doc --> <!-- end-user-doc -->
347: * @generated
348: */
349: @SuppressWarnings("unchecked")//$NON-NLS-1$
350: public void eSet(EStructuralFeature eFeature, Object newValue) {
351: switch (eDerivedStructuralFeatureID(eFeature)) {
352: case ProjectPackage.PROJECT_REGISTRY__CURRENT_PROJECT:
353: setCurrentProject((Project) newValue);
354: return;
355: case ProjectPackage.PROJECT_REGISTRY__PROJECTS:
356: getProjects().clear();
357: getProjects().addAll((Collection) newValue);
358: return;
359: }
360: eDynamicSet(eFeature, newValue);
361: }
362:
363: /**
364: * <!-- begin-user-doc --> <!-- end-user-doc -->
365: * @generated
366: */
367: public void eUnset(EStructuralFeature eFeature) {
368: switch (eDerivedStructuralFeatureID(eFeature)) {
369: case ProjectPackage.PROJECT_REGISTRY__CURRENT_PROJECT:
370: setCurrentProject((Project) null);
371: return;
372: case ProjectPackage.PROJECT_REGISTRY__PROJECTS:
373: getProjects().clear();
374: return;
375: }
376: eDynamicUnset(eFeature);
377: }
378:
379: /**
380: * <!-- begin-user-doc --> <!-- end-user-doc -->
381: * @generated
382: */
383: public boolean eIsSet(EStructuralFeature eFeature) {
384: switch (eDerivedStructuralFeatureID(eFeature)) {
385: case ProjectPackage.PROJECT_REGISTRY__CURRENT_PROJECT:
386: return currentProject != null;
387: case ProjectPackage.PROJECT_REGISTRY__PROJECTS:
388: return projects != null && !projects.isEmpty();
389: }
390: return eDynamicIsSet(eFeature);
391: }
392:
393: /**
394: * @see net.refractions.udig.IProjectRegistry#getDefaultIProject()
395: */
396: public IProject getDefaultIProject() {
397: return getDefaultProject();
398: }
399:
400: /**
401: * @see net.refractions.udig.IProjectRegistry#getCurrentIProject()
402: */
403: public IProject getCurrentIProject() {
404: return getCurrentProject();
405: }
406:
407: /**
408: * @see net.refractions.udig.IProjectRegistry#setICurrentProject(net.refractions.udig.IProject)
409: */
410: public void setICurrentProject(IProject value) {
411: setCurrentProject((Project) value);
412: }
413:
414: /**
415: * @see net.refractions.udig.IProjectRegistry#getIProject(java.lang.String)
416: */
417: public IProject getIProject(String projectPath) {
418: return getProject(projectPath);
419: }
420:
421: /**
422: * @see net.refractions.udig.IProjectRegistry#getIProjects()
423: */
424: public List getIProjects() {
425: return getProjects();
426: }
427:
428: private static ResourceSetImpl resourceSet;
429:
430: private static ProjectRegistry projectRegistry;
431:
432: /**
433: * Loads the project registry.
434: *
435: * @return the loaded project registry.
436: */
437: @SuppressWarnings("unchecked")
438: private static ProjectRegistry load() {
439: ProjectRegistry projectRegistry = null;
440: try {
441: IPath registrypath = ProjectPlugin.getPlugin()
442: .getStateLocation().append("ProjectRegistry"); //$NON-NLS-1$
443: URI uri = URI
444: .createURI("file://" + registrypath.toOSString()); //$NON-NLS-1$
445: // Load the resource through the editing domain.
446: //
447: resourceSet = new ResourceSetImpl();
448: if (registrypath.toFile().exists()) {
449: Resource resource = resourceSet.getResource(uri, true);
450: resourceSet.eSetDeliver(false);
451: projectRegistry = (ProjectRegistry) resource
452: .getContents().get(0);
453: if (projectRegistry == null) {
454: projectRegistry = ProjectFactory.eINSTANCE
455: .createProjectRegistry();
456: resource.getContents().add(projectRegistry);
457: }
458: } else {
459: Resource resource = resourceSet.createResource(uri);
460: if (resource == null)
461: throw new Exception(
462: "Unable to load or create ProjectRegistry resource"); //$NON-NLS-1$
463:
464: projectRegistry = new ProjectRegistryImpl();
465: resource.getContents().add(projectRegistry);
466: }
467: } catch (Exception exception) {
468: ProjectPlugin.INSTANCE.log(exception);
469: }
470: if (projectRegistry == null)
471: ProjectPlugin.log("Error getting project from resource"); //$NON-NLS-1$
472: else {
473: final ProjectRegistry registry = projectRegistry;
474: projectRegistry.eAdapters().add(new AdapterImpl() {
475: /**
476: * @see org.eclipse.emf.common.notify.impl.AdapterImpl#notifyChanged(org.eclipse.emf.common.notify.Notification)
477: */
478: public void notifyChanged(Notification msg) {
479: if (msg.getFeatureID(ProjectRegistry.class) == ProjectPackage.PROJECT_REGISTRY__PROJECTS) {
480: registry.eResource().setModified(true);
481: }
482: }
483: });
484: }
485: resourceSet.eSetDeliver(false);
486: return projectRegistry;
487: }
488:
489: /**
490: * @see net.refractions.udig.project.internal.ProjectFactory#getProjectRegistry()
491: * @uml.property name="projectRegistry"
492: */
493: public static ProjectRegistry getProjectRegistry() {
494: synchronized (ProjectRegistry.class) {
495: if (projectRegistry == null) {
496: projectRegistry = load();
497: }
498: if (projectRegistry.eResource() == null
499: || projectRegistry.eResource().getResourceSet() != resourceSet
500: || resourceSet == null)
501: throw new AssertionError();
502: }
503: return projectRegistry;
504: }
505:
506: } // ProjectRegistryImpl
|