User:TLULbot/jBot/GUI.java

/* * jBot - Java bot framework, for editing a wiki powered by MediaWiki. * Copyright (c) 2009-2010 The last username left and others. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;. * * This framework was based on AmauriceBot, created by Maurice Abraham. * * Contributors: * The last username left - Task framework, GUI * Maurice Abraham - Core utilities *--*/

package jBot;

import javax.swing.*;

import java.util.List; import java.awt.*; import java.awt.event.*; import java.io.IOException;

/** * Default GUI for jBot. * Creating a new instance of this class will automatically load any tasks added statically via the Core class. * @author The last username left */ public class GUI {   /**     * Graphical printer for this GUI. * @author The last username left */   private static class GUIPrinter implements Core.Printer //Class for providing customised input and output from Main {       private JTextArea outTextArea = null; private JTextArea errTextArea = null; private JProgressBar progBar = null; private JLabel progLabel = null; private String progString = "~s"; public GUIPrinter(JTextArea out, JTextArea err, JProgressBar bar, JLabel label) {           outTextArea = out; errTextArea = err; progBar = bar; progLabel = label; }       @Override public void errString(String str) { errTextArea.setText(str + "\n" + errTextArea.getText); }       @Override public void outString(String str) { outTextArea.setText(str + "\n" + outTextArea.getText); }       @Override public void setProg(int prog) { progBar.setValue(prog); progLabel.setText(progString.replace("~s", String.valueOf(prog))); }       @Override public void setProgString(String str) { progString = str; }   }

public WikiSession w = null; //So that the Main class and Core can interact directly if needed private JScrollPane scrollPane = null; private final JTextArea text = new JTextArea("--\n" +                                                "jBot - Java bot framework, for editing a wiki powered by MediaWiki.\n" +                                                 "Copyright (c) 2009-2010  The last username left and others.\n" +                                                 "\n" +                                                 "This program is free software: you can redistribute it and/or modify\n" +                                                 "it under the terms of the GNU General Public License as published by\n" +                                                 "the Free Software Foundation, either version 3 of the License, or\n" +                                                 "(at your option) any later version.\n" +                                                 "\n" +                                                 "This program is distributed in the hope that it will be useful,\n" + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" + "GNU General Public License for more details.\n" + "\n" + "You should have received a copy of the GNU General Public License\n" + "along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;.\n" + "\n" + "This framework was based on AmauriceBot, created by Maurice Abraham.\n" + "\n" + "Contributors:\n" + "The last username left - Task framework, GUI\n" + "Maurice Abraham - Core utilities", 1, 1);   private final JLabel progLabel = new JLabel("jBot GUI loaded.");    private final JProgressBar progBar = new JProgressBar;    private final JButton loginButton = new JButton("Log in");    private LoginAction loginActn = null;    private LoginButtonAction loginBtnActn = null;    private Core.Printer p = null;    private boolean isBusy = false;    private void createAndShowGUI    {        try        {            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName);        }        catch (Exception e) { } //Gulp        JFrame.setDefaultLookAndFeelDecorated(false);        JFrame main = new JFrame("jBot v" + Core.version);        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        loginButton.setMnemonic(KeyEvent.VK_L);        p = new GUIPrinter(text, text, progBar, progLabel); //Prepare wiki w.p = p;       //Prepare loginBtnActn JTextField loginBtnActnUserField = new JTextField; JPasswordField loginBtnActnPassField = new JPasswordField; JButton loginBtnActnButton = new JButton("Log in"); JPanel loginBtnActnPanel = new JPanel(new GridLayout(0, 1)); loginBtnActnPanel.add(loginBtnActnUserField); loginBtnActnPanel.add(loginBtnActnPassField); loginBtnActnPanel.add(loginBtnActnButton); loginBtnActnPanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30)); JFrame loginBtnActnWindow = new JFrame("Log in"); loginBtnActnWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); loginBtnActnWindow.getContentPane.add(loginBtnActnPanel, BorderLayout.CENTER); loginBtnActn = new LoginButtonAction(w, p, loginBtnActnWindow, loginButton); loginButton.addActionListener(loginBtnActn); //Prepare loginActn loginActn = new LoginAction(w, p, loginBtnActnUserField, loginBtnActnPassField, loginBtnActnWindow, loginButton, loginBtnActnButton); loginBtnActnButton.addActionListener(loginActn); loginBtnActnPassField.addKeyListener(new KeyListener       {            public void keyPressed(KeyEvent arg0)            {                if (arg0.getKeyCode == KeyEvent.VK_ENTER)                {                    loginActn.actionPerformed(null);                }            }            public void keyReleased(KeyEvent arg0)            { }            public void keyTyped(KeyEvent arg0)            { }        }); //Prepare tasks List&lt;Core.Task&gt; tasks = Core.getTasks; JButton[] buttons = new JButton[tasks.size]; int i = 0; for (Core.Task t : tasks) {           t.setWikiSession(w); buttons[i] = new JButton(t.getTaskName); buttons[i].addActionListener(new ButtonAction(p, t.getTaskCommand, this)); buttons[i++].setPreferredSize(new Dimension(800, 26)); }       //Prepare rest of window text.setEditable(false); text.setWrapStyleWord(true); text.setBackground(Color.BLACK); text.setForeground(Color.GREEN); text.setFont(new Font("Courier New", Font.PLAIN, 16)); progBar.setMinimum(0); progBar.setMaximum(100); scrollPane = new JScrollPane(text, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); loginButton.setPreferredSize(new Dimension(800, 26)); progLabel.setPreferredSize(new Dimension(800, 16)); progBar.setPreferredSize(new Dimension(800, 14)); scrollPane.setPreferredSize(new Dimension(800, 600)); JPanel mainPanel = new JPanel(new GridBagLayout); GridBagConstraints mainConstraints = new GridBagConstraints; mainConstraints.gridheight = 1; mainConstraints.gridwidth = 1; mainConstraints.gridx = 0; mainConstraints.gridy = 0; mainConstraints.weightx = 1.0; mainConstraints.weighty = 1.0; mainConstraints.fill = GridBagConstraints.BOTH; mainPanel.add(loginButton, mainConstraints); for (i = 0; i &lt; buttons.length; i++) {           mainConstraints.gridy++; mainPanel.add(buttons[i], mainConstraints); }       mainConstraints.gridy++; mainPanel.add(progLabel, mainConstraints); mainConstraints.gridy++; mainPanel.add(progBar, mainConstraints); mainConstraints.gridy++; mainPanel.add(scrollPane, mainConstraints); mainPanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30)); main.getContentPane.add(mainPanel, BorderLayout.NORTH); main.pack; main.setLocationRelativeTo(null); //Centre on screen main.setVisible(true); }   public GUI(WikiSession wiki) {       w = wiki; createAndShowGUI; }   private static class LoginButtonAction implements ActionListener //ActionListener handling login button {       private WikiSession w = null; private Core.Printer p = null; private JFrame loginWindow = null; //The frame to display private JButton parentButton = null; public LoginButtonAction(WikiSession wiki, Core.Printer printer, JFrame window, JButton button) {           w = wiki; p = printer; loginWindow = window; parentButton = button; }       @Override public void actionPerformed(ActionEvent e)       { if (w.isLoggedIn) {               try {                   w.logout; p.outString("Logged out successfully."); parentButton.setText("Log in"); }               catch (IOException ex) {                   p.errString("Failed to login: IOException."); }               catch (Core.jBotException ex) {                                       p.errString("Failed to login: Internal error."); }           }            else {               //parentButton.setEnabled(false); loginWindow.setLocation(100, 100); loginWindow.pack; loginWindow.setSize(300, 200); loginWindow.setVisible(true); }       }    }    private static class LoginAction implements ActionListener //ActionListener handling login from login window {       private WikiSession w = null; private Core.Printer p = null; private JTextField user = null; private JPasswordField pass = null; private JFrame parentWindow = null; private JButton mainLoginButton = null; private JButton myLoginButton = null; public LoginAction(WikiSession wiki, Core.Printer printer, JTextField userField, JPasswordField passField, JFrame window, JButton button, JButton button2) {           w = wiki; p = printer; user = userField; pass = passField; parentWindow = window; mainLoginButton = button; myLoginButton = button2; }       @Override public void actionPerformed(ActionEvent arg0) {           Runnable r = new Runnable {               public void run {                   try {                       mainLoginButton.setEnabled(false); myLoginButton.setEnabled(false); mainLoginButton.setText("Logging in..."); myLoginButton.setText("Logging in..."); w.login(user.getText, String.copyValueOf(pass.getPassword)); p.outString("Login to " + user.getText + " successful!"); mainLoginButton.setEnabled(true); myLoginButton.setEnabled(true); mainLoginButton.setText("Log out"); myLoginButton.setText("Log in"); }                   catch(Exception ex) {                       p.errString("Login failed: " + ex.getMessage); mainLoginButton.setEnabled(true); myLoginButton.setEnabled(true); mainLoginButton.setText("Log in"); myLoginButton.setText("Log in"); }                   parentWindow.setVisible(false); }           };            new Thread(r).start; }   }    private static class ButtonAction implements ActionListener //ActionListener handling task button clicks {       private Core.Printer p = null; private String actionCommand = null; private GUI main = null; public ButtonAction(Core.Printer printer, String action, GUI mainWindow) {           p = printer; actionCommand = action; main = mainWindow; }

public void actionPerformed(ActionEvent e)       { if (!main.isBusy) {               try {                   final Runnable r = Core.getTaskRunnable(actionCommand); Runnable r2 = new Runnable {                       public void run{ main.isBusy = true; r.run; main.isBusy = false; }                   };                    new Thread(r2).start; }               catch (IOException e1) {                   p.errString("IOException: " + e1.getMessage); }               catch (Core.jBotException e1) {                   p.errString("Error: " + e1.getMessage); }           }            else p.errString("--A task is already running--"); }   } }