Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 597ee11edfe6bb52e2132ebae1eec54b6b0237ce (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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*******************************************************************************
 * Copyright (c) 2009, 2012 Texas Instruments.
 * 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:
 *     Texas Instruments - Initial API and implementation
 *     Patrick Chuong (Texas Instruments) - Bug fix (329682)
 *******************************************************************************/
package org.eclipse.cdt.dsf.debug.internal.ui.disassembly.actions;

import org.eclipse.cdt.dsf.internal.ui.DsfUIPlugin;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.plugin.AbstractUIPlugin;

public class AddressBarContributionItem extends ContributionItem {
	private Combo addressBox;
	private IAction action;
	private ToolItem item;
	private int width;
	private String initialText;
	private String lastText;
	private Image warningImage = null;
	private Label warningLabel = null;
	private String warningText = null;

	/**
	 * Use this constructor to create an AddressBarContributionItem.
	 * 
	 * @param action
	 *            a contribution action.
	 */
	public AddressBarContributionItem(IAction action) {
		this.action = action;
	}

	/**
	 * After constructing this object, call this method to create an address
	 * box.
	 * 
	 * @param parent
	 *            a ToolBar object. Can be obtain with the getControl() method
	 *            in the ToolBarManager class.
	 * @param width
	 *            the width of the combo box.
	 * @param initialText
	 *            the initial text displayed in the combo box (e.g. 'Enter
	 *            address here')
	 * @param warningText
	 *            the tooltip of the warning label if it ever becomes visible.
	 *            May be null.
	 */
	public void createAddressBox(ToolBar parent, int width, String initialText,
			String warningText) {
		this.width = width;
		this.initialText = initialText;
		this.lastText = initialText;
		this.warningText = warningText;
		fill(parent, 0);

		parent.addDisposeListener(new DisposeListener() {

			@Override
			public void widgetDisposed(DisposeEvent e) {
				if (warningImage != null)
					warningImage.dispose();
			}
		});
	}

	/**
	 * Don't call this method from the client, use the createAddressBox method.
	 */
	@Override
	public void fill(ToolBar parent, int index) {
		item = new ToolItem(parent, SWT.SEPARATOR);
		Control box = internalCreateAddressBox(parent);
		item.setControl(box);
		item.setWidth(width);
		
		enableAddressBox(action.isEnabled());		
	}

	/**
	 * Set the address bar text
	 */
	public void setText(String text) {
		if (addressBox != null)
			addressBox.setText(text);
	}

	/**
	 * Get the address bar text
	 * 
	 * @return The text in the address bar.
	 */
	public String getText() {
		if (addressBox != null)
			return addressBox.getText();
		else
			return initialText;
	}

	/**
	 * Set the visibility of the warning icon. Should be set to true when there
	 * is a problem jumping to the specified address; false otherwise
	 * 
	 * @param visible
	 *            True for visible, false for hidden.
	 */
	public void setWarningIconVisible(boolean visible) {
		if (warningLabel == null)
			return;
		warningLabel.setVisible(visible);
	}

	/**
	 * Return whether the warning icon is visible or not.
	 * 
	 * @return True if visible, otherwise false.
	 */
	public boolean isWarningIconVisible() {
		if (warningLabel == null)
			return false;
		return warningLabel.isVisible();
	}

	/**
	 * Enable the address combo box.
	 * 
	 * @param enable
	 *            true to enable, else false.
	 */
	public void enableAddressBox(boolean enable) {
		if (addressBox != null) {
			item.setEnabled(enable);
			addressBox.setEnabled(enable);
		}
	}

	/**
	 * Creates the combo box and add it to the toolbar.
	 * 
	 * @param parent
	 *            the parent, toolbar.
	 * @return the combo box address control.
	 */
	private Control internalCreateAddressBox(Composite parent) {
		Composite top = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.marginHeight = layout.marginWidth = 0;
		layout.verticalSpacing = layout.horizontalSpacing = 2;
		layout.numColumns = 3;
		top.setLayout(layout);

		warningLabel = new Label(top, SWT.NONE);

		warningImage = AbstractUIPlugin
				.imageDescriptorFromPlugin(
						DsfUIPlugin.PLUGIN_ID, "icons/address_warning.gif").createImage(); //$NON-NLS-1$
		warningLabel.setImage(warningImage);
		warningLabel.setToolTipText(warningText);
		setWarningIconVisible(false);

		addressBox = new Combo(top, SWT.DROP_DOWN);

		addressBox.setText(initialText);
		action.setText(initialText);

		addressBox.addFocusListener(new FocusListener() {
			// [nmehregani]: Support Ctrl+C in address bar
			KeyListener keyListener = new KeyListener() {

				@Override
				public void keyPressed(KeyEvent e) {
					/* Not used */
				}

				@Override
				public void keyReleased(KeyEvent e) {
					if (e.stateMask == SWT.CTRL
							&& (((char) e.keyCode) == 'c' || ((char) e.keyCode) == 'C')) {
						String selection = null;

						Point selectionPoint = addressBox.getSelection();
						if (selectionPoint.x == selectionPoint.y)
							return;

						selection = addressBox.getText().substring(
								selectionPoint.x, selectionPoint.y);

						if ((selection != null)
								&& (!(selection.trim().length() == 0))) {
							Clipboard clipboard = null;
							try {
								clipboard = new Clipboard(addressBox
										.getDisplay());
								clipboard.setContents(
										new Object[] { selection },
										new Transfer[] { TextTransfer
												.getInstance() });
							} finally {
								if (clipboard != null)
									clipboard.dispose();
							}
						}
					}
				}

			};

			@Override
			public void focusGained(FocusEvent e) {
				// [nmehregani] bugzilla 297387: 'Home' shouldn't jump to PC address when focus is on location combo box
				if (action instanceof JumpToAddressAction) 
					((JumpToAddressAction)action).deactivateDisassemblyContext();				
				// end 297387
				
				lastText = addressBox.getText();
				
				// Erase the guide text when the focus is gained.
				if (lastText.trim().equals(initialText))
					addressBox.setText(""); //$NON-NLS-1$

				// [nmehregani]: Support Ctrl+C in address bar
				addressBox.addKeyListener(keyListener);
			}

			@Override
			public void focusLost(FocusEvent e) {
				// [nmehregani] bugzilla 297387: 'Home' shouldn't jump to PC address when focus is on location combo box
				if (action instanceof JumpToAddressAction) 
					((JumpToAddressAction)action).activateDisassemblyContext();				
				// end 297387
				
				// Re-insert the last text when the focus is lost and the text
				// field is empty.
				if (addressBox.getText().trim().length() == 0)
					addressBox.setText(lastText);

				// [nmehregani]: Support Ctrl+C in address bar
				addressBox.removeKeyListener(keyListener);
			}
		});

		addressBox.addTraverseListener(new TraverseListener() {

			@Override
			public void keyTraversed(TraverseEvent e) {
				if (e.detail == SWT.TRAVERSE_RETURN) {
					String addressBoxStr = addressBox.getText();

					// don't accept the initial text
					if (addressBoxStr.equals(initialText))
						return;

					Event event = new Event();
					event.data = addressBoxStr;
					action.runWithEvent(event);

					boolean bExist = false;
					for (int i = 0; i < addressBox.getItemCount(); ++i) {
						String itemText = addressBox.getItem(i);
						if (itemText.equals(addressBoxStr)) {
							bExist = true;
							break;
						}
					}

					if ((!bExist) && (addressBox.getText() != null)
							&& (!(addressBox.getText().trim().length() == 0)))
						addressBox.add(addressBox.getText());
				}
			}

		});

		addressBox.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent selectionEvent) {
				int selection = addressBox.getSelectionIndex();
				if (selection >= 0) {
					String addressBoxStr = addressBox.getItem(selection);
					Event event = new Event();
					event.data = addressBoxStr;
					action.runWithEvent(event);
				}
			}
		});

		addressBox.setLayoutData(new GridData(GridData.FILL,
				GridData.BEGINNING, true, false));
		return top;
	}

	// [nmehregani]: Support Ctrl+C in address bar
	public void clearSelection() {
		addressBox.clearSelection();
	}
}

Back to the top