Source code: ide/ConsoleView.java
1
2 /* **********************************
3 File: Consoleview.java
4 Author: Felix(felix@ulise.cs.pub.ro)
5 Created on ??
6 Comments: Part of the vIDE Project
7 Copyright 1999 the vIDE Team.
8 ***********************************/
9
10 package ide;
11
12 import java.awt.*;
13 import java.awt.event.*;
14 import java.util.*;
15 import javax.swing.*;
16 import javax.swing.border.*;
17 import javax.swing.event.*;
18 import javax.swing.plaf.*;
19 import javax.swing.text.*;
20 import javax.swing.tree.*;
21 import javax.swing.undo.*;
22 import javax.swing.text.html.*;
23
24 import middle.*;
25 /**
26 * This is the console for the interpretor.
27 * It contains a list acting like an output for the interpreter
28 * and an input field for commands (Alec wanted this.);
29 *
30 * @ver 1.0
31 * @author Lucian Felix Ghita
32 */
33 public class ConsoleView extends JPanel implements IOManager {
34
35 JList list;
36 JTextField inputField;
37 JScrollPane scrollPane;
38 private Project project;
39 private String command = "";
40 private boolean commandReady = false;
41 DefaultListModel listModel;
42 int consoleCellHeight;
43
44 /**
45 * The console capacity.
46 */
47 static int maxLineCount = 200;
48
49 static int tabSize = 8;
50
51 public ConsoleView(Project project) {
52 super();
53 vide.spl.showStatus("Creating console ..");
54
55 this.project = project;
56 project.cv =this;
57
58
59 listModel = new DefaultListModel();
60 list = new JList(listModel);
61 list.setFixedCellHeight(consoleCellHeight = (int) list.getCellRenderer().
62 getListCellRendererComponent(list, "Babau", 0, false, false).getPreferredSize().getHeight());
63
64 MouseListener mouseListener = new MouseAdapter() {
65 public void mouseClicked(MouseEvent e) {
66 if (e.getClickCount() == 2) itemDClicked(list.locationToIndex(e.getPoint()));
67 }};
68 list.addMouseListener(mouseListener);
69 list.setSelectionBackground(new Color(0, 200, 0));
70
71 JPanel bottomPanel = new JPanel(new BorderLayout());
72 inputField = new JTextField();
73
74 //inputField.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK + ActionEvent.ALT_MASK, false));
75 inputField.setFocusAccelerator('x');
76 inputField.addActionListener(new ActionListener() {
77 public void actionPerformed(ActionEvent e) {
78 commandDispatch(e.getActionCommand());
79 }
80 });
81
82 bottomPanel.add(inputField, BorderLayout.CENTER);
83 JButton clearConsole = new JButton("C");
84 clearConsole.addActionListener(new ActionListener() {
85 public void actionPerformed(ActionEvent e) {
86 //setVisible(false);
87 clear();
88 }
89 });
90
91 bottomPanel.add(clearConsole, BorderLayout.EAST);
92
93 scrollPane = new JScrollPane(list);
94 scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
95 scrollPane.setBackground(list.getBackground());
96 setBorder(new EtchedBorder());
97 setLayout(new BorderLayout());
98 add(scrollPane, BorderLayout.CENTER);
99 add(bottomPanel, BorderLayout.SOUTH);
100 vide.spl.showStatus("Done creating console ..");
101 }
102
103 public void commandDispatch(String s) {
104 command = s;
105 commandReady = true;
106 //writeLine(command);
107 try {
108 inputField.getDocument().remove(0, inputField.getDocument().getLength());
109 } catch (Exception e) { System.out.println(" " + e);}
110
111
112 //listModel.clear();
113 }
114
115 public String cin() {
116 project.go("Ready");
117 String s = readLine();
118 project.go("Running");
119 return s;
120 };
121
122 /**
123 * To be used _ONLY_ fom outside the message dispatcher (i.e. only
124 * from simulation threads. It will block otharwise !
125 */
126 public void cout(String string) {
127 int lineEnd;
128 while((lineEnd = string.indexOf('\n')) != -1) {
129 writeLine(string.substring(0, lineEnd));
130 string = string.substring(lineEnd+1, string.length());
131 }
132 if (string.length() > 0)
133 writeLine(string);
134 try {
135 SwingUtilities.invokeAndWait(new Runnable() {
136 public void run () {
137 repaint();
138 }
139 });
140 } catch (Exception ex) {}
141 }
142
143 /**
144 * This method does the same as cout, except
145 * throwing an Error when called from AWT thread.
146 */
147 public void message(String string) {
148 int lineEnd;
149 while((lineEnd = string.indexOf('\n')) != -1) {
150 writeLine(string.substring(0, lineEnd));
151 string = string.substring(lineEnd+1, string.length());
152 }
153 if (string.length() > 0)
154 writeLine(string);
155 repaint();
156 }
157
158
159 public void setConsoleCapacity(int newCapacity){
160 if(newCapacity < maxLineCount){
161 int toBeRemoved = listModel.size() - newCapacity;
162 if(toBeRemoved > 0)
163 listModel.removeRange(0, toBeRemoved);
164 }
165 maxLineCount = newCapacity;
166 }
167
168 /**
169 * Appends a line into the console, expanding any tabs according to tha
170 *tabSize property.
171 */
172 public void writeLine(String s) {
173 //scrollPane.getViewport().setViewPosition(new Point(0, 30000));
174 StringBuffer sb = new StringBuffer(s);
175 for (int i = 0; i < sb.length();) {
176 if (sb.charAt(i) == '\t') {
177 sb.deleteCharAt(i);
178 while(i % tabSize != 0)
179 sb.insert(i++, ' ');
180 } else i++;
181 }
182 if(listModel.size() == maxLineCount)listModel.remove(0);
183 listModel.addElement(sb.toString());
184 scrollPane.getViewport().
185 setViewPosition(new Point(0, consoleCellHeight * listModel.getSize()));
186 }
187
188 public String readLine() {
189 while (!commandReady)
190 try {
191 Thread.sleep(500);
192 } catch (Exception e) {
193 System.out.println("Exception in ConsoleView.readLine() :" + e);
194 } //todo -> wait for ThreadInterruptedException.
195 commandReady = false;
196 String dispCommand = command;
197 command = "";
198 return dispCommand;
199 }
200
201 public void clear() {
202 listModel.clear();
203 list.setFixedCellHeight(consoleCellHeight = (int) list.getCellRenderer().
204 getListCellRendererComponent(list, "Babau", 0, false, false).getPreferredSize().getHeight());
205 project.wv.clear(); //de ce? buna intrebare.
206 }
207
208 public void itemDClicked(int index) {
209 String fname, string = ((String)((listModel).elementAt(index))).trim();
210 int ln, i, j = string.indexOf(':');
211 if (j == -1) return;
212 fname = string.substring(0, j);
213 while (true) {
214 i = j + 1 + string.substring(j+1).indexOf(':');
215 if (i == j) return;
216 try {
217 ln = Integer.parseInt(string.substring(j+1, i));
218 break;
219 } catch (NumberFormatException ex) {
220 fname = string.substring(0, i);
221 j = i;
222 }
223 }
224 project.jumpToLocation(fname, ln);
225 }
226 }