eclipse plugin: add the "import map" to the main menu

This commit is contained in:
Timotei Dolean 2010-05-23 12:13:26 +00:00
parent b1190c8037
commit f1283fd943
5 changed files with 164 additions and 67 deletions

View file

@ -108,7 +108,7 @@
</contexts>
</extension>
<extension
<extension
point="org.eclipse.ui.newWizards">
<category name="Wesnoth"
id="wesnoth.eclipse.newWizards">
@ -118,22 +118,22 @@
category="wesnoth.eclipse.newWizards"
class="wesnoth_eclipse_plugin.wizards.CampaignNewWizard"
id="wesnoth_eclipse_plugin.wizards.CampaignNewWizard"
project="true">
project="true">
<description>Create a Wesnoth campaign.</description>
</wizard>
<!-- </extension> -->
<!-- <extension
<!-- <extension
point="org.eclipse.ui.newWizards">
<category name="Wesnoth"
id="wesnoth.eclipse.newWizards">
id="wesnoth.eclipse.newWizards">
</category> -->
<wizard name="Wesnoth Scenario"
icon="icons/sample.gif"
category="wesnoth.eclipse.newWizards"
class="wesnoth_eclipse_plugin.wizards.ScenarioNewWizard"
id="wesnoth_eclipse_plugin.wizards.ScenarioNewWizard"
project="false">
project="false">
<description>Create a Wesnoth campaign.</description>
</wizard>
</extension>
@ -149,6 +149,12 @@
id="Wesnoth_Eclipse_Plugin.commands.openEditor"
name="openeditor">
</command>
<command
defaultHandler="wesnoth_eclipse_plugin.handlers.ImportMapHandler"
description="Import an existing map into the current maps folder"
id="Wesnoth_Eclipse_Plugin.commands.importMap"
name="importMap">
</command>
</extension>
<extension
point="org.eclipse.ui.menus">
@ -170,6 +176,11 @@
name="separator1"
visible="true">
</separator>
<command
commandId="Wesnoth_Eclipse_Plugin.commands.importMap"
icon="icons/wesnoth_editor-icon_16.png"
label="Import Map">
</command>
</menu>
</menuContribution>
</extension>

View file

@ -1,86 +1,29 @@
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;
import wesnoth_eclipse_plugin.globalactions.MapActions;
public class ImportMap implements IObjectActionDelegate
{
private IProject selectedProject_;
private IContainer mapsFolder_;
public ImportMap()
{
}
/**
* This method is runned *ONLY* if the user selected a "maps" folder
*/
@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();
}
}
MapActions.importMap();
}
@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

View file

@ -0,0 +1,59 @@
/**
* @author Timotei Dolean
*/
package wesnoth_eclipse_plugin.globalactions;
import java.io.File;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Path;
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.IWorkbenchWindow;
import wesnoth_eclipse_plugin.Activator;
import wesnoth_eclipse_plugin.utils.FileUtils;
import wesnoth_eclipse_plugin.utils.WorkspaceUtils;
public class MapActions
{
/**
* Import a map file into the current directory
*/
public static void importMap(){
IWorkbenchWindow window = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow();
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 && WorkspaceUtils.getSelectedFolder(window) != null)
{
try
{
File source = new File(file);
File target = new File(WorkspaceUtils.getSelectedFolder(window).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);
WorkspaceUtils.getSelectedFolder(window).refreshLocal(IResource.DEPTH_INFINITE, null);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}

View file

@ -0,0 +1,36 @@
package wesnoth_eclipse_plugin.handlers;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFolder;
import org.eclipse.swt.widgets.MessageBox;
import wesnoth_eclipse_plugin.Activator;
import wesnoth_eclipse_plugin.globalactions.MapActions;
import wesnoth_eclipse_plugin.utils.WorkspaceUtils;
public class ImportMapHandler extends AbstractHandler
{
@Override
public Object execute(ExecutionEvent event) throws ExecutionException
{
MapActions.importMap();
return null;
}
@Override
public boolean isHandled()
{
IFolder selectedFolder = WorkspaceUtils.getSelectedFolder(WorkspaceUtils.getWorkbenchWindow());
if (!selectedFolder.getName().equals("maps"))
{
MessageBox box = new MessageBox(Activator.getShell());
box.setMessage("You need to select a \"maps\" folder before importing anything");
box.open();
}
return (selectedFolder != null && selectedFolder.getName().equals("maps"));
}
}

View file

@ -0,0 +1,48 @@
/**
* @author Timotei Dolean
*/
package wesnoth_eclipse_plugin.utils;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import wesnoth_eclipse_plugin.Activator;
public class WorkspaceUtils
{
public static IProject getSelectedProject(IWorkbenchWindow window)
{
IStructuredSelection selection = getSelectedStructuredSelection(window);
if (selection == null || !(selection.getFirstElement() instanceof IProject))
return null;
return (IProject)selection.getFirstElement();
}
public static IFolder getSelectedFolder(IWorkbenchWindow window)
{
IStructuredSelection selection = getSelectedStructuredSelection(window);
if (selection == null || !(selection.getFirstElement() instanceof IFolder))
return null;
return (IFolder)selection.getFirstElement();
}
public static IStructuredSelection getSelectedStructuredSelection(IWorkbenchWindow window)
{
if (window == null)
{
System.out.println("WokbenchWindow NULL!!");
return null;
}
if (!(window.getSelectionService().getSelection() instanceof IStructuredSelection))
return null;
return (IStructuredSelection)window.getSelectionService().getSelection();
}
public static IWorkbenchWindow getWorkbenchWindow()
{
return Activator.getDefault().getWorkbench().getActiveWorkbenchWindow();
}
}