e6efbb57db2e03d8440ceab5baecafe9fedcd828
[public/netxms.git] / src / java / netxms-eclipse / core / src / org / netxms / ui / eclipse / widgets / AbstractTraceWidget.java
1 /**
2 *
3 */
4 package org.netxms.ui.eclipse.widgets;
5
6 import java.util.ArrayList;
7 import java.util.List;
8 import org.eclipse.core.runtime.Platform;
9 import org.eclipse.jface.action.Action;
10 import org.eclipse.jface.action.GroupMarker;
11 import org.eclipse.jface.action.IMenuListener;
12 import org.eclipse.jface.action.IMenuManager;
13 import org.eclipse.jface.action.MenuManager;
14 import org.eclipse.jface.action.Separator;
15 import org.eclipse.jface.commands.ActionHandler;
16 import org.eclipse.jface.dialogs.IDialogSettings;
17 import org.eclipse.jface.viewers.ArrayContentProvider;
18 import org.eclipse.jface.viewers.TableViewer;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.DisposeEvent;
21 import org.eclipse.swt.events.DisposeListener;
22 import org.eclipse.swt.events.ModifyEvent;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.layout.FormAttachment;
25 import org.eclipse.swt.layout.FormData;
26 import org.eclipse.swt.layout.FormLayout;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Menu;
29 import org.eclipse.swt.widgets.TableColumn;
30 import org.eclipse.swt.widgets.TableItem;
31 import org.eclipse.ui.IViewPart;
32 import org.eclipse.ui.IWorkbenchActionConstants;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.handlers.IHandlerService;
35 import org.netxms.ui.eclipse.console.Activator;
36 import org.netxms.ui.eclipse.console.Messages;
37 import org.netxms.ui.eclipse.shared.SharedIcons;
38 import org.netxms.ui.eclipse.tools.WidgetHelper;
39 import org.netxms.ui.eclipse.views.helpers.AbstractTraceViewFilter;
40
41 /**
42 * Abstract widget for trace views
43 */
44 public abstract class AbstractTraceWidget extends Composite
45 {
46 private static final int MAX_ELEMENTS = 1000;
47
48 private IViewPart viewPart;
49 private FilterText filterText;
50 private TableViewer viewer;
51 private List<Object> data = new ArrayList<Object>(MAX_ELEMENTS);
52 private AbstractTraceViewFilter filter = null;
53 private boolean paused = false;
54 private boolean filterEnabled = false;
55 private Action actionPause;
56 private Action actionShowFilter;
57 private Action actionCopy;
58
59 public AbstractTraceWidget(Composite parent, int style, IViewPart viewPart)
60 {
61 super(parent, style);
62
63 this.viewPart = viewPart;
64
65 setLayout(new FormLayout());
66
67 filterText = new FilterText(this, SWT.NONE);
68 filterText.addModifyListener(new ModifyListener() {
69 @Override
70 public void modifyText(ModifyEvent e)
71 {
72 onFilterModify();
73 }
74 });
75 filterText.setCloseAction(new Action() {
76 @Override
77 public void run()
78 {
79 enableFilter(false);
80 actionShowFilter.setChecked(false);
81 }
82 });
83
84 viewer = new TableViewer(this, SWT.FULL_SELECTION | SWT.MULTI);
85 viewer.getTable().setHeaderVisible(true);
86 viewer.setContentProvider(new ArrayContentProvider());
87 setupViewer(viewer);
88 WidgetHelper.restoreColumnSettings(viewer.getTable(), getDialogSettings(), getConfigPrefix());
89 viewer.getTable().addDisposeListener(new DisposeListener() {
90 @Override
91 public void widgetDisposed(DisposeEvent e)
92 {
93 WidgetHelper.saveColumnSettings(viewer.getTable(), getDialogSettings(), getConfigPrefix());
94 }
95 });
96
97 // Setup layout
98 FormData fd = new FormData();
99 fd.left = new FormAttachment(0, 0);
100 fd.top = new FormAttachment(filterText);
101 fd.right = new FormAttachment(100, 0);
102 fd.bottom = new FormAttachment(100, 0);
103 viewer.getTable().setLayoutData(fd);
104
105 fd = new FormData();
106 fd.left = new FormAttachment(0, 0);
107 fd.top = new FormAttachment(0, 0);
108 fd.right = new FormAttachment(100, 0);
109 filterText.setLayoutData(fd);
110
111 createActions();
112 createPopupMenu();
113
114 enableFilter(filterEnabled);
115
116 addDisposeListener(new DisposeListener() {
117 @Override
118 public void widgetDisposed(DisposeEvent e)
119 {
120 saveConfig();
121 }
122 });
123 }
124
125 /**
126 * Create actions
127 */
128 protected void createActions()
129 {
130 final IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
131
132 actionPause = new Action(Messages.AbstractTraceView_Pause, Action.AS_CHECK_BOX) {
133 @Override
134 public void run()
135 {
136 setPaused(actionPause.isChecked());
137 }
138 };
139 actionPause.setImageDescriptor(Activator.getImageDescriptor("icons/pause.png")); //$NON-NLS-1$
140 actionPause.setActionDefinitionId("org.netxms.ui.eclipse.library.commands.pause_trace"); //$NON-NLS-1$
141 final ActionHandler pauseHandler = new ActionHandler(actionPause);
142 handlerService.activateHandler(actionPause.getActionDefinitionId(), pauseHandler);
143
144 actionShowFilter = new Action(Messages.AbstractTraceView_ShowFilter, Action.AS_CHECK_BOX) {
145 @Override
146 public void run()
147 {
148 enableFilter(actionShowFilter.isChecked());
149 }
150 };
151 actionShowFilter.setChecked(filterEnabled);
152 actionShowFilter.setActionDefinitionId("org.netxms.ui.eclipse.library.commands.show_trace_filter"); //$NON-NLS-1$
153 handlerService.activateHandler(actionShowFilter.getActionDefinitionId(), new ActionHandler(actionShowFilter));
154
155 actionCopy = new Action(Messages.AbstractTraceView_CopyToClipboard, SharedIcons.COPY) {
156 @Override
157 public void run()
158 {
159 copySelectionToClipboard();
160 }
161 };
162 actionCopy.setActionDefinitionId("org.netxms.ui.eclipse.library.commands.copy"); //$NON-NLS-1$
163 handlerService.activateHandler(actionCopy.getActionDefinitionId(), new ActionHandler(actionCopy));
164 }
165
166 /**
167 * Create viewer's popup menu
168 */
169 private void createPopupMenu()
170 {
171 // Create menu manager
172 MenuManager menuMgr = new MenuManager();
173 menuMgr.setRemoveAllWhenShown(true);
174 menuMgr.addMenuListener(new IMenuListener() {
175 public void menuAboutToShow(IMenuManager mgr)
176 {
177 fillContextMenu(mgr);
178 }
179 });
180
181 // Create menu
182 Menu menu = menuMgr.createContextMenu(viewer.getControl());
183 viewer.getControl().setMenu(menu);
184
185 // Register menu for extension.
186 if (viewPart != null)
187 {
188 viewPart.getSite().registerContextMenu(menuMgr, viewer);
189 }
190 }
191
192 /**
193 * Fill context menu
194 *
195 * @param manager Menu manager
196 */
197 protected void fillContextMenu(final IMenuManager manager)
198 {
199 manager.add(actionCopy);
200 manager.add(new Separator());
201 manager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
202 }
203
204 /* (non-Javadoc)
205 * @see org.eclipse.swt.widgets.Composite#setFocus()
206 */
207 @Override
208 public boolean setFocus()
209 {
210 return viewer.getControl().setFocus();
211 }
212
213 /**
214 * Setup table vewer
215 *
216 * @param viewer
217 */
218 protected abstract void setupViewer(TableViewer viewer);
219
220 /**
221 * Add column to viewer
222 *
223 * @param name
224 * @param width
225 */
226 protected void addColumn(String name, int width)
227 {
228 final TableColumn tc = new TableColumn(viewer.getTable(), SWT.LEFT);
229 tc.setText(name);
230 tc.setWidth(width);
231 }
232
233 /**
234 * Add new element to the viewer
235 *
236 * @param element
237 */
238 protected void addElement(Object element)
239 {
240 if (data.size() == MAX_ELEMENTS)
241 data.remove(data.size() - 1);
242
243 data.add(0, element);
244 if (!paused)
245 viewer.setInput(data.toArray());
246 }
247
248 /**
249 * Clear viewer
250 */
251 public void clear()
252 {
253 data.clear();
254 viewer.setInput(new Object[0]);
255 }
256
257 /**
258 * Refresh viewer
259 */
260 protected void refresh()
261 {
262 viewer.refresh();
263 }
264
265 /**
266 * @return the paused
267 */
268 protected boolean isPaused()
269 {
270 return paused;
271 }
272
273 /**
274 * @param paused the paused to set
275 */
276 protected void setPaused(boolean paused)
277 {
278 this.paused = paused;
279 if (!paused)
280 viewer.setInput(data.toArray());
281 }
282
283 /**
284 * Get dialog settings for saving viewer config
285 *
286 * @return
287 */
288 protected abstract IDialogSettings getDialogSettings();
289
290 /**
291 * Get configuration prefix for saving viewer config
292 *
293 * @return
294 */
295 protected abstract String getConfigPrefix();
296
297 /**
298 * Save config
299 */
300 protected void saveConfig()
301 {
302 }
303
304 /**
305 * Enable or disable filter
306 *
307 * @param enable New filter state
308 */
309 protected void enableFilter(boolean enable)
310 {
311 filterEnabled = enable;
312 filterText.setVisible(filterEnabled);
313 FormData fd = (FormData)viewer.getTable().getLayoutData();
314 fd.top = enable ? new FormAttachment(filterText) : new FormAttachment(0, 0);
315 layout(true, true);
316 if (enable)
317 filterText.setFocus();
318 else
319 setFilter(""); //$NON-NLS-1$
320 }
321
322 /**
323 * Set filter text
324 *
325 * @param text New filter text
326 */
327 protected void setFilter(final String text)
328 {
329 filterText.setText(text);
330 onFilterModify();
331 }
332
333 /**
334 * Handler for filter modification
335 */
336 private void onFilterModify()
337 {
338 final String text = filterText.getText();
339 if (filter != null)
340 {
341 filter.setFilterString(text);
342 viewer.refresh(false);
343 }
344 }
345
346 /**
347 * @param filter the filter to set
348 */
349 protected void setFilter(AbstractTraceViewFilter filter)
350 {
351 if (this.filter != null)
352 viewer.removeFilter(this.filter);
353
354 this.filter = filter;
355 viewer.addFilter(filter);
356 }
357
358 /**
359 * @param runnable
360 */
361 protected void runInUIThread(final Runnable runnable)
362 {
363 viewer.getControl().getDisplay().asyncExec(runnable);
364 }
365
366 /**
367 * Copy selection in the list to clipboard
368 */
369 private void copySelectionToClipboard()
370 {
371 TableItem[] selection = viewer.getTable().getSelection();
372 if (selection.length > 0)
373 {
374 StringBuilder sb = new StringBuilder();
375 final String newLine = Platform.getOS().equals(Platform.OS_WIN32) ? "\r\n" : "\n"; //$NON-NLS-1$ //$NON-NLS-2$
376 for(int i = 0; i < selection.length; i++)
377 {
378 if (i > 0)
379 sb.append(newLine);
380 sb.append(selection[i].getText(0));
381 for(int j = 1; j < viewer.getTable().getColumnCount(); j++)
382 {
383 sb.append('\t');
384 sb.append(selection[i].getText(j));
385 }
386 }
387 WidgetHelper.copyToClipboard(sb.toString());
388 }
389 }
390
391 /**
392 * @return the actionPause
393 */
394 public Action getActionPause()
395 {
396 return actionPause;
397 }
398
399 /**
400 * @return the actionShowFilter
401 */
402 public Action getActionShowFilter()
403 {
404 return actionShowFilter;
405 }
406
407 /**
408 * @return the actionCopy
409 */
410 public Action getActionCopy()
411 {
412 return actionCopy;
413 }
414 }