Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ddeaeb47fc8571441ed644acf5e6db71d052ce84 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*******************************************************************************
 * Copyright (c) 2006 IBM Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
 *******************************************************************************/

package org.eclipse.linuxtools.systemtap.ui.consolelog.structures;

import org.eclipse.linuxtools.systemtap.ui.consolelog.internal.ConsoleLogPlugin;
import org.eclipse.linuxtools.systemtap.ui.editor.SimpleEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;



/**
 * A class push data to a ScriptConsole.
 * @author Ryan Morse
 */
public class ErrorTableDisplay {
	public ErrorTableDisplay(Composite parent, String[] titles) {
		this.titles = titles;
		createControl(parent);
	}
	
	/**
	 * Creates the table for displaying error messages in.
	 * @param parent The container for the new error table.
	 */
	public void createControl(Composite parent) {
		table = new Table(parent, SWT.SINGLE);
		table.setHeaderVisible(true);
		table.getVerticalBar().setVisible(true);
		table.setLinesVisible(true);
		table.addMouseListener(mouseListener);
		
		TableColumn column;
		for(int i = 0; i < titles.length; i++) {
			column = new TableColumn(table, SWT.NONE);
			column.setText(titles[i]);
		}

		updateColumns();
	}

	/**
	 * Clears all items from the table.
	 */
	public void clear() {
		table.getDisplay().syncExec(new Runnable() {
			boolean stop = false;
			public void run() {
				if(stop) return;
				try {
					table.removeAll();
				} catch (Exception e) {
					stop = true;
				}
			}
			
		});
	}
	
	/**
	 * Adds a new row to the table with an error icon.
	 * @param row The pre-divied sections of the error message.
	 */
	public void addRow(final String[] row) {
		addRow(row, ConsoleLogPlugin.getImageDescriptor("icons/views/error_st_obj.gif").createImage());
	}

	/**
	 * Adds a new row to the table.
	 * @param row The pre-divied sections of the error message.
	 * @param img The image to display with the error.
	 */
	public void addRow(final String[] row, final Image img) {
		table.getDisplay().syncExec(new Runnable() {
			boolean stop = false;
			public void run() {
				if(stop) return;
				try {
					item = new TableItem(table, SWT.NULL);
					for(int i=0; i<row.length; i++)
						item.setText(i+1, row[i]);
					item.setImage(img);
					updateColumns();
				} catch (Exception e) {
					stop = true;
				}
			}
			
		});
	}
	
	/**
	 * Updates each of the columns in the table to ensure that the entries all fit
	 * as well as possible.
	 */
	private void updateColumns() {
		TableColumn[] columns = table.getColumns();
		for (int i = 0; i < columns.length; i++) {
			columns[i].pack();
			columns[i].setMoveable(true);
		}
	}
	
	public Control getControl() {
		return table;
	}

	/**
	 * Disposes of all internal references in the class. No method should be called after this.
	 */
	public void dispose() {
		if(null != table && !table.isDisposed()) {
			table.removeMouseListener(mouseListener);
			table.dispose();
			table = null;
		}

		if(titles != null)
			for(int i=0; i<titles.length; i++)
				titles[i] = null;
		titles = null;

		if(null != item)
			item.dispose();
		item = null;
	}
	
	/**
	 * MouseListener that is used to detect when the user clicks on a row in the table.
	 * When clicked it will find the line number the error occured on and then set the
	 * cursor location to that location in the active editor.
	 */
	private final MouseListener mouseListener = new MouseListener() {
		public void mouseDown(MouseEvent me) {}
		public void mouseUp(MouseEvent me) {}

		public void mouseDoubleClick(MouseEvent me) {
			try {
				String location = table.getSelection()[0].getText(4);

				if(location.length() > 0) {
					int line = 0;
					if(location.indexOf(':') > 0) {
						String[] pos = location.split(":");
						line = Integer.parseInt(pos[0]);
					} else
						line = Integer.parseInt(location);
					
					IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
					IEditorPart ed = page.getActiveEditor();

					if(ed instanceof SimpleEditor) {
						SimpleEditor editor = ((SimpleEditor)ed);
						editor.selectLine(line);
						editor.setFocus();
					}
				}
			} catch(Exception e) {}
		}
	};
	
	private Table table;
	private String[] titles;
	private TableItem item;
}

Back to the top