blob: 59f00c857b0804cc004a1c5d674c8bbb5a2cff97 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2008 Wind River Systems and others.
* 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:
* Wind River Systems - initial API and implementation
*******************************************************************************/
package org.eclipse.debug.examples.ui.pda.views;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IValue;
import org.eclipse.debug.examples.core.pda.model.PDADebugTarget;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
/**
* Pops a selected value off the data stack. The selection does <b>not</b> have to be
* the top element on the stack.
*/
public class PopHandler extends AbstractDataStackViewHandler {
protected void doExecute(DataStackView view, PDADebugTarget target, ISelection selection) throws ExecutionException {
TreeViewer viewer = (TreeViewer)view.getViewer();
Object popee = selection instanceof IStructuredSelection
? ((IStructuredSelection)selection).getFirstElement() : null;
if (popee != null) {
try {
IValue[] stack = target.getDataStack();
List restore = new ArrayList();
for (int i = 0; i < stack.length; i++) {
Object value = stack[i];
if (popee.equals(value)) {
// pop & stop
target.pop();
break;
} else {
// remember value to push back on
restore.add(target.pop());
}
}
while (!restore.isEmpty()) {
IValue value = (IValue) restore.remove(restore.size() - 1);
target.push(value.getValueString());
}
} catch (DebugException e) {
throw new ExecutionException("Failed to execute push command", e);
}
viewer.refresh();
}
}
}