added menu for launching the wesnoth editor

This commit is contained in:
Timotei Dolean 2010-05-18 19:22:33 +00:00
parent 42e0c27a82
commit 9d5a242c48
6 changed files with 325 additions and 94 deletions

View file

@ -122,5 +122,53 @@
<description>Create a Wesnoth campaign.</description>
</wizard>
</extension>
<extension
point="org.eclipse.ui.commands">
<category
id="Wesnoth_Eclipse_Plugin.commands.category"
name="Wesnoth">
</category>
<command
defaultHandler="wesnoth_eclipse_plugin.handlers.OpenEditorHandler"
description="Opens the wesnoth editor"
id="Wesnoth_Eclipse_Plugin.commands.openEditor"
name="openeditor">
</command>
</extension>
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="menu:org.eclipse.ui.main.menu?after=additions">
<menu
id="Wesnoth_Eclipse_Plugin.menus.wesnothMenu"
label="Wesnoth"
mnemonic="h">
<command
commandId="Wesnoth_Eclipse_Plugin.commands.openEditor"
id="Wesnoth_Eclipse_Plugin.menus.openEditor"
label="Open editor"
mnemonic="e">
</command>
<separator
name="separator1"
visible="true">
</separator>
</menu>
</menuContribution>
</extension>
<extension
point="org.eclipse.ui.preferencePages">
<page
class="wesnoth_eclipse_plugin.preferences.WesnothEditorPreferences"
id="wesnoth_eclipse_plugin.preferences.WesnothEditorPreferences"
name="Wesnoth UMC Plugin">
</page>
</extension>
<extension
point="org.eclipse.core.runtime.preferences">
<initializer
class="wesnoth_eclipse_plugin.preferences.PreferenceInitializer">
</initializer>
</extension>
</plugin>

View file

