Source Code Cross Referenced for SampleOperation.java in  » IDE-Eclipse » Eclipse-plug-in-development » org » eclipse » pde » internal » ui » samples » 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 » Eclipse plug in development » org.eclipse.pde.internal.ui.samples 
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.pde.internal.ui.samples;
011:
012:        import java.io.ByteArrayInputStream;
013:        import java.io.ByteArrayOutputStream;
014:        import java.io.IOException;
015:        import java.io.UnsupportedEncodingException;
016:        import java.lang.reflect.InvocationTargetException;
017:        import java.net.URL;
018:        import java.util.Properties;
019:        import java.util.zip.ZipFile;
020:
021:        import org.eclipse.core.resources.IFile;
022:        import org.eclipse.core.resources.IProject;
023:        import org.eclipse.core.resources.IWorkspace;
024:        import org.eclipse.core.resources.IWorkspaceRoot;
025:        import org.eclipse.core.resources.IWorkspaceRunnable;
026:        import org.eclipse.core.runtime.CoreException;
027:        import org.eclipse.core.runtime.FileLocator;
028:        import org.eclipse.core.runtime.IConfigurationElement;
029:        import org.eclipse.core.runtime.IPath;
030:        import org.eclipse.core.runtime.IProgressMonitor;
031:        import org.eclipse.core.runtime.IStatus;
032:        import org.eclipse.core.runtime.NullProgressMonitor;
033:        import org.eclipse.core.runtime.OperationCanceledException;
034:        import org.eclipse.core.runtime.Platform;
035:        import org.eclipse.core.runtime.Status;
036:        import org.eclipse.core.runtime.SubProgressMonitor;
037:        import org.eclipse.jface.operation.IRunnableWithProgress;
038:        import org.eclipse.pde.internal.ui.IPDEUIConstants;
039:        import org.eclipse.pde.internal.ui.PDEPlugin;
040:        import org.eclipse.pde.internal.ui.PDEUIMessages;
041:        import org.eclipse.ui.dialogs.IOverwriteQuery;
042:        import org.eclipse.ui.wizards.datatransfer.ImportOperation;
043:        import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider;
044:        import org.osgi.framework.Bundle;
045:
046:        public class SampleOperation implements  IRunnableWithProgress {
047:            private static final String SAMPLE_PROPERTIES = "sample.properties"; //$NON-NLS-1$
048:
049:            private IConfigurationElement sample;
050:
051:            private String[] projectNames;
052:
053:            private IFile sampleManifest;
054:
055:            private IOverwriteQuery query;
056:
057:            private boolean yesToAll;
058:
059:            private boolean cancel;
060:
061:            private IProject[] createdProjects;
062:
063:            /**
064:             *  
065:             */
066:            public SampleOperation(IConfigurationElement sample,
067:                    String[] projectNames, IOverwriteQuery query) {
068:                this .sample = sample;
069:                this .query = query;
070:                this .projectNames = projectNames;
071:            }
072:
073:            public IFile getSampleManifest() {
074:                return sampleManifest;
075:            }
076:
077:            public IProject[] getCreatedProjects() {
078:                return createdProjects;
079:            }
080:
081:            /*
082:             * (non-Javadoc)
083:             * 
084:             * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
085:             */
086:            public void run(IProgressMonitor monitor)
087:                    throws InvocationTargetException, InterruptedException {
088:                try {
089:                    IWorkspaceRunnable op = new IWorkspaceRunnable() {
090:                        public void run(IProgressMonitor monitor)
091:                                throws CoreException {
092:                            IConfigurationElement[] projects = sample
093:                                    .getChildren("project"); //$NON-NLS-1$
094:                            monitor.beginTask(
095:                                    PDEUIMessages.SampleOperation_creating,
096:                                    4 * projects.length);
097:                            createdProjects = new IProject[projects.length];
098:                            try {
099:                                for (int i = 0; i < projects.length; i++) {
100:                                    IFile file = importProject(projectNames[i],
101:                                            projects[i],
102:                                            new SubProgressMonitor(monitor, 4));
103:                                    if (file != null && sampleManifest == null)
104:                                        sampleManifest = file;
105:                                    if (file != null) {
106:                                        createdProjects[i] = file.getProject();
107:                                    }
108:                                    if (cancel)
109:                                        // if user has cancelled operation, exit.
110:                                        break;
111:                                }
112:                            } catch (InterruptedException e) {
113:                                throw new OperationCanceledException();
114:                            } catch (InvocationTargetException e) {
115:                                throwCoreException(e);
116:                            }
117:                        }
118:                    };
119:                    PDEPlugin.getWorkspace().run(op, monitor);
120:                } catch (CoreException e) {
121:                    throw new InvocationTargetException(e);
122:                } catch (OperationCanceledException e) {
123:                    throw e;
124:                } finally {
125:                    monitor.done();
126:                }
127:            }
128:
129:            private void throwCoreException(InvocationTargetException e)
130:                    throws CoreException {
131:                Throwable t = e.getCause();
132:                Status status = new Status(IStatus.ERROR,
133:                        IPDEUIConstants.PLUGIN_ID, IStatus.OK, e.getMessage(),
134:                        t);
135:                throw new CoreException(status);
136:            }
137:
138:            private IFile importProject(String name,
139:                    IConfigurationElement config, IProgressMonitor monitor)
140:                    throws CoreException, InvocationTargetException,
141:                    InterruptedException {
142:                String path = config.getAttribute("archive"); //$NON-NLS-1$
143:                if (name == null || path == null)
144:                    return null;
145:                IWorkspace workspace = PDEPlugin.getWorkspace();
146:                IWorkspaceRoot root = workspace.getRoot();
147:                IProject project = root.getProject(name);
148:                boolean skip = false;
149:                if (project.exists()) {
150:                    if (!yesToAll) {
151:                        String returnId = query.queryOverwrite(project
152:                                .getFullPath().toString());
153:                        if (returnId.equals(IOverwriteQuery.ALL)) {
154:                            yesToAll = true;
155:                            skip = false;
156:                        } else if (returnId.equals(IOverwriteQuery.YES)) {
157:                            skip = false;
158:                        } else if (returnId.equals(IOverwriteQuery.NO)) {
159:                            skip = true;
160:                        } else if (returnId.equals(IOverwriteQuery.CANCEL)) {
161:                            skip = true;
162:                            cancel = true;
163:                        }
164:                    }
165:                    if (!skip) {
166:                        project.delete(true, true, new SubProgressMonitor(
167:                                monitor, 1));
168:                        project = root.getProject(name);
169:                    } else
170:                        monitor.worked(1);
171:                }
172:                if (skip) {
173:                    monitor.worked(3);
174:                    IFile manifest = project.getFile(SAMPLE_PROPERTIES);
175:                    return manifest;
176:                }
177:
178:                project.create(new SubProgressMonitor(monitor, 1));
179:                project.open(new NullProgressMonitor());
180:                Bundle bundle = Platform.getBundle(sample
181:                        .getNamespaceIdentifier());
182:                ZipFile zipFile = getZipFileFromPluginDir(path, bundle);
183:                importFilesFromZip(zipFile, project.getFullPath(),
184:                        new SubProgressMonitor(monitor, 1));
185:                return createSampleManifest(project, config,
186:                        new SubProgressMonitor(monitor, 1));
187:            }
188:
189:            private IFile createSampleManifest(IProject project,
190:                    IConfigurationElement config, IProgressMonitor monitor)
191:                    throws CoreException {
192:                IFile file = project.getFile(SAMPLE_PROPERTIES);
193:                if (!file.exists()) {
194:                    try {
195:                        ByteArrayOutputStream out = new ByteArrayOutputStream();
196:                        Properties properties = new Properties();
197:                        createSampleManifestContent(
198:                                config.getAttribute("name"), properties); //$NON-NLS-1$
199:                        properties.store(out, ""); //$NON-NLS-1$
200:                        out.flush();
201:                        String contents = out.toString();
202:                        out.close();
203:                        ByteArrayInputStream stream = new ByteArrayInputStream(
204:                                contents.getBytes("UTF8")); //$NON-NLS-1$
205:                        file.create(stream, true, monitor);
206:                        stream.close();
207:                    } catch (UnsupportedEncodingException e) {
208:                    } catch (IOException e) {
209:                    }
210:                }
211:                return file;
212:            }
213:
214:            private void createSampleManifestContent(String projectName,
215:                    Properties properties) {
216:                writeProperty(properties, "id", sample.getAttribute("id")); //$NON-NLS-1$ //$NON-NLS-2$
217:                writeProperty(properties, "name", sample.getAttribute("name")); //$NON-NLS-1$ //$NON-NLS-2$
218:                writeProperty(properties, "projectName", projectName); //$NON-NLS-1$
219:                writeProperty(properties,
220:                        "launcher", sample.getAttribute("launcher")); //$NON-NLS-1$ //$NON-NLS-2$
221:                IConfigurationElement desc[] = sample
222:                        .getChildren("description"); //$NON-NLS-1$
223:                if (desc.length == 1) {
224:                    writeProperty(properties, "helpHref", desc[0] //$NON-NLS-1$
225:                            .getAttribute("helpHref")); //$NON-NLS-1$
226:                    writeProperty(properties, "description", desc[0].getValue()); //$NON-NLS-1$
227:                }
228:            }
229:
230:            private void writeProperty(Properties properties, String name,
231:                    String value) {
232:                if (value == null)
233:                    return;
234:                properties.setProperty(name, value);
235:            }
236:
237:            private ZipFile getZipFileFromPluginDir(String pluginRelativePath,
238:                    Bundle bundle) throws CoreException {
239:                try {
240:                    URL starterURL = FileLocator.resolve(bundle
241:                            .getEntry(pluginRelativePath));
242:                    return new ZipFile(FileLocator.toFileURL(starterURL)
243:                            .getFile());
244:                } catch (IOException e) {
245:                    String message = pluginRelativePath + ": " + e.getMessage(); //$NON-NLS-1$
246:                    Status status = new Status(IStatus.ERROR, PDEPlugin
247:                            .getPluginId(), IStatus.ERROR, message, e);
248:                    throw new CoreException(status);
249:                }
250:            }
251:
252:            private void importFilesFromZip(ZipFile srcZipFile, IPath destPath,
253:                    IProgressMonitor monitor) throws InvocationTargetException,
254:                    InterruptedException {
255:                ZipFileStructureProvider structureProvider = new ZipFileStructureProvider(
256:                        srcZipFile);
257:                ImportOperation op = new ImportOperation(destPath,
258:                        structureProvider.getRoot(), structureProvider, query);
259:                op.run(monitor);
260:            }
261:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.