/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
- /**
- * -----------------------------------------------------------------------------------------------
- * File: TaskInfoPopup.java
- *
- * Copyright (c) 2007 by Keymind Computing as.
- * All rights reserved.
- *
- * This file is subject to the terms and conditions of the Apache Licence 2.0.
- * See the file LICENCE in the main directory of the Keywatch distribution for more details.
- *
- * Revision History:
- * $URL: http://keywatch.googlecode.com/svn/trunk/src/server/gwtgui/src/keymind/keywatch/gui/client/taskwidget/TaskInfoPopup.java $
- * $Date: 2009-09-17 11:14:51 +0200 (Thu, 17 Sep 2009) $, $Rev: $
- * -----------------------------------------------------------------------------------------------
- */
- package keymind.keywatch.gui.client.taskwidget;
-
- import com.google.gwt.core.client.GWT;
- import com.google.gwt.event.dom.client.ClickEvent;
- import com.google.gwt.event.dom.client.ClickHandler;
- import com.google.gwt.user.client.rpc.AsyncCallback;
- import com.google.gwt.user.client.rpc.ServiceDefTarget;
- import com.google.gwt.user.client.ui.*;
- import keymind.keywatch.domainmodel.eventDomain.Task;
- import keymind.keywatch.gui.client.desktop.GUICtrl;
- import keymind.keywatch.gui.client.util.Constants;
- import keymind.keywatch.services.eventmanagement.ITaskController;
- import keymind.keywatch.services.eventmanagement.ITaskControllerAsync;
-
- /**
- * Info panel for tasks
- */
- public class TaskInfoPopup extends PopupPanel implements ClickHandler, Constants
- {
- Task task = null;
- TaskView taskView = null;
- Hyperlink lnkRefresh = null;
- Hyperlink lnkClose = null;
- Hyperlink lnkStart = null;
- Hyperlink lnkStop = null;
- Hyperlink lnkEnable = null;
- Hyperlink lnkDisable = null;
- int cancellationToken;
- ITaskControllerAsync taskMgmt = null;
-
- static final String CONFIRM_TERMINATE_TASK = "The task you want to disable is currently running. " +
- "Press OK to terminate the task before disabling it or Cancel to " +
- "leave it running.";
-
- /**
- * C'tor
- *
- * @param taskView
- */
- public TaskInfoPopup(TaskView taskView)
- {
- this.taskView = taskView;
- this.cancellationToken = 0;
-
- taskMgmt = (ITaskControllerAsync)GWT.create(ITaskController.class);
- ((ServiceDefTarget)taskMgmt).setServiceEntryPoint(SERVICE_ENTRYPOINT);
- }
-
-
- /**
- * Generate popup content based on task type and state
- *
- * @param task
- */
- void setTask(Task task)
- {
- this.task = task;
-
- setStyleName(STYLE_POPUP_TASK);
-
- VerticalPanel content = new VerticalPanel();
- content.setSpacing(15);
-
- // Add header
- HTML html = new HTML("<b>Task #" + task.getId() + ":</b> " + task.getName());
- html.setWordWrap(false);
- html.setWidth("300px");
- content.add(html);
-
- // Add description depending on job type and status
- if (TaskView.isScheduled(task))
- {
- String info = "Schedule: " + task.getSchedule() + "\n\n";
- if (!task.getIsActive().booleanValue())
- {
- info += "This task is inactive. If you enable the task, it will be started the next time " +
- "the schedule is due. You can also start and stop the task manually once enabled.";
- }
- else
- {
- info += "The task is active and will run each time the schedule is due. " +
- "You can also start and stop the task manually.";
- }
-
- Label schedule = new Label(info);
- content.add(schedule);
- }
- else
- {
- String info;
- if (task.getParentTask() == null)
- {
- info = "Event Provider plugin";
-
- if (task.getIsRunning().booleanValue())
- {
- info += ". If you stop the plugin, tasks may still keep running but no events will be " +
- "reported until the plugin is started.";
- }
- else
- {
- info += ". When you start the plugin, tasks will be able to report events.";
- }
- }
- else if (TaskView.isJob(task))
- {
- info = "This is a daemon task";
-
- if (!task.getIsActive().booleanValue())
- {
- info += ". If you enable the task, if will be started the next time the agent is started. " +
- "You can also start and stop the task manually when enabled";
- }
- else
- {
- if (!task.getIsRunning().booleanValue())
- {
- info += ". Last check indicates that the task is not running. " +
- "\n\nYou can start the task now or refresh the status by choosing 'Refresh status' on the provider task.";
- }
- else
- {
- info += ". The task is active and should be running.\n\nYou can manually stop the task and restart it later. " +
- "Disabling the task will prevent it from run when the agent is restarted.";
- }
- }
- }
- else
- {
- info = "";
-
- if (task.getIsRunning().booleanValue())
- {
- info += "If you stop this task you will need to restart it manually";
- }
- else
- {
- info += "You need to manually start this task. It should automatically register itself " +
- "with Keywatch and change the status to 'Running'";
- }
- }
-
- Label schedule = new Label(info);
- content.add(schedule);
- }
-
- // If the provider bundle isn't running, we really don't know the true state
- if (!TaskView.isProvider(task) && !TaskView.isProviderRunning(task))
- {
- Label warning = new Label("The status of this task may not be up to date " +
- "because the provider is not running");
-
- warning.setStyleName(STYLE_POPUP_TASK_WARNING);
- content.add(warning);
- }
-
- // Add links
- HorizontalPanel linkPanel = new HorizontalPanel();
- linkPanel.setSpacing(5);
-
- // No actions are possible when the parent task is dead, nor when the provider is down
- if (TaskView.isProvider(task) || (TaskView.isProviderRunning(task) &&
- (!TaskView.isProvider(task) && task.getParentTask().getIsRunning().booleanValue())))
- {
- // Refresh button (currently only available at Provider level, since
- // we can't deserialize ITaskController instances)
- if ((TaskView.isProvider(task) || TaskView.isAgent(task)) && task.getIsRunning().booleanValue())
- {
- lnkRefresh = new Hyperlink("Refresh status", "");
- lnkRefresh.setTitle("Get task status, including sub tasks");
- lnkRefresh.addClickHandler(this);
- lnkRefresh.setStyleName(STYLE_POPUP_TASK_BUTTON);
- linkPanel.add(lnkRefresh);
- }
-
- if (TaskView.isAgent(task) || task.getIsActive().booleanValue())
- {
- // An agent cannot be started. A provider bundle cannot be started if it is already running.
- if (TaskView.isJob(task) ||
- (TaskView.isProvider(task) && !task.getIsRunning().booleanValue()))
- {
- lnkStart = new Hyperlink("Run now", "Start the task");
- lnkStart.addClickHandler(this);
- lnkStart.setStyleName(STYLE_POPUP_TASK_BUTTON);
- linkPanel.add(lnkStart);
- }
-
- // Jobs can be attempted stopped regardless of run status (since the real
- // run status is hard to obtain, especially for scheduled jobs)
- //if(task.getParentTask() != null || task.getIsRunning().booleanValue())
- if (TaskView.isJob(task) || task.getIsRunning().booleanValue())
- {
- lnkStop = new Hyperlink("Stop now", "Stop the task");
- lnkStop.addClickHandler(this);
- lnkStop.setStyleName(STYLE_POPUP_TASK_BUTTON);
- linkPanel.add(lnkStop);
- }
-
- // Can only disable jobs
- if (TaskView.isJob(task))
- {
- lnkDisable = new Hyperlink("Disable task", "Disable the task");
- lnkDisable.addClickHandler(this);
- lnkDisable.setStyleName(STYLE_POPUP_TASK_BUTTON);
- linkPanel.add(lnkDisable);
- }
- }
-
- // Can only enable jobs
- else if (TaskView.isJob(task))
- {
- lnkEnable = new Hyperlink("Enable task", "Enable the task");
- lnkEnable.addClickHandler(this);
- lnkEnable.setStyleName(STYLE_POPUP_TASK_BUTTON);
- linkPanel.add(lnkEnable);
- }
- }
-
- // Add some air after the action buttons, if any
- if (linkPanel.getWidgetCount() > 0)
- {
- Label air = new Label();
- air.setWidth("20px");
- linkPanel.add(air);
- }
-
- lnkClose = new Hyperlink("Close", "Close this dialog");
- lnkClose.addClickHandler(this);
- lnkClose.setStyleName(STYLE_POPUP_TASK_BUTTON);
- linkPanel.add(lnkClose);
-
- // Add link panel
- content.add(linkPanel);
-
- setWidget(content);
- }
-
-
- /**
- * A panel link is clicked
- *
- * @param evt ClickEvent
- */
- public void onClick(ClickEvent evt)
- {
- Widget widget = (Widget)evt.getSource();
-
- GUICtrl.log("onClick");
-
- if (widget == lnkClose)
- {
- // Are we cancelling an operation?
- if (cancellationToken != 0)
- {
- cancellationToken = -cancellationToken;
- }
-
- hide();
- }
- else if (widget == lnkStart)
- {
- taskMgmt.startTask(GUICtrl.getContext(), task, new AsyncCallback()
- {
- /**
- * @param object
- */
- public void onSuccess(Object object)
- {
- taskView.showTaskTree();
-
- GUICtrl.log("Start OK");
- }
-
- /**
- * @param throwable
- */
- public void onFailure(Throwable throwable)
- {
- GUICtrl.log("Started failed: " + throwable.toString());
- }
- });
-
- hide();
- }
- else if (widget == lnkStop)
- {
- taskMgmt.stopTask(GUICtrl.getContext(), task, new AsyncCallback()
- {
- public void onSuccess(Object object)
- {
- taskView.showTaskTree();
- }
-
- public void onFailure(Throwable throwable)
- {
- GUICtrl.alert("Stop failed: " + throwable.toString());
- }
- });
-
- hide();
- }
- else if (widget == lnkEnable)
- {
- AsyncCallback callback = new AsyncCallback()
- {
- public void onSuccess(Object object)
- {
- taskView.showTaskTree();
- GUICtrl.log("Enable OK");
- }
-
- public void onFailure(Throwable throwable)
- {
- GUICtrl.alert("Enable failed: " + throwable.toString());
- }
- };
-
- taskMgmt.activateTask(GUICtrl.getContext(), task, callback);
-
- hide();
- }
- else if (widget == lnkDisable)
- {
- // If the task is running, ask if it should be stopped before disabled
- if(TaskView.isDaemon(task) && task.getIsRunning() != null && task.getIsRunning().booleanValue())
- {
- if(GUICtrl.confirm(CONFIRM_TERMINATE_TASK))
- {
- taskMgmt.stopTask(GUICtrl.getContext(), task, new AsyncCallback()
- {
- /** The task was stopped successfully */
- public void onSuccess(Object object)
- {
- deactivateTask();
- }
-
- public void onFailure(Throwable throwable)
- {
- GUICtrl.alert("Stop failed: " + throwable.toString());
- }
- });
- }
- else
- {
- deactivateTask();
- }
- }
- else
- {
- deactivateTask();
- }
-
- hide();
- }
- else if (widget == lnkRefresh)
- {
- if (-cancellationToken == TaskView.CANCELLATION_REFRESH)
- {
- GUICtrl.alert("A refresh operation is already in progress.");
- return;
- }
-
- VerticalPanel content = new VerticalPanel();
- content.setSpacing(15);
-
- HorizontalPanel statusLine = new HorizontalPanel();
- Image img = GUICtrl.getCoreImageBundle().reload().createImage();
- statusLine.add(img);
-
- Label note = new Label(" Querying for task status. This may take a while; please be patient...");
- note.setStyleName("kwTaskPopup headerText");
- statusLine.add(note);
- content.add(statusLine);
-
- lnkClose = new Hyperlink("Run in the background", "Run in the background");
- lnkClose.addClickHandler(this);
- lnkClose.setStyleName(STYLE_POPUP_TASK_BUTTON);
- content.add(lnkClose);
- setCancellationToken(TaskView.CANCELLATION_REFRESH);
- setWidget(content);
-
- setStyleName(STYLE_POPUP_TASK);
-
- taskMgmt.refreshTaskTree(GUICtrl.getContext(), task, new AsyncCallback()
- {
- public void onSuccess(Object object)
- {
- taskView.showTaskTree();
- hide();
- cancellationToken = 0;
- }
-
- public void onFailure(Throwable throwable)
- {
- hide();
- cancellationToken = 0;
- }
- });
- }
- }
-
- /**
- * Deactivate task
- */
- private void deactivateTask()
- {
- taskMgmt.deactivateTask(GUICtrl.getContext(), task, new AsyncCallback()
- {
- public void onSuccess(Object object)
- {
- taskView.showTaskTree();
- }
-
- public void onFailure(Throwable throwable)
- {
- GUICtrl.alert("Disable failed: " + throwable.toString());
- }
- });
- }
-
- /**
- * Set cancellation token
- */
- private void setCancellationToken(int token)
- {
- cancellationToken = token;
- }
- }