Revision: 16759
http://sourceforge.net/p/gate/code/16759
Author: markagreenwood
Date: 2013-08-01 11:57:45 +0000 (Thu, 01 Aug 2013)
Log Message:
-----------
finally after a burst of inspiration I've figured out an easy way to add
support for new right-click options on any resource without using a VR (which
is a pain because you have to open the VR to get the options). It uses the
existing tool support but with a parent class of ResourceHelper instead of
implementing ActionPublisher -- this should be considered experimental for now
and may change before the next release of GATE
Modified Paths:
--------------
gate/trunk/src/gate/gui/NameBearerHandle.java
Added Paths:
-----------
gate/trunk/src/gate/gui/ResourceHelper.java
Modified: gate/trunk/src/gate/gui/NameBearerHandle.java
===================================================================
--- gate/trunk/src/gate/gui/NameBearerHandle.java 2013-08-01 09:48:26 UTC
(rev 16758)
+++ gate/trunk/src/gate/gui/NameBearerHandle.java 2013-08-01 11:57:45 UTC
(rev 16759)
@@ -35,6 +35,7 @@
import gate.creole.AnnotationSchema;
import gate.creole.ConditionalController;
import gate.creole.ConditionalSerialAnalyserController;
+import gate.creole.PackagedController;
import gate.creole.ResourceData;
import gate.creole.ResourceInstantiationException;
import gate.creole.SerialAnalyserController;
@@ -237,6 +238,27 @@
}
}
}
+
+ if (target instanceof Resource) {
+ Set<String> toolTypes = Gate.getCreoleRegister().getToolTypes();
+ for(String type : toolTypes) {
+ List<Resource> instances = Gate.getCreoleRegister()
+ .get(type).getInstantiations();
+ for(Resource res : instances) {
+ if(res instanceof ResourceHelper) {
+ Iterator<Action> actionIter =
((ResourceHelper)res).getActions((Resource)target).iterator();
+ while(actionIter.hasNext()) {
+ Action anAction = (Action)actionIter.next();
+ if(anAction == null)
+ popup.addSeparator();
+ else {
+ popup.add(new XJMenuItem(anAction, sListenerProxy));
+ }
+ }
+ }
+ }
+ }
+ }
return popup;
}
Added: gate/trunk/src/gate/gui/ResourceHelper.java
===================================================================
--- gate/trunk/src/gate/gui/ResourceHelper.java (rev 0)
+++ gate/trunk/src/gate/gui/ResourceHelper.java 2013-08-01 11:57:45 UTC (rev
16759)
@@ -0,0 +1,124 @@
+/*
+ * ResourceHelper.java
+ *
+ * Copyright (c) 2013, The University of Sheffield. See the file COPYRIGHT.txt
+ * in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
+ *
+ * This file is part of GATE (see http://gate.ac.uk/), and is free software,
+ * licenced under the GNU Library General Public License, Version 2, June 1991
+ * (in the distribution as file licence.html, and also available at
+ * http://gate.ac.uk/gate/licence.html).
+ *
+ * Mark A. Greenwood, 01/08/2013
+ */
+
+package gate.gui;
+
+import gate.Gate;
+import gate.Resource;
+import gate.creole.AbstractResource;
+import gate.event.CreoleEvent;
+import gate.event.CreoleListener;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.swing.Action;
+
+public abstract class ResourceHelper extends AbstractResource implements
+ CreoleListener {
+
+ private static final long serialVersionUID = 1657147709821774423L;
+
+ // We cache the actions so we don't keep recreating them every time. If you
+ // don't want this behaviour (i.e. you want completely dynamic menus) then
+ // you'll need to override the getActions method
+ Map<Resource, List<Action>> actions = new HashMap<Resource, List<Action>>();
+
+ @Override
+ public Resource init() {
+ // we need to listen for unload events so we register ourselves with the
+ // creole register
+ Gate.getCreoleRegister().addCreoleListener(this);
+
+ // nothing else to do so just return ourself
+ return this;
+ }
+
+ /**
+ * Get the right-click menu items that this tool will add to the specified
+ * resource. Note that this implementation uses a cache so that the same
items
+ * will be returned each time the menu is displayed. For a fully dynamic
+ * approach a subclass should override this method to simply call @{link
+ * {@link #buildActions(Resource)}.
+ *
+ * @param resource
+ * the {@link gate.Resource} instance we are wanting to add new menu
+ * items to
+ * @return a list of {@link javax.swing.Action} instances which will be added
+ * to the right click menu of the resource
+ */
+ public List<Action> getActions(Resource resource) {
+
+ if(!actions.containsKey(resource)) {
+ // if we haven't seen this resource before then build the actions
+ actions.put(resource, buildActions(resource));
+ }
+
+ // return the actions from the cache
+ return actions.get(resource);
+ }
+
+ /**
+ * Build the {@link javax.swing.Action} instances that should be used to
+ * enhance the right-click menu of the specified {@link gate.Resource}.
+ *
+ * @param resource
+ * the {@link gate.Resource} instance we are adding to
+ * @return a list of {@link javax.swing.Action} instances that will be added
+ * to the right-click menu of the resource.
+ */
+ protected abstract List<Action> buildActions(Resource resource);
+
+ @Override
+ public void cleanup() {
+ // do the normal cleanup
+ super.cleanup();
+
+ // stop listening for creole events so that we can get fully unloaded
+ Gate.getCreoleRegister().removeCreoleListener(this);
+ }
+
+ @Override
+ public void resourceUnloaded(CreoleEvent e) {
+ // remove any cached menus for the resource that is being unloaded to help
+ // with memory consumption etc.
+ actions.remove(e.getResource());
+ }
+
+ @Override
+ public void resourceLoaded(CreoleEvent e) {
+ // we don't care about this event
+ }
+
+ @Override
+ public void datastoreOpened(CreoleEvent e) {
+ // we don't care about this event
+ }
+
+ @Override
+ public void datastoreCreated(CreoleEvent e) {
+ // we don't care about this event
+ }
+
+ @Override
+ public void datastoreClosed(CreoleEvent e) {
+ // we don't care about this event
+ }
+
+ @Override
+ public void resourceRenamed(Resource resource, String oldName, String
newName) {
+ // we don't care about this event
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
Get your SQL database under version control now!
Version control is standard for application code, but databases havent
caught up. So what steps can you take to put your SQL databases under
version control? Why should you start doing it? Read more to find out.
http://pubads.g.doubleclick.net/gampad/clk?id=49501711&iu=/4140/ostg.clktrk
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs