Revision 359

tmp/org.txm.core/schema/org.txm.commandapi.exsd (revision 359)
1
<?xml version='1.0' encoding='UTF-8'?>
2
<!-- Schema file written by PDE -->
3
<schema targetNamespace="org.txm.core" xmlns="http://www.w3.org/2001/XMLSchema">
4
<annotation>
5
      <appinfo>
6
         <meta.schema plugin="org.txm.core" id="org.txm.commandapi" name="CommandAPI"/>
7
      </appinfo>
8
      <documentation>
9
         use this extension point to declare new comamnds to the CommanddAPI
10
      </documentation>
11
   </annotation>
12

  
13
   <element name="extension">
14
      <annotation>
15
         <appinfo>
16
            <meta.element />
17
         </appinfo>
18
      </annotation>
19
      <complexType>
20
         <choice minOccurs="1" maxOccurs="unbounded">
21
            <element ref="CommandAPIDeclaration"/>
22
         </choice>
23
         <attribute name="point" type="string" use="required">
24
            <annotation>
25
               <documentation>
26
                  
27
               </documentation>
28
            </annotation>
29
         </attribute>
30
         <attribute name="id" type="string">
31
            <annotation>
32
               <documentation>
33
                  
34
               </documentation>
35
            </annotation>
36
         </attribute>
37
         <attribute name="name" type="string">
38
            <annotation>
39
               <documentation>
40
                  
41
               </documentation>
42
               <appinfo>
43
                  <meta.attribute translatable="true"/>
44
               </appinfo>
45
            </annotation>
46
         </attribute>
47
      </complexType>
48
   </element>
49

  
50
   <element name="CommandAPIDeclaration">
51
      <complexType>
52
         <attribute name="class" type="string">
53
            <annotation>
54
               <documentation>
55
                  
56
               </documentation>
57
               <appinfo>
58
                  <meta.attribute kind="java" basedOn="org.txm.functions.CommandsAPIDeclaration:"/>
59
               </appinfo>
60
            </annotation>
61
         </attribute>
62
         <attribute name="name" type="string" use="required">
63
            <annotation>
64
               <documentation>
65
                  
66
               </documentation>
67
            </annotation>
68
         </attribute>
69
         <attribute name="description" type="string" use="required">
70
            <annotation>
71
               <documentation>
72
                  
73
               </documentation>
74
               <appinfo>
75
                  <meta.attribute translatable="true"/>
76
               </appinfo>
77
            </annotation>
78
         </attribute>
79
      </complexType>
80
   </element>
81

  
82
   <annotation>
83
      <appinfo>
84
         <meta.section type="since"/>
85
      </appinfo>
86
      <documentation>
87
         [Enter the first release in which this extension point appears.]
88
      </documentation>
89
   </annotation>
90

  
91
   <annotation>
92
      <appinfo>
93
         <meta.section type="examples"/>
94
      </appinfo>
95
      <documentation>
96
         [Enter extension point usage example here.]
97
      </documentation>
98
   </annotation>
99

  
100
   <annotation>
101
      <appinfo>
102
         <meta.section type="apiinfo"/>
103
      </appinfo>
104
      <documentation>
105
         [Enter API information here.]
106
      </documentation>
107
   </annotation>
108

  
109
   <annotation>
110
      <appinfo>
111
         <meta.section type="implementation"/>
112
      </appinfo>
113
      <documentation>
114
         [Enter information about supplied implementation of this extension point.]
115
      </documentation>
116
   </annotation>
117

  
118

  
119
</schema>
0 120

  
tmp/org.txm.core/src/java/org/txm/functions/CommandsAPIDeclaration.java (revision 359)
1
package org.txm.functions;
2

  
3
import java.util.ArrayList;
4

  
5
/**
6
 * Extension point used to declare new commands to the TXM core commands API
7
 * 
8
 * The plugin can add Command objects (like RCPCommand)
9
 * 
10
 * @author mdecorde
11
 *
12
 */
13
public abstract class CommandsAPIDeclaration {
14
	
15
	/**
16
	 * returns the Commands list to install
17
	 */
18
	public abstract ArrayList<Command> getCommandsList();
19
}
0 20

  
tmp/org.txm.core/src/java/org/txm/functions/CommandsAPI.java (revision 359)
1
package org.txm.functions;
2

  
3
import java.util.HashMap;
4
import java.util.HashSet;
5

  
6
import org.txm.core.results.TXMResult;
7
import org.txm.utils.logger.Log;
8

  
9
/**
10
 * API to all knowned TXM commands.
11
 * 
12
 * Initialized during Toolbox initialisation using the TXMCommand extension point
13
 * 
14
 * For now a command is :
15
 * - an ID
16
 * - a TXMResult class
17
 * - a HashSet of mandatory parameter names (the commands must check itself the parameters class)
18
 * - a HashSet of optional parameter names (the commands must check itself the parameters class)
19
 * 
20
 * @author mdecorde
21
 *
22
 */
