2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2015 Victor Kirhenshtein
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.
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.
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.
19 package org
.netxms
.ui
.eclipse
.objectview
.objecttabs
;
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
;
63 public class InterfacesTab
extends ObjectTab
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;
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
;
98 private boolean filterEnabled
= false;
99 private FilterText filterText
;
100 private InterfacesTabFilter filter
;
101 private Composite interfacesArea
;
104 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#createTabContent(org.eclipse.swt.widgets.Composite)
107 protected void createTabContent(Composite parent
)
109 // Create interface area
110 interfacesArea
= new Composite(parent
, SWT
.BORDER
);
111 FormLayout formLayout
= new FormLayout();
112 interfacesArea
.setLayout(formLayout
);
114 filterText
= new FilterText(interfacesArea
, SWT
.BORDER
);
115 filterText
.addModifyListener(new ModifyListener() {
117 public void modifyText(ModifyEvent e
)
122 filterText
.setCloseAction(new Action() {
130 Action action
= new Action() {
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);
142 setFilterCloseAction(action
);
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
);
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
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() {
189 public void widgetDisposed(DisposeEvent e
)
191 WidgetHelper
.saveColumnSettings(viewer
.getTable(), Activator
.getDefault().getDialogSettings(), "InterfaceTable.V4"); //$NON-NLS-1$
197 fd
.left
= new FormAttachment(0, 0);
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
);
207 // Set initial focus to filter input line
209 filterText
.setFocus();
211 enableFilter(false); // Will hide filter area correctly
214 private void setFilterCloseAction(Action action
)
216 filterText
.setCloseAction(action
);
220 * Enable or disable filter
222 * @param enable New filter state
224 public void enableFilter(boolean enable
)
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();
233 filterText
.setFocus();
237 filterText
.setText("");
243 * Handler for filter modification
245 public void onFilterModify()
247 final String text
= filterText
.getText();
248 filter
.setFilterString(text
);
249 viewer
.refresh(false);
255 private void createActions()
257 actionCopyToClipboard
= new Action(Messages
.get().InterfacesTab_ActionCopy
, SharedIcons
.COPY
) {
265 actionCopyMacAddressToClipboard
= new Action(Messages
.get().InterfacesTab_ActionCopyMAC
) {
269 copyToClipboard(COLUMN_MAC_ADDRESS
);
273 actionCopyIpAddressToClipboard
= new Action(Messages
.get().InterfacesTab_ActionCopyIP
) {
277 copyToClipboard(COLUMN_IP_ADDRESS
);
281 actionCopyPeerNameToClipboard
= new Action(Messages
.get().InterfacesTab_ActionCopyPeerName
) {
285 copyToClipboard(COLUMN_PEER_NAME
);
289 actionCopyPeerMacToClipboard
= new Action(Messages
.get().InterfacesTab_ActionCopyPeerMAC
) {
293 copyToClipboard(COLUMN_PEER_MAC_ADDRESS
);
297 actionCopyPeerIpToClipboard
= new Action(Messages
.get().InterfacesTab_ActionCopyPeerIP
) {
301 copyToClipboard(COLUMN_PEER_IP_ADDRESS
);
305 actionExportToCsv
= new ExportToCsvAction(getViewPart(), viewer
, true);
311 private void createPopupMenu()
313 // Create menu manager.
314 MenuManager menuMgr
= new MenuManager();
315 menuMgr
.setRemoveAllWhenShown(true);
316 menuMgr
.addMenuListener(new IMenuListener() {
317 public void menuAboutToShow(IMenuManager manager
)
319 fillContextMenu(manager
);
324 Menu menu
= menuMgr
.createContextMenu(viewer
.getControl());
325 viewer
.getControl().setMenu(menu
);
327 // Register menu for extension.
328 if (getViewPart() != null)
329 getViewPart().getSite().registerContextMenu(menuMgr
, viewer
);
334 * @param mgr Menu manager
336 protected void fillContextMenu(IMenuManager manager
)
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
);
350 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#currentObjectUpdated(org.netxms.client.objects.AbstractObject)
353 public void currentObjectUpdated(AbstractObject object
)
355 objectChanged(object
);
359 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#refresh()
362 public void refresh()
364 if (getObject() != null)
365 viewer
.setInput(getObject().getAllChilds(AbstractObject
.OBJECT_INTERFACE
).toArray());
367 viewer
.setInput(new Interface
[0]);
371 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#objectChanged(org.netxms.client.objects.AbstractObject)
374 public void objectChanged(final AbstractObject object
)
376 labelProvider
.setNode((AbstractNode
)object
);
381 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#showForObject(org.netxms.client.objects.AbstractObject)
384 public boolean showForObject(AbstractObject object
)
386 return (object
instanceof AbstractNode
);
390 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#getSelectionProvider()
393 public ISelectionProvider
getSelectionProvider()
399 * Copy content to clipboard
401 * @param column column number or -1 to copy all columns
403 private void copyToClipboard(int column
)
405 final TableItem
[] selection
= viewer
.getTable().getSelection();
406 if (selection
.length
> 0)
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
++)
416 for(int j
= 0; j
< viewer
.getTable().getColumnCount(); j
++)
420 sb
.append(selection
[i
].getText(j
));
425 sb
.append(selection
[i
].getText(column
));
428 WidgetHelper
.copyToClipboard(sb
.toString());
433 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#selected()
436 public void selected()
439 IContextService contextService
= (IContextService
)getViewPart().getSite().getService(IContextService
.class);
440 if (contextService
!= null)
442 contextService
.activateContext("org.netxms.ui.eclipse.objectview.context.InterfacesTab"); //$NON-NLS-1$