blob: b6a650b42ee6c74a461265e1bc1f4d6671ac9666 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Istvan Rath and Daniel Varro
* 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:
* Istvan Rath - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.frameworkgui.views.console;
import java.lang.reflect.Field;
import org.eclipse.jface.action.Action;
/**
* Super reflective toggle action class to be used for the console view.
* @author istvan rath
*
*/
public class ToggleAction extends Action {
public ToggleAction(ConsoleView v, String text, String attrName)
{
cv=v;
targetName=attrName;
initField();
setChecked(getCurrentValue());
setText(text);
this.setToolTipText("Toggle on/off");
// this.setImageDescriptor(FrameworkGUIPlugin.getImageDescriptor("icons/buffer_on.png"));
// this.setDisabledImageDescriptor(FrameworkGUIPlugin.getImageDescriptor("icons/buffer_off.png"));
}
ConsoleView cv;
String targetName;
Field f;
private void initField() {
try {
f = cv.getClass().getDeclaredField(targetName);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
private Boolean getCurrentValue() {
try {
return f.getBoolean(cv);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return false;
}
@Override
public void run()
{
boolean b = getCurrentValue();
try {
f.setBoolean(cv, new Boolean(! b));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
b = getCurrentValue();
if (b)
{
setChecked(true);
setToolTipText("On");
}
else
{
setChecked(false);
setToolTipText("Off");
}
cv.getViewSite().getActionBars().getToolBarManager().update(true);
}
}