001: /*
002: * @(#)ContextView.java 1.2 04/12/06
003: *
004: * Copyright (c) 2003,2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.tools;
010:
011: import java.awt.Component;
012: import java.awt.Container;
013: import java.awt.Font;
014: import java.awt.GridBagConstraints;
015: import java.awt.GridBagLayout;
016: import java.awt.Insets;
017: import java.util.HashSet;
018: import java.util.Iterator;
019: import java.util.Set;
020: import java.util.StringTokenizer;
021:
022: import javax.swing.JFrame;
023: import javax.swing.JLabel;
024: import javax.swing.JList;
025: import javax.swing.JPanel;
026: import javax.swing.JTable;
027: import javax.swing.JTextArea;
028: import javax.swing.JTextField;
029:
030: import pnuts.ext.NonPublicMemberAccessor;
031: import pnuts.lang.Configuration;
032: import pnuts.lang.Context;
033:
034: class ContextView {
035: private final static Font monospaced = Font.getFont("monospaced");
036: private final static Configuration debugConfig = new NonPublicMemberAccessor();
037:
038: private JTextField currentPackage;
039: private JTextField classLoader;
040: private JList imports;
041: private JList modules;
042: private JTable locals;
043: private JTable contextLocals;
044: private JTextField inspect;
045: private JTextArea result;
046: private Context context;
047: private GridBagLayout gb; // We have to use GBL because this class is loaded by bootclassloader.
048: private VisualDebuggerView debuggerView;
049:
050: public ContextView(VisualDebuggerView debuggerView) {
051: this .debuggerView = debuggerView;
052: }
053:
054: public JFrame getFrame() {
055: JFrame jfr = new JFrame();
056: setupGUI(jfr.getContentPane());
057: jfr.setSize(jfr.getPreferredSize());
058: return jfr;
059: }
060:
061: public Container getContainer() {
062: JPanel panel = new JPanel();
063: setupGUI(panel);
064: panel.setSize(panel.getPreferredSize());
065: return panel;
066: }
067:
068: static void add(GridBagLayout gb, Container container,
069: Component component, int gridx, int gridy, int gridwidth,
070: int gridheight, int weightx, int weighty, int anchor,
071: int fill) {
072: GridBagConstraints c = new GridBagConstraints();
073: c.gridx = gridx;
074: c.gridy = gridy;
075: c.gridwidth = gridwidth;
076: c.gridheight = gridheight;
077: c.weightx = weightx;
078: c.weighty = weighty;
079: c.anchor = anchor;
080: c.fill = fill;
081: c.insets = new Insets(2, 2, 2, 2);
082: gb.setConstraints(component, c);
083: container.add(component);
084: }
085:
086: void addLabel(Container container, Component component, int gridx,
087: int gridy) {
088: add(gb, container, component, gridx, gridy, 1, 1, 0, 0,
089: GridBagConstraints.NORTHEAST, GridBagConstraints.NONE);
090: }
091:
092: void add_h(Container container, Component component, int gridx,
093: int gridy) {
094: add(gb, container, component, gridx, gridy, 1, 1, 1, 0,
095: GridBagConstraints.CENTER,
096: GridBagConstraints.HORIZONTAL);
097: }
098:
099: void add_b(Container container, Component component, int gridx,
100: int gridy) {
101: add(gb, container, component, gridx, gridy, 1, 1, 1, 1,
102: GridBagConstraints.CENTER, GridBagConstraints.BOTH);
103: }
104:
105: void add_n(Container container, Component component, int gridx,
106: int gridy) {
107: add(gb, container, component, gridx, gridy, 1, 1, 0, 0,
108: GridBagConstraints.CENTER, GridBagConstraints.NONE);
109: }
110:
111: JLabel createJLabel(String key) {
112: return new JLabel(debuggerView.getResourceString(key
113: + VisualDebuggerView.labelSuffix));
114: }
115:
116: public void setupGUI(Container contentPane) {
117: this .gb = new GridBagLayout();
118: Font monospaced = Font.getFont("monospaced");
119: contentPane.setLayout(gb);
120:
121: String beansDef = debuggerView
122: .getResourceString("inspector.beans");
123: StringTokenizer st = new StringTokenizer(beansDef, ",");
124: int idx = 0;
125: while (st.hasMoreTokens()) {
126: String token = st.nextToken();
127: String clsName = debuggerView
128: .getResourceString("inspector.bean." + token
129: + ".class");
130: if (clsName != null) {
131: try {
132: Class cls = Class.forName(clsName);
133: Component bean = (Component) cls.newInstance();
134: if (bean instanceof ContextListener) {
135: addContextlistener((ContextListener) bean);
136: }
137: addLabel(contentPane, createJLabel(token), 0, idx);
138: String layoutHint = debuggerView
139: .getResourceString("inspector.bean."
140: + token + ".layout");
141: if (layoutHint != null
142: && layoutHint.toLowerCase().equals("both")) {
143: add_b(contentPane, bean, 1, idx);
144: } else if (layoutHint != null
145: && layoutHint.toLowerCase().equals("none")) {
146: add_n(contentPane, bean, 1, idx);
147: } else { // "horizontal"
148: add_h(contentPane, bean, 1, idx);
149: }
150: idx++;
151: } catch (ClassNotFoundException cnf) {
152: // ignore
153: } catch (IllegalAccessException iae) {
154: // ignore
155: } catch (InstantiationException ie) {
156: // ignore
157: }
158: }
159: }
160: }
161:
162: HashSet listeners = new HashSet();
163:
164: public void addContextlistener(ContextListener listener) {
165: listeners.add(listener);
166: }
167:
168: public void setContext(Context context) {
169: this .context = context;
170: Set listeners = (Set) this .listeners.clone();
171: for (Iterator it = listeners.iterator(); it.hasNext();) {
172: ContextListener listener = (ContextListener) it.next();
173: listener.update(new ContextEvent(context));
174: }
175: }
176: }
|