aboutsummaryrefslogtreecommitdiffstats
blob: 9a095f7d1cf9a5d4d663ca8dab7677ae390b77e6 (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/********************************************************************************
 * Copyright (c) 2008 Motorola Inc. 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
 *
 * Initial Contributor:
 * Otávio Ferranti (Motorola)
 *
 * Contributors:
 * {Name} (company) - description of contribution.
 ********************************************************************************/

package org.eclipse.tml.linuxtools.tools.memorymap;

import org.eclipse.jface.viewers.*;
import org.eclipse.jface.action.*;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.SWT;

import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;

import org.eclipse.tml.linuxtools.LinuxToolsPlugin;
import org.eclipse.tml.linuxtools.tools.IListener;
import org.eclipse.tml.linuxtools.tools.INotifier;
import org.eclipse.tml.linuxtools.tools.ITool;
import org.eclipse.tml.linuxtools.ui.IToolViewPart;
import org.eclipse.tml.linuxtools.ui.DialogLogin;
import org.eclipse.tml.linuxtools.ui.ViewActionDisconnect;
import org.eclipse.tml.linuxtools.ui.ViewActionConnect;
import org.eclipse.tml.linuxtools.ui.ViewActionRefresh;
import org.eclipse.tml.linuxtools.network.IConstants.EventCode;
import org.eclipse.tml.linuxtools.network.IConstants.OperationCode;

/**
 * @author Otávio Ferranti
 */
public class MemoryMapView extends ViewPart implements IToolViewPart, IListener {
	
	private class AddressSorter extends ViewerSorter {
	
		public int compare(Viewer viewer, Object e1, Object e2) {
			int result = 0;
			try {
				int a = new Integer(((String[]) e1)[MAX_COLUMNS - 1]).intValue();
				int b = new Integer(((String[]) e2)[MAX_COLUMNS - 1]).intValue();
				if (a > b) {
					result = 1;
				} else if (a < b) {
					result = -1;
				};
			}
			catch (NumberFormatException nfe) {
				//TODO: Nothing ?
			}
			return result;
		}
	}
	final private String COL_LABEL_ADDRESS_START = Messages.MemoryMapView_Col_Label_Address_Start;
	final private String COL_LABEL_ADDRESS_END = Messages.MemoryMapView_Col_Label_Address_End;
	final private String COL_LABEL_REGION = Messages.MemoryMapView_Col_label_Region;
	
	final private int MAX_COLUMNS = 4;
	
	private ITool tool = null;
	private TableViewer viewer;

	private Action refreshAction;
	private Action disconnectAction;
	private Action connectAction;

	private IPartListener partActivationListener = new IPartListener() {
		public void partActivated(IWorkbenchPart part) {
		}
		
		public void partBroughtToTop(IWorkbenchPart part) {
		}
			 
		public void partClosed(IWorkbenchPart part) {
			if (MemoryMapView.this.getSite().getPart() == part) {
				ITool tool = MemoryMapView.this.getTool();
				if (null != tool) {
					tool.disconnect();
				}
			}
		}
		
		public void partDeactivated(IWorkbenchPart part) {
		}
			 
		public void partOpened(IWorkbenchPart part) {
		}
	};
	
	/**
	 * The constructor.
	 */
	public MemoryMapView() {
	}

	/**
	 * This is a callback that will allow us
	 * to create the viewer and initialize it.
	 */
	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createPartControl(Composite parent) {
		
		viewer = new TableViewer(parent, SWT.FULL_SELECTION |
				SWT.H_SCROLL | SWT.V_SCROLL);
		viewer.setContentProvider(new MemoryMapViewContentProvider());
		viewer.setLabelProvider(new MemoryMapVViewLabelProvider());
		viewer.setSorter(new AddressSorter());

		Table table = viewer.getTable();
		table.setHeaderVisible(true);
		
		new TableColumn(table, SWT.LEFT).setText(COL_LABEL_ADDRESS_START);
		new TableColumn(table, SWT.LEFT).setText(COL_LABEL_ADDRESS_END);
		new TableColumn(table, SWT.LEFT).setText(COL_LABEL_REGION);
		
		refresh();
		resize();
		
		makeActions();
		// hookDoubleClickAction();
		addToToolBar();
		
		getViewSite()
			.getWorkbenchWindow()
			.getPartService()
			.addPartListener(partActivationListener);
		
		setConnectEnabled(true);
		refreshAction.setEnabled(false);
	}

	private void addToToolBar() {
		IActionBars actionBars = getViewSite().getActionBars();
		IToolBarManager toolBarMmanager = actionBars.getToolBarManager();
		toolBarMmanager.add(refreshAction);
		toolBarMmanager.add(disconnectAction);
		toolBarMmanager.add(connectAction);
	}
	
	private void makeActions() {
		
		refreshAction = new Action() {
			public void run() {
				IViewActionDelegate delegate = new ViewActionRefresh();
		        delegate.init(MemoryMapView.this);
	            delegate.run(this);
	            this.setEnabled(false);
			}
		};
		refreshAction.setToolTipText(Messages.MemoryMapView_Action_Refresh);
		refreshAction.setImageDescriptor(
				LinuxToolsPlugin.getDefault().getImageDescriptor(LinuxToolsPlugin.ICON_REFRESH));
		
		disconnectAction = new Action() {
			public void run() {
				IViewActionDelegate delegate = new ViewActionDisconnect();
		        delegate.init(MemoryMapView.this);
	            delegate.run(this);
			}
		};
		disconnectAction.setToolTipText(Messages.MemoryMapView_Action_Disconnect);
		disconnectAction.setImageDescriptor(
				LinuxToolsPlugin.getDefault().getImageDescriptor(LinuxToolsPlugin.ICON_DISCONNECT));
		
		connectAction = new Action() {
			public void run() {
				IViewActionDelegate delegate = new ViewActionConnect();
		        delegate.init(MemoryMapView.this);
	            delegate.run(this);
			}
		};
		connectAction.setToolTipText(Messages.MemoryMapView_Action_Connect);
		connectAction.setImageDescriptor(
				LinuxToolsPlugin.getDefault().getImageDescriptor(LinuxToolsPlugin.ICON_CONNECT));
		
	}
	
	private void setConnectEnabled(boolean bool) {
		connectAction.setEnabled(bool);
		disconnectAction.setEnabled(!bool);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.tml.linuxtools.ui.IToolView#getTool()
	 */
	public ITool getTool() {
		if(null == tool) {
			tool = new MemoryMapTool();
			tool.addListener(this);
		}
		return tool;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.tml.linuxtools.network.IListener#notify(org.eclipse.tml.linuxtools.network.INotifier, org.eclipse.tml.linuxtools.network.IConstants.EventCode, java.lang.Object)
	 */
	public void notify(INotifier notifier, EventCode event, Object result) {
		if (notifier == tool) {
			final Object finalResult = result;
			final EventCode finalEvent = event;
			final ViewPart finalView = this;
			final ITool finalTool = this.tool;
			
			this.getViewSite().getShell().getDisplay().asyncExec(new Runnable() {
				public void run() {
					switch(finalEvent) {
						case EVT_TOOL_REFRESH_VIEW:
							viewer.setInput(finalResult);
							refreshAction.setEnabled(true);
		        			refresh();
		        			resize();
						break;
						case EVT_TOOL_CONNECT_FINISHED:
						case EVT_TOOL_LOGIN_FINISHED:
							switch ((OperationCode)finalResult) {
								case SUCCESS: 
									setConnectEnabled(false);
									refreshAction.setEnabled(true);
								break;
								case LOGIN_REQUIRED: {
									final DialogLogin dialog = new DialogLogin(
											finalView.getViewSite().getShell(),
											finalTool, false);
									dialog.open();
								}
								break;
								case LOGIN_FAILED: {
									final DialogLogin dialog = new DialogLogin(
											finalView.getViewSite().getShell(),
											finalTool, true);
									dialog.open();
								}
								break;
							}
						break;
						case EVT_TOOL_DISCONNECT_FINISHED:
							setConnectEnabled(true);
							refreshAction.setEnabled(false);
						break;
					}
				}
			});
		}
	}
	
	/**
	 * 
	 */
	public void refresh() {
		viewer.refresh();
	}

	/**
	 * 
	 */
	public void resize() {
		Table table = viewer.getTable();
	    for (int i = 0, n = table.getColumnCount(); i < n; i++) {
	    	table.getColumn(i).pack();
	    }
	}
	
	/**
	 * @param data
	 */
	public void setData (Object data) {
		viewer.setInput(data);
	}
	
	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		viewer.getControl().setFocus();
	}
}