package touchkey.actions;
import java.awt.AWTException;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
/**
* Our sample action implements workbench action delegate.
* The action proxy will be created by the workbench and
* shown in the UI. When the user tries to use the action,
* this delegate will be created and execution will be
* delegated to it.
* @see IWorkbenchWindowActionDelegate
*/
public class TouchKey implements IWorkbenchWindowActionDelegate {
private Shell shell = null;
private IWorkbenchWindow window;
Point originalPosition = null;
private Robot robot= null;
/**
* The constructor.
*/
public TouchKey() {
}
/**
* The action has been activated. The argument of the
* method represents the 'real' action sitting
* in the workbench UI.
* @see IWorkbenchWindowActionDelegate#run
*/
public void run(IAction action) {
// MyDialog myDialog = new MyDialog(window.getShell());
// myDialog.create();
//// myDialog.getShell().setAlpha(200);
// myDialog.open();
if(shell != null){
return;
}
shell = new Shell(window.getShell(), SWT.MODELESS);
shell.addMouseListener(new MouseListener() {
@Override
public void mouseUp(MouseEvent e) {
originalPosition = null;
}
@Override
public void mouseDown(MouseEvent e) {
originalPosition = new Point(e.x, e.y);
}
@Override
public void mouseDoubleClick(MouseEvent e) {
if(shell.getDisplay() != null){
shell.close();
shell = null;
}
}
});
shell.addMouseMoveListener(new MouseMoveListener() {
@Override
public void mouseMove(MouseEvent e) {
if(originalPosition == null){
return;
}
// Point point = window.getShell().getDisplay().map(shell, null, e.x, e.y);
// shell.setLocation(point.x - originalPosition.x, point.y - originalPosition.y);
Point dp = shell.toDisplay(e.x, e.y);
int x = dp.x - 1 - originalPosition.x;
int y = dp.y - 1 - originalPosition.y;
Rectangle shellBounds = shell.getBounds();
Rectangle bounds = window.getShell().getBounds();
Point windowXy = window.getShell().toDisplay(bounds.x, bounds.y);
x = x < windowXy.x ? windowXy.x : x;
y = y < windowXy.y ? windowXy.y : y;
int m = windowXy.x + bounds.width - shellBounds.width;
int n = windowXy.y + bounds.height - shellBounds.height;
x = x > m ? m : x;
y = y > n ? n : y;
shell.setLocation(x, y);
}
});
Button button = new Button(shell, SWT.PUSH);
button.setBounds(10, 10, 400, 60);
// button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false,false));
button.setText( "Open Declaration F3");
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screen = env.getDefaultScreenDevice();
try {
robot = new Robot(screen);
} catch (AWTException ex) {
ex.printStackTrace();
}
shell.getParent().setFocus();//�趨Ϊ���㣬�����������¼�.
robot.keyPress(KeyEvent.VK_F3);
robot.keyRelease(KeyEvent.VK_F3);
}
});
Button button1 = new Button(shell, SWT.PUSH);
button1.setBounds(10, 80, 400, 60);
button1.setText("Open Type Hierarchy F4");
button1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screen = env.getDefaultScreenDevice();
try {
robot = new Robot(screen);
} catch (AWTException ex) {
ex.printStackTrace();
}
shell.getParent().setFocus();//�趨Ϊ���㣬�����������¼�.
robot.keyPress(KeyEvent.VK_F4);
robot.keyRelease(KeyEvent.VK_F4);
}
});
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("Open Call Hierarchy Ctrl+Alt+H");
button2.setBounds(10, 150, 400, 60);
button2.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screen = env.getDefaultScreenDevice();
try {
robot = new Robot(screen);
} catch (AWTException ex) {
ex.printStackTrace();
}
shell.getParent().setFocus();//�趨Ϊ���㣬�����������¼�.
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_H);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_H);
}
});
Button button3 = new Button(shell, SWT.PUSH);
button3.setBounds(10, 220, 400, 60);
button3.setText("Quick Outline Ctrl+O");
button3.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screen = env.getDefaultScreenDevice();
try {
robot = new Robot(screen);
} catch (AWTException ex) {
ex.printStackTrace();
}
shell.getParent().setFocus();//�趨Ϊ���㣬�����������¼�.
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_O);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_O);
}
});
Button button4 = new Button(shell, SWT.PUSH);
button4.setBounds(10, 290, 400, 60);
button4.setText("Quick Type Hierarchy Ctrl+T");
button4.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screen = env.getDefaultScreenDevice();
try {
robot = new Robot(screen);
} catch (AWTException ex) {
ex.printStackTrace();
}
shell.getParent().setFocus();//�趨Ϊ���㣬�����������¼�.
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_T);
}
});
Button button5 = new Button(shell, SWT.PUSH);
button5.setBounds(10, 360, 400, 60);
button5.setText("Back to Alt+Left");
button5.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screen = env.getDefaultScreenDevice();
try {
robot = new Robot(screen);
} catch (AWTException ex) {
ex.printStackTrace();
}
shell.getParent().setFocus();//�趨Ϊ���㣬�����������¼�.
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_LEFT);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_LEFT);
}
});
Button button6 = new Button(shell, SWT.PUSH);
button6.setBounds(10, 430, 400, 60);
button6.setText("Forward to Alt+Right");
button6.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screen = env.getDefaultScreenDevice();
try {
robot = new Robot(screen);
} catch (AWTException ex) {
ex.printStackTrace();
}
shell.getParent().setFocus();//�趨Ϊ���㣬�����������¼�.
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_RIGHT);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_RIGHT);
}
});
Button button7 = new Button(shell, SWT.PUSH);
button7.setBounds(10, 500, 400, 60);
button7.setText("Escape Return");
button7.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screen = env.getDefaultScreenDevice();
try {
robot = new Robot(screen);
} catch (AWTException ex) {
ex.printStackTrace();
}
shell.getParent().setFocus();//�趨Ϊ���㣬�����������¼�.
robot.keyPress(KeyEvent.VK_ESCAPE);
robot.keyRelease(KeyEvent.VK_ESCAPE);
}
});
final Scale scale = new Scale(shell, SWT.HORIZONTAL);
scale.setBounds(10, 570, 400, 80);
scale.setMaximum(255);
scale.setMinimum(50);
scale.setPageIncrement(10);
scale.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
int perspectiveValue = scale.getMaximum() - scale.getSelection() + scale.getMinimum();
shell.setAlpha(perspectiveValue);
}
});
shell.setSize(420, 700);
shell.open();
}
/**
* Selection in the workbench has been changed. We
* can change the state of the 'real' action here
* if we want, but this can only happen after
* the delegate has been created.
* @see IWorkbenchWindowActionDelegate#selectionChanged
*/
public void selectionChanged(IAction action, ISelection selection) {
}
/**
* We can use this method to dispose of any system
* resources we previously allocated.
* @see IWorkbenchWindowActionDelegate#dispose
*/
public void dispose() {
}
/**
* We will cache window object in order to
* be able to provide parent shell for the message dialog.
* @see IWorkbenchWindowActionDelegate#init
*/
public void init(IWorkbenchWindow window) {
this.window = window;
}
}