Debian Codegraph @

doc/org.eclipse.emf.cheatsheets/src/org/eclipse/emf/cheatsheets/actions/CopyFileFromPluginAction.java

raw
1 /**
2 * Copyright (c) 2006 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 v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v20.html
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 */
11 package org.eclipse.emf.cheatsheets.actions;
12
13 import java.io.File;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.lang.reflect.InvocationTargetException;
18 import java.util.StringTokenizer;
19
20 import org.osgi.framework.Bundle;
21
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IFolder;
24 import org.eclipse.core.resources.IProject;
25 import org.eclipse.core.resources.IWorkspaceRoot;
26 import org.eclipse.core.resources.ResourcesPlugin;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.FileLocator;
29 import org.eclipse.core.runtime.IPath;
30 import org.eclipse.core.runtime.IProgressMonitor;
31 import org.eclipse.core.runtime.Path;
32 import org.eclipse.core.runtime.Platform;
33 import org.eclipse.jface.action.Action;
34 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
35 import org.eclipse.jface.operation.IRunnableWithProgress;
36 import org.eclipse.swt.widgets.Display;
37 import org.eclipse.ui.actions.WorkspaceModifyOperation;
38 import org.eclipse.ui.cheatsheets.ICheatSheetAction;
39 import org.eclipse.ui.cheatsheets.ICheatSheetManager;
40
41 import org.eclipse.emf.cheatsheets.CheatSheetsPlugin;
42 import org.eclipse.emf.common.util.BasicMonitor;
43
44
45 /**
46 * <p>Action that copies a file from plugin to project.</p>
47 * @since 2.2.0
48 */
49 public class CopyFileFromPluginAction extends Action implements ICheatSheetAction
50 {
51 protected String projectName; //Name of project
52 protected String javaPackageName; //Name of Java package
53 protected String sourceFile; //Source file Name - plugin + folder + filename
54 protected IWorkspaceRoot root; //Root of workspace
55
56 /**
57 * Execution of the action
58 * @param params Array of parameters - index 0: projectName, index 1: javaPackageName, index 2: sourceFile
59 * @param manager Cheatsheet Manager
60 */
61 public void run(String[] params, ICheatSheetManager manager)
62 {
63 projectName = params[0];
64 javaPackageName = params[1];
65 sourceFile = params[2];
66 root = ResourcesPlugin.getWorkspace().getRoot();
67 run();
68 }
69
70 /**
71 * Run the action
72 */
73 @Override
74 public void run()
75 {
76 WorkspaceModifyOperation operation = new WorkspaceModifyOperation()
77 {
78 @Override
79 protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException
80 {
81 if (!isNullString(projectName))
82 {
83 IProject project = getProject(projectName, !isNullString(javaPackageName), monitor);
84 copyFileToProject(project, javaPackageName, sourceFile, monitor);
85 }
86 else
87 {
88 copyFileToWorkspaceFolder(sourceFile);
89 }
90 }
91 };
92 try
93 {
94 runWithProgress(operation);
95 return;
96 }
97 catch (Exception e)
98 {
99 CheatSheetsPlugin.INSTANCE.log(e);
100 }
101 notifyResult(false);
102 }
103
104 /**
105 * Run with progress
106 * @param runnable
107 * @throws InvocationTargetException
108 * @throws InterruptedException
109 */
110 protected void runWithProgress(IRunnableWithProgress runnable) throws InvocationTargetException, InterruptedException
111 {
112 new ProgressMonitorDialog(Display.getCurrent().getActiveShell()).run(false, false, runnable);
113 }
114
115 /**
116 * Get the project from workspace
117 * @param projectName Name of project
118 * @param isJavaProject Boolean if the project is a Java project
119 * @param monitor Monitoring the action
120 * @return Project with projectName
121 * @throws CoreException
122 */
123 protected IProject getProject(String projectName, boolean isJavaProject, IProgressMonitor monitor) throws CoreException
124 {
125 monitor.beginTask(CheatSheetsPlugin.INSTANCE.getString("_UI_FindProject_message", new String []{ projectName }), 2);
126 IProject project = null;
127 if (isJavaProject)
128 {
129 NewJavaProjectAction newJavaProjectAction = new NewJavaProjectAction();
130 project = newJavaProjectAction.createProject(projectName, BasicMonitor.subProgress(monitor, 1));
131 }
132 else
133 {
134 NewProjectAction newProjectAction = new NewProjectAction();
135 project = newProjectAction.createProject(projectName, BasicMonitor.subProgress(monitor, 1));
136 }
137 monitor.done();
138 return project;
139 }
140
141 /**
142 * Copy a file from plugin to project
143 * @param project Target Project
144 * @param javaPackageName Java package name
145 * @param sourceFile Source file name - plugin id + folders + file
146 * @param monitor Monitoring the action
147 * @return Target file
148 * @throws CoreException
149 */
150 protected IFile copyFileToProject(IProject project, String javaPackageName, String sourceFile, IProgressMonitor monitor) throws CoreException
151 {
152 monitor.beginTask(CheatSheetsPlugin.INSTANCE.getString("_UI_CopyFileToProject_message", new String []{ projectName }), 2);
153 String targetFileName = sourceFile.substring(sourceFile.lastIndexOf('/') + 1);
154 IPath targetFolderPath = getFolderPath(project, javaPackageName, monitor);
155 IFile targetFile = project.getFile(targetFolderPath.append(targetFileName));
156 if (!targetFile.exists())
157 {
158 String sourcePluginId = sourceFile.substring(0, sourceFile.indexOf('/'));
159 String sourceFilePath = sourceFile.substring(sourceFile.indexOf('/'));
160 Bundle bundle = Platform.getBundle(sourcePluginId);
161 try
162 {
163 InputStream in = FileLocator.openStream(bundle, new Path(sourceFilePath), false);
164 targetFile.create(in, true, BasicMonitor.subProgress(monitor, 1));
165 in.close();
166 }
167 catch (IOException e)
168 {
169 CheatSheetsPlugin.INSTANCE.log(e);
170 }
171 }
172 monitor.done();
173 notifyResult(targetFile.exists());
174 return targetFile;
175 }
176
177 protected void copyFileToWorkspaceFolder(String sourceFile) throws CoreException
178 {
179 String targetFileName = sourceFile.substring(sourceFile.lastIndexOf('/') + 1);
180 String sourcePluginId = sourceFile.substring(0, sourceFile.indexOf('/'));
181 String sourceFilePath = sourceFile.substring(sourceFile.indexOf('/'));
182 Bundle bundle = Platform.getBundle(sourcePluginId);
183
184 File file = new File(root.getLocation().toPortableString() + "/" + targetFileName);
185 if (!file.exists())
186 {
187 try
188 {
189 InputStream in = FileLocator.openStream(bundle, new Path(sourceFilePath), false);
190 FileOutputStream out = new FileOutputStream(file);
191 int aChar = 0;
192 while ((aChar = in.read()) != -1)
193 {
194 out.write(aChar);
195 }
196 in.close();
197 out.close();
198 }
199 catch (IOException e)
200 {
201 CheatSheetsPlugin.INSTANCE.log(e);
202 }
203 }
204 notifyResult(file.exists());
205 }
206
207 /**
208 * Get folder path derived from Java package name
209 * @param project Target project
210 * @param javaPackageName Java package name
211 * @param monitor Monitor the action
212 * @return Path of the folder
213 * @throws CoreException
214 */
215 protected IPath getFolderPath(IProject project, String javaPackageName, IProgressMonitor monitor) throws CoreException
216 {
217 IPath folderPath = project.getProjectRelativePath();
218 if (!isNullString(javaPackageName))
219 {
220 folderPath = folderPath.append(NewJavaProjectAction.SOURCE_FOLDER);
221 StringTokenizer javaPackageToken = new StringTokenizer(javaPackageName, ".");
222 IFolder folder = null;
223 while (javaPackageToken.hasMoreElements())
224 {
225 folderPath = folderPath.append(javaPackageToken.nextToken());
226 folder = project.getFolder(folderPath);
227 if (!folder.exists())
228 {
229 folder.create(true, true, BasicMonitor.subProgress(monitor, 1));
230 }
231 }
232 }
233 return folderPath;
234 }
235
236 protected boolean isNullString(String value)
237 {
238 return value == null || value.length() == 0;
239 }
240 }