KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > BinaryRepositoryProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 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;
12
13 import org.eclipse.core.resources.IContainer;
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.IFileModificationValidator;
16 import org.eclipse.core.resources.IFolder;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IProjectDescription;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.team.IMoveDeleteHook;
21 import org.eclipse.core.resources.team.IResourceTree;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.Status;
26 import org.eclipse.team.core.RepositoryProvider;
27
28 public class BinaryRepositoryProvider extends RepositoryProvider {
29     private IMoveDeleteHook moveDeleteHook;
30     private IFileModificationValidator fileModificationValidator;
31     
32     public static final String JavaDoc EXTERNAL_PROJECT_VALUE = "external"; //$NON-NLS-1$
33

34     class BinaryMoveDeleteHook implements IMoveDeleteHook {
35         /**
36          * @see org.eclipse.core.resources.team.IMoveDeleteHook#deleteFile(org.eclipse.core.resources.team.IResourceTree, org.eclipse.core.resources.IFile, int, org.eclipse.core.runtime.IProgressMonitor)
37          */

38         public boolean deleteFile(
39             IResourceTree tree,
40             IFile file,
41             int updateFlags,
42             IProgressMonitor monitor) {
43             if (isBinaryResource(file, true))
44                 tree.failed(createProblemStatus());
45             else
46                 tree.standardDeleteFile(file, updateFlags, monitor);
47             return true;
48         }
49
50         /**
51          * @see org.eclipse.core.resources.team.IMoveDeleteHook#deleteFolder(org.eclipse.core.resources.team.IResourceTree, org.eclipse.core.resources.IFolder, int, org.eclipse.core.runtime.IProgressMonitor)
52          */

53         public boolean deleteFolder(
54             IResourceTree tree,
55             IFolder folder,
56             int updateFlags,
57             IProgressMonitor monitor) {
58             if (isBinaryResource(folder, true))
59                 tree.failed(createProblemStatus());
60             else
61                 tree.standardDeleteFolder(folder, updateFlags, monitor);
62             return true;
63         }
64
65         /**
66          * @see org.eclipse.core.resources.team.IMoveDeleteHook#deleteProject(org.eclipse.core.resources.team.IResourceTree, org.eclipse.core.resources.IProject, int, org.eclipse.core.runtime.IProgressMonitor)
67          */

68         public boolean deleteProject(
69             IResourceTree tree,
70             IProject project,
71             int updateFlags,
72             IProgressMonitor monitor) {
73             return false;
74         }
75
76         /**
77          * @see org.eclipse.core.resources.team.IMoveDeleteHook#moveFile(org.eclipse.core.resources.team.IResourceTree, org.eclipse.core.resources.IFile, org.eclipse.core.resources.IFile, int, org.eclipse.core.runtime.IProgressMonitor)
78          */

79         public boolean moveFile(
80             IResourceTree tree,
81             IFile source,
82             IFile destination,
83             int updateFlags,
84             IProgressMonitor monitor) {
85             if (isBinaryResource(source, false))
86                 tree.failed(createProblemStatus());
87             else
88                 tree.standardMoveFile(source, destination, updateFlags, monitor);
89             return true;
90         }
91
92         /**
93          * @see org.eclipse.core.resources.team.IMoveDeleteHook#moveFolder(org.eclipse.core.resources.team.IResourceTree, org.eclipse.core.resources.IFolder, org.eclipse.core.resources.IFolder, int, org.eclipse.core.runtime.IProgressMonitor)
94          */

95         public boolean moveFolder(
96             IResourceTree tree,
97             IFolder source,
98             IFolder destination,
99             int updateFlags,
100             IProgressMonitor monitor) {
101             if (isBinaryResource(source, false))
102                 tree.failed(createProblemStatus());
103             else
104                 tree.standardMoveFolder(source, destination, updateFlags, monitor);
105             return true;
106         }
107
108         /**
109          * @see org.eclipse.core.resources.team.IMoveDeleteHook#moveProject(org.eclipse.core.resources.team.IResourceTree, org.eclipse.core.resources.IProject, org.eclipse.core.resources.IProjectDescription, int, org.eclipse.core.runtime.IProgressMonitor)
110          */

111         public boolean moveProject(
112             IResourceTree tree,
113             IProject source,
114             IProjectDescription description,
115             int updateFlags,
116             IProgressMonitor monitor) {
117             return false;
118         }
119     }
120
121     class BinaryFileModificationValidator
122         implements IFileModificationValidator {
123         /**
124          * @see org.eclipse.core.resources.IFileModificationValidator#validateEdit(org.eclipse.core.resources.IFile, java.lang.Object)
125          */

126         public IStatus validateEdit(IFile[] files, Object JavaDoc context) {
127             for (int i = 0; i < files.length; i++) {
128                 if (isBinaryResource(files[i], false)) {
129                     return createProblemStatus();
130                 }
131             }
132             return createOKStatus();
133         }
134
135         /**
136          * @see org.eclipse.core.resources.IFileModificationValidator#validateSave(org.eclipse.core.resources.IFile)
137          */

138         public IStatus validateSave(IFile file) {
139             if (isBinaryResource(file, false)) {
140                 return createProblemStatus();
141             }
142             return createOKStatus();
143         }
144     }
145
146     public BinaryRepositoryProvider() {
147         moveDeleteHook = new BinaryMoveDeleteHook();
148         fileModificationValidator = new BinaryFileModificationValidator();
149     }
150
151     /**
152      * @see org.eclipse.team.core.RepositoryProvider#configureProject()
153      */

154     public void configureProject() throws CoreException {
155         IProject project = getProject();
156         project.setPersistentProperty(PDECore.EXTERNAL_PROJECT_PROPERTY, EXTERNAL_PROJECT_VALUE);
157     }
158
159     /**
160      * @see org.eclipse.core.resources.IProjectNature#deconfigure()
161      */

162     public void deconfigure() throws CoreException {
163         IProject project = getProject();
164         project.setPersistentProperty(PDECore.EXTERNAL_PROJECT_PROPERTY, null);
165     }
166
167     /**
168      * @see org.eclipse.team.core.RepositoryProvider#getFileModificationValidator()
169      */

170     public IFileModificationValidator getFileModificationValidator() {
171         return fileModificationValidator;
172     }
173
174     /**
175      * @see org.eclipse.team.core.RepositoryProvider#getID()
176      */

177     public String JavaDoc getID() {
178         return PDECore.BINARY_REPOSITORY_PROVIDER;
179     }
180
181     /**
182      * @see org.eclipse.team.core.RepositoryProvider#getMoveDeleteHook()
183      */

184     public IMoveDeleteHook getMoveDeleteHook() {
185         return moveDeleteHook;
186     }
187
188     private boolean isBinaryResource(IResource resource, boolean excludeProjectChildren) {
189         IContainer parent = resource.getParent();
190         
191         // Test for resource links
192
if (!excludeProjectChildren || !(parent instanceof IProject)) {
193             if (resource.isLinked())
194             return true;
195         }
196         
197         // Test for resources that are in linked folders
198

199         while (parent instanceof IFolder) {
200             IFolder folder = (IFolder) parent;
201             if (folder.isLinked())
202                 return true;
203             parent = folder.getParent();
204         }
205         return false;
206     }
207
208     private IStatus createProblemStatus() {
209         String JavaDoc message = PDECoreMessages.BinaryRepositoryProvider_veto;
210         return new Status(
211             IStatus.ERROR,
212             PDECore.PLUGIN_ID,
213             IStatus.OK,
214             message,
215             null);
216     }
217
218     private IStatus createOKStatus() {
219         return new Status(IStatus.OK, PDECore.PLUGIN_ID, IStatus.OK, "", //$NON-NLS-1$
220
null);
221     }
222     
223     public boolean canHandleLinkedResources() {
224         return true;
225     }
226 }
227
Popular Tags