@ -1,94 +1,121 @@
package wesnoth_eclipse_plugin.builder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
/**
* @author Timotei Dolean
*
*/
public class ExternalToolInvoker {
private Process process_;
private ProcessBuilder processBuilder_;
private Thread processThread_;
private BufferedReader bufferedReaderOutput_;
private BufferedReader bufferedReaderError_;
/**
* Creates an external tool invoker with specified options
* @param fileName the file name to be invoked
* @param arguments the arguments passed to the file
* @param useThread true if the process will run in a thread
* @throws IOException
*/
public ExternalToolInvoker(String fileName, String arguments, boolean useThread) throws IOException
{
processBuilder_ = new ProcessBuilder(fileName,arguments);
if (useThread)
processThread_ = new Thread(new Runnable() {
@Override
public void run() {
try {
process_ = processBuilder_.start();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public void run() throws IOException
{
if (processThread_ == null)
process_ = processBuilder_.start();
else
processThread_.run();
bufferedReaderOutput_ = new BufferedReader(new InputStreamReader(process_.getInputStream()));
bufferedReaderError_ = new BufferedReader(new InputStreamReader(process_.getErrorStream()));
}
public void waitFor() throws InterruptedException
{
process_.waitFor();
}
public String readOutputLine()
{
if (process_ == null)
return null;
try {
return bufferedReaderOutput_.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public String readErrorLine()
{
if (process_ == null)
return null;
try {
return bufferedReaderError_.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public OutputStream getOutputStream()
{
return process_.getOutputStream();
}
public InputStream getInputStream()
{
return process_.getInputStream();
}
public InputStream getErrorStream()
{
return process_.getErrorStream();
}
}
package wesnoth_eclipse_plugin.builder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
/**
* @author Timotei Dolean
*
*/
public class ExternalToolInvoker {
private Process process_;
private ProcessBuilder processBuilder_;
private Thread processThread_;
private BufferedReader bufferedReaderOutput_;
private BufferedReader bufferedReaderError_;
/**
* Creates an external tool invoker with specified options
* @param fileName the file name to be invoked
* @param arguments the arguments passed to the file
* @param useThread true if the process will run in a thread
* @throws IOException
*/
public ExternalToolInvoker(String fileName, String arguments, boolean useThread) throws IOException
{
processBuilder_ = new ProcessBuilder(fileName,arguments);
if (useThread)
processThread_ = new Thread(new Runnable() {
@Override
public void run() {
try {
process_ = processBuilder_.start();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public void run() throws IOException
{
if (processThread_ == null)
process_ = processBuilder_.start();
else
processThread_.run();
bufferedReaderOutput_ = new BufferedReader(new InputStreamReader(process_.getInputStream()));
bufferedReaderError_ = new BufferedReader(new InputStreamReader(process_.getErrorStream()));
}
public void waitFor() throws InterruptedException
{
process_.waitFor();
}
public String readOutputLine()
{
if (process_ == null)
return null;
try {
return bufferedReaderOutput_.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public String readErrorLine()
{
if (process_ == null)
return null;
try {
return bufferedReaderError_.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public OutputStream getOutputStream()
{
return process_.getOutputStream();
}
public InputStream getInputStream()
{
return process_.getInputStream();
}
public InputStream getErrorStream()
{
return process_.getErrorStream();
}
public static boolean launchTool(String fileName, String args, boolean showOutput, boolean useThread){
try{
ExternalToolInvoker toolInvoker = new ExternalToolInvoker(fileName, args, useThread);
toolInvoker.run();
toolInvoker.waitFor();
if (showOutput)
{
String line="";
System.out.println("Output: ");
while((line = toolInvoker.readOutputLine()) != null)
{
System.out.println(line);
}
System.out.println("Errors: ");
while((line = toolInvoker.readErrorLine()) != null)
{
System.out.println(line);
}
}
}
catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}

View file

@ -0,0 +1,52 @@
package wesnoth_eclipse_plugin.handlers;
import java.io.File;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import wesnoth_eclipse_plugin.builder.ExternalToolInvoker;
import wesnoth_eclipse_plugin.preferences.PreferenceConstants;
import wesnoth_eclipse_plugin.preferences.PreferenceInitializer;
public class OpenEditorHandler extends AbstractHandler
{
/**
* the arguments to launch the editor
* %s - the filename of the file to be edited
* %s - the data directory
*/
public final static String EDITOR_LAUNCH_ARGS = "-e %s";// %s";;
@Override
public Object execute(ExecutionEvent event) throws ExecutionException
{
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
String editorPath = PreferenceInitializer.getString(PreferenceConstants.P_WESNOTH_EXEC_PATH);
String workingDir = PreferenceInitializer.getString(PreferenceConstants.P_WESNOTH_WORKING_DIR);
if (workingDir == "")
workingDir = editorPath.substring(0,editorPath.lastIndexOf(new File(editorPath).getName()));
if (editorPath == "")
{
MessageBox box = new MessageBox(window.getShell());
box.setMessage(String.format("Please set the wesnoth's executable path first."));
box.open();
return null;
}
System.out.printf("Running: [%s] with args: [%s]\n", editorPath,getLaunchEditorArguments("", workingDir));
ExternalToolInvoker.launchTool(editorPath, getLaunchEditorArguments("", workingDir),true, true);
return null;
}
public static String getLaunchEditorArguments(String mapName, String workingDir)
{
String args = String.format(EDITOR_LAUNCH_ARGS, mapName,workingDir);
return args;
}
}

View file

@ -0,0 +1,10 @@
package wesnoth_eclipse_plugin.preferences;
/**
* Constant definitions for plug-in preferences
*/
public class PreferenceConstants {
public static final String P_WESNOTH_EXEC_PATH = "";
public static final String P_WESNOTH_WORKING_DIR ="";
}

View file

@ -0,0 +1,40 @@
package wesnoth_eclipse_plugin.preferences;
import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
import org.eclipse.jface.preference.IPreferenceStore;
import wesnoth_eclipse_plugin.Activator;
/**
* Class used to initialize default preference values.
*/
public class PreferenceInitializer extends AbstractPreferenceInitializer {
/*
* (non-Javadoc)
*
* @see org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer#initializeDefaultPreferences()
*/
@Override
public void initializeDefaultPreferences() {
//IPreferenceStore store = Activator.getDefault().getPreferenceStore();
//store.setDefault(name, value)
}
/**
* @return The preferences store of the plugin
*/
public static IPreferenceStore getPreferences()
{
return Activator.getDefault().getPreferenceStore();
}
public static String getString(String prefName)
{
return getPreferences().getString(prefName);
}
public static int getInt(String prefName)
{
return getPreferences().getInt(prefName);
}
}

View file

@ -0,0 +1,54 @@
package wesnoth_eclipse_plugin.preferences;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import wesnoth_eclipse_plugin.Activator;
/**
* This class represents a preference page that
* is contributed to the Preferences dialog. By
* subclassing <samp>FieldEditorPreferencePage</samp>, we
* can use the field support built into JFace that allows
* us to create a page that is small and knows how to
* save, restore and apply itself.
* <p>
* This page is used to modify preferences only. They
* are stored in the preference store that belongs to
* the main plug-in class. That way, preferences can
* be accessed directly via the preference store.
*/
public class WesnothEditorPreferences
extends FieldEditorPreferencePage
implements IWorkbenchPreferencePage {
public WesnothEditorPreferences() {
super(GRID);
setPreferenceStore(Activator.getDefault().getPreferenceStore());
setDescription("Wesnoth User-Made-Content Plugin preferences");
}
/**
* Creates the field editors. Field editors are abstractions of
* the common GUI blocks needed to manipulate various types
* of preferences. Each field editor knows how to save and
* restore itself.
*/
@Override
public void createFieldEditors() {
addField(new FileFieldEditor(PreferenceConstants.P_WESNOTH_EXEC_PATH,
"Wesnoth executable path:", getFieldEditorParent()));
addField(new StringFieldEditor(PreferenceConstants.P_WESNOTH_WORKING_DIR,
"Working directory:", getFieldEditorParent()));
}
/* (non-Javadoc)
* @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
*/
public void init(IWorkbench workbench) {
}
}