Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5e2a531e08fa0687a4102715a415d33fcfc003a4 (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) 2004, 2012 QNX Software Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * QNX Software Systems - Initial API and implementation
 * Nokia - adapt to new command framework
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.ui.actions;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.cdt.debug.core.model.ICastToType;
import org.eclipse.cdt.debug.internal.ui.CDebugImages;
import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.ui.IDebugView;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.ISources;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * The delegate of the "Cast To Type" action.
 */
public class CastToTypeActionHandler extends AbstractHandler {

	static protected class CastToTypeInputValidator implements IInputValidator {

		public CastToTypeInputValidator() {
		}

		@Override
		public String isValid(String newText) {
			if (newText.trim().length() == 0) {
				return ActionMessages.getString("CastToTypeActionDelegate.0"); //$NON-NLS-1$
			}
			return null;
		}
	}

	protected class CastToTypeDialog extends InputDialog {

		public CastToTypeDialog(Shell parentShell, String initialValue) {
			super(parentShell, ActionMessages.getString("CastToTypeActionDelegate.1"), //$NON-NLS-1$
					ActionMessages.getString("CastToTypeActionDelegate.2"), //$NON-NLS-1$
					initialValue, new CastToTypeInputValidator());
		}

		/*
		 * (non-Javadoc)
		 *
		 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
		 */
		@Override
		protected void configureShell(Shell shell) {
			super.configureShell(shell);
			shell.setImage(CDebugImages.get(CDebugImages.IMG_LCL_CAST_TO_TYPE));
		}
	}

	private ICastToType[] fCastableItems = new ICastToType[0];

	private IStatus fStatus = null;

	private IWorkbenchPart fTargetPart;

	public CastToTypeActionHandler() {
		super();
	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		fTargetPart = HandlerUtil.getActivePartChecked(event);

		if (getCastToType() == null || getCastToType().length == 0)
			return null;

		BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {

			@Override
			public void run() {
				try {
					doAction(getCastToType());
					setStatus(null);
				} catch (DebugException e) {
					setStatus(e.getStatus());
				}
			}
		});
		if (getStatus() != null && !getStatus().isOK()) {
			IWorkbenchWindow window = CDebugUIPlugin.getActiveWorkbenchWindow();
			if (window != null) {
				CDebugUIPlugin.errorDialog(ActionMessages.getString("CastToTypeActionDelegate.3"), getStatus()); //$NON-NLS-1$
			} else {
				CDebugUIPlugin.log(getStatus());
			}
		}

		return null;
	}

	@Override
	public void setEnabled(Object evaluationContext) {
		ICastToType[] castableItems = getCastToType(evaluationContext);
		setBaseEnabled(castableItems.length > 0);
		setCastToType(castableItems);
	}

	private ICastToType[] getCastToType(Object evaluationContext) {
		List<ICastToType> castableItems = new ArrayList<>();
		if (evaluationContext instanceof IEvaluationContext) {
			Object s = ((IEvaluationContext) evaluationContext).getVariable(ISources.ACTIVE_MENU_SELECTION_NAME);
			if (s instanceof IStructuredSelection) {
				Iterator<?> iter = ((IStructuredSelection) s).iterator();
				while (iter.hasNext()) {
					Object element = DebugPlugin.getAdapter(iter.next(), ICastToType.class);
					if (element instanceof ICastToType) {
						if (((ICastToType) element).canCast()) {
							castableItems.add((ICastToType) element);
						}
					}
				}
			}
		}
		return castableItems.toArray(new ICastToType[castableItems.size()]);
	}

	protected ICastToType[] getCastToType() {
		return fCastableItems;
	}

	protected void setCastToType(ICastToType[] castableItems) {
		fCastableItems = castableItems;
	}

	public IStatus getStatus() {
		return fStatus;
	}

	public void setStatus(IStatus status) {
		fStatus = status;
	}

	protected void doAction(ICastToType[] castableItems) throws DebugException {
		String currentType = castableItems[0].getCurrentType().trim();
		CastToTypeDialog dialog = new CastToTypeDialog(CDebugUIPlugin.getActiveWorkbenchShell(), currentType);
		if (dialog.open() == Window.OK) {
			String newType = dialog.getValue().trim();
			for (ICastToType castableItem : castableItems) {
				castableItem.cast(newType);
			}
			if (getSelectionProvider() != null)
				getSelectionProvider().setSelection(new StructuredSelection(castableItems));
		}
	}

	private ISelectionProvider getSelectionProvider() {
		return (fTargetPart instanceof IDebugView) ? ((IDebugView) fTargetPart).getViewer() : null;
	}
}

Back to the top