7b3a8ab27b56b4a1c5e36c563b9e4b029e41000c
[public/netxms.git] / webui / webapp / DataCollection / src / org / netxms / ui / eclipse / datacollection / propertypages / TableThresholds.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2017 Raden Solutions
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.propertypages;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Iterator;
24 import java.util.List;
25 import org.eclipse.core.runtime.Platform;
26 import org.eclipse.jface.viewers.ArrayContentProvider;
27 import org.eclipse.jface.viewers.DoubleClickEvent;
28 import org.eclipse.jface.viewers.IDoubleClickListener;
29 import org.eclipse.jface.viewers.ISelectionChangedListener;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.viewers.SelectionChangedEvent;
32 import org.eclipse.jface.viewers.StructuredSelection;
33 import org.eclipse.jface.viewers.TableViewer;
34 import org.eclipse.jface.window.Window;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.events.SelectionEvent;
37 import org.eclipse.swt.events.SelectionListener;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.layout.GridLayout;
40 import org.eclipse.swt.layout.RowData;
41 import org.eclipse.swt.layout.RowLayout;
42 import org.eclipse.swt.widgets.Button;
43 import org.eclipse.swt.widgets.Composite;
44 import org.eclipse.swt.widgets.Control;
45 import org.eclipse.swt.widgets.Label;
46 import org.eclipse.swt.widgets.Table;
47 import org.eclipse.swt.widgets.TableColumn;
48 import org.netxms.client.datacollection.DataCollectionTable;
49 import org.netxms.client.datacollection.TableThreshold;
50 import org.netxms.client.events.EventTemplate;
51 import org.netxms.ui.eclipse.datacollection.Activator;
52 import org.netxms.ui.eclipse.datacollection.Messages;
53 import org.netxms.ui.eclipse.datacollection.dialogs.EditTableThresholdDialog;
54 import org.netxms.ui.eclipse.datacollection.propertypages.helpers.DCIPropertyPageDialog;
55 import org.netxms.ui.eclipse.datacollection.propertypages.helpers.TableThresholdLabelProvider;
56 import org.netxms.ui.eclipse.tools.WidgetHelper;
57
58 /**
59 * "Thresholds" property page for table DCI
60 */
61 public class TableThresholds extends DCIPropertyPageDialog
62 {
63 private static final String COLUMN_SETTINGS_PREFIX = "TableThresholds.ColumnList"; //$NON-NLS-1$
64
65 private DataCollectionTable dci;
66 private List<TableThreshold> thresholds;
67 private TableViewer thresholdList;
68 private Button addButton;
69 private Button modifyButton;
70 private Button deleteButton;
71 private Button upButton;
72 private Button downButton;
73
74 /* (non-Javadoc)
75 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
76 */
77 @Override
78 protected Control createContents(Composite parent)
79 {
80 Composite dialogArea = (Composite)super.createContents(parent);
81 dci = editor.getObjectAsTable();
82
83 thresholds = new ArrayList<TableThreshold>();
84 for(TableThreshold t : dci.getThresholds())
85 thresholds.add(new TableThreshold(t));
86
87 // Initiate loading of event manager plugin if it was not loaded before
88 Platform.getAdapterManager().loadAdapter(new EventTemplate(0), "org.eclipse.ui.model.IWorkbenchAdapter"); //$NON-NLS-1$
89
90 GridLayout layout = new GridLayout();
91 layout.verticalSpacing = WidgetHelper.OUTER_SPACING;
92 layout.marginWidth = 0;
93 layout.marginHeight = 0;
94 dialogArea.setLayout(layout);
95
96 Composite thresholdListArea = new Composite(dialogArea, SWT.NONE);
97 GridData gd = new GridData();
98 gd.horizontalAlignment = SWT.FILL;
99 gd.grabExcessHorizontalSpace = true;
100 gd.verticalAlignment = SWT.FILL;
101 gd.grabExcessVerticalSpace = true;
102 gd.horizontalSpan = 2;
103 thresholdListArea.setLayoutData(gd);
104 layout = new GridLayout();
105 layout.verticalSpacing = WidgetHelper.INNER_SPACING;
106 layout.marginWidth = 0;
107 layout.marginHeight = 0;
108 layout.numColumns = 2;
109 thresholdListArea.setLayout(layout);
110
111 new Label(thresholdListArea, SWT.NONE).setText(Messages.get().TableThresholds_Thresholds);
112
113 thresholdList = new TableViewer(thresholdListArea, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
114 gd = new GridData();
115 gd.horizontalAlignment = SWT.FILL;
116 gd.grabExcessHorizontalSpace = true;
117 gd.verticalAlignment = SWT.FILL;
118 gd.grabExcessVerticalSpace = true;
119 gd.horizontalSpan = 2;
120 thresholdList.getControl().setLayoutData(gd);
121 setupThresholdList();
122 thresholdList.setInput(thresholds.toArray());
123
124 Composite leftButtons = new Composite(thresholdListArea, SWT.NONE);
125 gd = new GridData();
126 gd.horizontalAlignment = SWT.LEFT;
127 leftButtons.setLayoutData(gd);
128 RowLayout buttonsLayout = new RowLayout(SWT.HORIZONTAL);
129 buttonsLayout.marginBottom = 0;
130 buttonsLayout.marginLeft = 0;
131 buttonsLayout.marginRight = 0;
132 buttonsLayout.marginTop = 0;
133 buttonsLayout.spacing = WidgetHelper.OUTER_SPACING;
134 buttonsLayout.fill = true;
135 buttonsLayout.pack = false;
136 leftButtons.setLayout(buttonsLayout);
137
138 upButton = new Button(leftButtons, SWT.PUSH);
139 upButton.setText(Messages.get().TableThresholds_Up);
140 RowData rd = new RowData();
141 rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
142 upButton.setLayoutData(rd);
143 upButton.addSelectionListener(new SelectionListener() {
144 @Override
145 public void widgetDefaultSelected(SelectionEvent e)
146 {
147 widgetSelected(e);
148 }
149
150 @Override
151 public void widgetSelected(SelectionEvent e)
152 {
153 moveSelectionUp();
154 }
155 });
156
157 downButton = new Button(leftButtons, SWT.PUSH);
158 downButton.setText(Messages.get().TableThresholds_Down);
159 rd = new RowData();
160 rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
161 downButton.setLayoutData(rd);
162 downButton.addSelectionListener(new SelectionListener() {
163 @Override
164 public void widgetDefaultSelected(SelectionEvent e)
165 {
166 widgetSelected(e);
167 }
168
169 @Override
170 public void widgetSelected(SelectionEvent e)
171 {
172 moveSelectionDown();
173 }
174 });
175
176 Composite buttons = new Composite(thresholdListArea, SWT.NONE);
177 gd = new GridData();
178 gd.horizontalAlignment = SWT.RIGHT;
179 buttons.setLayoutData(gd);
180 buttonsLayout = new RowLayout(SWT.HORIZONTAL);
181 buttonsLayout.marginBottom = 0;
182 buttonsLayout.marginLeft = 0;
183 buttonsLayout.marginRight = 0;
184 buttonsLayout.marginTop = 0;
185 buttonsLayout.spacing = WidgetHelper.OUTER_SPACING;
186 buttonsLayout.fill = true;
187 buttonsLayout.pack = false;
188 buttons.setLayout(buttonsLayout);
189
190 addButton = new Button(buttons, SWT.PUSH);
191 addButton.setText(Messages.get().TableThresholds_Add);
192 rd = new RowData();
193 rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
194 addButton.setLayoutData(rd);
195 addButton.addSelectionListener(new SelectionListener() {
196 @Override
197 public void widgetDefaultSelected(SelectionEvent e)
198 {
199 widgetSelected(e);
200 }
201
202 @Override
203 public void widgetSelected(SelectionEvent e)
204 {
205 addThreshold();
206 }
207 });
208
209 modifyButton = new Button(buttons, SWT.PUSH);
210 modifyButton.setText(Messages.get().TableThresholds_Edit);
211 rd = new RowData();
212 rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
213 modifyButton.setLayoutData(rd);
214 modifyButton.setEnabled(false);
215 modifyButton.addSelectionListener(new SelectionListener() {
216 @Override
217 public void widgetDefaultSelected(SelectionEvent e)
218 {
219 widgetSelected(e);
220 }
221
222 @Override
223 public void widgetSelected(SelectionEvent e)
224 {
225 editThreshold();
226 }
227 });
228
229 deleteButton = new Button(buttons, SWT.PUSH);
230 deleteButton.setText(Messages.get().TableThresholds_Delete);
231 rd = new RowData();
232 rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
233 deleteButton.setLayoutData(rd);
234 deleteButton.setEnabled(false);
235 deleteButton.addSelectionListener(new SelectionListener() {
236 @Override
237 public void widgetDefaultSelected(SelectionEvent e)
238 {
239 widgetSelected(e);
240 }
241
242 @Override
243 public void widgetSelected(SelectionEvent e)
244 {
245 deleteThresholds();
246 }
247 });
248
249 /*** Selection change listener for thresholds list ***/
250 thresholdList.addSelectionChangedListener(new ISelectionChangedListener() {
251 @Override
252 public void selectionChanged(SelectionChangedEvent event)
253 {
254 IStructuredSelection selection = (IStructuredSelection)event.getSelection();
255 deleteButton.setEnabled(selection.size() > 0);
256 if (selection.size() == 1)
257 {
258 modifyButton.setEnabled(true);
259 upButton.setEnabled(thresholds.indexOf(selection.getFirstElement()) > 0);
260 downButton.setEnabled(thresholds.indexOf(selection.getFirstElement()) < thresholds.size() - 1);
261 }
262 else
263 {
264 modifyButton.setEnabled(false);
265 upButton.setEnabled(false);
266 downButton.setEnabled(false);
267 }
268 }
269 });
270
271 thresholdList.addDoubleClickListener(new IDoubleClickListener() {
272 @Override
273 public void doubleClick(DoubleClickEvent event)
274 {
275 editThreshold();
276 }
277 });
278
279 return dialogArea;
280 }
281
282 /**
283 * Setup threshold list control
284 */
285 private void setupThresholdList()
286 {
287 Table table = thresholdList.getTable();
288 table.setLinesVisible(true);
289 table.setHeaderVisible(true);
290
291 TableColumn column = new TableColumn(table, SWT.LEFT);
292 column.setText(Messages.get().TableThresholds_Condition);
293 column.setWidth(200);
294
295 column = new TableColumn(table, SWT.LEFT);
296 column.setText(Messages.get().TableThresholds_ActivationEvent);
297 column.setWidth(140);
298
299 column = new TableColumn(table, SWT.LEFT);
300 column.setText(Messages.get().TableThresholds_DeactivationEvent);
301 column.setWidth(140);
302
303 WidgetHelper.restoreColumnSettings(table, Activator.getDefault().getDialogSettings(), COLUMN_SETTINGS_PREFIX);
304
305 thresholdList.setContentProvider(new ArrayContentProvider());
306 thresholdList.setLabelProvider(new TableThresholdLabelProvider());
307 }
308
309 /**
310 * Delete selected thresholds
311 */
312 private void deleteThresholds()
313 {
314 final IStructuredSelection selection = (IStructuredSelection)thresholdList.getSelection();
315 if (!selection.isEmpty())
316 {
317 Iterator<?> it = selection.iterator();
318 while(it.hasNext())
319 {
320 thresholds.remove(it.next());
321 }
322 thresholdList.setInput(thresholds.toArray());
323 }
324 }
325
326 /**
327 * Edit selected threshold
328 */
329 private void editThreshold()
330 {
331 final IStructuredSelection selection = (IStructuredSelection)thresholdList.getSelection();
332 if (selection.size() == 1)
333 {
334 final TableThreshold t = (TableThreshold)selection.getFirstElement();
335 EditTableThresholdDialog dlg = new EditTableThresholdDialog(getShell(), t, editor.getCallback());
336 if (dlg.open() == Window.OK)
337 {
338 thresholdList.update(t, null);
339 editor.modify();
340 }
341 }
342 }
343
344 /**
345 * Add new threshold
346 */
347 private void addThreshold()
348 {
349 final TableThreshold t = new TableThreshold();
350 final EditTableThresholdDialog dlg = new EditTableThresholdDialog(getShell(), t, editor.getCallback());
351 if (dlg.open() == Window.OK)
352 {
353 thresholds.add(t);
354 thresholdList.setInput(thresholds.toArray());
355 thresholdList.setSelection(new StructuredSelection(t));
356 editor.modify();
357 }
358 }
359
360 /**
361 * Move selected element up
362 */
363 private void moveSelectionUp()
364 {
365 final IStructuredSelection selection = (IStructuredSelection)thresholdList.getSelection();
366 if (selection.size() != 1)
367 return;
368
369 final TableThreshold t = (TableThreshold)selection.getFirstElement();
370 int index = thresholds.indexOf(t);
371 if (index > 0)
372 {
373 Collections.swap(thresholds, index, index - 1);
374 thresholdList.setInput(thresholds.toArray());
375 thresholdList.setSelection(new StructuredSelection(t));
376 }
377 }
378
379 /**
380 * Move selected element down
381 */
382 private void moveSelectionDown()
383 {
384 final IStructuredSelection selection = (IStructuredSelection)thresholdList.getSelection();
385 if (selection.size() != 1)
386 return;
387
388 final TableThreshold t = (TableThreshold)selection.getFirstElement();
389 int index = thresholds.indexOf(t);
390 if (index < thresholds.size() - 1)
391 {
392 Collections.swap(thresholds, index, index + 1);
393 thresholdList.setInput(thresholds.toArray());
394 thresholdList.setSelection(new StructuredSelection(t));
395 }
396 }
397
398 /**
399 * Apply changes
400 *
401 * @param isApply true if update operation caused by "Apply" button
402 */
403 protected void applyChanges(final boolean isApply)
404 {
405 dci.getThresholds().clear();
406 dci.getThresholds().addAll(thresholds);
407 editor.modify();
408 }
409
410 /* (non-Javadoc)
411 * @see org.eclipse.jface.preference.PreferencePage#performApply()
412 */
413 @Override
414 protected void performApply()
415 {
416 saveSettings();
417 applyChanges(true);
418 }
419
420 /* (non-Javadoc)
421 * @see org.eclipse.jface.preference.PreferencePage#performOk()
422 */
423 @Override
424 public boolean performOk()
425 {
426 saveSettings();
427 applyChanges(false);
428 return true;
429 }
430
431 /* (non-Javadoc)
432 * @see org.eclipse.jface.preference.PreferencePage#performCancel()
433 */
434 @Override
435 public boolean performCancel()
436 {
437 saveSettings();
438 return true;
439 }
440
441 /**
442 * Save settings
443 */
444 private void saveSettings()
445 {
446 WidgetHelper.saveColumnSettings(thresholdList.getTable(), Activator.getDefault().getDialogSettings(), COLUMN_SETTINGS_PREFIX);
447 }
448 }