/src/server/gwtgui/src/keymind/keywatch/gui/client/taskwidget/TaskInfoPopup.java

http://keywatch.googlecode.com/ · Java · 444 lines · 327 code · 54 blank · 63 comment · 54 complexity · fb89d41785d5063e07d4f1f8ff895bf6 MD5 · raw file

  1. /**
  2. * -----------------------------------------------------------------------------------------------
  3. * File: TaskInfoPopup.java
  4. *
  5. * Copyright (c) 2007 by Keymind Computing as.
  6. * All rights reserved.
  7. *
  8. * This file is subject to the terms and conditions of the Apache Licence 2.0.
  9. * See the file LICENCE in the main directory of the Keywatch distribution for more details.
  10. *
  11. * Revision History:
  12. * $URL: http://keywatch.googlecode.com/svn/trunk/src/server/gwtgui/src/keymind/keywatch/gui/client/taskwidget/TaskInfoPopup.java $
  13. * $Date: 2009-09-17 11:14:51 +0200 (Thu, 17 Sep 2009) $, $Rev: $
  14. * -----------------------------------------------------------------------------------------------
  15. */
  16. package keymind.keywatch.gui.client.taskwidget;
  17. import com.google.gwt.core.client.GWT;
  18. import com.google.gwt.event.dom.client.ClickEvent;
  19. import com.google.gwt.event.dom.client.ClickHandler;
  20. import com.google.gwt.user.client.rpc.AsyncCallback;
  21. import com.google.gwt.user.client.rpc.ServiceDefTarget;
  22. import com.google.gwt.user.client.ui.*;
  23. import keymind.keywatch.domainmodel.eventDomain.Task;
  24. import keymind.keywatch.gui.client.desktop.GUICtrl;
  25. import keymind.keywatch.gui.client.util.Constants;
  26. import keymind.keywatch.services.eventmanagement.ITaskController;
  27. import keymind.keywatch.services.eventmanagement.ITaskControllerAsync;
  28. /**
  29. * Info panel for tasks
  30. */
  31. public class TaskInfoPopup extends PopupPanel implements ClickHandler, Constants
  32. {
  33. Task task = null;
  34. TaskView taskView = null;
  35. Hyperlink lnkRefresh = null;
  36. Hyperlink lnkClose = null;
  37. Hyperlink lnkStart = null;
  38. Hyperlink lnkStop = null;
  39. Hyperlink lnkEnable = null;
  40. Hyperlink lnkDisable = null;
  41. int cancellationToken;
  42. ITaskControllerAsync taskMgmt = null;
  43. static final String CONFIRM_TERMINATE_TASK = "The task you want to disable is currently running. " +
  44. "Press OK to terminate the task before disabling it or Cancel to " +
  45. "leave it running.";
  46. /**
  47. * C'tor
  48. *
  49. * @param taskView
  50. */
  51. public TaskInfoPopup(TaskView taskView)
  52. {
  53. this.taskView = taskView;
  54. this.cancellationToken = 0;
  55. taskMgmt = (ITaskControllerAsync)GWT.create(ITaskController.class);
  56. ((ServiceDefTarget)taskMgmt).setServiceEntryPoint(SERVICE_ENTRYPOINT);
  57. }
  58. /**
  59. * Generate popup content based on task type and state
  60. *
  61. * @param task
  62. */
  63. void setTask(Task task)
  64. {
  65. this.task = task;
  66. setStyleName(STYLE_POPUP_TASK);
  67. VerticalPanel content = new VerticalPanel();
  68. content.setSpacing(15);
  69. // Add header
  70. HTML html = new HTML("<b>Task #" + task.getId() + ":</b> " + task.getName());
  71. html.setWordWrap(false);
  72. html.setWidth("300px");
  73. content.add(html);
  74. // Add description depending on job type and status
  75. if (TaskView.isScheduled(task))
  76. {
  77. String info = "Schedule: " + task.getSchedule() + "\n\n";
  78. if (!task.getIsActive().booleanValue())
  79. {
  80. info += "This task is inactive. If you enable the task, it will be started the next time " +
  81. "the schedule is due. You can also start and stop the task manually once enabled.";
  82. }
  83. else
  84. {
  85. info += "The task is active and will run each time the schedule is due. " +
  86. "You can also start and stop the task manually.";
  87. }
  88. Label schedule = new Label(info);
  89. content.add(schedule);
  90. }
  91. else
  92. {
  93. String info;
  94. if (task.getParentTask() == null)
  95. {
  96. info = "Event Provider plugin";
  97. if (task.getIsRunning().booleanValue())
  98. {
  99. info += ". If you stop the plugin, tasks may still keep running but no events will be " +
  100. "reported until the plugin is started.";
  101. }
  102. else
  103. {
  104. info += ". When you start the plugin, tasks will be able to report events.";
  105. }
  106. }
  107. else if (TaskView.isJob(task))
  108. {
  109. info = "This is a daemon task";
  110. if (!task.getIsActive().booleanValue())
  111. {
  112. info += ". If you enable the task, if will be started the next time the agent is started. " +
  113. "You can also start and stop the task manually when enabled";
  114. }
  115. else
  116. {
  117. if (!task.getIsRunning().booleanValue())
  118. {
  119. info += ". Last check indicates that the task is not running. " +
  120. "\n\nYou can start the task now or refresh the status by choosing 'Refresh status' on the provider task.";
  121. }
  122. else
  123. {
  124. info += ". The task is active and should be running.\n\nYou can manually stop the task and restart it later. " +
  125. "Disabling the task will prevent it from run when the agent is restarted.";
  126. }
  127. }
  128. }
  129. else
  130. {
  131. info = "";
  132. if (task.getIsRunning().booleanValue())
  133. {
  134. info += "If you stop this task you will need to restart it manually";
  135. }
  136. else
  137. {
  138. info += "You need to manually start this task. It should automatically register itself " +
  139. "with Keywatch and change the status to 'Running'";
  140. }
  141. }
  142. Label schedule = new Label(info);
  143. content.add(schedule);
  144. }
  145. // If the provider bundle isn't running, we really don't know the true state
  146. if (!TaskView.isProvider(task) && !TaskView.isProviderRunning(task))
  147. {
  148. Label warning = new Label("The status of this task may not be up to date " +
  149. "because the provider is not running");
  150. warning.setStyleName(STYLE_POPUP_TASK_WARNING);
  151. content.add(warning);
  152. }
  153. // Add links
  154. HorizontalPanel linkPanel = new HorizontalPanel();
  155. linkPanel.setSpacing(5);
  156. // No actions are possible when the parent task is dead, nor when the provider is down
  157. if (TaskView.isProvider(task) || (TaskView.isProviderRunning(task) &&
  158. (!TaskView.isProvider(task) && task.getParentTask().getIsRunning().booleanValue())))
  159. {
  160. // Refresh button (currently only available at Provider level, since
  161. // we can't deserialize ITaskController instances)
  162. if ((TaskView.isProvider(task) || TaskView.isAgent(task)) && task.getIsRunning().booleanValue())
  163. {
  164. lnkRefresh = new Hyperlink("Refresh status", "");
  165. lnkRefresh.setTitle("Get task status, including sub tasks");
  166. lnkRefresh.addClickHandler(this);
  167. lnkRefresh.setStyleName(STYLE_POPUP_TASK_BUTTON);
  168. linkPanel.add(lnkRefresh);
  169. }
  170. if (TaskView.isAgent(task) || task.getIsActive().booleanValue())
  171. {
  172. // An agent cannot be started. A provider bundle cannot be started if it is already running.
  173. if (TaskView.isJob(task) ||
  174. (TaskView.isProvider(task) && !task.getIsRunning().booleanValue()))
  175. {
  176. lnkStart = new Hyperlink("Run now", "Start the task");
  177. lnkStart.addClickHandler(this);
  178. lnkStart.setStyleName(STYLE_POPUP_TASK_BUTTON);
  179. linkPanel.add(lnkStart);
  180. }
  181. // Jobs can be attempted stopped regardless of run status (since the real
  182. // run status is hard to obtain, especially for scheduled jobs)
  183. //if(task.getParentTask() != null || task.getIsRunning().booleanValue())
  184. if (TaskView.isJob(task) || task.getIsRunning().booleanValue())
  185. {
  186. lnkStop = new Hyperlink("Stop now", "Stop the task");
  187. lnkStop.addClickHandler(this);
  188. lnkStop.setStyleName(STYLE_POPUP_TASK_BUTTON);
  189. linkPanel.add(lnkStop);
  190. }
  191. // Can only disable jobs
  192. if (TaskView.isJob(task))
  193. {
  194. lnkDisable = new Hyperlink("Disable task", "Disable the task");
  195. lnkDisable.addClickHandler(this);
  196. lnkDisable.setStyleName(STYLE_POPUP_TASK_BUTTON);
  197. linkPanel.add(lnkDisable);
  198. }
  199. }
  200. // Can only enable jobs
  201. else if (TaskView.isJob(task))
  202. {
  203. lnkEnable = new Hyperlink("Enable task", "Enable the task");
  204. lnkEnable.addClickHandler(this);
  205. lnkEnable.setStyleName(STYLE_POPUP_TASK_BUTTON);
  206. linkPanel.add(lnkEnable);
  207. }
  208. }
  209. // Add some air after the action buttons, if any
  210. if (linkPanel.getWidgetCount() > 0)
  211. {
  212. Label air = new Label();
  213. air.setWidth("20px");
  214. linkPanel.add(air);
  215. }
  216. lnkClose = new Hyperlink("Close", "Close this dialog");
  217. lnkClose.addClickHandler(this);
  218. lnkClose.setStyleName(STYLE_POPUP_TASK_BUTTON);
  219. linkPanel.add(lnkClose);
  220. // Add link panel
  221. content.add(linkPanel);
  222. setWidget(content);
  223. }
  224. /**
  225. * A panel link is clicked
  226. *
  227. * @param evt ClickEvent
  228. */
  229. public void onClick(ClickEvent evt)
  230. {
  231. Widget widget = (Widget)evt.getSource();
  232. GUICtrl.log("onClick");
  233. if (widget == lnkClose)
  234. {
  235. // Are we cancelling an operation?
  236. if (cancellationToken != 0)
  237. {
  238. cancellationToken = -cancellationToken;
  239. }
  240. hide();
  241. }
  242. else if (widget == lnkStart)
  243. {
  244. taskMgmt.startTask(GUICtrl.getContext(), task, new AsyncCallback()
  245. {
  246. /**
  247. * @param object
  248. */
  249. public void onSuccess(Object object)
  250. {
  251. taskView.showTaskTree();
  252. GUICtrl.log("Start OK");
  253. }
  254. /**
  255. * @param throwable
  256. */
  257. public void onFailure(Throwable throwable)
  258. {
  259. GUICtrl.log("Started failed: " + throwable.toString());
  260. }
  261. });
  262. hide();
  263. }
  264. else if (widget == lnkStop)
  265. {
  266. taskMgmt.stopTask(GUICtrl.getContext(), task, new AsyncCallback()
  267. {
  268. public void onSuccess(Object object)
  269. {
  270. taskView.showTaskTree();
  271. }
  272. public void onFailure(Throwable throwable)
  273. {
  274. GUICtrl.alert("Stop failed: " + throwable.toString());
  275. }
  276. });
  277. hide();
  278. }
  279. else if (widget == lnkEnable)
  280. {
  281. AsyncCallback callback = new AsyncCallback()
  282. {
  283. public void onSuccess(Object object)
  284. {
  285. taskView.showTaskTree();
  286. GUICtrl.log("Enable OK");
  287. }
  288. public void onFailure(Throwable throwable)
  289. {
  290. GUICtrl.alert("Enable failed: " + throwable.toString());
  291. }
  292. };
  293. taskMgmt.activateTask(GUICtrl.getContext(), task, callback);
  294. hide();
  295. }
  296. else if (widget == lnkDisable)
  297. {
  298. // If the task is running, ask if it should be stopped before disabled
  299. if(TaskView.isDaemon(task) && task.getIsRunning() != null && task.getIsRunning().booleanValue())
  300. {
  301. if(GUICtrl.confirm(CONFIRM_TERMINATE_TASK))
  302. {
  303. taskMgmt.stopTask(GUICtrl.getContext(), task, new AsyncCallback()
  304. {
  305. /** The task was stopped successfully */
  306. public void onSuccess(Object object)
  307. {
  308. deactivateTask();
  309. }
  310. public void onFailure(Throwable throwable)
  311. {
  312. GUICtrl.alert("Stop failed: " + throwable.toString());
  313. }
  314. });
  315. }
  316. else
  317. {
  318. deactivateTask();
  319. }
  320. }
  321. else
  322. {
  323. deactivateTask();
  324. }
  325. hide();
  326. }
  327. else if (widget == lnkRefresh)
  328. {
  329. if (-cancellationToken == TaskView.CANCELLATION_REFRESH)
  330. {
  331. GUICtrl.alert("A refresh operation is already in progress.");
  332. return;
  333. }
  334. VerticalPanel content = new VerticalPanel();
  335. content.setSpacing(15);
  336. HorizontalPanel statusLine = new HorizontalPanel();
  337. Image img = GUICtrl.getCoreImageBundle().reload().createImage();
  338. statusLine.add(img);
  339. Label note = new Label(" Querying for task status. This may take a while; please be patient...");
  340. note.setStyleName("kwTaskPopup headerText");
  341. statusLine.add(note);
  342. content.add(statusLine);
  343. lnkClose = new Hyperlink("Run in the background", "Run in the background");
  344. lnkClose.addClickHandler(this);
  345. lnkClose.setStyleName(STYLE_POPUP_TASK_BUTTON);
  346. content.add(lnkClose);
  347. setCancellationToken(TaskView.CANCELLATION_REFRESH);
  348. setWidget(content);
  349. setStyleName(STYLE_POPUP_TASK);
  350. taskMgmt.refreshTaskTree(GUICtrl.getContext(), task, new AsyncCallback()
  351. {
  352. public void onSuccess(Object object)
  353. {
  354. taskView.showTaskTree();
  355. hide();
  356. cancellationToken = 0;
  357. }
  358. public void onFailure(Throwable throwable)
  359. {
  360. hide();
  361. cancellationToken = 0;
  362. }
  363. });
  364. }
  365. }
  366. /**
  367. * Deactivate task
  368. */
  369. private void deactivateTask()
  370. {
  371. taskMgmt.deactivateTask(GUICtrl.getContext(), task, new AsyncCallback()
  372. {
  373. public void onSuccess(Object object)
  374. {
  375. taskView.showTaskTree();
  376. }
  377. public void onFailure(Throwable throwable)
  378. {
  379. GUICtrl.alert("Disable failed: " + throwable.toString());
  380. }
  381. });
  382. }
  383. /**
  384. * Set cancellation token
  385. */
  386. private void setCancellationToken(int token)
  387. {
  388. cancellationToken = token;
  389. }
  390. }