001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.script;
020
021import icy.gui.frame.IcyFrame;
022import icy.gui.util.WindowPositionSaver;
023
024import java.awt.Dimension;
025import java.awt.FlowLayout;
026import java.awt.GridLayout;
027import java.awt.Point;
028import java.awt.event.ActionEvent;
029import java.awt.event.ActionListener;
030
031import javax.script.ScriptEngine;
032import javax.script.ScriptEngineManager;
033import javax.script.ScriptException;
034import javax.swing.BoxLayout;
035import javax.swing.JButton;
036import javax.swing.JMenu;
037import javax.swing.JMenuBar;
038import javax.swing.JMenuItem;
039import javax.swing.JPanel;
040import javax.swing.JSplitPane;
041import javax.swing.JTextArea;
042
043/**
044 * @author fab
045 */
046public class ScriptEditor extends IcyFrame implements ActionListener
047{
048
049    private JTextArea codeArea = new JTextArea("print('Hello, world!')", 15, 20);
050    private JTextArea messageArea = new JTextArea("message text", 5, 20);
051    private JPanel mainPanel = new JPanel();
052    JPanel codePanel = new JPanel();
053    JPanel codeActionPanel = new JPanel();
054    JPanel messagePanel = new JPanel();
055    JPanel messageActionPanel = new JPanel();
056
057    JButton runButton = new JButton("Run");
058
059    JMenuItem loadFileMenuItem = new JMenuItem("Load...");
060    JMenuItem saveFileMenuItem = new JMenuItem("Save...");
061
062    // we need to keep reference on it as the object only use weak reference
063    final WindowPositionSaver positionSaver;
064
065    public ScriptEditor()
066    {
067        super("Script Editor", true, true, true, true);
068
069        positionSaver = new WindowPositionSaver(this, "frame/scriptEditor", new Point(300, 300),
070                new Dimension(400, 300));
071
072        JMenuBar menuBar = new JMenuBar();
073        this.setJMenuBar(menuBar);
074        JMenu fileMenu = new JMenu("File");
075        fileMenu.add(loadFileMenuItem);
076        fileMenu.add(saveFileMenuItem);
077        loadFileMenuItem.addActionListener(this);
078        saveFileMenuItem.addActionListener(this);
079        menuBar.add(fileMenu);
080
081        this.setVisible(true);
082
083        codePanel.setLayout(new BoxLayout(codePanel, BoxLayout.PAGE_AXIS));
084        codeActionPanel.add(runButton);
085        runButton.addActionListener(this);
086        codeActionPanel.add(new JButton("Compile"));
087
088        codePanel.add(codeArea);
089        codePanel.add(codeActionPanel);
090        codeArea.setLineWrap(true);
091
092        messagePanel.setLayout(new BoxLayout(messagePanel, BoxLayout.PAGE_AXIS));
093        messageActionPanel.add(new JButton("Clear"));
094
095        messagePanel.add(messageArea);
096        messagePanel.add(messageActionPanel);
097
098        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
099
100        mainPanel.add(codePanel);
101        mainPanel.add(messagePanel);
102
103        codeArea.setVisible(true);
104        codeArea.setTabSize(2);
105        messageArea.setLineWrap(true);
106
107        this.getContentPane().setLayout(new FlowLayout());
108
109        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, codePanel, messagePanel);
110
111        this.getContentPane().setLayout(new GridLayout(1, 1));
112        this.getContentPane().add(splitPane);
113
114        this.pack();
115
116        addToDesktopPane();
117
118    }
119
120    public void actionPerformed(ActionEvent e)
121    {
122
123        if (e.getSource() == runButton)
124        {
125            messageArea.setText("");
126
127            ScriptEngineManager mgr = new ScriptEngineManager();
128            ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
129            if (jsEngine == null)
130            {
131                System.err.println("Unable to use ScriptEngine with JavaScript.");
132                return;
133            }
134            try
135            {
136                jsEngine.eval(codeArea.getText());
137            }
138            catch (ScriptException ex)
139            {
140                messageArea.append("Line : " + ex.getLineNumber() + "\n");
141                messageArea.append(ex.getMessage());
142            }
143        }
144
145        if (e.getSource() == loadFileMenuItem)
146        {
147
148        }
149
150        if (e.getSource() == saveFileMenuItem)
151        {
152
153        }
154
155    }
156
157}