eclipse plugin: add hyperlinking for maps

This commit is contained in:
Timotei Dolean 2010-07-28 19:18:03 +00:00
parent 9731756c86
commit 6d7b7e40c7
7 changed files with 290 additions and 5 deletions

View file

@ -1,5 +1,8 @@
Eclipse plugin changelog
0.3.0
* Added hyperlink on maps, so they are opened in the game editor when clicked or F3 is pressed
0.2.1
* The workspace setup is done automatically on plugin start (first usage of it's features)
* Faster launching of campaign/scenario due to some enhancement in the wmlparser

View file

@ -8,16 +8,21 @@
*******************************************************************************/
package org.wesnoth.ui;
import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.xtext.resource.ILocationInFileProvider;
import org.eclipse.xtext.ui.editor.XtextEditor;
import org.eclipse.xtext.ui.editor.bracketmatching.IBracketMatcher;
import org.eclipse.xtext.ui.editor.hyperlinking.HyperlinkHelper;
import org.eclipse.xtext.ui.editor.outline.transformer.ISemanticModelTransformer;
import org.eclipse.xtext.ui.editor.syntaxcoloring.HighlightingReconciler;
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightingConfiguration;
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightingHelper;
import org.eclipse.xtext.ui.editor.syntaxcoloring.ISemanticHighlightingCalculator;
import org.eclipse.xtext.ui.editor.syntaxcoloring.antlr.AbstractAntlrTokenToAttributeIdMapper;
import org.wesnoth.ui.navigation.WMLHyperlinkHelper;
import org.wesnoth.ui.outline.WMLTransformer;
import org.wesnoth.ui.resource.WMLLocationInFileProvider;
import org.wesnoth.ui.syntax.WMLAntlrTokenToAttributeIdMapper;
import org.wesnoth.ui.syntax.WMLHighlightingConfiguration;
import org.wesnoth.ui.syntax.WMLHighlightingHelper;
@ -80,13 +85,23 @@ public class WMLUiModule extends org.wesnoth.ui.AbstractWMLUiModule
return WMLHighlightingHelper.class;
}
// public Class<? extends MergingHighlightedPositionAcceptor> bindMergingHighlightedPositionAcceptor()
// {
// return WMLMergingHighlightedPositionAcceptor.class;
// }
public Class<? extends HighlightingReconciler> bindHighlightingReconciler()
{
return WMLHighlightingReconciler.class;
}
public Class<? extends ILocationInFileProvider> bindILocationInFileProvider()
{
return WMLLocationInFileProvider.class;
}
@Override
public Class<? extends IHyperlinkDetector> bindIHyperlinkDetector()
{
return super.bindIHyperlinkDetector();
}
public Class<? extends HyperlinkHelper> bindHyperlinkHelper()
{
return WMLHyperlinkHelper.class;
}
}

View file

@ -0,0 +1,58 @@
/*******************************************************************************
* Copyright (c) 2010 by Timotei Dolean <timotei21@gmail.com>
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.wesnoth.ui.emf;
import java.util.HashMap;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
/**
* An adapter that provides storage for data in a hashmap
*/
public class HashMapStorageAdapter implements Adapter
{
private HashMap<String, Object> data_;
private Notifier notifier_;
public HashMapStorageAdapter()
{
data_ = new HashMap<String, Object>();
}
public void setData(String name, Object data)
{
data_.put(name, data);
}
public Object getData(String name)
{
return data_.get(name);
}
public Notifier getTarget()
{
return notifier_;
}
public boolean isAdapterForType(Object arg0)
{
return true;
}
public void notifyChanged(Notification arg0)
{
}
public void setTarget(Notifier arg0)
{
notifier_ = arg0;
}
}

View file

@ -0,0 +1,56 @@
/*******************************************************************************
* Copyright (c) 2010 by Timotei Dolean <timotei21@gmail.com>
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.wesnoth.ui.emf;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
/**
* A simple adapter that stores an object
*/
public class ObjectStorageAdapter implements Adapter
{
private Object object_;
private Notifier notifier_;
public ObjectStorageAdapter(Object data)
{
object_ = data;
}
public Object getObject()
{
return object_;
}
public void setObject(Object obj)
{
object_ = obj;
}
public Notifier getTarget()
{
return notifier_;
}
public boolean isAdapterForType(Object arg0)
{
return true;
}
public void notifyChanged(Notification arg0)
{
}
public void setTarget(Notifier arg0)
{
notifier_ = arg0;
}
}

