Beispiel #1
0
 public static void Create() {
   if (cacheWriter == null) {
     cacheWriter = new CacheWriter(GlobalConfiguration.Paths.getWebCache());
   }
 }
  public Scripter() {
    setTitle("Runedev Script Editor");
    setVisible(true);
    setBackground(new Color(245, 245, 245));
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

    addWindowListener(
        new WindowAdapter() {

          @Override
          public void windowClosing(final WindowEvent e) {
            // BotGUI.scripter = null;
          }
        });
    setResizable(false);
    final File icon = new File(GlobalConfiguration.Paths.getIconDirectory() + "/edit.png");
    setIconImage(GlobalConfiguration.getImageFile(icon));

    document = new HighLightedDocument();
    jSeparator1 = new JPopupMenu.Separator();

    textPane = new JTextPane(document);
    textPane.setCaretPosition(0);
    textPane.setMargin(new Insets(5, 5, 5, 5));
    textPane.setPreferredSize(new Dimension(minSize));
    final JScrollPane scrollPane = new JScrollPane(textPane);
    scrollPane.setPreferredSize(new Dimension(minSize));

    final JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.add(scrollPane, BorderLayout.CENTER);
    setContentPane(contentPane);

    final JMenuBar menuBar = new JMenuBar();

    final JMenu fileMenu = new JMenu("File");
    final JMenuItem newScript = new JMenuItem("New");
    newScript.setAccelerator(
        KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
    newScript.setIcon(new ImageIcon(GlobalConfiguration.Paths.getIconDirectory() + "/pencil.png"));
    fileMenu.add(newScript);

    final JMenuItem openScript = new JMenuItem("Open");
    openScript.setAccelerator(
        KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
    final Image Open =
        (Toolkit.getDefaultToolkit().getImage(GlobalConfiguration.Paths.Resources.PLAY));
    openScript.setIcon(new ImageIcon(Open));
    fileMenu.add(openScript);

    final JMenuItem saveScript = new JMenuItem("Save");
    saveScript.setAccelerator(
        javax.swing.KeyStroke.getKeyStroke(
            java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
    final Image Save =
        (Toolkit.getDefaultToolkit().getImage(GlobalConfiguration.Paths.Resources.SAVE));
    saveScript.setIcon(new ImageIcon(Save));
    fileMenu.add(saveScript);
    menuBar.add(fileMenu);

    final JMenu editMenu = new JMenu("Edit");
    final JMenuItem cut = new JMenuItem("Cut");
    cut.setAccelerator(
        javax.swing.KeyStroke.getKeyStroke(
            java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
    final Image Cut =
        (Toolkit.getDefaultToolkit().getImage(GlobalConfiguration.Paths.Resources.CUT));
    cut.setIcon(new javax.swing.ImageIcon(Cut));
    editMenu.add(cut);

    final JMenuItem copy = new JMenuItem("Copy");
    copy.setAccelerator(
        javax.swing.KeyStroke.getKeyStroke(
            java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
    final Image Copy =
        (Toolkit.getDefaultToolkit().getImage(GlobalConfiguration.Paths.Resources.COPY));
    copy.setIcon(new javax.swing.ImageIcon(Copy));
    editMenu.add(copy);

    final JMenuItem paste = new JMenuItem("Paste");
    paste.setAccelerator(
        javax.swing.KeyStroke.getKeyStroke(
            java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
    final Image Paste =
        (Toolkit.getDefaultToolkit().getImage(GlobalConfiguration.Paths.Resources.PASTE));
    paste.setIcon(new javax.swing.ImageIcon(Paste));
    editMenu.add(paste);
    editMenu.add(jSeparator1);

    final JMenuItem selectAll = new JMenuItem("Select All");
    selectAll.setAccelerator(
        javax.swing.KeyStroke.getKeyStroke(
            java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
    editMenu.add(selectAll);
    menuBar.add(editMenu);

    final JMenu infoMenu = new JMenu("Info");
    final JMenuItem troubleshooting = new JMenuItem("Troubleshooting");
    troubleshooting.setIcon(
        new ImageIcon(GlobalConfiguration.Paths.getIconDirectory() + "/web.png"));
    infoMenu.add(troubleshooting);

    final JMenuItem about = new JMenuItem("about");
    about.setAccelerator(
        javax.swing.KeyStroke.getKeyStroke(
            java.awt.event.KeyEvent.VK_F1, java.awt.event.InputEvent.CTRL_MASK));
    about.setIcon(new ImageIcon(GlobalConfiguration.Paths.getIconDirectory() + "/gui.png"));
    infoMenu.add(about);
    menuBar.add(infoMenu);

    class newClick implements ActionListener {

      public void actionPerformed(final ActionEvent e) {
        if (JOptionPane.showConfirmDialog(
                contentPane,
                "You really want to start a new script? \n"
                    + "All un-saved work on this script will be lost.")
            == 0) {
          try {
            document.remove(0, document.getLength());
            document.insertString(document.getLength(), defaultLayout, getStyle("text"));
          } catch (final BadLocationException e1) {
          }
        }
      }
    }

    class openClick implements ActionListener {

      public void actionPerformed(final ActionEvent e) {
        final JFileChooser fc = new JFileChooser(file);
        fc.setCurrentDirectory(new File("./scripts/"));
        fc.addChoosableFileFilter(new JavaFilter());
        if (fc.showOpenDialog(contentPane) == JFileChooser.APPROVE_OPTION) {
          file = fc.getSelectedFile();
          try {
            document.remove(0, document.getLength());
            final BufferedReader in = new BufferedReader(new FileReader(file));
            String line = null;
            while ((line = in.readLine()) != null) {
              if (document.getLength() != 0) {
                document.insertString(document.getLength(), "\n", getStyle("text"));
              }
              document.insertString(document.getLength(), line, getStyle("text"));
            }
          } catch (final Exception ee) {
          }
        }
      }
    }

    class saveClick implements ActionListener {

      public void actionPerformed(final ActionEvent e) {
        final JFileChooser fc = new JFileChooser(file);
        fc.setCurrentDirectory(new File("./scripts/"));
        fc.addChoosableFileFilter(new JavaFilter());
        if (fc.showSaveDialog(contentPane) == JFileChooser.APPROVE_OPTION) {
          try {
            file = fc.getSelectedFile();
            final BufferedWriter out = new BufferedWriter(new FileWriter(file));
            out.write(document.getText(0, document.getLength()));

            out.close();
          } catch (final Exception ee) {
          }
        }
      }
    }

    class Cut implements ActionListener {

      public void actionPerformed(final ActionEvent e) {
        textPane.cut();
      }
    }

    class Copy implements ActionListener {

      public void actionPerformed(final ActionEvent e) {
        textPane.copy();
      }
    }

    class Paste implements ActionListener {

      public void actionPerformed(final ActionEvent e) {
        textPane.paste();
      }
    }

    class SelectAll implements ActionListener {

      public void actionPerformed(final ActionEvent e) {
        textPane.selectAll();
      }
    }

    class tsClick implements ActionListener {

      public void actionPerformed(final ActionEvent e) {
        openURL("http://forum.runedev.info/viewtopic.php?f=40&t=2629");
      }

      public void openURL(final String url) {
        final OperatingSystem os = GlobalConfiguration.getCurrentOperatingSystem();
        try {
          if (os == OperatingSystem.MAC) {
            final Class<?> fileMgr = Class.forName("com.apple.eio.FileManager");
            final Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] {String.class});
            openURL.invoke(null, url);
          } else if (os == OperatingSystem.WINDOWS) {
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
          } else {
              /* assume Unix or Linux */
            final String[] browsers = {
              "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"
            };
            String browser = null;
            for (int count = 0; (count < browsers.length) && (browser == null); count++) {
              if (Runtime.getRuntime().exec(new String[] {"which", browsers[count]}).waitFor()
                  == 0) {
                browser = browsers[count];
              }
            }
            if (browser == null) {
              throw new Exception("Could not find web browser");
            } else {
              Runtime.getRuntime().exec(new String[] {browser, url});
            }
          }
        } catch (final Exception e) {
        }
      }
    }

    class aboutClick implements ActionListener {

      public void actionPerformed(final ActionEvent e) {
        JOptionPane.showMessageDialog(
            contentPane,
            new String[] {
              "A Script Editor Made for RuneDev.",
              "\nThis Editor was designed by Sorcermus\n"
                  + "for the RuneDev gaming client\n"
                  + "For more information, \nvisit; "
                  + GlobalConfiguration.Paths.URLs.SITE
            },
            "\n" + "About this editor, and the game client",
            JOptionPane.INFORMATION_MESSAGE);
      }
    }

    newScript.addActionListener(new newClick());
    openScript.addActionListener(new openClick());
    saveScript.addActionListener(new saveClick());
    cut.addActionListener(new Cut());
    copy.addActionListener(new Copy());
    paste.addActionListener(new Paste());
    selectAll.addActionListener(new SelectAll());
    troubleshooting.addActionListener(new tsClick());
    about.addActionListener(new aboutClick());

    setJMenuBar(menuBar);

    final JTextArea lines = new JTextArea("");
    lines.setSelectionEnd(document.getLength());
    lines.setFont(new Font("Monospaced", Font.PLAIN, 12));
    lines.setBackground(new Color(210, 210, 210));
    lines.setEditable(false);
    lines.setMargin(new Insets(5, 2, 5, 2));

    document.addDocumentListener(
        new DocumentListener() {

          public void changedUpdate(final DocumentEvent de) {
            lines.setText(getText());
          }

          public String getText() {
            final int caretPosition = document.getEndPosition().getOffset();
            final Element root = document.getDefaultRootElement();
            String text = "1" + System.getProperty("line.separator");
            for (int i = 2; i < root.getElementIndex(caretPosition) + 2; i++) {
              text += i + System.getProperty("line.separator");
            }
            return text;
          }

          public void insertUpdate(final DocumentEvent de) {
            lines.setText(getText());
          }

          public void removeUpdate(final DocumentEvent de) {
            lines.setText(getText());
          }
        });

    scrollPane.setRowHeaderView(lines);

    colorer = new Colorer();
    colorer.start();

    initStyles(12);

    documentReader = new DocumentReader(document);

    initDocument();

    pack();
    setVisible(true);
  }