1 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 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 61 public boolean isManagingFocus() { 62 return true; 63 } 64 65 public ColorChangingLabel getStatusLabel() { 66 return statusLabel; 67 } 68 69 70 void addText(String str) { 71 String 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 str = text.getText(); 84 executeCommand(str); 85 } 86 87 void pressBackSpace() { 88 String 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 125 129 void executeCommand(String cmd) { 130 ComponentRepository.getAJDebugger().executeCommand(cmd); 131 text.setText(""); 132 } 133 134 protected JTextField getTextField() { 135 return text; 136 } 137 138 public static String d() { return "Command Line"; } 139 public String toString() { return d(); } 140 } 141 142 143 class AJTextField extends JTextField { 144 AJTextField(String s, int t) { 145 super(s, t); 146 } 147 167 169 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 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 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 newText) { 221 label.setText(newText); 222 turnOn(); 223 } 224 225 void turnOff(String 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 |