1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * GroovyPanel.java |
---|
19 | * Copyright (C) 2009 University of Waikato, Hamilton, New Zealand |
---|
20 | */ |
---|
21 | |
---|
22 | package weka.gui.scripting; |
---|
23 | |
---|
24 | import weka.core.Utils; |
---|
25 | import weka.gui.BrowserHelper; |
---|
26 | import weka.gui.ComponentHelper; |
---|
27 | import weka.gui.visualize.VisualizeUtils; |
---|
28 | |
---|
29 | import java.awt.BorderLayout; |
---|
30 | import java.awt.Color; |
---|
31 | import java.awt.Font; |
---|
32 | import java.awt.GridLayout; |
---|
33 | import java.util.Properties; |
---|
34 | |
---|
35 | import javax.swing.BorderFactory; |
---|
36 | import javax.swing.ImageIcon; |
---|
37 | import javax.swing.JLabel; |
---|
38 | import javax.swing.JPanel; |
---|
39 | import javax.swing.JTextPane; |
---|
40 | import javax.swing.text.Document; |
---|
41 | |
---|
42 | /** |
---|
43 | * A scripting panel for <a href="http://groovy.codehaus.org/" target="_blank">Groovy</a>. |
---|
44 | * |
---|
45 | * @author fracpete (fracpete at waikato dot ac dot nz) |
---|
46 | * @version $Revision: 5837 $ |
---|
47 | */ |
---|
48 | public class GroovyPanel |
---|
49 | extends FileScriptingPanel { |
---|
50 | |
---|
51 | /** for serialization. */ |
---|
52 | private static final long serialVersionUID = -3475707604414854111L; |
---|
53 | |
---|
54 | /** the Groovy setup. */ |
---|
55 | public final static String PROPERTIES_FILE = "weka/gui/scripting/Groovy.props"; |
---|
56 | |
---|
57 | /** |
---|
58 | * Creates a new JTextPane for the code. |
---|
59 | * |
---|
60 | * @return the text pane |
---|
61 | */ |
---|
62 | protected JTextPane newCodePane() { |
---|
63 | JTextPane result; |
---|
64 | SyntaxDocument doc; |
---|
65 | Properties props; |
---|
66 | |
---|
67 | try { |
---|
68 | props = Utils.readProperties(PROPERTIES_FILE); |
---|
69 | } |
---|
70 | catch (Exception e) { |
---|
71 | e.printStackTrace(); |
---|
72 | props = new Properties(); |
---|
73 | } |
---|
74 | |
---|
75 | result = new JTextPane(); |
---|
76 | if (props.getProperty("Syntax", "false").equals("true")) { |
---|
77 | doc = new SyntaxDocument(props); |
---|
78 | result.setDocument(doc); |
---|
79 | result.setBackground(doc.getBackgroundColor()); |
---|
80 | } |
---|
81 | else { |
---|
82 | result.setForeground(VisualizeUtils.processColour(props.getProperty("ForegroundColor", "black"), Color.BLACK)); |
---|
83 | result.setBackground(VisualizeUtils.processColour(props.getProperty("BackgroundColor", "white"), Color.WHITE)); |
---|
84 | result.setFont(new Font(props.getProperty("FontName", "monospaced"), Font.PLAIN, Integer.parseInt(props.getProperty("FontSize", "12")))); |
---|
85 | } |
---|
86 | |
---|
87 | return result; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Returns an icon to be used in a frame. |
---|
92 | * |
---|
93 | * @return the icon |
---|
94 | */ |
---|
95 | public ImageIcon getIcon() { |
---|
96 | return ComponentHelper.getImageIcon(IMAGES_DIR + "/groovy_small.png"); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Returns a panel to be displayed with the AboutAction. |
---|
101 | * |
---|
102 | * @return the panel with some information on the scripting panel |
---|
103 | */ |
---|
104 | protected JPanel getAboutPanel() { |
---|
105 | JPanel result; |
---|
106 | JPanel panel; |
---|
107 | |
---|
108 | result = new JPanel(new BorderLayout()); |
---|
109 | result.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); |
---|
110 | |
---|
111 | // image |
---|
112 | result.add(new JLabel(ComponentHelper.getImageIcon(IMAGES_DIR + "/groovy_medium.png")), BorderLayout.CENTER); |
---|
113 | |
---|
114 | // links |
---|
115 | panel = new JPanel(new GridLayout(5, 1)); |
---|
116 | panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); |
---|
117 | result.add(panel, BorderLayout.SOUTH); |
---|
118 | |
---|
119 | panel.add(new JLabel("Groovy homepage")); |
---|
120 | panel.add(BrowserHelper.createLink("http://groovy.codehaus.org/", null)); |
---|
121 | panel.add(new JLabel(" ")); |
---|
122 | panel.add(new JLabel("Weka and Groovy")); |
---|
123 | panel.add(BrowserHelper.createLink("http://weka.wikispaces.com/Using+Weka+from+Groovy", null)); |
---|
124 | |
---|
125 | return result; |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * Returns the title (without the filename). |
---|
130 | * |
---|
131 | * @return the plain title |
---|
132 | */ |
---|
133 | public String getPlainTitle() { |
---|
134 | return "Groovy Console"; |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * Returns an initialized script object. |
---|
139 | * |
---|
140 | * @param doc the document to use as basis |
---|
141 | * @return the initialized script |
---|
142 | */ |
---|
143 | protected Script newScript(Document doc) { |
---|
144 | return new GroovyScript(doc); |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * Displays the panel in a frame. |
---|
149 | * |
---|
150 | * @param args can take a file as first argument |
---|
151 | */ |
---|
152 | public static void main(String[] args) { |
---|
153 | showPanel(new GroovyPanel(), args); |
---|
154 | } |
---|
155 | } |
---|