KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > model > ProjectPersistentPropertyTester


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.ui.internal.ide.model;
12
13 import org.eclipse.core.expressions.PropertyTester;
14 import org.eclipse.core.internal.resources.mapping.ResourceMapping;
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.QualifiedName;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.ui.IActionFilter;
23 import org.eclipse.ui.IResourceActionFilter;
24 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
25
26 /**
27  * Property tester for detecting the exisitance of a particular persitent
28  * property on all the projects of a ResourceMapping.
29  *
30  * @since 3.1
31  */

32 public class ProjectPersistentPropertyTester extends PropertyTester {
33     
34     private static final String JavaDoc ALLOW_UNSET_PROJECTS = "allowUnsetProjects"; //$NON-NLS-1$
35

36     private static final IActionFilter filter = new WorkbenchResource() {
37         
38         /* (non-Javadoc)
39          * @see org.eclipse.ui.internal.ide.model.WorkbenchResource#getBaseImage(org.eclipse.core.resources.IResource)
40          */

41         protected ImageDescriptor getBaseImage(IResource resource) {
42             return null;
43         }
44     };
45
46     /* (non-Javadoc)
47      * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object)
48      */

49     public boolean test(Object JavaDoc receiver, String JavaDoc property, Object JavaDoc[] args, Object JavaDoc expectedValue) {
50         if (receiver instanceof ResourceMapping) {
51             if (property.equals(IResourceActionFilter.PROJECT_PERSISTENT_PROPERTY)) {
52                 if(args == null) return false;
53                 String JavaDoc persitentPropertyEntry = (String JavaDoc)args[0];
54                 boolean allowUnsetProjects = false;
55                 if (args.length > 1)
56                     allowUnsetProjects = args[1].equals(ALLOW_UNSET_PROJECTS);
57                 IProject[] projects = ((ResourceMapping)receiver).getProjects();
58                 boolean atLeastOne = false;
59                 for (int i = 0; i < projects.length; i++) {
60                     IProject project = projects[i];
61                     if (filter.testAttribute(project, property, persitentPropertyEntry)) {
62                         atLeastOne = true;
63                     } else if (!allowUnsetProjects) {
64                         return false;
65                     } else {
66                         // Check to see if the persistant property is present
67
// If it is, we fail since it must be set to somethings else
68
try {
69                             if (project != null && project.isAccessible() && project.getPersistentProperty(getPropertyKey(persitentPropertyEntry)) != null)
70                                 return false;
71                         } catch (CoreException e) {
72                             final String JavaDoc message = "Core exception while testing project persistent property"; //$NON-NLS-1$
73
IDEWorkbenchPlugin.log(message,
74                                     new Status(IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH,
75                                             IStatus.ERROR, message, e));
76                             // Just continue
77
}
78                     }
79                 }
80                 return atLeastOne;
81             }
82         } else if (receiver instanceof IResource) {
83             if (property.equals(IResourceActionFilter.PROJECT_PERSISTENT_PROPERTY)) {
84                 if(args == null) return false;
85                 String JavaDoc persitentPropertyEntry = (String JavaDoc)args[0];
86                 IProject project = ((IResource)receiver).getProject();
87                 return filter.testAttribute(project, property, persitentPropertyEntry);
88             }
89         }
90         
91         return false;
92     }
93
94     private QualifiedName getPropertyKey(String JavaDoc value) {
95         String JavaDoc propertyName;
96         int i = value.indexOf('=');
97         if (i != -1) {
98             propertyName = value.substring(0, i).trim();
99         } else {
100             propertyName = value.trim();
101         }
102         QualifiedName key;
103         int dot = propertyName.lastIndexOf('.');
104         if (dot != -1) {
105             key = new QualifiedName(propertyName.substring(0, dot),
106                     propertyName.substring(dot + 1));
107         } else {
108             key = new QualifiedName(null, propertyName);
109         }
110         return key;
111     }
112
113 }
114
Popular Tags