492b4b9a50378ad5808add55a3405ebcc0b48077
[public/netxms.git] / src / java / netxms-eclipse / ServerConfig / src / org / netxms / ui / eclipse / serverconfig / views / ServerConfigurationEditor.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2012 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.serverconfig.views;
20
21 import java.util.ArrayList;
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.window.Window;
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.PlatformUI;
52 import org.eclipse.ui.contexts.IContextService;
53 import org.eclipse.ui.handlers.IHandlerService;
54 import org.eclipse.ui.part.ViewPart;
55 import org.netxms.client.NXCSession;
56 import org.netxms.client.server.ServerVariable;
57 import org.netxms.ui.eclipse.actions.ExportToCsvAction;
58 import org.netxms.ui.eclipse.actions.RefreshAction;
59 import org.netxms.ui.eclipse.console.resources.SharedIcons;
60 import org.netxms.ui.eclipse.jobs.ConsoleJob;
61 import org.netxms.ui.eclipse.serverconfig.Activator;
62 import org.netxms.ui.eclipse.serverconfig.Messages;
63 import org.netxms.ui.eclipse.serverconfig.dialogs.VariableEditDialog;
64 import org.netxms.ui.eclipse.serverconfig.views.helpers.ServerVariableComparator;
65 import org.netxms.ui.eclipse.serverconfig.views.helpers.ServerVariablesFilter;
66 import org.netxms.ui.eclipse.serverconfig.views.helpers.ServerVariablesLabelProvider;
67 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
68 import org.netxms.ui.eclipse.tools.MessageDialogHelper;
69 import org.netxms.ui.eclipse.tools.WidgetHelper;
70 import org.netxms.ui.eclipse.widgets.FilterText;
71 import org.netxms.ui.eclipse.widgets.SortableTableViewer;
72
73 /**
74 * Editor for server configuration variables
75 */
76 public class ServerConfigurationEditor extends ViewPart
77 {
78 public static final String ID = "org.netxms.ui.eclipse.serverconfig.view.server_config"; //$NON-NLS-1$
79 public static final String JOB_FAMILY = "ServerConfigJob"; //$NON-NLS-1$
80
81 private SortableTableViewer viewer;
82 private NXCSession session;
83 private Map<String, ServerVariable> varList;
84 private boolean filterEnabled = false;
85 private Composite content;
86 private FilterText filterText;
87 private ServerVariablesFilter filter;
88
89 private Action actionAdd;
90 private Action actionEdit;
91 private Action actionDelete;
92 private Action actionExportToCsv;
93 private Action actionExportAllToCsv;
94 private Action actionRefresh;
95 private Action actionShowFilter;
96
97 // Columns
98 public static final int COLUMN_NAME = 0;
99 public static final int COLUMN_VALUE = 1;
100 public static final int COLUMN_NEED_RESTART = 2;
101
102 /* (non-Javadoc)
103 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
104 */
105 public void createPartControl(Composite parent)
106 {
107 content = new Composite(parent, SWT.NONE);
108 content.setLayout(new FormLayout());
109
110 // Create filter area
111 filterText = new FilterText(content, SWT.NONE);
112 filterText.addModifyListener(new ModifyListener() {
113 @Override
114 public void modifyText(ModifyEvent e)
115 {
116 onFilterModify();
117 }
118 });
119
120 final String[] names = { Messages.get().ServerConfigurationEditor_ColName, Messages.get().ServerConfigurationEditor_ColValue, Messages.get().ServerConfigurationEditor_ColRestart };
121 final int[] widths = { 200, 150, 80 };
122 viewer = new SortableTableViewer(content, names, widths, 0, SWT.UP, SortableTableViewer.DEFAULT_STYLE);
123 viewer.setContentProvider(new ArrayContentProvider());
124 viewer.setLabelProvider(new ServerVariablesLabelProvider());
125 viewer.setComparator(new ServerVariableComparator());
126 filter = new ServerVariablesFilter();
127 viewer.addFilter(filter);
128 viewer.addDoubleClickListener(new IDoubleClickListener() {
129 @Override
130 public void doubleClick(DoubleClickEvent event)
131 {
132 editVariable();
133 }
134 });
135 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
136 @Override
137 public void selectionChanged(SelectionChangedEvent event)
138 {
139 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
140 actionEdit.setEnabled(selection.size() == 1);
141 actionDelete.setEnabled(selection.size() > 0);
142 }
143 });
144
145 // Setup layout
146 FormData fd = new FormData();
147 fd.left = new FormAttachment(0, 0);
148 fd.top = new FormAttachment(filterText);
149 fd.right = new FormAttachment(100, 0);
150 fd.bottom = new FormAttachment(100, 0);
151 viewer.getTable().setLayoutData(fd);
152
153 fd = new FormData();
154 fd.left = new FormAttachment(0, 0);
155 fd.top = new FormAttachment(0, 0);
156 fd.right = new FormAttachment(100, 0);
157 filterText.setLayoutData(fd);
158
159 filterText.setCloseAction(actionShowFilter);
160
161 final IDialogSettings settings = Activator.getDefault().getDialogSettings();
162 WidgetHelper.restoreTableViewerSettings(viewer, settings, "ServerConfigurationEditor"); //$NON-NLS-1$
163 viewer.getTable().addDisposeListener(new DisposeListener() {
164 @Override
165 public void widgetDisposed(DisposeEvent e)
166 {
167 WidgetHelper.saveTableViewerSettings(viewer, settings, "ServerConfigurationEditor"); //$NON-NLS-1$
168 }
169 });
170
171 // Create the help context id for the viewer's control
172 PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "org.netxms.nxmc.serverconfig.viewer"); //$NON-NLS-1$
173 activateContext();
174 createActions();
175 contributeToActionBars();
176 createPopupMenu();
177
178 // Set initial focus to filter input line
179 if (filterEnabled)
180 filterText.setFocus();
181 else
182 enableFilter(false); // Will hide filter area correctly
183
184 session = ConsoleSharedData.getSession();
185 refresh();
186 }
187
188 /**
189 * Refresh viewer
190 */
191 public void refresh()
192 {
193 new ConsoleJob(Messages.get().ServerConfigurationEditor_LoadJobName, this, Activator.PLUGIN_ID, JOB_FAMILY) {
194 @Override
195 protected String getErrorMessage()
196 {
197 return Messages.get().ServerConfigurationEditor_LoadJobError;
198 }
199
200 @Override
201 protected void runInternal(IProgressMonitor monitor) throws Exception
202 {
203 varList = session.getServerVariables();
204 runInUIThread(new Runnable() {
205 @Override
206 public void run()
207 {
208 synchronized(varList)
209 {
210 viewer.setInput(varList.values().toArray());
211 }
212 }
213 });
214 }
215 }.start();
216 }
217
218 /**
219 * Fill action bars
220 */
221 private void contributeToActionBars()
222 {
223 IActionBars bars = getViewSite().getActionBars();
224 fillLocalPullDown(bars.getMenuManager());
225 fillLocalToolBar(bars.getToolBarManager());
226 }
227
228 /**
229 * @param manager
230 */
231 private void fillLocalPullDown(IMenuManager manager)
232 {
233 manager.add(actionAdd);
234 manager.add(actionExportAllToCsv);
235 manager.add(actionShowFilter);
236 manager.add(new Separator());
237 manager.add(actionRefresh);
238 }
239
240 /**
241 * @param manager
242 */
243 private void fillLocalToolBar(IToolBarManager manager)
244 {
245 manager.add(actionAdd);
246 manager.add(actionExportAllToCsv);
247 manager.add(actionShowFilter);
248 manager.add(new Separator());
249 manager.add(actionRefresh);
250 }
251
252 /**
253 * Activate context
254 */
255 private void activateContext()
256 {
257 IContextService contextService = (IContextService)getSite().getService(IContextService.class);
258 if (contextService != null)
259 {
260 contextService.activateContext("org.netxms.ui.eclipse.serverconfig.context.ServerConfigurationEditor"); //$NON-NLS-1$
261 }
262 }
263
264 /**
265 * Create actions
266 */
267 private void createActions()
268 {
269 final IHandlerService handlerService = (IHandlerService)getSite().getService(IHandlerService.class);
270
271 actionRefresh = new RefreshAction(this) {
272 @Override
273 public void run()
274 {
275 refresh();
276 }
277 };
278
279 actionAdd = new Action(Messages.get().ServerConfigurationEditor_ActionCreate, SharedIcons.ADD_OBJECT) {
280 @Override
281 public void run()
282 {
283 addVariable();
284 }
285 };
286 actionAdd.setActionDefinitionId("org.netxms.ui.eclipse.serverconfig.commands.add_config_variable"); //$NON-NLS-1$
287 handlerService.activateHandler(actionAdd.getActionDefinitionId(), new ActionHandler(actionAdd));
288
289 actionEdit = new Action(Messages.get().ServerConfigurationEditor_ActionEdit, SharedIcons.EDIT) {
290 @Override
291 public void run()
292 {
293 editVariable();
294 }
295 };
296 actionEdit.setEnabled(false);
297
298 actionDelete = new Action(Messages.get().ServerConfigurationEditor_ActionDelete, SharedIcons.DELETE_OBJECT) {
299 @Override
300 public void run()
301 {
302 deleteVariables();
303 }
304 };
305 actionDelete.setEnabled(false);
306
307 actionShowFilter = new Action("Show filter", Action.AS_CHECK_BOX) {
308 @Override
309 public void run()
310 {
311 enableFilter(!filterEnabled);
312 actionShowFilter.setChecked(filterEnabled);
313 }
314 };
315 actionShowFilter.setImageDescriptor(SharedIcons.FILTER);
316 actionShowFilter.setChecked(filterEnabled);
317 actionShowFilter.setActionDefinitionId("org.netxms.ui.eclipse.serverconfig.commands.show_filter"); //$NON-NLS-1$
318 handlerService.activateHandler(actionShowFilter.getActionDefinitionId(), new ActionHandler(actionShowFilter));
319
320 actionExportToCsv = new ExportToCsvAction(this, viewer, true);
321 actionExportAllToCsv = new ExportToCsvAction(this, viewer, false);
322 }
323
324 /**
325 * Enable or disable filter
326 *
327 * @param enable New filter state
328 */
329 private void enableFilter(boolean enable)
330 {
331 filterEnabled = enable;
332 filterText.setVisible(filterEnabled);
333 FormData fd = (FormData)viewer.getTable().getLayoutData();
334 fd.top = enable ? new FormAttachment(filterText) : new FormAttachment(0, 0);
335 content.layout();
336 if (enable)
337 {
338 filterText.setFocus();
339 }
340 else
341 {
342 filterText.setText(""); //$NON-NLS-1$
343 onFilterModify();
344 }
345 }
346
347 /**
348 * Handler for filter modification
349 */
350 private void onFilterModify()
351 {
352 final String text = filterText.getText();
353 filter.setFilterString(text);
354 viewer.refresh(false);
355 }
356
357 /**
358 * Create pop-up menu for variable list
359 */
360 private void createPopupMenu()
361 {
362 // Create menu manager.
363 MenuManager menuMgr = new MenuManager();
364 menuMgr.setRemoveAllWhenShown(true);
365 menuMgr.addMenuListener(new IMenuListener() {
366 public void menuAboutToShow(IMenuManager mgr)
367 {
368 fillContextMenu(mgr);
369 }
370 });
371
372 // Create menu.
373 Menu menu = menuMgr.createContextMenu(viewer.getControl());
374 viewer.getControl().setMenu(menu);
375
376 // Register menu for extension.
377 getSite().registerContextMenu(menuMgr, viewer);
378 }
379
380 /**
381 * Fill context menu
382 * @param mgr Menu manager
383 */
384 protected void fillContextMenu(IMenuManager mgr)
385 {
386 mgr.add(actionAdd);
387 mgr.add(actionEdit);
388 mgr.add(actionDelete);
389 mgr.add(new Separator());
390 mgr.add(actionExportToCsv);
391 }
392
393 /**
394 * Passing the focus request to the viewer's control.
395 */
396 public void setFocus()
397 {
398 viewer.getControl().setFocus();
399 }
400
401 /**
402 * Add new variable
403 */
404 private void addVariable()
405 {
406 final VariableEditDialog dlg = new VariableEditDialog(getSite().getShell(), null, null);
407 if (dlg.open() == Window.OK)
408 {
409 new ConsoleJob(Messages.get().ServerConfigurationEditor_CreateJobName, this, Activator.PLUGIN_ID, JOB_FAMILY) {
410 @Override
411 protected String getErrorMessage()
412 {
413 return Messages.get().ServerConfigurationEditor_CreateJobError;
414 }
415
416 @Override
417 protected void runInternal(IProgressMonitor monitor) throws Exception
418 {
419 session.setServerVariable(dlg.getVarName(), dlg.getVarValue());
420 refresh();
421 }
422 }.start();
423 }
424 }
425
426 /**
427 * Edit currently selected variable
428 * @param var
429 */
430 private void editVariable()
431 {
432 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
433 if ((selection == null) || (selection.size() != 1))
434 return;
435
436 final ServerVariable var = (ServerVariable)selection.getFirstElement();
437 final VariableEditDialog dlg = new VariableEditDialog(getSite().getShell(), var.getName(), var.getValue());
438 if (dlg.open() == Window.OK)
439 {
440 new ConsoleJob(Messages.get().ServerConfigurationEditor_ModifyJobName, this, Activator.PLUGIN_ID, JOB_FAMILY) {
441 @Override
442 protected String getErrorMessage()
443 {
444 return Messages.get().ServerConfigurationEditor_ModifyJobError;
445 }
446
447 @Override
448 protected void runInternal(IProgressMonitor monitor) throws Exception
449 {
450 session.setServerVariable(dlg.getVarName(), dlg.getVarValue());
451 refresh();
452 }
453 }.start();
454 }
455 }
456
457 /**
458 * Delete selected variables
459 */
460 private void deleteVariables()
461 {
462 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
463 if ((selection == null) || (selection.size() == 0))
464 return;
465
466 if (!MessageDialogHelper.openQuestion(getSite().getShell(), Messages.get().ServerConfigurationEditor_DeleteConfirmation, Messages.get().ServerConfigurationEditor_DeleteConfirmationText))
467 return;
468
469 final List<String> names = new ArrayList<String>(selection.size());
470 for(Object o : selection.toList())
471 {
472 if (o instanceof ServerVariable)
473 names.add(((ServerVariable)o).getName());
474 }
475 new ConsoleJob(Messages.get().ServerConfigurationEditor_DeleteJobName, this, Activator.PLUGIN_ID, ServerConfigurationEditor.JOB_FAMILY) {
476 @Override
477 protected void runInternal(IProgressMonitor monitor) throws Exception
478 {
479 for(String n : names)
480 {
481 session.deleteServerVariable(n);
482 }
483 runInUIThread(new Runnable() {
484 @Override
485 public void run()
486 {
487 refresh();
488 }
489 });
490 }
491
492 @Override
493 protected String getErrorMessage()
494 {
495 return Messages.get().ServerConfigurationEditor_DeleteJobError;
496 }
497 }.start();
498 }
499 }