Advertisement
ChristophX86

AutoJava

May 5th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.67 KB | None | 0 0
  1. //
  2. // AutoJava: AutoIt-like Scripting in Java
  3. // http://pastebin.com/u/ChristophX86
  4. //
  5.  
  6. import javax.swing.*;
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. import java.lang.reflect.Method;
  10.  
  11. public abstract class AutoIt
  12. {
  13.     public static class GUI extends JFrame
  14.     {
  15.         public GUI(String title)
  16.         {
  17.             super(title);
  18.         }
  19.     }
  20.     public static class Button extends JButton
  21.     {
  22.         public Object __obj;
  23.         public String __method;
  24.  
  25.         public Button(String text)
  26.         {
  27.             super(text);
  28.         }
  29.     }
  30.     public static class Input extends JTextField
  31.     {
  32.         public Input(String text)
  33.         {
  34.             super(text);
  35.         }
  36.     }
  37.     public static class Edit extends JTextArea
  38.     {
  39.         public Edit(String text)
  40.         {
  41.             super(text);
  42.         }
  43.     }
  44.     public static class Label extends JLabel
  45.     {
  46.         public Label(String text)
  47.         {
  48.             super(text);
  49.         }
  50.     }
  51.     public static class Progress extends JProgressBar
  52.     {
  53.         public Progress()
  54.         {
  55.             super(0, 100);
  56.         }
  57.     }
  58.  
  59.     /** Create a GUI window. */
  60.     public static GUI GUICreate(String title, int width, int height)
  61.     {
  62.         GUI GUI = new GUI(title);
  63.         GUI.getContentPane().setLayout(null);
  64.         GUI.setSize(width, height);
  65.         GUI.setResizable(false);
  66.         GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  67.         return GUI;
  68.     }
  69.     /** Create a GUI window. */
  70.     public static GUI GUICreate(String title, int width, int height, int left, int top)
  71.     {
  72.         GUI GUI = new GUI(title);
  73.         GUI.getContentPane().setLayout(null);
  74.         GUI.setSize(width, height);
  75.         GUI.setResizable(false);
  76.         GUI.setLocation(left, top);
  77.         GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  78.         return GUI;
  79.     }
  80.  
  81.     /** Creates a Button control for the GUI. */
  82.     public static Button GUICtrlCreateButton(String text, int left, int top, int width, int height, GUI GUI)
  83.     {
  84.         Button Button = new Button(text);
  85.         Button.setBounds(left, top, width, height);
  86.         GUI.getContentPane().add(Button);
  87.         return Button;
  88.     }
  89.     /** Creates an Input control for the GUI. */
  90.     public static Input GUICtrlCreateInput(String text, int left, int top, int width, int height, GUI GUI)
  91.     {
  92.         Input Input = new Input(text);
  93.         Input.setBounds(left, top, width, height);
  94.         GUI.getContentPane().add(Input);
  95.         return Input;
  96.     }
  97.     /** Creates an Edit control for the GUI. */
  98.     public static Edit GUICtrlCreateEdit(String text, int left, int top, int width, int height, GUI GUI)
  99.     {
  100.         Edit Edit = new Edit(text);
  101.         Edit.setBounds(left, top, width, height);
  102.         GUI.getContentPane().add(Edit);
  103.         return Edit;
  104.     }
  105.     /** Creates a static Label control for the GUI. */
  106.     public static Label GUICtrlCreateLabel(String text, int left, int top, int width, int height, GUI GUI)
  107.     {
  108.         Label Label = new Label(text);
  109.         Label.setBounds(left, top, width, height);
  110.         GUI.getContentPane().add(Label);
  111.         return Label;
  112.     }
  113.     /** Creates a Progress control for the GUI. */
  114.     public static Progress GUICtrlCreateProgress(int left, int top, int width, int height, GUI GUI)
  115.     {
  116.         Progress Progress = new Progress();
  117.         Progress.setBounds(left, top, width, height);
  118.         GUI.getContentPane().add(Progress);
  119.         return Progress;
  120.     }
  121.  
  122.     public static void GUISetState(boolean visible, GUI GUI)
  123.     {
  124.         GUI.setVisible(visible);
  125.     }
  126.  
  127.     /**
  128.     * Ruft eine bestimmte Methode eines Objekts anhand ihres Namens ohne Parameter auf.
  129.     * @author Carsten S.
  130.     * @param obj das Objekt, dessen Methode aufgerufen werden soll
  131.     * @param methodName der Name der Methode
  132.     * @return true bei Erfolg, sonst false.
  133.     */
  134.     private static boolean callNamedMethod(Object obj, String methodName)
  135.     {
  136.         try {
  137.             Method method = obj.getClass().getDeclaredMethod(methodName, (Class<?>[]) null);
  138.             method.setAccessible(true);
  139.             method.invoke(obj, (Object[]) null);
  140.         } catch (Exception e) { return false; }
  141.         return true;
  142.     }
  143.     /** Defines a user-defined function to be called when a control is clicked. */
  144.     public static void GUICtrlSetOnEvent(final Button controlID, Object obj, String method)
  145.     {
  146.         controlID.__obj = obj;
  147.         controlID.__method = method;
  148.         controlID.addActionListener(
  149.             new ActionListener()
  150.             {
  151.                 public void actionPerformed(ActionEvent arg0)
  152.                 {
  153.                     callNamedMethod(controlID.__obj, controlID.__method);
  154.                 }
  155.             }
  156.         );
  157.     }
  158.  
  159.     /** Read state or data of a control. */
  160.     public static String GUICtrlRead(Button controlID)
  161.     {
  162.         return controlID.getText();
  163.     }
  164.     public static String GUICtrlRead(Input controlID)
  165.     {
  166.         return controlID.getText();
  167.     }
  168.     public static String GUICtrlRead(Edit controlID)
  169.     {
  170.         return controlID.getText();
  171.     }
  172.     public static String GUICtrlRead(Label controlID)
  173.     {
  174.         return controlID.getText();
  175.     }
  176.     public static int GUICtrlRead(Progress controlID)
  177.     {
  178.         return controlID.getValue();
  179.     }
  180.  
  181.     /** Modifies the data for a control. */
  182.     public static void GUICtrlSetData(Button controlID, String data)
  183.     {
  184.         controlID.setText(data);
  185.     }
  186.     public static void GUICtrlSetData(Input controlID, String data)
  187.     {
  188.         controlID.setText(data);
  189.     }
  190.     public static void GUICtrlSetData(Edit controlID, String data)
  191.     {
  192.         controlID.setText(data);
  193.     }
  194.     public static void GUICtrlSetData(Label controlID, String data)
  195.     {
  196.         controlID.setText(data);
  197.     }
  198.     public static void GUICtrlSetData(Progress controlID, int data)
  199.     {
  200.         controlID.setValue(data);
  201.     }
  202.  
  203.     /** Sets the text color of a control. */
  204.     // GUICtrlSetColor()
  205.  
  206.     /** Writes data to the STDOUT stream. Some text editors can read this stream as can other programs which may be expecting data on this stream. */
  207.     public static void ConsoleWrite(String data)
  208.     {
  209.         System.out.print(data);
  210.     }
  211.  
  212.     /** Returns a handle that can be passed to TimerDiff() to calculate the difference in milliseconds. */
  213.     public static long TimerInit()
  214.     {
  215.         return System.currentTimeMillis();
  216.     }
  217.     /** Returns the difference in time from a previous call to TimerInit(). */
  218.     public static long TimerDiff(long handle)
  219.     {
  220.         return System.currentTimeMillis()-handle;
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement