📄 flow4jplugin.java
字号:
/*
* Copyright (c) 2003, Alexander Greif
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Flow4J-Eclipse project nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.orthanc.flow4j.designer.core;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import net.orthanc.flow4j.base.IOUtils;
import net.orthanc.flow4j.base.PrettyPrinterUtils;
import net.orthanc.flow4j.bsf.Flow4JBSFManager;
import net.orthanc.flow4j.designer.core.adapters.Flow4JElementAdapterFactory;
import net.orthanc.flow4j.designer.core.adapters.Flow4JResourceAdapterFactory;
import net.orthanc.flow4j.designer.core.elements.IFlow4JElement;
import net.orthanc.flow4j.designer.ui.editors.FlowEditor;
import net.orthanc.flow4j.designer.ui.preferences.BSFPreferencePage;
import net.orthanc.flow4j.model.codegen.javasrc.JavaSrcModelDigester;
import net.orthanc.flow4j.runtime.FlowManager;
import net.orthanc.flow4j.runtime.ITaskFlowlet;
import org.acm.seguin.util.FileSettings;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdapterManager;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IPluginDescriptor;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaModel;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorRegistry;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.plugin.AbstractUIPlugin;
/**
* The main plugin class to be used in the desktop.
*/
public class Flow4JPlugin extends AbstractUIPlugin {
public static final String FLOW4J_PLUGIN_ID = "net.orthanc.flow4j";
public static final String FLOW4J_RESOURCES_VIEW_ID =
FLOW4J_PLUGIN_ID + ".flow4j_resources_view";
public static final String FLOW4J_NATURE_ID =
FLOW4J_PLUGIN_ID + ".flow4j_nature";
public static final String FLOW4J_BUILDER_ID =
FLOW4J_PLUGIN_ID + ".flow4j_builder";
public static final String FLOW4J_REPOSITORY_BUILDER_ID =
FLOW4J_PLUGIN_ID + ".flow4j_repository_builder";
public static final String FLOW4J_FILE_EXTENSION = "f4j";
public static final String JAVA_FILE_EXTENSION = "java";
//The shared instance.
private static Flow4JPlugin plugin;
//Resource bundle.
private ResourceBundle resourceBundle;
private static FileSettings prettySettings;
private static IType taskFlowletBaseType; // Peter Friese
/**
* The constructor.
*/
public Flow4JPlugin(IPluginDescriptor descriptor) {
super(descriptor);
plugin = this;
try {
resourceBundle =
ResourceBundle.getBundle(
"net.orthanc.flow4j.designer.ui.Flow4JPluginResources");
} catch (MissingResourceException x) {
resourceBundle = null;
}
}
/**
* @see org.eclipse.core.runtime.Plugin#startup()
*/
public void startup() throws CoreException {
super.startup();
IAdapterManager manager = Platform.getAdapterManager();
manager.registerAdapters(
new Flow4JElementAdapterFactory(),
IFlow4JElement.class);
manager.registerAdapters(
new Flow4JResourceAdapterFactory(),
IResource.class);
BSFPreferencePage.loadBSFPreferences();
try {
//castor loadCastorMapping();
prettySettings = loadPrettySettings(getPrettySettingsFile());
// JavaSrcModelDigester.setPrettySettings(prettySettings);
JavaSrcModelDigester.setPrettySettings(prettySettings);
registerFlows();
// } catch (MappingException e) {
// log(e.getMessage());
// message(e.getMessage());
} catch (IOException e) {
log(e.getMessage());
message(e.getMessage());
}
}
/**
* Returns the shared instance.
*/
public static Flow4JPlugin getDefault() {
return plugin;
}
/**
* Returns the task class loader from the project, thi file belongs to.
* @param file a file in the project.
* @return the task class loader from the project, thi file belongs to.
*/
public static JavaTaskFlowletClassLoader getTaskFlowletClassLoader(IFile file) {
List projectClasspathEntries = new ArrayList();
Flow4JPlugin.getResolvedClasspath(file, projectClasspathEntries);
// the projects segment is the first
JavaTaskFlowletClassLoader taskFlowletClassLoader = null;
try {
taskFlowletClassLoader =
new JavaTaskFlowletClassLoader(
convertPaths2URLs(projectClasspathEntries),
Flow4JPlugin
.getDefault()
.getDescriptor()
.getPluginClassLoader());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return taskFlowletClassLoader;
}
/**
* Returns a task flowlet instance. That is searched in the project's classpath.
* @param taskClassName
* @param javaProject
* @return
* @throws MalformedURLException
* @throws Throwable
*/
public static ITaskFlowlet getTaskFlowletInstance(String taskClassName, IJavaProject javaProject)
throws MalformedURLException, Throwable {
List classpathPaths = new ArrayList();
Flow4JPlugin.getResolvedClasspath(Flow4JPlugin.getProject(javaProject), classpathPaths);
List classpathURLs = convertPaths2URLs(classpathPaths);
return getTaskFlowletInstance(taskClassName, classpathURLs);
}
/**
* Returns a task flowlet instance. That is searched in the given classpath.
* @param taskClassName
* @param classpathURLs
* @return
* @throws MalformedURLException
* @throws Throwable
*/
public static ITaskFlowlet getTaskFlowletInstance(
String taskClassName,
List classpathURLs)
throws MalformedURLException, Throwable {
JavaTaskFlowletClassLoader taskFlowletClassLoader =
new JavaTaskFlowletClassLoader(
classpathURLs,
Flow4JPlugin
.getDefault()
.getDescriptor()
.getPluginClassLoader());
ITaskFlowlet taskFlowletInstance =
taskFlowletClassLoader.getTaskInstance(taskClassName);
return taskFlowletInstance;
}
/**
* Returns a task flowlet instance.
* Tries to get the active Editor, and then determines the files project.
* to get the classpath from.
* @param taskClassName
* @return a task flowlet instance.
* @throws MalformedURLException
* @throws ClassNotFoundException
* @throws Throwable
*/
public static ITaskFlowlet getTaskFlowletInstance(String taskClassName) throws MalformedURLException, ClassNotFoundException, Throwable {
IEditorPart activeEditor = Flow4JPlugin.getActiveEditor();
if (activeEditor != null && (activeEditor instanceof FlowEditor)) {
List classpathEntries = new ArrayList();
Flow4JPlugin.getResolvedClasspath(
(FlowEditor) activeEditor,
classpathEntries);
List classpathURLs = Flow4JPlugin.convertPaths2URLs(classpathEntries);
return getTaskFlowletInstance(taskClassName, classpathURLs);
}
throw new ClassNotFoundException("Class not found: " + taskClassName);
}
/*
public static List getProjectClasspathPaths(IProject project) {
List classpathPaths = new ArrayList();
Flow4JPlugin.getResolvedClasspath(project, classpathList);
for (Iterator iter = classpathList.iterator(); iter.hasNext();) {
IPath path = (IPath) iter.next();
runtimeLibs.add(path.toFile().toURL());
}
}
*/
/**
* Converts Paths to URLs
*/
public static List convertPaths2URLs(List pathList)
throws MalformedURLException {
List result = new ArrayList();
for (Iterator iter = pathList.iterator(); iter.hasNext();) {
IPath path = (IPath) iter.next();
if (path != null)
result.add(path.toFile().toURL());
}
return result;
}
/**
* Converts Files to URLs
*/
public static List convertPaths2Files(List pathList)
throws MalformedURLException {
List result = new ArrayList();
for (Iterator iter = pathList.iterator(); iter.hasNext();) {
IPath path = (IPath) iter.next();
if (path != null)
result.add(path.toFile().getAbsoluteFile());
}
return result;
}
/**
* Returns the plugin ID
* @return the plugin ID
*/
public static String getPluginId() {
return getDefault().getDescriptor().getUniqueIdentifier();
}
/**
* Returns the casor mapping file <code>File</code> representation.
* @return the casor mapping file <code>File</code> representation.
*/
// static public File getCastorMappingFile() {
// return Flow4JPlugin
// .getDefault()
// .getStateLocation()
// .append(FlowModelBind.MAPPING_FILENAME)
// .toFile();
// }
/**
* Returns the pretty printer settings file <code>File</code> representation.
* @return the pretty printer settings file <code>File</code> representation.
*/
static public File getPrettySettingsFile() {
return Flow4JPlugin
.getDefault()
.getStateLocation()
.append(".Refactory")
.append(JavaSrcModelDigester.PRETTY_SETTINGS_FILENAME)
.toFile();
}
/**
* Loads the pretty printer settings and returns them.
* Copies the settings file from the classpath into the given File location and loads it from there.
* @param settingsFile where the settings should be stored
* @throws IOException
*/
static FileSettings loadPrettySettings(File settingsFile) throws IOException {
InputStream settingsResourceIn =
JavaSrcModelDigester.class.getResourceAsStream(
"/" + JavaSrcModelDigester.PRETTY_SETTINGS_FILENAME);
settingsFile.getParentFile().mkdirs();
OutputStream fileOut = new FileOutputStream(settingsFile);
IOUtils.copyBufferedStream(settingsResourceIn, fileOut);
settingsResourceIn.close();
fileOut.close();
FileSettings prettySettings = PrettyPrinterUtils.loadPrettySettings(settingsFile);
return prettySettings;
}
/**
* Loads the mapping if it's not loaded yet.
* Copies the mapping file from the classpath into the projects temporary
* folder and loads it from there.
* @throws MappingException
* @throws IOException
*/
// static public void loadCastorMapping()
// throws MappingException, IOException {
//
// // remove the default xerces parser from the castor props
// // so force castor to use jaxp
// Configuration.getDefault().remove(Configuration.Property.Parser);
//
//
// File mappingFile = getCastorMappingFile();
// InputStream mappingResourceIn =
// FlowModelBind.class.getResourceAsStream(
// "/" + FlowModelBind.MAPPING_FILENAME);
// OutputStream fileOut = new FileOutputStream(mappingFile);
// IOUtils.copyBufferedStream(mappingResourceIn, fileOut);
// mappingResourceIn.close();
// fileOut.close();
//
// FlowModelBind.loadMapping(mappingFile);
// }
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -