91cfbb1dcd6490fa8cc08d04c1c11cfd970de9b0
[public/netxms.git] / webui / webapp / ServerJobManager / src / org / netxms / ui / eclipse / serverjobmanager / views / ServerJobManager.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2013 Victor Kirhenshtein
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 package org.netxms.ui.eclipse.serverjobmanager.views;
20
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.jface.action.Action;
26 import org.eclipse.jface.action.GroupMarker;
27 import org.eclipse.jface.action.IMenuListener;
28 import org.eclipse.jface.action.IMenuManager;
29 import org.eclipse.jface.action.IToolBarManager;
30 import org.eclipse.jface.action.MenuManager;
31 import org.eclipse.jface.action.Separator;
32 import org.eclipse.jface.viewers.ArrayContentProvider;
33 import org.eclipse.jface.viewers.ISelectionChangedListener;
34 import org.eclipse.jface.viewers.IStructuredSelection;
35 import org.eclipse.jface.viewers.SelectionChangedEvent;
36 import org.eclipse.jface.viewers.StructuredSelection;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.events.DisposeEvent;
39 import org.eclipse.swt.events.DisposeListener;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Menu;
42 import org.eclipse.ui.IActionBars;
43 import org.eclipse.ui.IWorkbenchActionConstants;
44 import org.eclipse.ui.part.ViewPart;
45 import org.netxms.client.NXCException;
46 import org.netxms.client.NXCSession;
47 import org.netxms.client.SessionListener;
48 import org.netxms.client.SessionNotification;
49 import org.netxms.client.constants.RCC;
50 import org.netxms.client.server.ServerJob;
51 import org.netxms.ui.eclipse.actions.RefreshAction;
52 import org.netxms.ui.eclipse.jobs.ConsoleJob;
53 import org.netxms.ui.eclipse.serverjobmanager.Activator;
54 import org.netxms.ui.eclipse.serverjobmanager.Messages;
55 import org.netxms.ui.eclipse.serverjobmanager.views.helpers.ServerJobComparator;
56 import org.netxms.ui.eclipse.serverjobmanager.views.helpers.ServerJobLabelProvider;
57 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
58 import org.netxms.ui.eclipse.tools.WidgetHelper;
59 import org.netxms.ui.eclipse.widgets.SortableTableViewer;
60
61 /**
62 * Server job manager - provides view to manage server-side jobs
63 */
64 public class ServerJobManager extends ViewPart
65 {
66 public static final String ID = "org.netxms.ui.eclipse.serverjobmanager.views.ServerJobManager"; //$NON-NLS-1$
67 public static final String JOB_FAMILY = "ServerJobManagerJob"; //$NON-NLS-1$
68
69 // Columns
70 public static final int COLUMN_STATUS = 0;
71 public static final int COLUMN_USER = 1;
72 public static final int COLUMN_NODE = 2;
73 public static final int COLUMN_DESCRIPTION = 3;
74 public static final int COLUMN_PROGRESS = 4;
75 public static final int COLUMN_MESSAGE = 5;
76
77 private static final String TABLE_CONFIG_PREFIX = "ServerJobManager"; //$NON-NLS-1$
78
79 private static final int CANCEL_JOB = 0;
80 private static final int HOLD_JOB = 1;
81 private static final int UNHOLD_JOB = 2;
82
83 private SortableTableViewer viewer;
84 private NXCSession session = null;
85 private SessionListener clientListener = null;
86
87 private RefreshAction actionRefresh;
88 private Action actionRestartJob;
89 private Action actionCancelJob;
90 private Action actionHoldJob;
91 private Action actionUnholdJob;
92
93 /* (non-Javadoc)
94 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
95 */
96 @Override
97 public void createPartControl(Composite parent)
98 {
99 final String[] names = { Messages.get().ServerJobManager_ColStatus, Messages.get().ServerJobManager_ColInitiator, Messages.get().ServerJobManager_ColNode, Messages.get().ServerJobManager_ColDescription, Messages.get().ServerJobManager_ColProgress, Messages.get().ServerJobManager_ColMessage };
100 final int[] widths = { 80, 100, 150, 250, 100, 300 };
101 viewer = new SortableTableViewer(parent, names, widths, 0, SWT.DOWN, SortableTableViewer.DEFAULT_STYLE);
102 viewer.setContentProvider(new ArrayContentProvider());
103 viewer.setLabelProvider(new ServerJobLabelProvider());
104 viewer.setComparator(new ServerJobComparator());
105 WidgetHelper.restoreTableViewerSettings(viewer, Activator.getDefault().getDialogSettings(), TABLE_CONFIG_PREFIX);
106
107 createActions();
108 contributeToActionBars();
109 createPopupMenu();
110
111 session = (NXCSession)ConsoleSharedData.getSession();
112
113 viewer.getTable().addDisposeListener(new DisposeListener() {
114 @Override
115 public void widgetDisposed(DisposeEvent e)
116 {
117 WidgetHelper.saveTableViewerSettings(viewer, Activator.getDefault().getDialogSettings(), TABLE_CONFIG_PREFIX);
118 }
119 });
120
121 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
122 @Override
123 public void selectionChanged(SelectionChangedEvent event)
124 {
125 IStructuredSelection selection = (IStructuredSelection)event.getSelection();
126 if (selection != null)
127 {
128 actionCancelJob.setEnabled(selection.size() > 0);
129 actionHoldJob.setEnabled(selection.size() > 0);
130 actionUnholdJob.setEnabled(selection.size() > 0);
131 }
132 }
133 });
134
135 // Create listener for notifications received from server via client library
136 clientListener = new SessionListener() {
137 @Override
138 public void notificationHandler(SessionNotification n)
139 {
140 if (n.getCode() != SessionNotification.JOB_CHANGE)
141 return;
142 refreshJobList(false);
143 }
144 };
145 session.addListener(clientListener);
146
147 refreshJobList(false);
148 }
149
150 /**
151 * Contribute actions to action bars
152 */
153 private void contributeToActionBars()
154 {
155 IActionBars bars = getViewSite().getActionBars();
156 fillLocalPullDown(bars.getMenuManager());
157 fillLocalToolBar(bars.getToolBarManager());
158 }
159
160 /**
161 * Fill local pulldown menu
162 * @param manager menu manager
163 */
164 private void fillLocalPullDown(IMenuManager manager)
165 {
166 manager.add(actionRestartJob);
167 manager.add(actionCancelJob);
168 manager.add(new Separator());
169 manager.add(actionHoldJob);
170 manager.add(actionUnholdJob);
171 manager.add(new Separator());
172 manager.add(actionRefresh);
173 }
174
175 /**
176 * Fill local toolbar
177 * @param manager menu manager
178 */
179 private void fillLocalToolBar(IToolBarManager manager)
180 {
181 //manager.add(actionRestartJob);
182 manager.add(actionCancelJob);
183 manager.add(actionRefresh);
184 }
185
186 /**
187 * Create pop-up menu
188 */
189 private void createPopupMenu()
190 {
191 // Create menu manager.
192 MenuManager menuMgr = new MenuManager();
193 menuMgr.setRemoveAllWhenShown(true);
194 menuMgr.addMenuListener(new IMenuListener() {
195 public void menuAboutToShow(IMenuManager mgr)
196 {
197 fillContextMenu(mgr);
198 }
199 });
200
201 // Create menu.
202 Menu menu = menuMgr.createContextMenu(viewer.getControl());
203 viewer.getControl().setMenu(menu);
204
205 // Register menu for extension.
206 getSite().registerContextMenu(menuMgr, viewer);
207 }
208
209 /**
210 * Fill context menu
211 * @param manager Menu manager
212 */
213 protected void fillContextMenu(IMenuManager manager)
214 {
215 manager.add(actionRestartJob);
216 manager.add(actionCancelJob);
217 manager.add(new Separator());
218 manager.add(actionHoldJob);
219 manager.add(actionUnholdJob);
220 manager.add(new Separator());
221 manager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
222 }
223
224 /**
225 * Create actions
226 */
227 private void createActions()
228 {
229 actionRefresh = new RefreshAction() {
230 @Override
231 public void run()
232 {
233 refreshJobList(true);
234 }
235 };
236
237 actionCancelJob = new Action(Messages.get().ServerJobManager_Cancel) {
238 @Override
239 public void run()
240 {
241 cancelServerJob();
242 }
243 };
244 actionCancelJob.setImageDescriptor(Activator.getImageDescriptor("icons/cancel.png")); //$NON-NLS-1$
245 actionCancelJob.setEnabled(false);
246
247 actionHoldJob = new Action(Messages.get().ServerJobManager_Hold) {
248 @Override
249 public void run()
250 {
251 holdServerJob();
252 }
253 };
254 actionHoldJob.setImageDescriptor(Activator.getImageDescriptor("icons/hold.gif")); //$NON-NLS-1$
255 actionHoldJob.setEnabled(false);
256
257 actionUnholdJob = new Action(Messages.get().ServerJobManager_Unhold) {
258 @Override
259 public void run()
260 {
261 unholdServerJob();
262 }
263 };
264 actionUnholdJob.setImageDescriptor(Activator.getImageDescriptor("icons/unhold.gif")); //$NON-NLS-1$
265 actionUnholdJob.setEnabled(false);
266
267 actionRestartJob = new Action(Messages.get().ServerJobManager_Restart) {
268 @Override
269 public void run()
270 {
271 }
272 };
273 actionRestartJob.setEnabled(false);
274 }
275
276 /* (non-Javadoc)
277 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
278 */
279 @Override
280 public void setFocus()
281 {
282 viewer.getControl().setFocus();
283 }
284
285 /* (non-Javadoc)
286 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
287 */
288 @Override
289 public void dispose()
290 {
291 if ((session != null) && (clientListener != null))
292 session.removeListener(clientListener);
293 super.dispose();
294 }
295
296 /**
297 * Refresh job list
298 */
299 private void refreshJobList(boolean userInitiated)
300 {
301 ConsoleJob job = new ConsoleJob(Messages.get().ServerJobManager_RefreshJobName, this, Activator.PLUGIN_ID, JOB_FAMILY) {
302 @Override
303 protected void runInternal(IProgressMonitor monitor) throws Exception
304 {
305 final ServerJob[] jobList = session.getServerJobList();
306 runInUIThread(new Runnable() {
307 @Override
308 public void run()
309 {
310 // Remember current selection
311 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
312 Object[] selectedObjects = selection.toArray();
313
314 viewer.setInput(jobList);
315
316 // Build new list of selected jobs - add object to selection if
317 // object with same id was selected before
318 List<ServerJob> selectedJobs = new ArrayList<ServerJob>(selectedObjects.length);
319 for(int i = 0; i < selectedObjects.length; i++)
320 {
321 for(int j = 0; j < jobList.length; j++)
322 {
323 if (((ServerJob)selectedObjects[i]).getId() == jobList[j].getId())
324 {
325 selectedJobs.add(jobList[j]);
326 break;
327 }
328 }
329 }
330 viewer.setSelection(new StructuredSelection(selectedJobs));
331 }
332 });
333 }
334
335 @Override
336 protected String getErrorMessage()
337 {
338 return Messages.get().ServerJobManager_RefreshJobError;
339 }
340 };
341 job.setUser(userInitiated);
342 job.start();
343 }
344
345 /**
346 * Cancel server job
347 */
348 private void cancelServerJob()
349 {
350 doJobAction(Messages.get().ServerJobManager_ActionName_Cancel, Messages.get().ServerJobManager_ActionErrorName_Cancel, CANCEL_JOB);
351 }
352
353 /**
354 * Hold server job
355 */
356 private void holdServerJob()
357 {
358 doJobAction(Messages.get().ServerJobManager_ActionName_Hold, Messages.get().ServerJobManager_ActionErrorName_Hold, HOLD_JOB);
359 }
360
361 /**
362 * Unhold server job
363 */
364 private void unholdServerJob()
365 {
366 doJobAction(Messages.get().ServerJobManager_ActionName_Unhold, Messages.get().ServerJobManager_ActionErrorName_Unhold, UNHOLD_JOB);
367 }
368
369 /**
370 * Do job action: cancel, hold, unhold
371 *
372 * @param actionName
373 * @param actionId
374 */
375 private void doJobAction(final String actionName, final String actionErrorName, final int actionId)
376 {
377 final IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
378 new ConsoleJob(String.format(Messages.get().ServerJobManager_ActionJobName, actionName), this, Activator.PLUGIN_ID, JOB_FAMILY) {
379 @SuppressWarnings("rawtypes")
380 @Override
381 protected void runInternal(IProgressMonitor monitor) throws Exception
382 {
383 Iterator it = selection.iterator();
384 while(it.hasNext())
385 {
386 Object object = it.next();
387 if (object instanceof ServerJob)
388 {
389 final ServerJob jobObject = (ServerJob)object;
390 switch(actionId)
391 {
392 case CANCEL_JOB:
393 session.cancelServerJob(jobObject.getId());
394 break;
395 case HOLD_JOB:
396 session.holdServerJob(jobObject.getId());
397 break;
398 case UNHOLD_JOB:
399 session.unholdServerJob(jobObject.getId());
400 break;
401 default:
402 throw new NXCException(RCC.INTERNAL_ERROR);
403 }
404 }
405 else
406 {
407 throw new NXCException(RCC.INTERNAL_ERROR);
408 }
409 }
410 }
411
412 @Override
413 protected String getErrorMessage()
414 {
415 return String.format(Messages.get().ServerJobManager_ActionJobError, actionErrorName);
416 }
417
418 @Override
419 protected void jobFinalize()
420 {
421 refreshJobList(false);
422 }
423 }.start();
424 }
425 }