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