eclipse plugin: add the 'setup workspace' menu entry

This commit is contained in:
Timotei Dolean 2010-07-04 20:24:10 +00:00
parent e764900778
commit 855d48ab99
4 changed files with 118 additions and 26 deletions

View file

@ -246,6 +246,12 @@
id="Wesnoth_Eclipse_Plugin.commands.openMapInEditor"
name="openMapInEditor">
</command>
<command
defaultHandler="wesnoth_eclipse_plugin.handlers.SetupWorkspaceHandler"
description="Setups the workspace"
id="Wesnoth_Eclipse_Plugin.commands.setupWorkspace"
name="Setup Workspace">
</command>
</extension>
<extension
point="org.eclipse.ui.menus">
@ -282,6 +288,11 @@
name="separator2"
visible="true">
</separator>
<command
commandId="Wesnoth_Eclipse_Plugin.commands.setupWorkspace"
label="Setup Workspace"
style="push">
</command>
<command
commandId="org.eclipse.ui.window.preferences"
label="Open plugin&apos;s preferences"

View file

@ -1,10 +1,18 @@
package wesnoth_eclipse_plugin;
import java.io.File;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import wesnoth_eclipse_plugin.preferences.PreferenceConstants;
import wesnoth_eclipse_plugin.preferences.PreferenceInitializer;
import wesnoth_eclipse_plugin.utils.GUIUtils;
import wesnoth_eclipse_plugin.utils.WorkspaceUtils;
/**
* The activator class controls the plug-in life cycle
*/
@ -23,25 +31,15 @@ public class Activator extends AbstractUIPlugin
public Activator() {
}
/*
* (non-Javadoc)
* @see
* org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
* )
*/
@Override
public void start(BundleContext context) throws Exception
{
super.start(context);
plugin = this;
checkConditions();
}
/*
* (non-Javadoc)
* @see
* org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
* )
*/
@Override
public void stop(BundleContext context) throws Exception
{
@ -51,7 +49,7 @@ public class Activator extends AbstractUIPlugin
/**
* Returns the shared instance
*
*
* @return the shared instance
*/
public static Activator getDefault()
@ -62,7 +60,7 @@ public class Activator extends AbstractUIPlugin
/**
* Returns an image descriptor for the image file at the given plug-in
* relative path
*
*
* @param path the path
* @return the image descriptor
*/
@ -79,4 +77,35 @@ public class Activator extends AbstractUIPlugin
{
return plugin.getWorkbench().getDisplay().getActiveShell();
}
/**
* Checks if the user has set some needed preferences
*/
private static void checkConditions()
{
String execDir = PreferenceInitializer.getString(PreferenceConstants.P_WESNOTH_EXEC_PATH);
String userDir = PreferenceInitializer.getString(PreferenceConstants.P_WESNOTH_USER_DIR);
String wmltoolsDir = PreferenceInitializer.getString(PreferenceConstants.P_WESNOTH_WMLTOOLS_DIR);
String workingDir = PreferenceInitializer.getString(PreferenceConstants.P_WESNOTH_WORKING_DIR);
if (!validPath(execDir) || !validPath(userDir) || !validPath(wmltoolsDir) || !validPath(workingDir))
{
GUIUtils.showMessageBox(WorkspaceUtils.getWorkbenchWindow(),
"Please set all plugin's preferences before using it.");
}
// check if workspace is setup - the "userdir addons" directory created
// as a project
if (!ResourcesPlugin.getWorkspace().getRoot().getProject("User Addons").exists())
{
GUIUtils.showMessageBox(WorkspaceUtils.getWorkbenchWindow(),
"Please setup the workspace before using the plugin. Go to \"Wesnoth\" menu," +
" and then click on the \"Setup Workspace\" entry following the instructions on the screen.");
}
}
private static boolean validPath(String path)
{
return !path.isEmpty() && new File(path).exists();
}
}

View file

@ -0,0 +1,17 @@
package wesnoth_eclipse_plugin.handlers;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import wesnoth_eclipse_plugin.utils.WorkspaceUtils;
public class SetupWorkspaceHandler extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
WorkspaceUtils.setupWorkspace();
return null;
}
}

View file

