Eclipseplugins
QueryGeneratorHandler.java
1 package com.proalpha.java.oea.plugins.querygenerator.handlers;
2 
3 import java.util.Map;
4 
5 import org.eclipse.core.commands.AbstractHandler;
6 import org.eclipse.core.commands.ExecutionEvent;
7 import org.eclipse.core.commands.ExecutionException;
8 import org.eclipse.swt.widgets.Display;
9 
10 import com.openedge.core.runtime.IAVMClient;
11 import com.openedge.core.runtime.IAVMRuntimeListener;
12 import com.openedge.pdt.project.OEProjectPlugin;
13 import com.openedge.pdt.project.sharedavm.SharedAVM;
14 import com.proalpha.java.oea.plugins.querygenerator.EditorTools;
15 import com.proalpha.java.oea.plugins.querygenerator.runtime.HandlerTools;
16 import com.proalpha.java.oea.plugins.querygenerator.runtime.QueryGeneratorPrograms;
17 
23 public class QueryGeneratorHandler extends AbstractHandler {
24 
29 
30  }
31 
36  public Object execute(ExecutionEvent event) throws ExecutionException {
37 
38 // IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
39 // MessageDialog.openInformation(window.getShell(), "QueryGenerator", "Start Query Generator");
40 
41  Display.getDefault().syncExec(new QueryGeneratorCommand(event.getParameters()));
42  return null;
43 
44  }
45 
46  @Override
47  public boolean isEnabled() {
48  return true;
49  }
50 
51  @Override
52  public boolean isHandled() {
53  return true;
54  }
55 
56  private class QueryGeneratorCommand implements Runnable {
57 
58  private Map<String,String> parameters = null;
59 
60  public QueryGeneratorCommand(Map<String, String> parameters) {
61  super();
62  this.parameters = parameters;
63  }
64 
65  @Override
66  public void run() {
67 
68  String commandParameter = String.format(
69  "EditorInsertRequestId=%s%sSavePluginSettingId=%s%sLoadPluginSettingId=%s",
70  EditorInsertCommandHandler.getRequestId(),
71  HandlerTools.PARAM_SPLITTER,
72  SavePluginSettingHandler.getRequestId(),
73  HandlerTools.PARAM_SPLITTER,
74  LoadPluginSettingHandler.getRequestId());
75 
76  IAVMClient runtime = null;
77 
78  String menuItemType = parameters.get("com.proalpha.java.oea.plugins.querygenerator.avmTypeParameter");
79 
80  if (menuItemType == null
81  || menuItemType.isEmpty()
82  || menuItemType.equals("DefaultAVM"))
83  runtime = HandlerTools.getInstance().getRuntime();
84 
85  else if (menuItemType.equals("ProjectAVM")) {
86 
87  OEProjectPlugin projectPlugin = OEProjectPlugin.getDefault();
88  String avmName = (String) parameters.get("com.proalpha.java.oea.plugins.querygenerator.avmNameParameter");
89 
90  if (avmName != null && !avmName.isEmpty())
91  runtime = projectPlugin.getOpenEdgeModel().getOpenEdgeProject(avmName).getRuntime();
92 
93  } else if (menuItemType.equals("SharedAVM")) {
94 
95  if (SharedAVM.isRuntimeStarted())
96  runtime = SharedAVM.getSharedAVM();
97 
98  else {
99 
100  // In this case query generator will be startet by
101  // the SharedAVMRuntimeListener instance and not by this
102  // procedure. It has to be done this way because
103  // getSharedAVM returns immediately and doesn't wait for
104  // the progress runtime to finish startup.
105  IAVMClient sharedRuntime = SharedAVM.getSharedAVM();
106  sharedRuntime.addAVMRuntimeListener(new SharedAVMRuntimeListener(commandParameter));
107 
108  }
109 
110  }
111 
112  if (runtime != null) {
113 
114  startQueryGenerator(runtime, commandParameter);
115 
116  String selectionText = EditorTools.getInstance().getSelectionText();
117 
118  if (selectionText != null
119  && !selectionText.isEmpty()
120  && selectionText.length() <= 1024)
121  HandlerTools.getInstance().runProgram(
122  runtime,
123  QueryGeneratorPrograms.EXECUTE_COMMAND,
124  String.format("command=select_table%stable_name=%s",
125  HandlerTools.PARAM_SPLITTER,
126  selectionText),
127  false);
128 
129  }
130 
131  }
132 
133  }
134 
135  private void startQueryGenerator(IAVMClient runtime, String commandParameter) {
136 
137  if (runtime != null && runtime.isAvailable())
138  HandlerTools.getInstance().runProgram(
139  runtime,
140  QueryGeneratorPrograms.START_QUERY_GENERATOR,
141  commandParameter,
142  true);
143 
144  }
145 
146  private class SharedAVMRuntimeListener implements IAVMRuntimeListener {
147 
148  private String commandParameter = "";
149 
150  public SharedAVMRuntimeListener(String commandParameter) {
151  super();
152  this.commandParameter = commandParameter;
153  }
154 
155  @Override
156  public void runtimeAvailable(IAVMClient runtime) { }
157 
158  @Override
159  public void runtimeInitialized(IAVMClient runtime) { }
160 
161  @Override
162  public void runtimeShutdown(IAVMClient runtime) { }
163 
164  @Override
165  public void runtimeStarted(IAVMClient runtime) {
166  runtime.removeAVMRuntimeListener(this);
167  startQueryGenerator(runtime, commandParameter);
168  }
169 
170  @Override
171  public void runtimeStopped(IAVMClient runtime) { }
172 
173  }
174 
175 }