3100bfa9fcf93716ab89b81eae639ddec1d8044e
[public/netxms.git] / src / java / netxms-eclipse / ObjectView / src / org / netxms / ui / eclipse / objectview / objecttabs / InterfacesTab.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2015 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.objectview.objecttabs;
20
21 import org.eclipse.core.commands.Command;
22 import org.eclipse.core.commands.State;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.jface.action.IMenuListener;
26 import org.eclipse.jface.action.IMenuManager;
27 import org.eclipse.jface.action.MenuManager;
28 import org.eclipse.jface.action.Separator;
29 import org.eclipse.jface.viewers.ArrayContentProvider;
30 import org.eclipse.jface.viewers.ISelectionProvider;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.DisposeEvent;
33 import org.eclipse.swt.events.DisposeListener;
34 import org.eclipse.swt.events.ModifyEvent;
35 import org.eclipse.swt.events.ModifyListener;
36 import org.eclipse.swt.layout.FormAttachment;
37 import org.eclipse.swt.layout.FormData;
38 import org.eclipse.swt.layout.FormLayout;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Menu;
41 import org.eclipse.swt.widgets.TableItem;
42 import org.eclipse.ui.PlatformUI;
43 import org.eclipse.ui.commands.ICommandService;
44 import org.eclipse.ui.contexts.IContextService;
45 import org.netxms.client.objects.AbstractNode;
46 import org.netxms.client.objects.AbstractObject;
47 import org.netxms.client.objects.Interface;
48 import org.netxms.ui.eclipse.actions.ExportToCsvAction;
49 import org.netxms.ui.eclipse.console.resources.SharedIcons;
50 import org.netxms.ui.eclipse.objectbrowser.api.ObjectContextMenu;
51 import org.netxms.ui.eclipse.objectview.Activator;
52 import org.netxms.ui.eclipse.objectview.Messages;
53 import org.netxms.ui.eclipse.objectview.objecttabs.helpers.InterfaceListComparator;
54 import org.netxms.ui.eclipse.objectview.objecttabs.helpers.InterfaceListLabelProvider;
55 import org.netxms.ui.eclipse.objectview.objecttabs.helpers.InterfacesTabFilter;
56 import org.netxms.ui.eclipse.tools.WidgetHelper;
57 import org.netxms.ui.eclipse.widgets.FilterText;
58 import org.netxms.ui.eclipse.widgets.SortableTableViewer;
59
60 /**
61 * "Interfaces" tab
62 */
63 public class InterfacesTab extends ObjectTab
64 {
65 public static final int COLUMN_ID = 0;
66 public static final int COLUMN_NAME = 1;
67 public static final int COLUMN_ALIAS = 2;
68 public static final int COLUMN_TYPE = 3;
69 public static final int COLUMN_INDEX = 4;
70 public static final int COLUMN_SLOT = 5;
71 public static final int COLUMN_PORT = 6;
72 public static final int COLUMN_MTU = 7;
73 public static final int COLUMN_SPEED = 8;
74 public static final int COLUMN_DESCRIPTION = 9;
75 public static final int COLUMN_MAC_ADDRESS = 10;
76 public static final int COLUMN_IP_ADDRESS = 11;
77 public static final int COLUMN_PEER_NAME = 12;
78 public static final int COLUMN_PEER_MAC_ADDRESS = 13;
79 public static final int COLUMN_PEER_IP_ADDRESS = 14;
80 public static final int COLUMN_PEER_PROTOCOL = 15;
81 public static final int COLUMN_ADMIN_STATE = 16;
82 public static final int COLUMN_OPER_STATE = 17;
83 public static final int COLUMN_EXPECTED_STATE = 18;
84 public static final int COLUMN_STATUS = 19;
85 public static final int COLUMN_8021X_PAE_STATE = 20;
86 public static final int COLUMN_8021X_BACKEND_STATE = 21;
87
88 private SortableTableViewer viewer;
89 private InterfaceListLabelProvider labelProvider;
90 private Action actionCopyToClipboard;
91 private Action actionCopyMacAddressToClipboard;
92 private Action actionCopyIpAddressToClipboard;
93 private Action actionCopyPeerNameToClipboard;
94 private Action actionCopyPeerMacToClipboard;
95 private Action actionCopyPeerIpToClipboard;
96 private Action actionExportToCsv;
97
98 private boolean filterEnabled = false;
99 private FilterText filterText;
100 private InterfacesTabFilter filter;
101 private Composite interfacesArea;
102
103 /* (non-Javadoc)
104 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#createTabContent(org.eclipse.swt.widgets.Composite)
105 */
106 @Override
107 protected void createTabContent(Composite parent)
108 {
109 // Create interface area
110 interfacesArea = new Composite(parent, SWT.BORDER);
111 FormLayout formLayout = new FormLayout();
112 interfacesArea.setLayout(formLayout);
113 // Create filter
114 filterText = new FilterText(interfacesArea, SWT.BORDER);
115 filterText.addModifyListener(new ModifyListener() {
116 @Override
117 public void modifyText(ModifyEvent e)
118 {
119 onFilterModify();
120 }
121 });
122 filterText.setCloseAction(new Action() {
123 @Override
124 public void run()
125 {
126 enableFilter(false);
127 }
128 });
129
130 Action action = new Action() {
131 @Override
132 public void run()
133 {
134 enableFilter(false);
135 ICommandService service = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
136 Command command = service.getCommand("org.netxms.ui.eclipse.objectview.commands.show_filter");
137 State state = command.getState("org.netxms.ui.eclipse.objectview.commands.show_filter.state");
138 state.setValue(false);
139 service.refreshElements(command.getId(), null);
140 }
141 };
142 setFilterCloseAction(action);
143
144 // Setup layout
145 FormData fd = new FormData();
146 fd.left = new FormAttachment(0, 0);
147 fd.top = new FormAttachment(0, 0);
148 fd.right = new FormAttachment(100, 0);
149 filterText.setLayoutData(fd);
150
151 final String[] names = {
152 Messages.get().InterfacesTab_ColId,
153 Messages.get().InterfacesTab_ColName,
154 Messages.get().InterfacesTab_Alias,
155 Messages.get().InterfacesTab_ColIfType,
156 Messages.get().InterfacesTab_ColIfIndex,
157 Messages.get().InterfacesTab_ColSlot,
158 Messages.get().InterfacesTab_ColPort,
159 Messages.get().InterfacesTab_MTU,
160 Messages.get().InterfacesTab_Speed,
161 Messages.get().InterfacesTab_ColDescription,
162 Messages.get().InterfacesTab_ColMacAddr,
163 Messages.get().InterfacesTab_ColIpAddr,
164 Messages.get().InterfacesTab_ColPeerNode,
165 Messages.get().InterfacesTab_ColPeerMAC,
166 Messages.get().InterfacesTab_ColPeerIP,
167 Messages.get().InterfacesTab_PeerDiscoveryProtocol,
168 Messages.get().InterfacesTab_ColAdminState,
169 Messages.get().InterfacesTab_ColOperState,
170 Messages.get().InterfacesTab_ColExpState,
171 Messages.get().InterfacesTab_ColStatus,
172 Messages.get().InterfacesTab_Col8021xPAE,
173 Messages.get().InterfacesTab_Col8021xBackend
174 };
175
176 final int[] widths = { 60, 150, 150, 150, 70, 70, 70, 70, 90, 150, 100, 90, 150, 100, 90, 80, 80, 80, 80, 80, 80, 80 };
177 viewer = new SortableTableViewer(interfacesArea, names, widths, COLUMN_NAME, SWT.UP, SWT.FULL_SELECTION | SWT.MULTI);
178 labelProvider = new InterfaceListLabelProvider();
179 viewer.setLabelProvider(labelProvider);
180 viewer.setContentProvider(new ArrayContentProvider());
181 viewer.setComparator(new InterfaceListComparator());
182 viewer.getTable().setHeaderVisible(true);
183 viewer.getTable().setLinesVisible(true);
184 filter = new InterfacesTabFilter();
185 viewer.addFilter(filter);
186 WidgetHelper.restoreTableViewerSettings(viewer, Activator.getDefault().getDialogSettings(), "InterfaceTable.V4"); //$NON-NLS-1$
187 viewer.getTable().addDisposeListener(new DisposeListener() {
188 @Override
189 public void widgetDisposed(DisposeEvent e)
190 {
191 WidgetHelper.saveColumnSettings(viewer.getTable(), Activator.getDefault().getDialogSettings(), "InterfaceTable.V4"); //$NON-NLS-1$
192 }
193 });
194
195 // Setup layout
196 fd = new FormData();
197 fd.left = new FormAttachment(0, 0);
198 ;
199 fd.top = new FormAttachment(filterText, 0, SWT.BOTTOM);
200 fd.bottom = new FormAttachment(100, 0);
201 fd.right = new FormAttachment(100, 0);
202 viewer.getControl().setLayoutData(fd);
203
204 createActions();
205 createPopupMenu();
206
207 // Set initial focus to filter input line
208 if (filterEnabled)
209 filterText.setFocus();
210 else
211 enableFilter(false); // Will hide filter area correctly
212 }
213
214 private void setFilterCloseAction(Action action)
215 {
216 filterText.setCloseAction(action);
217 }
218
219 /**
220 * Enable or disable filter
221 *
222 * @param enable New filter state
223 */
224 public void enableFilter(boolean enable)
225 {
226 filterEnabled = enable;
227 filterText.setVisible(filterEnabled);
228 FormData fd = (FormData)viewer.getTable().getLayoutData();
229 fd.top = enable ? new FormAttachment(filterText, 0, SWT.BOTTOM) : new FormAttachment(0, 0);
230 interfacesArea.layout();
231 if (enable)
232 {
233 filterText.setFocus();
234 }
235 else
236 {
237 filterText.setText("");
238 onFilterModify();
239 }
240 }
241
242 /**
243 * Handler for filter modification
244 */
245 public void onFilterModify()
246 {
247 final String text = filterText.getText();
248 filter.setFilterString(text);
249 viewer.refresh(false);
250 }
251
252 /**
253 * Create actions
254 */
255 private void createActions()
256 {
257 actionCopyToClipboard = new Action(Messages.get().InterfacesTab_ActionCopy, SharedIcons.COPY) {
258 @Override
259 public void run()
260 {
261 copyToClipboard(-1);
262 }
263 };
264
265 actionCopyMacAddressToClipboard = new Action(Messages.get().InterfacesTab_ActionCopyMAC) {
266 @Override
267 public void run()
268 {
269 copyToClipboard(COLUMN_MAC_ADDRESS);
270 }
271 };
272
273 actionCopyIpAddressToClipboard = new Action(Messages.get().InterfacesTab_ActionCopyIP) {
274 @Override
275 public void run()
276 {
277 copyToClipboard(COLUMN_IP_ADDRESS);
278 }
279 };
280
281 actionCopyPeerNameToClipboard = new Action(Messages.get().InterfacesTab_ActionCopyPeerName) {
282 @Override
283 public void run()
284 {
285 copyToClipboard(COLUMN_PEER_NAME);
286 }
287 };
288
289 actionCopyPeerMacToClipboard = new Action(Messages.get().InterfacesTab_ActionCopyPeerMAC) {
290 @Override
291 public void run()
292 {
293 copyToClipboard(COLUMN_PEER_MAC_ADDRESS);
294 }
295 };
296
297 actionCopyPeerIpToClipboard = new Action(Messages.get().InterfacesTab_ActionCopyPeerIP) {
298 @Override
299 public void run()
300 {
301 copyToClipboard(COLUMN_PEER_IP_ADDRESS);
302 }
303 };
304
305 actionExportToCsv = new ExportToCsvAction(getViewPart(), viewer, true);
306 }
307
308 /**
309 * Create pop-up menu
310 */
311 private void createPopupMenu()
312 {
313 // Create menu manager.
314 MenuManager menuMgr = new MenuManager();
315 menuMgr.setRemoveAllWhenShown(true);
316 menuMgr.addMenuListener(new IMenuListener() {
317 public void menuAboutToShow(IMenuManager manager)
318 {
319 fillContextMenu(manager);
320 }
321 });
322
323 // Create menu.
324 Menu menu = menuMgr.createContextMenu(viewer.getControl());
325 viewer.getControl().setMenu(menu);
326
327 // Register menu for extension.
328 if (getViewPart() != null)
329 getViewPart().getSite().registerContextMenu(menuMgr, viewer);
330 }
331
332 /**
333 * Fill context menu
334 * @param mgr Menu manager
335 */
336 protected void fillContextMenu(IMenuManager manager)
337 {
338 manager.add(actionCopyToClipboard);
339 manager.add(actionCopyMacAddressToClipboard);
340 manager.add(actionCopyIpAddressToClipboard);
341 manager.add(actionCopyPeerNameToClipboard);
342 manager.add(actionCopyPeerMacToClipboard);
343 manager.add(actionCopyPeerIpToClipboard);
344 manager.add(actionExportToCsv);
345 manager.add(new Separator());
346 ObjectContextMenu.fill(manager, getViewPart().getSite(), viewer);
347 }
348
349 /* (non-Javadoc)
350 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#currentObjectUpdated(org.netxms.client.objects.AbstractObject)
351 */
352 @Override
353 public void currentObjectUpdated(AbstractObject object)
354 {
355 objectChanged(object);
356 }
357
358 /* (non-Javadoc)
359 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#refresh()
360 */
361 @Override
362 public void refresh()
363 {
364 if (getObject() != null)
365 viewer.setInput(getObject().getAllChilds(AbstractObject.OBJECT_INTERFACE).toArray());
366 else
367 viewer.setInput(new Interface[0]);
368 }
369
370 /* (non-Javadoc)
371 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#objectChanged(org.netxms.client.objects.AbstractObject)
372 */
373 @Override
374 public void objectChanged(final AbstractObject object)
375 {
376 labelProvider.setNode((AbstractNode)object);
377 refresh();
378 }
379
380 /* (non-Javadoc)
381 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#showForObject(org.netxms.client.objects.AbstractObject)
382 */
383 @Override
384 public boolean showForObject(AbstractObject object)
385 {
386 return (object instanceof AbstractNode);
387 }
388
389 /* (non-Javadoc)
390 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#getSelectionProvider()
391 */
392 @Override
393 public ISelectionProvider getSelectionProvider()
394 {
395 return viewer;
396 }
397
398 /**
399 * Copy content to clipboard
400 *
401 * @param column column number or -1 to copy all columns
402 */
403 private void copyToClipboard(int column)
404 {
405 final TableItem[] selection = viewer.getTable().getSelection();
406 if (selection.length > 0)
407 {
408 final String newLine = Platform.getOS().equals(Platform.OS_WIN32) ? "\r\n" : "\n"; //$NON-NLS-1$ //$NON-NLS-2$
409 final StringBuilder sb = new StringBuilder();
410 for(int i = 0; i < selection.length; i++)
411 {
412 if (i > 0)
413 sb.append(newLine);
414 if (column == -1)
415 {
416 for(int j = 0; j < viewer.getTable().getColumnCount(); j++)
417 {
418 if (j > 0)
419 sb.append('\t');
420 sb.append(selection[i].getText(j));
421 }
422 }
423 else
424 {
425 sb.append(selection[i].getText(column));
426 }
427 }
428 WidgetHelper.copyToClipboard(sb.toString());
429 }
430 }
431
432 /* (non-Javadoc)
433 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#selected()
434 */
435 @Override
436 public void selected()
437 {
438 super.selected();
439 IContextService contextService = (IContextService)getViewPart().getSite().getService(IContextService.class);
440 if (contextService != null)
441 {
442 contextService.activateContext("org.netxms.ui.eclipse.objectview.context.InterfacesTab"); //$NON-NLS-1$
443 }
444 refresh();
445 }
446 }