eclipse plugin: Add context menu for importing maps

This commit is contained in:
Timotei Dolean 2010-05-23 10:16:00 +00:00
parent f3b8f91b58
commit b1190c8037
4 changed files with 140 additions and 2 deletions

View file

@ -45,8 +45,8 @@
nameFilter="*maps*"
objectClass="org.eclipse.core.resources.IFolder">
<action
class="action.ImportMap"
enablesFor="+"
class="wesnoth_eclipse_plugin.action.ImportMap"
enablesFor="1"
icon="icons/wesnoth_editor-icon.png"
id="actions.importmap"
label="Import map"

View file

@ -1,6 +1,7 @@
package wesnoth_eclipse_plugin;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
@ -25,6 +26,7 @@ public class Activator extends AbstractUIPlugin {
* (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;
@ -35,6 +37,7 @@ public class Activator extends AbstractUIPlugin {
* (non-Javadoc)
* @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
*/
@Override
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
@ -59,4 +62,13 @@ public class Activator extends AbstractUIPlugin {
public static ImageDescriptor getImageDescriptor(String path) {
return imageDescriptorFromPlugin(PLUGIN_ID, path);
}
/**
* Returns the plugin's shell
* @return
*/
public static Shell getShell()
{
return plugin.getWorkbench().getDisplay().getActiveShell();
}
}

View file

@ -0,0 +1,89 @@
package wesnoth_eclipse_plugin.action;
import java.io.File;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import wesnoth_eclipse_plugin.Activator;
import wesnoth_eclipse_plugin.utils.FileUtils;
public class ImportMap implements IObjectActionDelegate
{
private IProject selectedProject_;
private IContainer mapsFolder_;
public ImportMap()
{
}
@Override
public void run(IAction action)
{
Shell shell = Activator.getShell();
FileDialog mapDialog = new FileDialog(shell,SWT.OPEN);
mapDialog.setText("Import wesnoth map");
mapDialog.setFilterExtensions(new String[] {"*.map" });
String file = mapDialog.open();
if (file != null && selectedProject_ != null)
{
try
{
File source = new File(file);
File target = new File(mapsFolder_.getLocation().toOSString() +
Path.SEPARATOR + source.getName());
if (target.exists())
{
MessageBox confirmBox =new MessageBox(shell,
SWT.ICON_QUESTION | SWT.YES | SWT.NO);
confirmBox.setMessage("There is already an existing map with the same name. Overwrite?");
if (confirmBox.open() == SWT.NO)
return;
}
FileUtils.copyTo(source, target);
mapsFolder_.refreshLocal(IResource.DEPTH_INFINITE, null);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
@Override
public void selectionChanged(IAction action, ISelection selection){
if (selection instanceof IStructuredSelection)
{
Object selection2 = ((IStructuredSelection)selection).getFirstElement();
if (selection2 instanceof IResource) {
mapsFolder_ = (IContainer)selection2;
// get the project
while(selection2 instanceof IContainer &&
((IContainer)selection2).getParent() != null &&
!(selection2 instanceof IProject))
selection2 = ((IContainer) selection2).getParent();
if (selection2 instanceof IProject)
selectedProject_ = (IProject)selection2;
}
}
}
@Override
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
}
}

View file

@ -0,0 +1,37 @@
/**
* @author Timotei Dolean
*/
package wesnoth_eclipse_plugin.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class FileUtils
{
/**
* Copies a file from source to target
* @param source
* @param target
* @throws Exception
*/
public static void copyTo(File source, File target) throws Exception
{
if (source == null || target == null)
return;
InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(target);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}