aac7e6266ea7c069e1936612b66d29fff3200aec
[public/netxms.git] / src / java / netxms-eclipse / DataCollection / src / org / netxms / ui / eclipse / datacollection / views / SummaryTableManager.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.datacollection.views;
20
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.jface.action.Action;
26 import org.eclipse.jface.action.IMenuListener;
27 import org.eclipse.jface.action.IMenuManager;
28 import org.eclipse.jface.action.IToolBarManager;
29 import org.eclipse.jface.action.MenuManager;
30 import org.eclipse.jface.action.Separator;
31 import org.eclipse.jface.commands.ActionHandler;
32 import org.eclipse.jface.dialogs.IDialogSettings;
33 import org.eclipse.jface.viewers.ArrayContentProvider;
34 import org.eclipse.jface.viewers.DoubleClickEvent;
35 import org.eclipse.jface.viewers.IDoubleClickListener;
36 import org.eclipse.jface.viewers.ISelectionChangedListener;
37 import org.eclipse.jface.viewers.IStructuredSelection;
38 import org.eclipse.jface.viewers.SelectionChangedEvent;
39 import org.eclipse.jface.viewers.StructuredSelection;
40 import org.eclipse.swt.SWT;
41 import org.eclipse.swt.events.DisposeEvent;
42 import org.eclipse.swt.events.DisposeListener;
43 import org.eclipse.swt.events.ModifyEvent;
44 import org.eclipse.swt.events.ModifyListener;
45 import org.eclipse.swt.layout.FormAttachment;
46 import org.eclipse.swt.layout.FormData;
47 import org.eclipse.swt.layout.FormLayout;
48 import org.eclipse.swt.widgets.Composite;
49 import org.eclipse.swt.widgets.Menu;
50 import org.eclipse.ui.IActionBars;
51 import org.eclipse.ui.IViewSite;
52 import org.eclipse.ui.PartInitException;
53 import org.eclipse.ui.contexts.IContextService;
54 import org.eclipse.ui.handlers.IHandlerService;
55 import org.eclipse.ui.internal.dialogs.PropertyDialog;
56 import org.eclipse.ui.part.ViewPart;
57 import org.netxms.client.NXCSession;
58 import org.netxms.client.SessionListener;
59 import org.netxms.client.SessionNotification;
60 import org.netxms.client.datacollection.DciSummaryTable;
61 import org.netxms.client.datacollection.DciSummaryTableDescriptor;
62 import org.netxms.ui.eclipse.actions.RefreshAction;
63 import org.netxms.ui.eclipse.console.resources.SharedIcons;
64 import org.netxms.ui.eclipse.datacollection.Activator;
65 import org.netxms.ui.eclipse.datacollection.Messages;
66 import org.netxms.ui.eclipse.datacollection.views.helpers.SummaryTableComparator;
67 import org.netxms.ui.eclipse.datacollection.views.helpers.SummaryTableFilter;
68 import org.netxms.ui.eclipse.datacollection.views.helpers.SummaryTableLabelProvider;
69 import org.netxms.ui.eclipse.jobs.ConsoleJob;
70 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
71 import org.netxms.ui.eclipse.tools.MessageDialogHelper;
72 import org.netxms.ui.eclipse.widgets.FilterText;
73 import org.netxms.ui.eclipse.widgets.SortableTableViewer;
74
75 /**
76 * Manager of DCI summary tables
77 */
78 @SuppressWarnings("restriction")
79 public class SummaryTableManager extends ViewPart
80 {
81 public static final String ID = "org.netxms.ui.eclipse.datacollection.views.SummaryTableManager"; //$NON-NLS-1$
82
83 public static final int COLUMN_ID = 0;
84 public static final int COLUMN_MENU_PATH = 1;
85 public static final int COLUMN_TITLE = 2;
86
87 private SortableTableViewer viewer;
88 private NXCSession session = (NXCSession)ConsoleSharedData.getSession();
89 private SessionListener listener = null;
90 private Map<Integer, DciSummaryTableDescriptor> descriptors = new HashMap<Integer, DciSummaryTableDescriptor>();
91
92 private Action actionRefresh;
93 private Action actionCreateSingleValue;
94 private Action actionCreateTableValue;
95 private Action actionEdit;
96 private Action actionDelete;
97 private Action actionShowFilter;
98
99 private Composite tableArea;
100 private FilterText filterText;
101 private SummaryTableFilter filter;
102 private boolean initShowFilter = true;
103 private IDialogSettings settings;
104
105 @Override
106 public void init(IViewSite site) throws PartInitException
107 {
108 super.init(site);
109 settings = Activator.getDefault().getDialogSettings();
110 initShowFilter = safeCast(settings.get("SummaryTableManager.showFilter"), settings.getBoolean("SummaryTableManager.showFilter"), initShowFilter);
111 }
112
113 /**
114 * @param b
115 * @param defval
116 * @return
117 */
118 private static boolean safeCast(String s, boolean b, boolean defval)
119 {
120 return (s != null) ? b : defval;
121 }
122
123 /* (non-Javadoc)
124 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
125 */
126 @Override
127 public void createPartControl(Composite parent)
128 {
129 // Create interface area
130 tableArea = new Composite(parent, SWT.BORDER);
131 FormLayout formLayout = new FormLayout();
132 tableArea.setLayout(formLayout);
133 // Create filter
134 filterText = new FilterText(tableArea, SWT.NONE);
135 filterText.addModifyListener(new ModifyListener() {
136 @Override
137 public void modifyText(ModifyEvent e)
138 {
139 onFilterModify();
140 }
141 });
142 filterText.addDisposeListener(new DisposeListener() {
143
144 @Override
145 public void widgetDisposed(DisposeEvent e)
146 {
147 settings.put("SummaryTableManager.showFilter", initShowFilter);
148 }
149
150 });
151
152 final String[] names = { Messages.get().SummaryTableManager_ID, Messages.get().SummaryTableManager_MenuPath, Messages.get().SummaryTableManager_Title };
153 final int[] widths = { 90, 250, 200 };
154 viewer = new SortableTableViewer(tableArea, names, widths, COLUMN_MENU_PATH, SWT.UP, SortableTableViewer.DEFAULT_STYLE);
155 viewer.setContentProvider(new ArrayContentProvider());
156 viewer.setLabelProvider(new SummaryTableLabelProvider());
157 viewer.setComparator(new SummaryTableComparator());
158
159 filter = new SummaryTableFilter();
160 viewer.addFilter(filter);
161
162 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
163 @Override
164 public void selectionChanged(SelectionChangedEvent event)
165 {
166 IStructuredSelection selection = (IStructuredSelection)event.getSelection();
167 actionEdit.setEnabled(selection.size() == 1);
168 actionDelete.setEnabled(selection.size() > 0);
169 }
170 });
171
172 viewer.addDoubleClickListener(new IDoubleClickListener() {
173 @Override
174 public void doubleClick(DoubleClickEvent event)
175 {
176 editSummaryTable();
177 }
178 });
179
180 // Setup layout
181 FormData fd = new FormData();
182 fd.left = new FormAttachment(0, 0);
183 fd.top = new FormAttachment(filterText);
184 fd.right = new FormAttachment(100, 0);
185 fd.bottom = new FormAttachment(100, 0);
186 viewer.getControl().setLayoutData(fd);
187
188 fd = new FormData();
189 fd.left = new FormAttachment(0, 0);
190 fd.top = new FormAttachment(0, 0);
191 fd.right = new FormAttachment(100, 0);
192 filterText.setLayoutData(fd);
193
194 createActions();
195 contributeToActionBars();
196 createPopupMenu();
197
198 filterText.setCloseAction(actionShowFilter);
199 enableFilter(initShowFilter);
200
201 refresh();
202
203 listener = new SessionListener() {
204 @Override
205 public void notificationHandler(SessionNotification n)
206 {
207 if (n.getCode() == SessionNotification.DCI_SUMMARY_TABLE_DELETED)
208 {
209 final int id = (int)n.getSubCode();
210 getSite().getShell().getDisplay().asyncExec(new Runnable() {
211 @Override
212 public void run()
213 {
214 if (descriptors.remove(id) != null)
215 viewer.setInput(descriptors.values().toArray());
216 }
217 });
218 }
219 else if (n.getCode() == SessionNotification.DCI_SUMMARY_TABLE_UPDATED)
220 {
221 getSite().getShell().getDisplay().asyncExec(new Runnable() {
222 @Override
223 public void run()
224 {
225 refresh();
226 }
227 });
228 }
229 }
230 };
231 session.addListener(listener);
232 activateContext();
233 }
234
235 /**
236 * Activate context
237 */
238 protected void activateContext()
239 {
240 IContextService contextService = (IContextService)getSite().getService(IContextService.class);
241 if (contextService != null)
242 {
243 contextService.activateContext("org.netxms.ui.eclipse.datacollection.context.LastValues"); //$NON-NLS-1$
244 }
245 }
246
247 /**
248 * Enable or disable filter
249 *
250 * @param enable New filter state
251 */
252 public void enableFilter(boolean enable)
253 {
254 initShowFilter = enable;
255 filterText.setVisible(initShowFilter);
256 FormData fd = (FormData)viewer.getTable().getLayoutData();
257 fd.top = enable ? new FormAttachment(filterText, 0, SWT.BOTTOM) : new FormAttachment(0, 0);
258 tableArea.layout();
259 if (enable)
260 {
261 filterText.setFocus();
262 }
263 else
264 {
265 filterText.setText("");
266 onFilterModify();
267 }
268
269 }
270
271 /**
272 * Handler for filter modification
273 */
274 public void onFilterModify()
275 {
276 final String text = filterText.getText();
277 filter.setFilterString(text);
278 viewer.refresh(false);
279 }
280
281 /* (non-Javadoc)
282 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
283 */
284 @Override
285 public void setFocus()
286 {
287 viewer.getTable().setFocus();
288 }
289
290 /**
291 * Refresh viewer
292 */
293 private void refresh()
294 {
295 new ConsoleJob("Get configured DCI summary tables", this, Activator.PLUGIN_ID, null) { //$NON-NLS-1$
296 @Override
297 protected void runInternal(IProgressMonitor monitor) throws Exception
298 {
299 final List<DciSummaryTableDescriptor> list = session.listDciSummaryTables();
300 runInUIThread(new Runnable() {
301 @Override
302 public void run()
303 {
304 descriptors.clear();
305 for(DciSummaryTableDescriptor d : list)
306 descriptors.put(d.getId(), d);
307 viewer.setInput(descriptors.values().toArray());
308 }
309 });
310 }
311
312 @Override
313 protected String getErrorMessage()
314 {
315 return Messages.get().SummaryTableManager_JobError;
316 }
317 }.start();
318 }
319
320 /**
321 *
322 */
323 private void createActions()
324 {
325 final IHandlerService handlerService = (IHandlerService)getSite().getService(IHandlerService.class);
326
327 // create show filter action
328 actionShowFilter = new Action("Show filter", Action.AS_CHECK_BOX) {
329 @Override
330 public void run()
331 {
332 enableFilter(!initShowFilter);
333 actionShowFilter.setChecked(initShowFilter);
334 }
335 };
336 actionShowFilter.setChecked(initShowFilter);
337 actionShowFilter.setActionDefinitionId("org.netxms.ui.eclipse.datacollection.commands.show_dci_filter"); //$NON-NLS-1$
338 handlerService.activateHandler(actionShowFilter.getActionDefinitionId(), new ActionHandler(actionShowFilter));
339
340 // create refresh action
341 actionRefresh = new RefreshAction(this) {
342 @Override
343 public void run()
344 {
345 refresh();
346 }
347 };
348
349 // create add action for single value table
350 actionCreateSingleValue = new Action("Create new single value table", SharedIcons.ADD_OBJECT) {
351 @Override
352 public void run()
353 {
354 createSummaryTable(true);
355 }
356 };
357
358 actionCreateTableValue = new Action("Create new table value table", Activator.getImageDescriptor("icons/new.png")) {
359 @Override
360 public void run()
361 {
362 createSummaryTable(false);
363 }
364 };
365
366 // create edit action
367 actionEdit = new Action(Messages.get().SummaryTableManager_ActionEdit, SharedIcons.EDIT) {
368 @Override
369 public void run()
370 {
371 editSummaryTable();
372 }
373 };
374 actionEdit.setEnabled(false);
375
376 // create delete action
377 actionDelete = new Action(Messages.get().SummaryTableManager_ActionDelete, SharedIcons.DELETE_OBJECT) {
378 @Override
379 public void run()
380 {
381 deleteSelection();
382 }
383 };
384 actionDelete.setEnabled(false);
385 }
386
387 /**
388 * Contribute actions to action bar
389 */
390 private void contributeToActionBars()
391 {
392 IActionBars bars = getViewSite().getActionBars();
393 fillLocalPullDown(bars.getMenuManager());
394 fillLocalToolBar(bars.getToolBarManager());
395 }
396
397 /**
398 * Fill local pull-down menu
399 *
400 * @param manager Menu manager for pull-down menu
401 */
402 private void fillLocalPullDown(IMenuManager manager)
403 {
404 manager.add(actionCreateSingleValue);
405 manager.add(actionCreateTableValue);
406 manager.add(actionShowFilter);
407 manager.add(new Separator());
408 manager.add(actionRefresh);
409 }
410
411 /**
412 * Fill local tool bar
413 *
414 * @param manager Menu manager for local toolbar
415 */
416 private void fillLocalToolBar(IToolBarManager manager)
417 {
418 manager.add(actionCreateSingleValue);
419 manager.add(actionCreateTableValue);
420 manager.add(new Separator());
421 manager.add(actionRefresh);
422 }
423
424 /**
425 * Create pop-up menu for variable list
426 */
427 private void createPopupMenu()
428 {
429 // Create menu manager.
430 MenuManager menuMgr = new MenuManager();
431 menuMgr.setRemoveAllWhenShown(true);
432 menuMgr.addMenuListener(new IMenuListener() {
433 public void menuAboutToShow(IMenuManager mgr)
434 {
435 mgr.add(actionCreateSingleValue);
436 mgr.add(actionCreateTableValue);
437 mgr.add(actionEdit);
438 mgr.add(actionDelete);
439 }
440 });
441
442 // Create menu.
443 Menu menu = menuMgr.createContextMenu(viewer.getControl());
444 viewer.getControl().setMenu(menu);
445
446 // Register menu for extension.
447 getSite().registerContextMenu(menuMgr, viewer);
448 }
449
450 /**
451 * Create new loyalty program
452 */
453 private void createSummaryTable(boolean isSingleValue)
454 {
455 DciSummaryTable t = new DciSummaryTable("", "", isSingleValue); //$NON-NLS-1$ //$NON-NLS-2$
456 PropertyDialog dlg = PropertyDialog.createDialogOn(getSite().getShell(), null, t);
457 if (dlg != null)
458 {
459 dlg.getShell().setText(Messages.get().SummaryTableManager_TitleCreate);
460 dlg.open();
461 if (t.getId() != 0)
462 {
463 // was saved to server
464 DciSummaryTableDescriptor d = new DciSummaryTableDescriptor(t);
465 descriptors.put(d.getId(), d);
466 viewer.setInput(descriptors.values().toArray());
467 viewer.setSelection(new StructuredSelection(d));
468 }
469 }
470 }
471
472 /**
473 * Edit existing dataset
474 */
475 private void editSummaryTable()
476 {
477 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
478 if (selection.size() != 1)
479 return;
480
481 final DciSummaryTableDescriptor d = (DciSummaryTableDescriptor)selection.getFirstElement();
482 new ConsoleJob(Messages.get().SummaryTableManager_ReadJobName, this, Activator.PLUGIN_ID, null) {
483 @Override
484 protected void runInternal(IProgressMonitor monitor) throws Exception
485 {
486 final DciSummaryTable t = session.getDciSummaryTable(d.getId());
487 runInUIThread(new Runnable() {
488 @Override
489 public void run()
490 {
491 PropertyDialog dlg = PropertyDialog.createDialogOn(getSite().getShell(), null, t);
492 dlg.getShell().setText(Messages.get().SummaryTableManager_TitleEdit);
493 dlg.open();
494 d.updateFromTable(t);
495 viewer.update(d, null);
496 }
497 });
498 }
499
500 @Override
501 protected String getErrorMessage()
502 {
503 return Messages.get().SummaryTableManager_ReadJobError;
504 }
505 }.start();
506 }
507
508 /**
509 * Delete selected datasets
510 */
511 private void deleteSelection()
512 {
513 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
514 if (selection.size() == 0)
515 return;
516
517 if (!MessageDialogHelper.openQuestion(getSite().getShell(), Messages.get().SummaryTableManager_ConfirmDelete, Messages.get().SummaryTableManager_Confirmation))
518 return;
519
520 final int[] idList = new int[selection.size()];
521 int i = 0;
522 for(Object o : selection.toList())
523 idList[i++] = ((DciSummaryTableDescriptor)o).getId();
524
525 new ConsoleJob(Messages.get().SummaryTableManager_DeleteJobName, this, Activator.PLUGIN_ID, null) {
526 @Override
527 protected void runInternal(IProgressMonitor monitor) throws Exception
528 {
529 for(int id : idList)
530 session.deleteDciSummaryTable(id);
531 runInUIThread(new Runnable() {
532 @Override
533 public void run()
534 {
535 for(int id : idList)
536 descriptors.remove(id);
537 viewer.setInput(descriptors.values().toArray());
538 }
539 });
540 }
541
542 @Override
543 protected String getErrorMessage()
544 {
545 return Messages.get().SummaryTableManager_DeleteJobError;
546 }
547 }.start();
548 }
549
550 /* (non-Javadoc)
551 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
552 */
553 @Override
554 public void dispose()
555 {
556 if ((session != null) && (listener != null))
557 session.removeListener(listener);
558 super.dispose();
559 }
560 }