001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.swing;
024:
025: import java.awt.GridBagConstraints;
026: import java.awt.GridBagLayout;
027: import java.awt.event.WindowEvent;
028:
029: import javax.swing.JFrame;
030: import javax.swing.JScrollPane;
031: import javax.swing.JSplitPane;
032: import javax.swing.JTable;
033: import javax.swing.JTree;
034: import javax.swing.WindowConstants;
035: import javax.swing.table.TableModel;
036: import javax.swing.tree.DefaultMutableTreeNode;
037: import javax.swing.tree.DefaultTreeModel;
038: import javax.swing.tree.MutableTreeNode;
039: import javax.swing.tree.TreeNode;
040: import javax.swing.tree.TreePath;
041:
042: /**
043: * @author Pavel Vlasov
044: *
045: * @version $Revision: 1.2 $
046: */
047: public class Browser extends JFrame {
048:
049: private javax.swing.JPanel jContentPane = null;
050:
051: private JSplitPane jSplitPane = null;
052:
053: private JScrollPane jScrollPane = null;
054:
055: private JScrollPane jDetailsPane = null;
056:
057: private JTree jTree = null;
058:
059: private JTable jTable = null;
060:
061: /**
062: * This method initializes jSplitPane
063: *
064: * @return javax.swing.JSplitPane
065: */
066: private JSplitPane getJSplitPane() {
067: if (jSplitPane == null) {
068: jSplitPane = new JSplitPane();
069: jSplitPane.setLeftComponent(getJScrollPane());
070: jSplitPane.setRightComponent(getJScrollPane1());
071: jSplitPane.setDividerLocation(400);
072: }
073: return jSplitPane;
074: }
075:
076: /**
077: * This method initializes jScrollPane
078: *
079: * @return javax.swing.JScrollPane
080: */
081: private JScrollPane getJScrollPane() {
082: if (jScrollPane == null) {
083: jScrollPane = new JScrollPane();
084: jScrollPane.setViewportView(getJTree());
085: }
086: return jScrollPane;
087: }
088:
089: /**
090: * This method initializes jScrollPane1
091: *
092: * @return javax.swing.JScrollPane
093: */
094: private JScrollPane getJScrollPane1() {
095: if (jDetailsPane == null) {
096: jDetailsPane = new JScrollPane();
097: jDetailsPane.setViewportView(getJTable());
098: }
099: return jDetailsPane;
100: }
101:
102: /**
103: * This method initializes jTree
104: *
105: * @return javax.swing.JTree
106: */
107: private JTree getJTree() {
108: if (jTree == null) {
109: jTree = new JTree();
110: jTree
111: .addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
112: public void valueChanged(
113: javax.swing.event.TreeSelectionEvent e) {
114: TreePath newLeadSelectionPath = e
115: .getNewLeadSelectionPath();
116: if (newLeadSelectionPath == null) {
117: jTable.setVisible(false);
118: } else {
119: DefaultMutableTreeNode lastPathComponent = (DefaultMutableTreeNode) newLeadSelectionPath
120: .getLastPathComponent();
121: Visualizable v = CompositeVisualizer
122: .getThreadInstance()
123: .toVisualizable(
124: lastPathComponent
125: .getUserObject());
126: TableModel toTable = v.toTable();
127: if (toTable != null) {
128: jTable.setModel(toTable);
129: }
130: jTable.setVisible(toTable != null);
131: //jTable.getColumnModel().getColumn(0).setMaxWidth(150);
132: }
133: }
134: });
135: }
136: return jTree;
137: }
138:
139: /**
140: * This method initializes jTable
141: *
142: * @return javax.swing.JTable
143: */
144: private JTable getJTable() {
145: if (jTable == null) {
146: jTable = new JTable();
147: }
148: return jTable;
149: }
150:
151: /**
152: * @param root
153: */
154: public Browser(String title, TreeNode root) {
155: super ();
156: initialize();
157: setTitle(title);
158: getJTree().setModel(new DefaultTreeModel(root));
159: }
160:
161: /**
162: * This method initializes this
163: *
164: * @return void
165: */
166: private void initialize() {
167: this .setSize(921, 616);
168: this .setContentPane(getJContentPane());
169: this .setTitle("JFrame");
170: }
171:
172: /**
173: * This method initializes jContentPane
174: *
175: * @return javax.swing.JPanel
176: */
177: private javax.swing.JPanel getJContentPane() {
178: if (jContentPane == null) {
179: GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
180: jContentPane = new javax.swing.JPanel();
181: jContentPane.setLayout(new GridBagLayout());
182: gridBagConstraints1.gridx = 0;
183: gridBagConstraints1.gridy = 0;
184: gridBagConstraints1.weightx = 1.0;
185: gridBagConstraints1.weighty = 1.0;
186: gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
187: jContentPane.add(getJSplitPane(), gridBagConstraints1);
188: }
189: return jContentPane;
190: }
191:
192: /**
193: * Shows objects in Browser. Blocks current thread until browser closes.
194: * @param o
195: * @param title
196: */
197: public static void show(Object o, String title) {
198: MutableTreeNode node = CompositeVisualizer.getThreadInstance()
199: .toVisualizable(o).toTree(title);
200: final Object monitor = new Object();
201: Browser browser = new Browser(title, node) {
202: protected void processWindowEvent(WindowEvent e) {
203: super .processWindowEvent(e);
204: if (e.getID() == WindowEvent.WINDOW_CLOSED) {
205: synchronized (monitor) {
206: monitor.notifyAll();
207: }
208: }
209: }
210: };
211:
212: browser
213: .setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
214: browser.setVisible(true);
215:
216: synchronized (monitor) {
217: try {
218: monitor.wait();
219: } catch (InterruptedException ex) {
220: // Ignore
221: }
222: }
223: }
224:
225: /**
226: * Shows object in browser with default close operation <code>WindowConstants.EXIT_ON_CLOSE</code>
227: * @param o
228: * @param title
229: */
230: public static void showAndExitOnClose(Object o, String title) {
231: MutableTreeNode node = CompositeVisualizer.getThreadInstance()
232: .toVisualizable(o).toTree(title);
233: Browser browser = new Browser(title, node);
234: browser.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
235: browser.setVisible(true);
236: }
237:
238: } // @jve:decl-index=0:visual-constraint="10,10"
|