KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > CreatePackageFragmentOperation


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.jdt.internal.core;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.resources.IContainer;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.jdt.core.IJavaElement;
20 import org.eclipse.jdt.core.IJavaModelStatus;
21 import org.eclipse.jdt.core.IJavaModelStatusConstants;
22 import org.eclipse.jdt.core.IJavaProject;
23 import org.eclipse.jdt.core.IPackageFragment;
24 import org.eclipse.jdt.core.IPackageFragmentRoot;
25 import org.eclipse.jdt.core.JavaConventions;
26 import org.eclipse.jdt.core.JavaCore;
27 import org.eclipse.jdt.core.JavaModelException;
28 import org.eclipse.jdt.core.compiler.CharOperation;
29 import org.eclipse.jdt.internal.core.util.Messages;
30 import org.eclipse.jdt.internal.core.util.Util;
31
32 /**
33  * This operation creates a new package fragment under a given package fragment root.
34  * The following must be specified: <ul>
35  * <li>the package fragment root
36  * <li>the package name
37  * </ul>
38  * <p>Any needed folders/package fragments are created.
39  * If the package fragment already exists, this operation has no effect.
40  * The result elements include the <code>IPackageFragment</code> created and any side effect
41  * package fragments that were created.
42  *
43  * <p>NOTE: A default package fragment exists by default for a given root.
44  *
45  * <p>Possible exception conditions: <ul>
46  * <li>Package fragment root is read-only
47  * <li>Package fragment's name is taken by a simple (non-folder) resource
48  * </ul>
49  */

50 public class CreatePackageFragmentOperation extends JavaModelOperation {
51     /**
52      * The fully qualified, dot-delimited, package name.
53      */

54     protected String JavaDoc[] pkgName;
55 /**
56  * When executed, this operation will create a package fragment with the given name
57  * under the given package fragment root. The dot-separated name is broken into
58  * segments. Intermediate folders are created as required for each segment.
59  * If the folders already exist, this operation has no effect.
60  */

61 public CreatePackageFragmentOperation(IPackageFragmentRoot parentElement, String JavaDoc packageName, boolean force) {
62     super(null, new IJavaElement[]{parentElement}, force);
63     this.pkgName = packageName == null ? null : Util.getTrimmedSimpleNames(packageName);
64 }
65 /**
66  * Execute the operation - creates the new package fragment and any
67  * side effect package fragments.
68  *
69  * @exception JavaModelException if the operation is unable to complete
70  */

71 protected void executeOperation() throws JavaModelException {
72     try {
73         JavaElementDelta delta = null;
74         PackageFragmentRoot root = (PackageFragmentRoot) getParentElement();
75         beginTask(Messages.operation_createPackageFragmentProgress, this.pkgName.length);
76         IContainer parentFolder = (IContainer) root.getResource();
77         String JavaDoc[] sideEffectPackageName = CharOperation.NO_STRINGS;
78         ArrayList JavaDoc results = new ArrayList JavaDoc(this.pkgName.length);
79         char[][] inclusionPatterns = root.fullInclusionPatternChars();
80         char[][] exclusionPatterns = root.fullExclusionPatternChars();
81         int i;
82         for (i = 0; i < this.pkgName.length; i++) {
83             String JavaDoc subFolderName = this.pkgName[i];
84             sideEffectPackageName = Util.arrayConcat(sideEffectPackageName, subFolderName);
85             IResource subFolder = parentFolder.findMember(subFolderName);
86             if (subFolder == null) {
87                 createFolder(parentFolder, subFolderName, force);
88                 parentFolder = parentFolder.getFolder(new Path(subFolderName));
89                 IPackageFragment addedFrag = root.getPackageFragment(sideEffectPackageName);
90                 if (!Util.isExcluded(parentFolder, inclusionPatterns, exclusionPatterns)) {
91                     if (delta == null) {
92                         delta = newJavaElementDelta();
93                     }
94                     delta.added(addedFrag);
95                 }
96                 results.add(addedFrag);
97             } else {
98                 parentFolder = (IContainer) subFolder;
99             }
100             worked(1);
101         }
102         if (results.size() > 0) {
103             this.resultElements = new IJavaElement[results.size()];
104             results.toArray(this.resultElements);
105             if (delta != null) {
106                 addDelta(delta);
107             }
108         }
109     } finally {
110         done();
111     }
112 }
113 /**
114  * Possible failures: <ul>
115  * <li>NO_ELEMENTS_TO_PROCESS - the root supplied to the operation is
116  * <code>null</code>.
117  * <li>INVALID_NAME - the name provided to the operation
118  * is <code>null</code> or is not a valid package fragment name.
119  * <li>READ_ONLY - the root provided to this operation is read only.
120  * <li>NAME_COLLISION - there is a pre-existing resource (file)
121  * with the same name as a folder in the package fragment's hierarchy.
122  * <li>ELEMENT_NOT_PRESENT - the underlying resource for the root is missing
123  * </ul>
124  * @see IJavaModelStatus
125  * @see JavaConventions
126  */

127 public IJavaModelStatus verify() {
128     IJavaElement parentElement = getParentElement();
129     if (parentElement == null) {
130         return new JavaModelStatus(IJavaModelStatusConstants.NO_ELEMENTS_TO_PROCESS);
131     }
132     
133     String JavaDoc packageName = this.pkgName == null ? null : Util.concatWith(this.pkgName, '.');
134     IJavaProject project = parentElement.getJavaProject();
135     if (this.pkgName == null || (this.pkgName.length > 0 && JavaConventions.validatePackageName(packageName, project.getOption(JavaCore.COMPILER_SOURCE, true), project.getOption(JavaCore.COMPILER_COMPLIANCE, true)).getSeverity() == IStatus.ERROR)) {
136         return new JavaModelStatus(IJavaModelStatusConstants.INVALID_NAME, packageName);
137     }
138     IPackageFragmentRoot root = (IPackageFragmentRoot) getParentElement();
139     if (root.isReadOnly()) {
140         return new JavaModelStatus(IJavaModelStatusConstants.READ_ONLY, root);
141     }
142     IContainer parentFolder = (IContainer) root.getResource();
143     int i;
144     for (i = 0; i < this.pkgName.length; i++) {
145         IResource subFolder = parentFolder.findMember(this.pkgName[i]);
146         if (subFolder != null) {
147             if (subFolder.getType() != IResource.FOLDER) {
148                 return new JavaModelStatus(
149                     IJavaModelStatusConstants.NAME_COLLISION,
150                     Messages.bind(Messages.status_nameCollision, subFolder.getFullPath().toString()));
151             }
152             parentFolder = (IContainer) subFolder;
153         }
154     }
155     return JavaModelStatus.VERIFIED_OK;
156 }
157 }
158
Popular Tags