KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > gui > CommandLine


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  */

22
23 package org.aspectj.debugger.gui;
24
25 import org.aspectj.debugger.base.*;
26
27 import java.awt.*;
28 import java.awt.event.*;
29 import java.io.*;
30 import java.util.*;
31 import javax.swing.*;
32
33 import com.sun.jdi.event.*;
34
35 public class CommandLine extends JPanel {
36
37     private String JavaDoc TITLE = "Command";
38     private int WIDTH = 40;
39     private AJTextField text = null;
40     private JLabel label = null;
41
42     private ColorChangingLabel statusLabel;
43
44     public CommandLine(ComponentDirector gui) {
45         label = new JLabel(TITLE + ": ");
46         text = new AJTextField("", WIDTH);
47         text.addActionListener(new ActionListener() {
48             public void actionPerformed(ActionEvent e) {
49                 pressEnter();
50         }});
51         setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
52         add(label);
53         add(text);
54         statusLabel = new ColorChangingLabel();
55     }
56
57     /**
58      * We want <b>all</b> key presses (including <code>tab</code>s) sent to
59      * this field.
60      */

61     public boolean isManagingFocus() {
62         return true;
63     }
64
65     public ColorChangingLabel getStatusLabel() {
66         return statusLabel;
67     }
68
69
70     void addText(String JavaDoc str) {
71         String JavaDoc getText = text.getText();
72         int pos = text.getCaretPosition();
73         if (getText.length() == 0) {
74             text.setText(str);
75         } else {
76             text.setText(getText.substring(0, pos) + str +
77                          getText.substring(pos, getText.length()));
78             text.setCaretPosition(pos+1);
79         }
80     }
81
82     void pressEnter() {
83         String JavaDoc str = text.getText();
84         executeCommand(str);
85     }
86
87     void pressBackSpace() {
88         String JavaDoc getText = text.getText();
89         int pos = text.getCaretPosition();
90         if (getText.length() != 0 && pos != 0) {
91             text.setText(getText.substring(0, pos-1) +
92                          (pos < getText.length() ?
93                             getText.substring(pos, getText.length()) :
94                             ""));
95             text.setCaretPosition(pos-1);
96         }
97     }
98
99     void pressRight() {
100         int newPos = text.getCaretPosition() + 1;
101         if (newPos <= text.getText().length()) {
102             text.setCaretPosition(newPos);
103         }
104     }
105
106     void pressLeft() {
107         int newPos = text.getCaretPosition() - 1;
108         if (newPos >= 0) {
109             text.setCaretPosition(newPos);
110         }
111     }
112
113     void pressHome() {
114         text.setCaretPosition(0);
115     }
116
117     void pressEnd() {
118         text.setCaretPosition(text.getText().length());
119     }
120
121 // void processKeyStroke(KeyStroke k) {
122
// text.processKeyStroke(k);
123
// }
124

125 // KeyStroke[] getTextKeyStrokes() {
126
// return text.getRegisteredKeyStrokes();
127
// }
128

129     void executeCommand(String JavaDoc cmd) {
130         ComponentRepository.getAJDebugger().executeCommand(cmd);
131         text.setText("");
132     }
133
134     protected JTextField getTextField() {
135         return text;
136     }
137
138     public static String JavaDoc d() { return "Command Line"; }
139     public String JavaDoc toString() { return d(); }
140 }
141
142
143 class AJTextField extends JTextField {
144     AJTextField(String JavaDoc s, int t) {
145         super(s, t);
146     }
147 // void processKeyStroke(KeyStroke k) {
148
// KeyEvent e2 = new KeyEvent(this, KeyEvent.KEY_PRESSED,
149
// System.currentTimeMillis(),
150
// k.getModifiers(), k.getKeyCode());
151
// KeyEvent e3 = new KeyEvent(this, KeyEvent.KEY_RELEASED,
152
// System.currentTimeMillis(),
153
// k.getModifiers(), k.getKeyCode());
154
// try {
155
// this.processComponentKeyEvent(e2);
156
// this.processComponentKeyEvent(e3);
157
// return;
158
// } catch (Exception e) {
159
// }
160
// try {
161
// this.processKeyEvent(e2);
162
// Util.msg("e2", "processKeyEvent: " + e2, Color.red);
163
// return;
164
// } catch (Exception e) {
165
// }
166

167 // }
168

169     /**
170      * We want <b>all</b> key presses (including <code>tab</code>s) sent to
171      * this field.
172      */

173     public boolean isManagingFocus() {
174         return true;
175     }
176
177     public boolean isFocusTraversable() {
178         return false;
179     }
180 }
181
182 class ColorChangingLabel extends JPanel {
183
184     private String JavaDoc text;
185     private JLabel label;
186
187     public static Color ON_COLOR = Color.red;
188     public static Color OFF_COLOR = Color.green.darker().darker();
189     public static int WIDTH = 170;
190
191     ColorChangingLabel(String JavaDoc text) {
192         super();
193         this.text = text;
194         this.label = new JLabel(text, SwingConstants.CENTER);
195         label.setPreferredSize
196             (new Dimension(WIDTH,
197                            (int)label.getPreferredSize().getHeight()));
198         turnOff();
199         setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
200         add(new JLabel("Status:"));
201         add(label);
202     }
203
204     ColorChangingLabel() {
205         this("Idle");
206     }
207
208     void turnOn() {
209         label.setForeground(ON_COLOR);
210         giveUpFocus();
211     }
212
213     void giveUpFocus() {
214         CommandLine cl = ComponentRepository.getCommandLine();
215         if (cl != null) {
216             cl.requestFocus();
217         }
218     }
219
220     void turnOn(String JavaDoc newText) {
221         label.setText(newText);
222         turnOn();
223     }
224
225     void turnOff(String JavaDoc newText) {
226         if (newText == null) {
227             label.setText(text);
228         } else {
229             label.setText(newText);
230         }
231         label.setForeground(OFF_COLOR);
232         giveUpFocus();
233     }
234
235     void turnOff() {
236         turnOff(text);
237     }
238 }
239
Popular Tags