Source Code Cross Referenced for ContainerGenerator.java in  » IDE-Eclipse » ui-ide » org » eclipse » ui » dialogs » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Eclipse » ui ide » org.eclipse.ui.dialogs 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Copyright (c) 2000, 2006 IBM Corporation and others.
003:         * All rights reserved. This program and the accompanying materials
004:         * are made available under the terms of the Eclipse Public License v1.0
005:         * which accompanies this distribution, and is available at
006:         * http://www.eclipse.org/legal/epl-v10.html
007:         *
008:         * Contributors:
009:         *     IBM Corporation - initial API and implementation
010:         *******************************************************************************/package org.eclipse.ui.dialogs;
011:
012:        import org.eclipse.core.resources.IContainer;
013:        import org.eclipse.core.resources.IFolder;
014:        import org.eclipse.core.resources.IProject;
015:        import org.eclipse.core.resources.IResource;
016:        import org.eclipse.core.resources.IWorkspaceRoot;
017:        import org.eclipse.core.resources.IWorkspaceRunnable;
018:        import org.eclipse.core.runtime.CoreException;
019:        import org.eclipse.core.runtime.IPath;
020:        import org.eclipse.core.runtime.IProgressMonitor;
021:        import org.eclipse.core.runtime.IStatus;
022:        import org.eclipse.core.runtime.OperationCanceledException;
023:        import org.eclipse.core.runtime.Path;
024:        import org.eclipse.core.runtime.Status;
025:        import org.eclipse.core.runtime.SubProgressMonitor;
026:        import org.eclipse.osgi.util.NLS;
027:        import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
028:        import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
029:
030:        /**
031:         * For creating folder resources that currently do not exist, 
032:         * along a given workspace path.
033:         * <p>
034:         * This class may be instantiated; it is not intended to be subclassed.
035:         * </p>
036:         * <p>
037:         * Example usage:
038:         * <pre>
039:         * ContainerGenerator gen = new ContainerGenerator(new Path("/A/B"));
040:         * IContainer res = null;
041:         * try {
042:         *   res = gen.getContainer(monitor); // creates project A and folder B if required
043:         * } catch (CoreException e) {
044:         *   // handle failure
045:         * } catch (OperationCanceledException e) {
046:         *   // handle cancelation
047:         * }
048:         * </pre>
049:         * </p>
050:         */
051:        public class ContainerGenerator {
052:            private IPath containerFullPath;
053:
054:            private IContainer container;
055:
056:            /**
057:             * Creates a generator for the container resource (folder or project) at the
058:             * given workspace path. Assumes the path has already been validated.
059:             * <p>
060:             * Call <code>getContainer</code> to create any missing resources along the
061:             * path.
062:             * </p>
063:             *
064:             * @param containerPath the workspace path of the container
065:             */
066:            public ContainerGenerator(IPath containerPath) {
067:                super ();
068:                this .containerFullPath = containerPath;
069:            }
070:
071:            /**
072:             * Creates a folder resource for the given folder handle.
073:             *
074:             * @param folderHandle the handle to create a folder resource
075:             * @param monitor the progress monitor to show visual progress
076:             * @return the folder handle (<code>folderHandle</code>)
077:             * @exception CoreException if the operation fails
078:             * @exception OperationCanceledException if the operation is canceled
079:             */
080:            private IFolder createFolder(IFolder folderHandle,
081:                    IProgressMonitor monitor) throws CoreException {
082:                folderHandle.create(false, true, monitor);
083:
084:                if (monitor.isCanceled()) {
085:                    throw new OperationCanceledException();
086:                }
087:
088:                return folderHandle;
089:            }
090:
091:            /**
092:             * Creates a folder resource handle for the folder with the given name.
093:             * This method does not create the folder resource; this is the responsibility
094:             * of <code>createFolder</code>.
095:             *
096:             * @param container the resource container
097:             * @param folderName the name of the folder
098:             * @return the new folder resource handle
099:             */
100:            private IFolder createFolderHandle(IContainer container,
101:                    String folderName) {
102:                return container.getFolder(new Path(folderName));
103:            }
104:
105:            /**
106:             * Creates a project resource for the given project handle.
107:             *
108:             * @param projectHandle the handle to create a project resource
109:             * @param monitor the progress monitor to show visual progress
110:             * @return the project handle (<code>projectHandle</code>)
111:             * @exception CoreException if the operation fails
112:             * @exception OperationCanceledException if the operation is canceled
113:             */
114:            private IProject createProject(IProject projectHandle,
115:                    IProgressMonitor monitor) throws CoreException {
116:                try {
117:                    monitor.beginTask("", 2000);//$NON-NLS-1$
118:
119:                    projectHandle.create(new SubProgressMonitor(monitor, 1000));
120:                    if (monitor.isCanceled()) {
121:                        throw new OperationCanceledException();
122:                    }
123:
124:                    projectHandle.open(new SubProgressMonitor(monitor, 1000));
125:                    if (monitor.isCanceled()) {
126:                        throw new OperationCanceledException();
127:                    }
128:                } finally {
129:                    monitor.done();
130:                }
131:
132:                return projectHandle;
133:            }
134:
135:            /**
136:             * Creates a project resource handle for the project with the given name.
137:             * This method does not create the project resource; this is the responsibility
138:             * of <code>createProject</code>.
139:             *
140:             * @param root the workspace root resource
141:             * @param projectName the name of the project
142:             * @return the new project resource handle
143:             */
144:            private IProject createProjectHandle(IWorkspaceRoot root,
145:                    String projectName) {
146:                return root.getProject(projectName);
147:            }
148:
149:            /**
150:             * Ensures that this generator's container resource exists. Creates any missing
151:             * resource containers along the path; does nothing if the container resource
152:             * already exists.
153:             * <p>
154:             * Note: This method should be called within a workspace modify operation since
155:             * it may create resources.
156:             * </p>
157:             *
158:             * @param monitor a progress monitor
159:             * @return the container resource
160:             * @exception CoreException if the operation fails
161:             * @exception OperationCanceledException if the operation is canceled
162:             */
163:            public IContainer generateContainer(IProgressMonitor monitor)
164:                    throws CoreException {
165:                IDEWorkbenchPlugin.getPluginWorkspace().run(
166:                        new IWorkspaceRunnable() {
167:                            public void run(IProgressMonitor monitor)
168:                                    throws CoreException {
169:                                monitor
170:                                        .beginTask(
171:                                                IDEWorkbenchMessages.ContainerGenerator_progressMessage,
172:                                                1000 * containerFullPath
173:                                                        .segmentCount());
174:                                if (container != null) {
175:                                    return;
176:                                }
177:
178:                                // Does the container exist already?
179:                                IWorkspaceRoot root = getWorkspaceRoot();
180:                                container = (IContainer) root
181:                                        .findMember(containerFullPath);
182:                                if (container != null) {
183:                                    return;
184:                                }
185:
186:                                // Create the container for the given path
187:                                container = root;
188:                                for (int i = 0; i < containerFullPath
189:                                        .segmentCount(); i++) {
190:                                    String currentSegment = containerFullPath
191:                                            .segment(i);
192:                                    IResource resource = container
193:                                            .findMember(currentSegment);
194:                                    if (resource != null) {
195:                                        if (resource.getType() == IResource.FILE) {
196:                                            String msg = NLS
197:                                                    .bind(
198:                                                            IDEWorkbenchMessages.ContainerGenerator_pathOccupied,
199:                                                            resource
200:                                                                    .getFullPath()
201:                                                                    .makeRelative());
202:                                            throw new CoreException(
203:                                                    new Status(
204:                                                            IStatus.ERROR,
205:                                                            IDEWorkbenchPlugin.IDE_WORKBENCH,
206:                                                            1, msg, null));
207:                                        }
208:                                        container = (IContainer) resource;
209:                                        monitor.worked(1000);
210:                                    } else {
211:                                        if (i == 0) {
212:                                            IProject projectHandle = createProjectHandle(
213:                                                    root, currentSegment);
214:                                            container = createProject(
215:                                                    projectHandle,
216:                                                    new SubProgressMonitor(
217:                                                            monitor, 1000));
218:                                        } else {
219:                                            IFolder folderHandle = createFolderHandle(
220:                                                    container, currentSegment);
221:                                            container = createFolder(
222:                                                    folderHandle,
223:                                                    new SubProgressMonitor(
224:                                                            monitor, 1000));
225:                                        }
226:                                    }
227:                                }
228:                            }
229:                        }, null, IResource.NONE, monitor);
230:                return container;
231:            }
232:
233:            /**
234:             * Returns the workspace root resource handle.
235:             *
236:             * @return the workspace root resource handle
237:             */
238:            private IWorkspaceRoot getWorkspaceRoot() {
239:                return IDEWorkbenchPlugin.getPluginWorkspace().getRoot();
240:            }
241:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.