-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommandLine.java
More file actions
181 lines (161 loc) · 5.69 KB
/
CommandLine.java
File metadata and controls
181 lines (161 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class CommandLine extends JScrollPane {
JTextArea textArea = new JTextArea();
String command = new String();
Ghc ghc = null;
int lastStop=0;
String[] commands = new String[100];
int commandNo = 0;
int commandShowing=0;
Action defaultBackSpaceAction;
public CommandLine(final CPL cpl){
Graph graph = cpl.graph;
setViewportView(textArea);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.addKeyListener(new MyKeyAdapter());
textArea.addCaretListener(new CaretListener(){
public void caretUpdate(CaretEvent e){
//System.out.println(e.getDot());
if(e.getDot() == 0) return;
if (e.getDot() < lastStop){
//System.out.println(lastStop);
//System.out.println(textArea.getText().length());
textArea.setCaretPosition(lastStop);
}
}
});
Keymap map = JTextComponent.addKeymap("My Map",textArea.getKeymap());
KeyStroke left = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0,false);
KeyStroke up = KeyStroke.getKeyStroke(KeyEvent.VK_UP,0,false);
KeyStroke down = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0,false);
KeyStroke backspace = KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE,0,false);
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,false);
defaultBackSpaceAction = map.getAction(backspace);
if(map.getAction(backspace) == null) System.out.println("Surprising");
map.addActionForKeyStroke(left,new MyLeftAction());
map.addActionForKeyStroke(up,new MyUpAction());
map.addActionForKeyStroke(down,new MyDownAction());
map.addActionForKeyStroke(backspace,new MyBackSpaceAction());
map.addActionForKeyStroke(enter, new MyEnterAction());
textArea.setKeymap(map);
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
setBorder(BorderFactory.createEtchedBorder());
ghc = new Ghc(this,cpl);
}
public void append(String output)
{
int maxLength = 8000;
String current = textArea.getText();
String newS = current + output;
int len = newS.length();
if (len > maxLength){
textArea.setText(newS.substring(len-maxLength));
}
else textArea.append(output);
lastStop = textArea.getText().length();
//System.out.println(lastStop);
textArea.setCaretPosition(lastStop);
}
class MyLeftAction extends AbstractAction{
public MyLeftAction(){ super("Left Action");}
public void actionPerformed(ActionEvent ev){
//System.out.println(caret.getDot());
int pos = textArea.getCaretPosition();
if (pos > lastStop) textArea.setCaretPosition(pos-1);
else Toolkit.getDefaultToolkit().beep();
}
}
class MyUpAction extends AbstractAction{
public MyUpAction(){ super("Up Action");}
public void actionPerformed(ActionEvent ev){
if(commandShowing > 0){
int l = textArea.getText().length();
textArea.replaceRange(commands[--commandShowing],l-command.length(),l);
command = commands[commandShowing];
}
}
}
class MyDownAction extends AbstractAction{
public MyDownAction(){ super("Down Action");}
public void actionPerformed(ActionEvent ev){
if(commandShowing < commandNo){
int l = textArea.getText().length();
int cl = command.length();
if(commandShowing == commandNo-1){
command ="";
commandShowing++;
}
else if(commandShowing < (commandNo-1))
command = commands[++commandShowing];
textArea.replaceRange(command,l-cl,l);
}
}
}
class MyEnterAction extends AbstractAction{
public MyEnterAction(){super("Enter Action");}
public void actionPerformed(ActionEvent ev){
int pos = textArea.getCaretPosition();
if (pos > lastStop) textArea.setCaretPosition(textArea.getText().length());
textArea.append("\n");
}
}
class MyBackSpaceAction extends AbstractAction{
public MyBackSpaceAction(){super("BackSpace Action");}
public void actionPerformed(ActionEvent ev){
int pos = textArea.getCaretPosition();
//System.out.println(pos);
if(pos >= lastStop){
if (defaultBackSpaceAction == null);
//System.out.println("It is NULL");
else defaultBackSpaceAction.actionPerformed(ev);
//System.out.println("OK");
//String s = textArea.getText();
//System.out.println(s);
//textArea.setText(s.substring(0,s.length()));
//System.out.println("********************");
//System.out.println(textArea.getText().length());
}
else {Toolkit.getDefaultToolkit().beep();System.out.println("nomore");}
}
}
public void setActiveChart(String file){
ghc.writeToGhc("setActiveChart \""+file+"\"\n");
}
public void runHaskellCmd(String cmd){
append(cmd);
ghc.writeToGhc(cmd);
}
class MyKeyAdapter extends KeyAdapter{
/** Handle the key typed event from the text field. */
public void keyTyped(KeyEvent e) {
/*char key = e.getKeyChar();
if (key=='\b'){
command = command.substring(0,command.length()-1);
//System.out.println("Back space");
}
else command = command + key;*/
}
/** Handle the key released event from the text field. */
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()== KeyEvent.VK_ENTER){
//System.out.print(command);
//get the command
String text = textArea.getText();
int pos = textArea.getCaretPosition();
if (pos > lastStop) textArea.setCaretPosition(text.length());
String command = text.substring(lastStop,text.length());
ghc.writeToGhc(command);
if (command.length() > 1){
commands[commandNo++] =command.substring(0,command.length()-1);
commandShowing = commandNo;
}
//command = "";
}
}
}
}