001 /*
002 * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003 *
004 * This file is part of the Java Data Mining Package (JDMP).
005 * See the NOTICE file distributed with this work for additional
006 * information regarding copyright ownership and licensing.
007 *
008 * JDMP is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation; either version 2
011 * of the License, or (at your option) any later version.
012 *
013 * JDMP is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public
019 * License along with JDMP; if not, write to the
020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021 * Boston, MA 02110-1301 USA
022 */
023
024 package org.jdmp.gui.interpreter;
025
026 import java.awt.BorderLayout;
027 import java.awt.Color;
028 import java.awt.Dimension;
029 import java.awt.Font;
030 import java.awt.event.KeyEvent;
031 import java.awt.event.KeyListener;
032
033 import javax.swing.BorderFactory;
034 import javax.swing.JPanel;
035 import javax.swing.JScrollPane;
036 import javax.swing.JTextPane;
037 import javax.swing.text.AttributeSet;
038 import javax.swing.text.Document;
039 import javax.swing.text.Element;
040 import javax.swing.text.SimpleAttributeSet;
041 import javax.swing.text.StyleConstants;
042 import javax.swing.text.StyleContext;
043
044 import org.jdmp.core.module.Module;
045 import org.jdmp.core.script.Result;
046
047 public class CommandWindow extends JPanel implements KeyListener {
048 private static final long serialVersionUID = 5931204715283753689L;
049
050 private Module module = null;
051
052 private int endPos = 0;
053 private JTextPane textField = null;
054
055 private Document doc = null;
056
057 public CommandWindow(Module m) {
058 this.module = m;
059
060 textField = new JTextPane() {
061 @Override
062 public boolean getScrollableTracksViewportWidth() {
063 return false;
064 }
065
066 @Override
067 public void setSize(Dimension d) {
068 if (d.width < getParent().getSize().width) {
069 d.width = getParent().getSize().width;
070 }
071 super.setSize(d);
072 }
073 };
074
075 doc = textField.getDocument();
076
077 textField.setFont(new Font("Monospaced", Font.PLAIN, 13));
078 textField.addKeyListener(this);
079
080 appendText(">> ");
081 endPos = doc.getLength();
082
083 setBorder(BorderFactory.createTitledBorder("Command Window"));
084 setLayout(new BorderLayout());
085 JScrollPane scrollPane = new JScrollPane(textField);
086 add(scrollPane, BorderLayout.CENTER);
087 }
088
089 public synchronized Result execute(String text) throws Exception {
090 Result result = null;
091
092 if (text.endsWith(";")) {
093 result = module.execute(text);
094 if (result.getException() != null) {
095 appendError("\n" + result);
096 }
097 } else {
098 result = module.execute(text + ";");
099 if (result != null) {
100 if (result.getException() != null) {
101 appendError("\n" + result);
102 } else {
103 appendText("\n" + result);
104 }
105 }
106 }
107
108 appendText("\n\n>> ");
109 endPos = doc.getLength();
110
111 // TODO: this is a hack and should be solved via
112 // EventListeners correctly
113 getParent().getParent().repaint();
114
115 return result;
116 }
117
118 @Override
119 public void keyPressed(KeyEvent e) {
120 filterKeys(e);
121
122 try {
123
124 if (e.getKeyCode() == KeyEvent.VK_ENTER) {
125
126 int line = doc.getDefaultRootElement().getElementCount() - 1;
127
128 Element map = doc.getDefaultRootElement();
129 Element lineElem = map.getElement(line);
130 int lineStart = lineElem.getStartOffset();
131
132 int lineCount = map.getElementCount();
133 int endOffset = lineElem.getEndOffset();
134 int lineEnd = ((line == lineCount - 1) ? (endOffset - 1) : endOffset);
135
136 int length = lineEnd - lineStart;
137 String text = doc.getText(lineStart + 3, length - 3);
138
139 execute(text);
140 e.consume();
141
142 return;
143
144 }
145 } catch (Exception ex) {
146 ex.printStackTrace();
147 }
148 }
149
150 @Override
151 public void keyReleased(KeyEvent e) {
152 }
153
154 @Override
155 public void keyTyped(KeyEvent e) {
156 }
157
158 public void appendError(String s) {
159 StyleContext sc = StyleContext.getDefaultStyleContext();
160 AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground,
161 Color.RED);
162
163 textField.setCharacterAttributes(aset, false);
164 textField.replaceSelection(s);
165 int len = doc.getLength();
166 textField.setCaretPosition(len);
167 }
168
169 public void appendText(String s) {
170 StyleContext sc = StyleContext.getDefaultStyleContext();
171 AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground,
172 Color.BLACK);
173
174 textField.setCharacterAttributes(aset, false);
175 textField.replaceSelection(s);
176 int len = doc.getLength();
177 textField.setCaretPosition(len);
178 }
179
180 public void filterKeys(KeyEvent e) {
181 try {
182
183 textField.setCaretPosition(doc.getLength());
184
185 if (e.getKeyCode() == KeyEvent.VK_UP) {
186 e.consume();
187 return;
188 }
189
190 if (textField.getCaretPosition() <= endPos) {
191 if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
192 e.consume();
193 return;
194 } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
195 e.consume();
196 return;
197 }
198 }
199
200 } catch (Exception ex) {
201 ex.printStackTrace();
202 }
203 }
204
205 }