Package: VdmDebugLogView$2

VdmDebugLogView$2

nameinstructionbranchcomplexitylinemethod
selectionChanged(SelectionChangedEvent)
M: 26 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.debug
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.debug.ui.log;
23:
24: import java.util.ArrayList;
25: import java.util.Arrays;
26: import java.util.List;
27: import java.util.Vector;
28:
29: import org.eclipse.jface.action.Action;
30: import org.eclipse.jface.action.IAction;
31: import org.eclipse.jface.action.IMenuListener;
32: import org.eclipse.jface.action.IMenuManager;
33: import org.eclipse.jface.action.IToolBarManager;
34: import org.eclipse.jface.action.MenuManager;
35: import org.eclipse.jface.action.Separator;
36: import org.eclipse.jface.resource.JFaceResources;
37: import org.eclipse.jface.text.Document;
38: import org.eclipse.jface.text.IDocument;
39: import org.eclipse.jface.text.TextViewer;
40: import org.eclipse.jface.util.IPropertyChangeListener;
41: import org.eclipse.jface.util.PropertyChangeEvent;
42: import org.eclipse.jface.viewers.ISelectionChangedListener;
43: import org.eclipse.jface.viewers.IStructuredSelection;
44: import org.eclipse.jface.viewers.SelectionChangedEvent;
45: import org.eclipse.jface.viewers.TableViewer;
46: import org.eclipse.jface.viewers.ViewerFilter;
47: import org.eclipse.swt.SWT;
48: import org.eclipse.swt.custom.SashForm;
49: import org.eclipse.swt.widgets.Composite;
50: import org.eclipse.swt.widgets.Display;
51: import org.eclipse.swt.widgets.Event;
52: import org.eclipse.swt.widgets.Listener;
53: import org.eclipse.swt.widgets.Menu;
54: import org.eclipse.swt.widgets.Table;
55: import org.eclipse.swt.widgets.TableColumn;
56: import org.eclipse.ui.IWorkbenchActionConstants;
57: import org.eclipse.ui.part.ViewPart;
58:
59: public class VdmDebugLogView extends ViewPart
60: {
61:         public static final String VIEW_ID = "org.overture.ide.debug.ui.log.dbgpLogView"; //$NON-NLS-1$
62:         public static final String THEME_ID = "org.eclipse.dltk.debug.ui.dbgpLogView.txtViewFont"; //$NON-NLS-1$
63:
64:         private final List<VdmDebugLogItem> items = new ArrayList<VdmDebugLogItem>();
65:         private TableViewer viewer;
66:         private TextViewer textViewer;
67:         private IDocument textDocument;
68:         private IPropertyChangeListener fontRegistryChangeListener;
69:         private IAction executionFilterAction;
70:
71:         public VdmDebugLogView()
72:         {
73:                 super();
74:         }
75:
76:         public void setFocus()
77:         {
78:                 viewer.getControl().setFocus();
79:         }
80:
81:         public void createPartControl(Composite parent)
82:         {
83:                 final SashForm sashForm = new SashForm(parent, SWT.HORIZONTAL);
84:                 viewer = new TableViewer(sashForm, SWT.H_SCROLL | SWT.V_SCROLL
85:                                 | SWT.MULTI | SWT.FULL_SELECTION | SWT.VIRTUAL);
86:                 viewer.getTable().setHeaderVisible(true);
87:                 viewer.getTable().setLinesVisible(true);
88:                 // addColumn(Messages.Column_Date, 100, true);
89:
90:                 addColumn(Messages.Column_Time, 100, true);
91:                 addColumn(Messages.Column_Type, 80, true);
92:                 addColumn(Messages.Column_Session, 60, true);
93:                 addColumn(Messages.Column_Message, 500, false);
94:                 viewer.getTable().addListener(SWT.Resize, new Listener()
95:                 {
96:
97:                         public void handleEvent(Event event)
98:                         {
99:                                 final Table table = (Table) event.widget;
100:                                 final int columnCount = table.getColumnCount();
101:                                 int w = table.getClientArea().width;
102:                                 for (int i = 0; i < columnCount - 1; ++i)
103:                                 {
104:                                         w -= table.getColumn(i).getWidth();
105:                                 }
106:                                 if (w > 0)
107:                                 {
108:                                         table.getColumn(columnCount - 1).setWidth(w);
109:                                 }
110:                         }
111:
112:                 });
113:                 viewer.addSelectionChangedListener(new ISelectionChangedListener()
114:                 {
115:
116:                         public void selectionChanged(SelectionChangedEvent event)
117:                         {
118:•                                if (event.getSelection() instanceof IStructuredSelection)
119:                                 {
120:                                         final Object first = ((IStructuredSelection) event.getSelection()).getFirstElement();
121:•                                        if (first instanceof VdmDebugLogItem)
122:                                         {
123:                                                 textDocument.set(((VdmDebugLogItem) first).getMessage());
124:                                                 return;
125:                                         }
126:                                 }
127:                                 textDocument.set(""); //$NON-NLS-1$
128:                         }
129:
130:                 });
131:                 viewer.setContentProvider(new VdmDebugLogContentProvider());
132:                 viewer.setLabelProvider(new VdmDebugLogLabelProvider());
133:                 viewer.setInput(items);
134:                 textDocument = new Document();
135:                 textViewer = new TextViewer(sashForm, SWT.V_SCROLL | SWT.H_SCROLL
136:                                 | SWT.WRAP | SWT.READ_ONLY);
137:                 textViewer.setDocument(textDocument);
138:                 fontRegistryChangeListener = new IPropertyChangeListener()
139:                 {
140:                         public void propertyChange(PropertyChangeEvent event)
141:                         {
142:                                 handlePropertyChangeEvent(event);
143:                         }
144:                 };
145:                 JFaceResources.getFontRegistry().addListener(fontRegistryChangeListener);
146:
147:                 updateFont();
148:                 sashForm.setWeights(new int[] { 75, 25 });
149:                 createActions();
150:                 createMenu();
151:                 createToolbar();
152:                 createContextMenu();
153:         }
154:
155:         public void dispose()
156:         {
157:                 if (fontRegistryChangeListener != null)
158:                 {
159:                         JFaceResources.getFontRegistry().removeListener(fontRegistryChangeListener);
160:                         fontRegistryChangeListener = null;
161:                 }
162:                 super.dispose();
163:         }
164:
165:         /**
166:          * @param event
167:          */
168:         protected void handlePropertyChangeEvent(PropertyChangeEvent event)
169:         {
170:                 final String key = event.getProperty();
171:                 if (key.equals(THEME_ID))
172:                 {
173:                         updateFont();
174:                 }
175:         }
176:
177:         private void updateFont()
178:         {
179:                 textViewer.getTextWidget().setFont(JFaceResources.getFont(THEME_ID));
180:         }
181:
182:         /**
183:          * @param caption
184:          * @param width
185:          */
186:         private void addColumn(String caption, int width, boolean center)
187:         {
188:                 final TableColumn column = new TableColumn(viewer.getTable(), SWT.LEFT);
189:                 column.setText(caption);
190:                 column.setWidth(width);
191:         }
192:
193:         public void append(final VdmDebugLogItem item)
194:         {
195:                 synchronized (items)
196:                 {
197:                         items.add(item);
198:                 }
199:                 final Table table = viewer.getTable();
200:                 if (table.isDisposed())
201:                 {
202:                         return;
203:                 }
204:                 final Display display = table.getDisplay();
205:                 if (display.isDisposed())
206:                 {
207:                         return;
208:                 }
209:                 display.asyncExec(new Runnable()
210:                 {
211:
212:                         public void run()
213:                         {
214:                                 viewer.refresh(false, false);
215:                                 if (table.isDisposed() || table.getDisplay().isDisposed())
216:                                 {
217:                                         return;
218:                                 }
219:                                 final int itemCount = table.getItemCount();
220:                                 if (itemCount > 0)
221:                                 {
222:                                         table.showItem(table.getItem(itemCount - 1));
223:                                 }
224:                         }
225:
226:                 });
227:         }
228:
229:         private IAction copyAction;
230:         private IAction clearAction;
231:
232:         public void clear()
233:         {
234:                 Display.getDefault().syncExec(new Runnable()
235:                 {
236:                         public void run()
237:                         {
238:                                 clearAction.run();
239:                         }
240:
241:                 });
242:
243:         }
244:
245:         public void createActions()
246:         {
247:                 copyAction = new VdmDebugLogCopyAction(viewer);
248:                 clearAction = new Action(Messages.VdmDebugLogView_clear)
249:                 {
250:                         public void run()
251:                         {
252:                                 synchronized (items)
253:                                 {
254:                                         items.clear();
255:                                 }
256:                                 viewer.refresh();
257:                         }
258:                 };
259:
260:                 executionFilterAction = new Action("Execution filter", SWT.TOGGLE)
261:                 {
262:                         public void run()
263:                         {
264:                                 synchronized (viewer)
265:                                 {
266:                                         List<ViewerFilter> filters = new Vector<ViewerFilter>();
267:                                         filters.addAll(Arrays.asList(viewer.getFilters()));
268:                                         if (executionFilterAction.isChecked())
269:                                         {
270:                                                 filters.add(new VdmDebugLogExecutionFilter());
271:                                         } else
272:                                         {
273:                                                 for (int i = 0; i < filters.size(); i++)
274:                                                 {
275:                                                         if (filters.get(i) instanceof VdmDebugLogExecutionFilter)
276:                                                         {
277:                                                                 filters.remove(i);
278:                                                                 break;
279:                                                         }
280:                                                 }
281:                                         }
282:                                         viewer.setFilters(filters.toArray(new ViewerFilter[filters.size()]));
283:                                         viewer.refresh();
284:                                 }
285:                         }
286:                 };
287:         }
288:
289:         private void createMenu()
290:         {
291:                 IMenuManager manager = getViewSite().getActionBars().getMenuManager();
292:                 manager.add(copyAction);
293:                 manager.add(clearAction);
294:                 manager.add(executionFilterAction);
295:         }
296:
297:         private void createToolbar()
298:         {
299:                 IToolBarManager manager = getViewSite().getActionBars().getToolBarManager();
300:                 manager.add(copyAction);
301:                 manager.add(clearAction);
302:         }
303:
304:         private void createContextMenu()
305:         {
306:                 // Create menu manager.
307:                 MenuManager menuManager = new MenuManager();
308:                 menuManager.setRemoveAllWhenShown(true);
309:                 menuManager.addMenuListener(new IMenuListener()
310:                 {
311:                         public void menuAboutToShow(IMenuManager manager)
312:                         {
313:                                 fillContextMenu(manager);
314:                         }
315:                 });
316:
317:                 // Create menu.
318:                 Menu menu = menuManager.createContextMenu(viewer.getControl());
319:                 viewer.getControl().setMenu(menu);
320:
321:                 // Register menu for extension.
322:                 getSite().registerContextMenu(menuManager, viewer);
323:         }
324:
325:         private void fillContextMenu(IMenuManager manager)
326:         {
327:                 manager.add(copyAction);
328:                 manager.add(clearAction);
329:                 manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
330:         }
331:
332: }