001: /*
002: * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
003: *
004: * See the file "LICENSE.txt" for information on usage and redistribution of
005: * this file, and for a DISCLAIMER OF ALL WARRANTIES.
006: */
007: package org.pnuts.tools;
008:
009: import java.awt.Color;
010: import java.awt.Dimension;
011: import java.awt.Font;
012: import java.awt.GridBagConstraints;
013: import java.awt.GridBagLayout;
014: import java.awt.Insets;
015: import java.awt.event.ActionEvent;
016: import java.awt.event.ActionListener;
017: import javax.swing.JTextField;
018: import javax.swing.JTextArea;
019: import javax.swing.JScrollPane;
020: import javax.swing.JPanel;
021: import pnuts.ext.NonPublicMemberAccessor;
022: import pnuts.lang.Configuration;
023: import pnuts.lang.Context;
024: import pnuts.lang.Pnuts;
025: import pnuts.tools.ContextEvent;
026: import pnuts.tools.ContextListener;
027: import pnuts.tools.DebugContext;
028:
029: public class InspectWatchView extends JPanel implements ContextListener {
030: private final static Configuration debugConfig = new NonPublicMemberAccessor();
031: private final static Font monospaced = Font.getFont("monospaced");
032:
033: JTextField inspectField;
034: JTextArea result;
035: Context currentContext;
036:
037: public InspectWatchView() {
038: inspectField = new JTextField(20);
039: inspectField.setBackground(Color.white);
040: inspectField.setFont(monospaced);
041: inspectField.setAutoscrolls(true);
042:
043: result = new JTextArea(2, 0);
044: result.setLineWrap(true);
045: result.setEditable(false);
046: JScrollPane resultpane = new JScrollPane(result);
047: resultpane.setPreferredSize(new Dimension(300, 180));
048:
049: GridBagLayout gb = new GridBagLayout();
050: setLayout(gb);
051: GridBagConstraints c = new GridBagConstraints();
052: c.gridx = 0;
053: c.gridy = 0;
054: c.gridwidth = 1;
055: c.gridheight = 1;
056: c.weightx = 1;
057: c.weighty = 0;
058: c.anchor = GridBagConstraints.CENTER;
059: c.fill = GridBagConstraints.HORIZONTAL;
060: c.insets = new Insets(2, 2, 2, 2);
061: gb.setConstraints(inspectField, c);
062: add(inspectField);
063:
064: c.gridy = 1;
065: c.weighty = 1;
066: c.fill = GridBagConstraints.BOTH;
067: gb.setConstraints(resultpane, c);
068: add(resultpane);
069:
070: inspectField.addActionListener(new ActionListener() {
071: public void actionPerformed(ActionEvent e) {
072: if (currentContext == null) {
073: return;
074: }
075: try {
076: Context c = (Context) ((DebugContext) currentContext)
077: .clone(false, false, true);
078: c.setConfiguration(debugConfig);
079: Object r = Pnuts.eval(inspectField.getText(), c);
080: result.setText(Pnuts.format(r));
081: } catch (Exception e2) {
082: result.setText(e2.toString());
083: }
084: }
085: });
086:
087: }
088:
089: public void update(ContextEvent event) {
090: this .currentContext = event.getContext();
091: String watch = inspectField.getText();
092: if (watch != null && watch.length() > 0) {
093: try {
094: Context c = (Context) ((DebugContext) currentContext)
095: .clone(false, false, true);
096: c.setConfiguration(debugConfig);
097: Object r = Pnuts.eval(watch, c);
098: result.setText(Pnuts.format(r));
099: } catch (Exception e2) {
100: result.setText(e2.toString());
101: }
102: } else {
103: result.setText("");
104: }
105: }
106: }
|