d5ba400103de3305e751cc9ec05b214d638296bd
[public/netxms.git] / webui / webapp / 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.jface.action.Action;
24 import org.eclipse.jface.action.IMenuListener;
25 import org.eclipse.jface.action.IMenuManager;
26 import org.eclipse.jface.action.MenuManager;
27 import org.eclipse.jface.action.Separator;
28 import org.eclipse.jface.viewers.ArrayContentProvider;
29 import org.eclipse.jface.viewers.ISelectionProvider;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.DisposeEvent;
32 import org.eclipse.swt.events.DisposeListener;
33 import org.eclipse.swt.events.ModifyEvent;
34 import org.eclipse.swt.events.ModifyListener;
35 import org.eclipse.swt.layout.FormAttachment;
36 import org.eclipse.swt.layout.FormData;
37 import org.eclipse.swt.layout.FormLayout;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Menu;
40 import org.eclipse.ui.PlatformUI;
41 import org.eclipse.ui.commands.ICommandService;
42 import org.eclipse.ui.contexts.IContextService;
43 import org.netxms.client.objects.AbstractNode;
44 import org.netxms.client.objects.AbstractObject;
45 import org.netxms.client.objects.Interface;
46 import org.netxms.ui.eclipse.actions.ExportToCsvAction;
47 import org.netxms.ui.eclipse.objectbrowser.api.ObjectContextMenu;
48 import org.netxms.ui.eclipse.objectview.Activator;
49 import org.netxms.ui.eclipse.objectview.Messages;
50 import org.netxms.ui.eclipse.objectview.objecttabs.helpers.InterfaceListComparator;
51 import org.netxms.ui.eclipse.objectview.objecttabs.helpers.InterfaceListLabelProvider;
52 import org.netxms.ui.eclipse.objectview.objecttabs.helpers.InterfacesTabFilter;
53 import org.netxms.ui.eclipse.tools.WidgetHelper;
54 import org.netxms.ui.eclipse.widgets.FilterText;
55 import org.netxms.ui.eclipse.widgets.SortableTableViewer;
56
57 /**
58 * "Interfaces" tab
59 */
60 public class InterfacesTab extends ObjectTab
61 {
62 public static final int COLUMN_ID = 0;
63 public static final int COLUMN_NAME = 1;
64 public static final int COLUMN_ALIAS = 2;
65 public static final int COLUMN_TYPE = 3;
66 public static final int COLUMN_INDEX = 4;
67 public static final int COLUMN_SLOT = 5;
68 public static final int COLUMN_PORT = 6;
69 public static final int COLUMN_MTU = 7;
70 public static final int COLUMN_SPEED = 8;
71 public static final int COLUMN_DESCRIPTION = 9;
72 public static final int COLUMN_MAC_ADDRESS = 10;
73 public static final int COLUMN_IP_ADDRESS = 11;
74 public static final int COLUMN_PEER_NAME = 12;
75 public static final int COLUMN_PEER_MAC_ADDRESS = 13;
76 public static final int COLUMN_PEER_IP_ADDRESS = 14;
77 public static final int COLUMN_PEER_PROTOCOL = 15;
78 public static final int COLUMN_ADMIN_STATE = 16;
79 public static final int COLUMN_OPER_STATE = 17;
80 public static final int COLUMN_EXPECTED_STATE = 18;
81 public static final int COLUMN_STATUS = 19;
82 public static final int COLUMN_8021X_PAE_STATE = 20;
83 public static final int COLUMN_8021X_BACKEND_STATE = 21;
84
85 private SortableTableViewer viewer;
86 private InterfaceListLabelProvider labelProvider;
87 private Action actionExportToCsv;
88
89 private boolean filterEnabled = false;
90 private FilterText filterText;
91 private InterfacesTabFilter filter;
92 private Composite interfacesArea;
93
94 /* (non-Javadoc)
95 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#createTabContent(org.eclipse.swt.widgets.Composite)
96 */
97 @Override
98 protected void createTabContent(Composite parent)
99 {
100 // Create interface area
101 interfacesArea = new Composite(parent, SWT.BORDER);
102 FormLayout formLayout = new FormLayout();
103 interfacesArea.setLayout(formLayout);
104 // Create filter
105 filterText = new FilterText(interfacesArea, SWT.BORDER);
106 filterText.addModifyListener(new ModifyListener() {
107 @Override
108 public void modifyText(ModifyEvent e)
109 {
110 onFilterModify();
111 }
112 });
113 filterText.setCloseAction(new Action() {
114 @Override
115 public void run()
116 {
117 enableFilter(false);
118 }
119 });
120
121 Action action = new Action() {
122 @Override
123 public void run()
124 {
125 enableFilter(false);
126 ICommandService service = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
127 Command command = service.getCommand("org.netxms.ui.eclipse.objectview.commands.show_filter");
128 State state = command.getState("org.netxms.ui.eclipse.objectview.commands.show_filter.state");
129 state.setValue(false);
130 service.refreshElements(command.getId(), null);
131 }
132 };
133 setFilterCloseAction(action);
134
135 // Setup layout
136 FormData fd = new FormData();
137 fd.left = new FormAttachment(0, 0);
138 fd.top = new FormAttachment(0, 0);
139 fd.right = new FormAttachment(100, 0);
140 filterText.setLayoutData(fd);
141
142 final String[] names = {
143 Messages.get().InterfacesTab_ColId,
144 Messages.get().InterfacesTab_ColName,
145 Messages.get().InterfacesTab_Alias,
146 Messages.get().InterfacesTab_ColIfType,
147 Messages.get().InterfacesTab_ColIfIndex,
148 Messages.get().InterfacesTab_ColSlot,
149 Messages.get().InterfacesTab_ColPort,
150 Messages.get().InterfacesTab_MTU,
151 Messages.get().InterfacesTab_Speed,
152 Messages.get().InterfacesTab_ColDescription,
153 Messages.get().InterfacesTab_ColMacAddr,
154 Messages.get().InterfacesTab_ColIpAddr,
155 Messages.get().InterfacesTab_ColPeerNode,
156 Messages.get().InterfacesTab_ColPeerMAC,
157 Messages.get().InterfacesTab_ColPeerIP,
158 Messages.get().InterfacesTab_PeerDiscoveryProtocol,
159 Messages.get().InterfacesTab_ColAdminState,
160 Messages.get().InterfacesTab_ColOperState,
161 Messages.get().InterfacesTab_ColExpState,
162 Messages.get().InterfacesTab_ColStatus,
163 Messages.get().InterfacesTab_Col8021xPAE,
164 Messages.get().InterfacesTab_Col8021xBackend
165 };
166 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 };
167 viewer = new SortableTableViewer(interfacesArea, names, widths, COLUMN_NAME, SWT.UP, SWT.FULL_SELECTION | SWT.MULTI);
168 labelProvider = new InterfaceListLabelProvider();
169 viewer.setLabelProvider(labelProvider);
170 viewer.setContentProvider(new ArrayContentProvider());
171 viewer.setComparator(new InterfaceListComparator());
172 viewer.getTable().setHeaderVisible(true);
173 viewer.getTable().setLinesVisible(true);
174 filter = new InterfacesTabFilter();
175 viewer.addFilter(filter);
176 WidgetHelper.restoreTableViewerSettings(viewer, Activator.getDefault().getDialogSettings(), "InterfaceTable.V4"); //$NON-NLS-1$
177 viewer.getTable().addDisposeListener(new DisposeListener() {
178 @Override
179 public void widgetDisposed(DisposeEvent e)
180 {
181 WidgetHelper.saveColumnSettings(viewer.getTable(), Activator.getDefault().getDialogSettings(), "InterfaceTable.V4"); //$NON-NLS-1$
182 }
183 });
184
185 // Setup layout
186 fd = new FormData();
187 fd.left = new FormAttachment(0, 0);
188 ;
189 fd.top = new FormAttachment(filterText, 0, SWT.BOTTOM);
190 fd.bottom = new FormAttachment(100, 0);
191 fd.right = new FormAttachment(100, 0);
192 viewer.getControl().setLayoutData(fd);
193
194 createActions();
195 createPopupMenu();
196
197 // Set initial focus to filter input line
198 if (filterEnabled)
199 filterText.setFocus();
200 else
201 enableFilter(false); // Will hide filter area correctly
202 }
203
204 private void setFilterCloseAction(Action action)
205 {
206 filterText.setCloseAction(action);
207 }
208
209 /**
210 * Enable or disable filter
211 *
212 * @param enable New filter state
213 */
214 public void enableFilter(boolean enable)
215 {
216 filterEnabled = enable;
217 filterText.setVisible(filterEnabled);
218 FormData fd = (FormData)viewer.getTable().getLayoutData();
219 fd.top = enable ? new FormAttachment(filterText, 0, SWT.BOTTOM) : new FormAttachment(0, 0);
220 interfacesArea.layout();
221 if (enable)
222 {
223 filterText.setFocus();
224 }
225 else
226 {
227 filterText.setText("");
228 onFilterModify();
229 }
230 }
231
232 /**
233 * Handler for filter modification
234 */
235 public void onFilterModify()
236 {
237 final String text = filterText.getText();
238 filter.setFilterString(text);
239 viewer.refresh(false);
240 }
241
242 /**
243 * Create actions
244 */
245 private void createActions()
246 {
247 actionExportToCsv = new ExportToCsvAction(getViewPart(), viewer, true);
248 }
249
250 /**
251 * Create pop-up menu
252 */
253 private void createPopupMenu()
254 {
255 // Create menu manager.
256 MenuManager menuMgr = new MenuManager();
257 menuMgr.setRemoveAllWhenShown(true);
258 menuMgr.addMenuListener(new IMenuListener() {
259 public void menuAboutToShow(IMenuManager manager)
260 {
261 fillContextMenu(manager);
262 }
263 });
264
265 // Create menu.
266 Menu menu = menuMgr.createContextMenu(viewer.getControl());
267 viewer.getControl().setMenu(menu);
268
269 // Register menu for extension.
270 if (getViewPart() != null)
271 getViewPart().getSite().registerContextMenu(menuMgr, viewer);
272 }
273
274 /**
275 * Fill context menu
276 * @param mgr Menu manager
277 */
278 protected void fillContextMenu(IMenuManager manager)
279 {
280 manager.add(actionExportToCsv);
281 manager.add(new Separator());
282 ObjectContextMenu.fill(manager, getViewPart().getSite(), viewer);
283 }
284
285 /* (non-Javadoc)
286 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#currentObjectUpdated(org.netxms.client.objects.AbstractObject)
287 */
288 @Override
289 public void currentObjectUpdated(AbstractObject object)
290 {
291 objectChanged(object);
292 }
293
294 /* (non-Javadoc)
295 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#refresh()
296 */
297 @Override
298 public void refresh()
299 {
300 if (getObject() != null)
301 viewer.setInput(getObject().getAllChilds(AbstractObject.OBJECT_INTERFACE).toArray());
302 else
303 viewer.setInput(new Interface[0]);
304 }
305
306 /* (non-Javadoc)
307 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#objectChanged(org.netxms.client.objects.AbstractObject)
308 */
309 @Override
310 public void objectChanged(final AbstractObject object)
311 {
312 labelProvider.setNode((AbstractNode)object);
313 refresh();
314 }
315
316 /* (non-Javadoc)
317 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#showForObject(org.netxms.client.objects.AbstractObject)
318 */
319 @Override
320 public boolean showForObject(AbstractObject object)
321 {
322 return (object instanceof AbstractNode);
323 }
324
325 /* (non-Javadoc)
326 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#getSelectionProvider()
327 */
328 @Override
329 public ISelectionProvider getSelectionProvider()
330 {
331 return viewer;
332 }
333
334 /* (non-Javadoc)
335 * @see org.netxms.ui.eclipse.objectview.objecttabs.ObjectTab#selected()
336 */
337 @Override
338 public void selected()
339 {
340 super.selected();
341 IContextService contextService = (IContextService)getViewPart().getSite().getService(IContextService.class);
342 if (contextService != null)
343 {
344 contextService.activateContext("org.netxms.ui.eclipse.objectview.context.InterfacesTab"); //$NON-NLS-1$
345 }
346 refresh();
347 }
348 }