package tool;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.resources.IResourceProxyVisitor;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.ui.statushandlers.StatusManager;
import org.osgi.framework.BundleContext;
/**
* The activator class controls the plug-in life cycle
*/
public class ToolDecoratorActivator extends AbstractUIPlugin {
public static final String PROJECT_QUALIFIED_NAME = "Tool";
public static final String SOURCE_FOLDER = "SourceFolder";
public static final QualifiedName sourceFolderQualifiedName = new QualifiedName(PROJECT_QUALIFIED_NAME, SOURCE_FOLDER);
// The plug-in ID
public static final String PLUGIN_ID = "ToolDecorator"; //$NON-NLS-1$
// The shared instance
private static ToolDecoratorActivator plugin;
/**
* The constructor
*/
public ToolDecoratorActivator() {
}
/*
* (non-Javadoc)
* @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;
findSourceFolders();
}
/*
* (non-Javadoc)
* @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
}
private void findSourceFolders() throws CoreException{
IWorkspace workspace = ResourcesPlugin.getWorkspace();
workspace.getRoot().accept(new IResourceProxyVisitor() {
public boolean visit(IResourceProxy proxy) throws CoreException {
if (proxy.getType() == IResource.FOLDER &&
isSourceFolder(proxy.requestResource())){
return true;
}
if (proxy.getType() == IResource.FILE) {
IFile file = (IFile) proxy.requestResource();
IContainer parent = file.getParent();
if (file.getLocation().getFileExtension() != null
&& file.getLocation().getFileExtension().matches("pex")
&& parent instanceof IFolder
&& !parent.getName().equals(ToolProjectSupport.LIBRARY_FOLDER_NAME)) {
ToolProjectSupport.setSourceFolder((IFolder)file.getParent());
}
}
return true;
}
}, IResource.DEPTH_INFINITE);
}
/**
* this method returns true of the folder contains project source
* @param resource
* @return
*/
public static boolean isSourceFolder(IResource resource){
if (!(resource instanceof IFolder))
return false;
if (resource.getName().equals(ToolProjectSupport.LIBRARY_FOLDER_NAME))
return false;
IFolder folder = (IFolder)resource;
String sourceFolder;
try {
sourceFolder = folder.getPersistentProperty(ToolProjectSupport.sourceFolderQualifiedName);
if (sourceFolder != null && sourceFolder.equalsIgnoreCase("true"))
return true;
} catch (CoreException e) {
ToolDecoratorActivator.showError("Decorator Error", e);
}
return false;
}
/**
* Returns the shared instance
*
* @return the shared instance
*/
public static ToolDecoratorActivator getDefault() {
return plugin;
}
/**
* Returns an image descriptor for the image file at the given
* plug-in relative path
*
* @param path the path
* @return the image descriptor
*/
public static ImageDescriptor getImageDescriptor(String path) {
return imageDescriptorFromPlugin(PLUGIN_ID, path);
}
/**
* Returns an Image for the image file at the given
* plug-in relative path
* @param iconString
* @return
*/
public static Image getImage(String iconString) {
ImageDescriptor img = getImageDescriptor(iconString);
return img.createImage();
}
/**
* shows and logs an error
*
* @param message
* @param e
*/
public static void showError(String message, Throwable e) {
IStatus status = new Status(IStatus.ERROR, PLUGIN_ID, message, e);
StatusManager.getManager().handle(status, StatusManager.LOG | StatusManager.SHOW);
}
}