23
public class CommandsAPI {
24
	
25
	protected static final HashMap<String, Command> cmds = new HashMap<String, Command>();
26
	
27
	public static void install(String cmd, Class<TXMResult> clazz) {
28
		CommandsAPI.cmds.put(cmd, new Command(cmd,clazz));
29
	}
30
	
31
	/**
32
	 * Manually install a command
33
	 * 
34
	 * @param cmd
35
	 * @param clazz
36
	 * @param mandatory_parameters
37
	 */
38
	public static void install(Command command) {
39
		if (CommandsAPI.cmds.containsKey(command.cmd)) {
40
			Log.warning("The "+command.cmd+" command is already installed. Installing the new one: "+command);
41
		}
42
		CommandsAPI.cmds.put(command.cmd, command);
43
	}
44
	
45
	/**
46
	 * Manually install a command
47
	 * 
48
	 * @param cmd
49
	 * @param clazz
50
	 * @param mandatory_parameters
51
	 */
52
	public static void install(String cmd, Class<TXMResult> clazz, HashSet<String> mandatoryParameters) {
53
		Command command = new Command(cmd,clazz);
54
		command.mandatoryParameters.addAll(mandatoryParameters);
55
		CommandsAPI.cmds.put(cmd, command);
56
	}
57
	
58
	/**
59
	 * Manually install a command
60
	 * 
61
	 * @param cmd
62
	 * @param clazz
63
	 * @param mandatory_parameters
64
	 * @param optional_parameters
65
	 */
66
	public static void install(String cmd, Class<TXMResult> clazz, HashSet<String> mandatoryParameters, HashSet<String> optionalParameters) {
67
		Command command = new Command(cmd,clazz);
68
		command.mandatoryParameters.addAll(mandatoryParameters);
69
		command.optionalParameters.addAll(optionalParameters);
70
		CommandsAPI.cmds.put(cmd, command);
71
	}
72
	
73
	/**
74
	 * Manually uninstall a command
75
	 * 
76
	 * @param cmd
77
	 */
78
	public static void uninstall(String cmd) {
79
		CommandsAPI.cmds.remove(cmd);
80
	}
81
	
82
	public Object call(String cmd, TXMResult parent, HashMap<String, Object> parameters) {
83
		
84
		Command command = cmds.get(cmd);
85
		if (command == null) {
86
			System.out.println("No command found with name: "+cmd);
87
			return null;
88
		}
89
		
90
		// compute directly the command using the parameters 
91
		TXMResult result = null;
92
		try {
93
			if (command.mandatoryParameters.containsAll(parameters.keySet())) {
94
				System.out.println("Computing "+command.cmd+"...");
95
				result = command.clazz.newInstance();
96
				//result.compute(parameters);
97
				//System.out.println(result);
98
			} else {
99
				System.out.println("Failed to compute result with "+cmd+" command: missing some mandatory parameters: "+command.mandatoryParameters);
100
			}
101
		} catch (Exception e) {
102
			System.out.println("Failed to compute result with "+cmd+" command and "+parameters+" parameters: "+e.getLocalizedMessage());
103
			Log.printStackTrace(e);
104
		}
105
		return result;
106
	}
107
	
108
	// portail : ???
109
	// groovy : CommandsApi.call("Concordances", ["query":"Je|je]");
110
	// bash-like : compute Concordances query:"Je|je"
111
}
0 112

  
tmp/org.txm.core/src/java/org/txm/functions/Command.java (revision 359)
1
package org.txm.functions;
2

  
3
import java.util.HashSet;
4

  
5
import org.txm.core.results.TXMResult;
6

  
7
/**
8
 * Command description
9
 * 
10
 * @author mdecorde
11
 *
12
 */
13
public class Command {
14
	
15
	public final String cmd;
16
	public final Class<TXMResult> clazz;
17
	public final HashSet<String> mandatoryParameters = new HashSet<String>();
18
	public final HashSet<String> optionalParameters = new HashSet<String>();
19
	
20
	public Command(Class<TXMResult> clazz) {
21
		this.cmd = clazz.getSimpleName();
22
		this.clazz = clazz;
23
	}
24
	
25
	public Command(String cmd, Class<TXMResult> clazz) {
26
		this.cmd = cmd;
27
		this.clazz = clazz;
28
	}
29
	
30
	public String toString() {
31
		return cmd+" -> "+clazz.getSimpleName()+"("+mandatoryParameters+", "+optionalParameters+")";
32
	}
33
}
0 34

  
tmp/org.txm.core/plugin.xml (revision 359)
2 2
<?eclipse version="3.4"?>
3 3
<plugin>
4 4
   <extension-point id="org.txm.engine" name="Engine" schema="schema/engine.exsd"/>