View file

@ -0,0 +1,46 @@
/*******************************************************************************
* Copyright (c) 2010 by Timotei Dolean <timotei21@gmail.com>
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.wesnoth.ui.navigation;
import java.io.File;
import org.eclipse.xtext.ui.editor.hyperlinking.XtextHyperlink;
import wesnoth_eclipse_plugin.utils.GameUtils;
public class MapOpenerHyperlink extends XtextHyperlink
{
private String location_;
public void setLocation(String location)
{
location_ = location;
}
public String getLocation()
{
return location_;
}
@Override
public void open()
{
if (new File(location_).exists())
{
GameUtils.startEditor(location_);
}
// else
// {
// // create a new file
// //TODO: create the map
// Logger.getInstance().log("Creating new map: " + mapLocation);
//
// }
}
}

View file

@ -0,0 +1,78 @@
/*******************************************************************************
* Copyright (c) 2010 by Timotei Dolean <timotei21@gmail.com>
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.wesnoth.ui.navigation;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jface.text.Region;
import org.eclipse.xtext.parsetree.CompositeNode;
import org.eclipse.xtext.parsetree.LeafNode;
import org.eclipse.xtext.parsetree.NodeUtil;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.hyperlinking.HyperlinkHelper;
import org.eclipse.xtext.ui.editor.hyperlinking.IHyperlinkAcceptor;
import org.wesnoth.ui.emf.ObjectStorageAdapter;
import wesnoth_eclipse_plugin.Constants;
import wesnoth_eclipse_plugin.preferences.Preferences;
public class WMLHyperlinkHelper extends HyperlinkHelper
{
@Override
public void createHyperlinksByOffset(XtextResource resource, int offset,
IHyperlinkAcceptor acceptor)
{
super.createHyperlinksByOffset(resource, offset, acceptor);
CompositeNode rootNode = NodeUtil.getRootNode(resource.getContents().get(0));
if (rootNode == null)
return;
LeafNode node = (LeafNode)NodeUtil.findLeafNodeAtOffset(rootNode, offset);
if (node == null)
return;
LeafNode prevNode = (LeafNode)NodeUtil.findLeafNodeAtOffset(rootNode,
node.getOffset() - 1);
if(prevNode == null)
return;
createMapHyperlink(prevNode, node, acceptor, resource);
}
/**
* Creates a hyperlink for opening the map.
* @param key The key (must me 'map_data' in this case)
* @param value The value of key, that is, the location of the map
*/
private void createMapHyperlink(LeafNode key, LeafNode value,
IHyperlinkAcceptor acceptor, XtextResource resource)
{
if (!(key.getText().equals("map_data")))
return;
// trim the " and the {
String mapLocation = value.getText().substring(2, value.getLength() - 2);
mapLocation = mapLocation.replace("~",
Preferences.getString(Constants.P_WESNOTH_USER_DIR) + "/data/");
ObjectStorageAdapter adapter = (ObjectStorageAdapter)EcoreUtil.getAdapter(value.eAdapters(),
ObjectStorageAdapter.class);
if (adapter == null)
{
adapter = new ObjectStorageAdapter(mapLocation);
value.eAdapters().add(adapter);
}
else
adapter.setObject(mapLocation);
MapOpenerHyperlink hyperlink = new MapOpenerHyperlink();
hyperlink.setHyperlinkRegion(new Region(value.getOffset(), value.getLength()));
hyperlink.setLocation(mapLocation);
acceptor.accept(hyperlink);
}
}

View file

@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright (c) 2010 by Timotei Dolean <timotei21@gmail.com>
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.wesnoth.ui.resource;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.resource.DefaultLocationInFileProvider;
public class WMLLocationInFileProvider extends DefaultLocationInFileProvider
{
@Override
protected boolean useKeyword(Keyword keyword, EObject context)
{
return super.useKeyword(keyword, context);
}
@Override
protected EStructuralFeature getIdentifierFeature(EObject obj)
{
return super.getIdentifierFeature(obj);
}
}