001: /*
002: * JdbControlDialog.java
003: *
004: * Copyright (C) 2002-2003 Peter Graves
005: * $Id: JdbControlDialog.java,v 1.7 2003/05/20 15:35:15 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j.jdb;
023:
024: import java.awt.Dimension;
025: import java.awt.Font;
026: import java.awt.FontMetrics;
027: import java.awt.Point;
028: import java.awt.Rectangle;
029: import java.awt.Toolkit;
030: import java.awt.event.ActionEvent;
031: import java.awt.event.ActionListener;
032: import java.awt.event.ComponentEvent;
033: import java.awt.event.ComponentListener;
034: import java.awt.event.KeyEvent;
035: import java.awt.event.KeyListener;
036: import java.awt.event.WindowAdapter;
037: import java.awt.event.WindowEvent;
038: import java.net.URL;
039: import javax.swing.BorderFactory;
040: import javax.swing.BoxLayout;
041: import javax.swing.ImageIcon;
042: import javax.swing.JButton;
043: import javax.swing.JDialog;
044: import javax.swing.JLabel;
045: import javax.swing.JPanel;
046: import javax.swing.JTabbedPane;
047: import javax.swing.JToolBar;
048: import org.armedbear.j.Constants;
049: import org.armedbear.j.DefaultTextFieldHandler;
050: import org.armedbear.j.Editor;
051: import org.armedbear.j.EditorIterator;
052: import org.armedbear.j.Expansion;
053: import org.armedbear.j.History;
054: import org.armedbear.j.HistoryTextField;
055: import org.armedbear.j.SessionProperties;
056: import org.armedbear.j.StandardButton;
057:
058: public final class JdbControlDialog extends JDialog implements
059: JdbConstants, Constants, ContextListener, ActionListener,
060: ComponentListener, KeyListener {
061: private static final String commandKey = "jdb.command";
062:
063: private static final SessionProperties sessionProperties = Editor
064: .getSessionProperties();
065:
066: private final Jdb jdb;
067: private final JToolBar toolBar;
068: private final HistoryTextField commandTextField;
069: private final History commandHistory;
070: private final StandardButton suspendButton;
071: private final StandardButton continueButton;
072:
073: public JdbControlDialog(Jdb jdb) {
074: super (Editor.getCurrentFrame(), "Jdb", false);
075: this .jdb = jdb;
076: toolBar = new JToolBar();
077: toolBar.setOrientation(JToolBar.HORIZONTAL);
078: toolBar.setFloatable(false);
079: toolBar.putClientProperty("JToolBar.isRollover", Boolean.FALSE);
080: addButton("Next", null, "jdbNext");
081: addButton("Step", null, "jdbStep");
082: addButton("Step Out", null, "jdbFinish");
083: addSeparator();
084: suspendButton = addButton("Suspend", null, "jdbSuspend");
085: continueButton = addButton("Continue", null, "jdbContinue");
086: addSeparator();
087: addButton("Restart", null, "jdbRestart");
088: addButton("Quit", null, "jdbQuit");
089: getContentPane().add(toolBar, "North");
090: JTabbedPane tabbedPane = new JTabbedPane();
091: StackPanel stackPanel = new StackPanel(jdb, this );
092: tabbedPane.addTab("Stack", stackPanel.getComponent());
093: ThreadPanel threadPanel = new ThreadPanel(jdb, this );
094: tabbedPane.addTab("Threads", threadPanel.getComponent());
095: BreakpointPanel breakpointPanel = new BreakpointPanel(jdb, this );
096: tabbedPane
097: .addTab("Breakpoints", breakpointPanel.getComponent());
098: getContentPane().add(tabbedPane, "Center");
099: JPanel commandPanel = new JPanel();
100: commandPanel.setLayout(new BoxLayout(commandPanel,
101: BoxLayout.X_AXIS));
102: commandPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 4,
103: 3));
104: commandPanel.add(new JLabel("Command:"));
105: commandTextField = new HistoryTextField(20);
106: commandHistory = new History(commandKey);
107: commandTextField.setHistory(commandHistory);
108: commandTextField.setHandler(new CommandTextFieldHandler(
109: commandTextField));
110: commandPanel.add(commandTextField);
111: getContentPane().add(commandPanel, "South");
112: pack();
113: jdb.addContextListener(this );
114: addComponentListener(this );
115: addWindowListener(new WindowMonitor());
116: commandTextField.addKeyListener(this );
117: if (jdb.getStartSuspended())
118: tabbedPane.setSelectedComponent(breakpointPanel
119: .getComponent());
120: contextChanged();
121: requestDefaultFocus();
122: }
123:
124: public void requestDefaultFocus() {
125: commandTextField.requestFocus();
126: }
127:
128: private StandardButton addButton(String text, String iconFile,
129: String command) {
130: StandardButton button = new StandardButton(text);
131: Font font = button.getFont();
132: FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(
133: font);
134: int width = fm.stringWidth(text);
135: Dimension dim = new Dimension(width + 14,
136: StandardButton.DEFAULT_HEIGHT);
137: button.setMinimumSize(dim);
138: button.setMaximumSize(dim);
139: button.setPreferredSize(dim);
140: if (command != null)
141: button.setActionCommand(command);
142: button.addActionListener(this );
143: button.setRequestFocusEnabled(false);
144: toolBar.add(button);
145: return button;
146: }
147:
148: private void addSeparator() {
149: toolBar.addSeparator();
150: }
151:
152: public void show() {
153: int width = sessionProperties.getIntegerProperty("jdb.width",
154: 425);
155: int height = sessionProperties.getIntegerProperty("jdb.height",
156: 250);
157: setSize(width, height);
158: int x = sessionProperties.getIntegerProperty("jdb.x", -1);
159: int y = sessionProperties.getIntegerProperty("jdb.y", -1);
160: if (x >= 0 && y >= 0) {
161: setLocation(x, y);
162: } else {
163: // First time.
164: Editor editor = null;
165: for (EditorIterator it = new EditorIterator(); it.hasNext();) {
166: Editor ed = it.nextEditor();
167: if (ed.getBuffer() == jdb) {
168: editor = ed;
169: break;
170: }
171: }
172: if (editor != null) {
173: Dimension parent = editor.getFrame().getSize();
174: Dimension dialog = getSize();
175: Point p = editor.getFrame().getLocation();
176: p.translate(parent.width - dialog.width - 30,
177: parent.height - dialog.height - 50);
178: setLocation(p);
179: } else
180: Editor.currentEditor().centerDialog(this );
181: }
182: super .show();
183: }
184:
185: public void contextChanged() {
186: if (jdb.getVM() == null) {
187: suspendButton.setEnabled(false);
188: continueButton.setEnabled(false);
189: } else if (jdb.isSuspended()) {
190: suspendButton.setEnabled(false);
191: continueButton.setEnabled(true);
192: } else {
193: suspendButton.setEnabled(true);
194: continueButton.setEnabled(false);
195: }
196: }
197:
198: public void actionPerformed(ActionEvent e) {
199: Editor.currentEditor().getDispatcher().actionPerformed(e);
200: }
201:
202: public void keyPressed(KeyEvent e) {
203: if (e.getKeyCode() == KeyEvent.VK_ENTER) {
204: // Mask off the bits we don't care about (Java 1.4).
205: if ((e.getModifiers() & 0x0f) == 0) {
206: if (commandTextField.getText().trim().length() > 0) {
207: jdb.doCommand(commandTextField.getText());
208: commandTextField.setText("");
209: } else {
210: int command = jdb.getLastCommand();
211: if (command == JDB_CONTINUE || command == JDB_NEXT
212: || command == JDB_STEP)
213: jdb.doCommand(command, null);
214: }
215: }
216: }
217: }
218:
219: public void keyReleased(KeyEvent e) {
220: }
221:
222: public void keyTyped(KeyEvent e) {
223: }
224:
225: public void componentResized(ComponentEvent e) {
226: saveWindowPlacement();
227: }
228:
229: public void componentMoved(ComponentEvent e) {
230: saveWindowPlacement();
231: }
232:
233: public void componentShown(ComponentEvent e) {
234: saveWindowPlacement();
235: }
236:
237: public void componentHidden(ComponentEvent e) {
238: }
239:
240: private void saveWindowPlacement() {
241: Rectangle r = getBounds();
242: sessionProperties.setIntegerProperty("jdb.x", r.x);
243: sessionProperties.setIntegerProperty("jdb.y", r.y);
244: sessionProperties.setIntegerProperty("jdb.width", r.width);
245: sessionProperties.setIntegerProperty("jdb.height", r.height);
246: }
247:
248: private class WindowMonitor extends WindowAdapter {
249: public void windowClosing(WindowEvent e) {
250: setVisible(false);
251: dispose();
252: jdb.doCommand("quit");
253: }
254: }
255:
256: private class CommandTextFieldHandler extends
257: DefaultTextFieldHandler {
258: CommandTextFieldHandler(HistoryTextField textField) {
259: super (textField);
260: }
261:
262: public void enter() {
263: commandHistory.append(textField.getText());
264: }
265:
266: public void escape() {
267: textField.setText("");
268: }
269:
270: public Expansion getExpansion(String prefix) {
271: Expansion expansion = new Expansion(jdb, prefix, prefix);
272: EditorIterator iter = new EditorIterator();
273: while (iter.hasNext()) {
274: Editor ed = iter.nextEditor();
275: if (ed.getModeId() == JAVA_MODE) {
276: Expansion exp = new Expansion(ed.getBuffer(),
277: prefix, prefix, ed.getDot());
278: expansion.appendCandidates(exp.getCandidates());
279: }
280: }
281: return expansion;
282: }
283: }
284: }
|