@ -8,18 +8,21 @@ import java.io.File;
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.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import wesnoth_eclipse_plugin.Activator;
import wesnoth_eclipse_plugin.builder.WesnothProjectNature;
import wesnoth_eclipse_plugin.preferences.PreferenceConstants;
import wesnoth_eclipse_plugin.preferences.PreferenceInitializer;
public class WorkspaceUtils
{
private static String temporaryFolder_ = "";
private static String temporaryFolder_ = "";
public static IProject getSelectedProject(IWorkbenchWindow window)
{
@ -27,7 +30,7 @@ public class WorkspaceUtils
if (selection == null || !(selection.getFirstElement() instanceof IProject))
return null;
return (IProject)selection.getFirstElement();
return (IProject) selection.getFirstElement();
}
public static IFolder getSelectedFolder(IWorkbenchWindow window)
@ -36,7 +39,7 @@ public class WorkspaceUtils
if (selection == null || !(selection.getFirstElement() instanceof IFolder))
return null;
return (IFolder)selection.getFirstElement();
return (IFolder) selection.getFirstElement();
}
public static IFile getSelectedFile(IWorkbenchWindow window)
@ -45,7 +48,7 @@ public class WorkspaceUtils
if (selection == null || !(selection.getFirstElement() instanceof IFile))
return null;
return (IFile)selection.getFirstElement();
return (IFile) selection.getFirstElement();
}
public static IStructuredSelection getSelectedStructuredSelection(IWorkbenchWindow window)
@ -55,7 +58,7 @@ public class WorkspaceUtils
if (!(window.getSelectionService().getSelection() instanceof IStructuredSelection))
return null;
return (IStructuredSelection)window.getSelectionService().getSelection();
return (IStructuredSelection) window.getSelectionService().getSelection();
}
public static IProject getSelectedProject()
@ -79,8 +82,8 @@ public class WorkspaceUtils
}
/**
* Returns the first WorkbenchWindow available.
* This is not always the same with ActiveWorkbecnWindow
* Returns the first WorkbenchWindow available. This is not always the same
* with ActiveWorkbecnWindow
* @return
*/
public static IWorkbenchWindow getWorkbenchWindow()
@ -98,8 +101,7 @@ public class WorkspaceUtils
{
if (temporaryFolder_.isEmpty())
{
temporaryFolder_ = System.getProperty("java.io.tmpdir") + Path.SEPARATOR +
"wesnoth_plugin" + Path.SEPARATOR;
temporaryFolder_ = System.getProperty("java.io.tmpdir") + Path.SEPARATOR + "wesnoth_plugin" + Path.SEPARATOR;
File tmpFile = new File(temporaryFolder_);
if (!tmpFile.exists())
@ -115,8 +117,41 @@ public class WorkspaceUtils
*/
public static String getPathRelativeToUserDir(IResource resource)
{
return PreferenceInitializer.getString(PreferenceConstants.P_WESNOTH_USER_DIR) + Path.SEPARATOR +
"data/add-ons/" + resource.getProject().getName() +
Path.SEPARATOR + resource.getProjectRelativePath().toOSString();
return PreferenceInitializer.getString(PreferenceConstants.P_WESNOTH_USER_DIR) + Path.SEPARATOR + "data/add-ons/" + resource.getProject().getName()
+ Path.SEPARATOR + resource.getProjectRelativePath().toOSString();
}
public static void setupWorkspace()
{
// automatically import "WesnothUserDir/data/add-ons as a project
// container
String userDir = PreferenceInitializer.getString(PreferenceConstants.P_WESNOTH_USER_DIR);
if (userDir.isEmpty() || !new File(userDir).exists())
{
GUIUtils.showMessageBox(WorkspaceUtils.getWorkbenchWindow(),
"Please set all plugin's preferences before using it.");
return;
}
try
{
IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject("User Addons");
if (!proj.exists())
{
IProjectDescription description = ResourcesPlugin.getWorkspace().newProjectDescription("User Addons");
description.setLocation(new Path(userDir + Path.SEPARATOR + "data/add-ons/"));
proj.create(description, null);
proj.open(null);
// the nature isn't set on creation so the nature adds the builder aswell
description.setNatureIds(new String[] { WesnothProjectNature.NATURE_ID });
proj.setDescription(description, null);
}
GUIUtils.showMessageBox(WorkspaceUtils.getWorkbenchWindow(),
"Workspace was set up successfully.");
} catch (Exception e)
{
e.printStackTrace();
}
}
}