5
   <extension-point id="org.txm.commandapi" name="CommandAPI" schema="schema/org.txm.commandapi.exsd"/>
5 6
   <extension
6 7
         point="org.eclipse.core.runtime.preferences">
7 8
      <initializer
tmp/org.txm.rcp/src/main/java/org/txm/rcp/utils/JobHandler.java (revision 359)
45 45
 * 
46 46
 * the implemented run methods returns a Status.OK_STATUS or Status.CANCEL_STATUS that can be tested with the getResult() after the join method is called.
47 47
 * 
48
 * it must also call the runInit method to allow th user to force stop the JobThread
48
 * it must also call the runInit method to allow the user to force stop the JobThread
49 49
 * 
50 50
 * @author mdecorde, nkredens
51 51
 */
tmp/org.txm.rcp/src/main/java/org/txm/rcp/editors/RCPCommand.java (revision 359)
1
package org.txm.rcp.editors;
2

  
3
import org.txm.core.results.TXMResult;
4
import org.txm.functions.Command;
5

  
6
/**
7
 * A command that opens an editor (which will computes the result).
8
 * 
9
 * The class adds a new mandatory member 'editor_id'
10
 * 
11
 * @author mdecorde
12
 *
13
 */
14
public class RCPCommand extends Command {
15

  
16
	public final String editor_id;
17
	
18
	public RCPCommand(Class<TXMResult> clazz, String editor_id) {
19
		super(clazz);
20
		this.editor_id = editor_id;
21
	}
22

  
23
	public RCPCommand(String cmd, Class<TXMResult> clazz, String editor_id) {
24
		super(cmd, clazz);
25
		this.editor_id = editor_id;
26
	}
27
	
28
	public String toString() {
29
		return super.toString()+" -> "+editor_id;
30
	}
31
	
32
	// 
33
}
0 34

  
tmp/org.txm.rcp/src/main/java/org/txm/rcp/editors/RCPCommandsAPI.java (revision 359)
1
package org.txm.rcp.editors;
2

  
3
import java.util.HashMap;
4

  
5
import org.eclipse.ui.PartInitException;
6
import org.txm.core.results.TXMResult;
7
import org.txm.functions.Command;
8
import org.txm.functions.CommandsAPI;
9
import org.txm.rcp.TXMWindows;
10
import org.txm.utils.logger.Log;
11

  
12
/**
13
 * Interface to call commands from a RCP plugin
14
 * 
15
 * @author mdecorde
16
 *
17
 */
18
public class RCPCommandsAPI extends CommandsAPI {
19
	
20
	/**
21
	 * @param cmd the command to call or the command to ope nwith the right editor
22
	 * @param parent the result parent
23
	 * @param parameters the parameters to compute the command or the parameters to set editor parameters with
24
	 * 
25
	 * @return A result or the editor's result. null if the command failed to compute  or to open editor. 
26
	 */
27
	public Object call(String cmd, TXMResult parent, HashMap<String, Object> parameters) {
28

  
29
		Command command = cmds.get(cmd);
30
		if (command == null) {
31
			System.out.println("No command found with name: "+cmd);
32
			return null;
33
		}
34
		
35
		if (command instanceof RCPCommand) {
36
			
37
			RCPCommand rcpcommand = (RCPCommand)command;
38
			
39
			if (rcpcommand.editor_id == null) {
40
				System.out.println("Failed to open editor: no context given.");
41
				return null;
42
			}
43
			
44
			// the editor will compute the command
45
			TXMResultEditorInput editorInput = new TXMResultEditorInput(parent, parameters);
46
			try {
47
				TXMEditorPart editor = (TXMEditorPart) TXMWindows.getActiveWindow().getActivePage()
48
						.openEditor(editorInput, rcpcommand.editor_id);
49
				if (editor.isReady()) {
50
					return editor;
51
				} else {
52
					System.out.println("Failed to open editor: internal error during initialisation.");
53
					return null;
54
				}
55
			} catch (PartInitException e) {
56
				System.out.println("Failed to open editor with "+cmd+" command and "+parameters+" parameters: "+e.getLocalizedMessage());
57
				Log.printStackTrace(e);
58
			}
59
			return null;
60
			
61
		} else {
62
			Object result = super.call(cmd, parent, parameters);
63
			System.out.println("Done: "+result);
64
			return result;
65
		}
66
	}
67
	
68
	//dans Handler.execute() RCPCommandsAPI.call("Concordance", params);
69
}
0 70

  

Also available in: Unified diff