001: /*
002: JSmooth: a VM wrapper toolkit for Windows
003: Copyright (C) 2003 Rodrigo Reyes <reyes@charabia.net>
004:
005: This program is free software; you can redistribute it and/or modify
006: it under the terms of the GNU General Public License as published by
007: the Free Software Foundation; either version 2 of the License, or
008: (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public License
016: along with this program; if not, write to the Free Software
017: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018:
019: */
020:
021: package net.charabia.jsmoothgen.application.gui.util;
022:
023: import java.util.jar.*;
024: import java.util.*;
025: import javax.swing.tree.*;
026: import javax.swing.event.*;
027:
028: public class ClassChooserDialog extends javax.swing.JDialog {
029: JarEntryTreeNode m_root;
030:
031: private boolean m_valid = false;
032:
033: public class ClassTreeListener implements TreeSelectionListener {
034: public void valueChanged(TreeSelectionEvent e) {
035: TreePath tp = e.getPath();
036: JarEntryTreeNode last = (JarEntryTreeNode) tp
037: .getLastPathComponent();
038: if (last.getChildCount() > 0) {
039: m_buttonSelect.setEnabled(false);
040: } else {
041: m_buttonSelect.setEnabled(true);
042: }
043: }
044: }
045:
046: public class JarEntryTreeNode extends
047: javax.swing.tree.DefaultMutableTreeNode {
048: public JarEntryTreeNode(String value) {
049: super (value);
050: DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
051: java.net.URL imgurl = getClass().getResource(
052: "/icons/stock_form-autopilots-16.png");
053: if (imgurl != null) {
054: javax.swing.ImageIcon leaf = new javax.swing.ImageIcon(
055: imgurl);
056: renderer.setLeafIcon(leaf);
057: }
058: m_tree.setCellRenderer(renderer);
059: m_tree.addTreeSelectionListener(new ClassTreeListener());
060: m_tree.setEditable(false);
061: }
062:
063: /**
064: * Attach a java path to this node.
065: */
066: public void add(String[] items) {
067: JarEntryTreeNode current = this ;
068: for (int i = 0; i < items.length; i++) {
069: JarEntryTreeNode next = null;
070:
071: for (Enumeration e = current.children(); (e
072: .hasMoreElements())
073: && (next == null);) {
074: JarEntryTreeNode jtn = (JarEntryTreeNode) e
075: .nextElement();
076: if (jtn.getUserObject().equals(items[i])) {
077: next = jtn;
078: }
079: }
080:
081: if (next == null) {
082: next = new JarEntryTreeNode(items[i]);
083: current.add(next);
084: current = next;
085: } else {
086: current = next;
087: }
088: }
089: }
090: }
091:
092: /** Creates new form ClassChooserDialog */
093: public ClassChooserDialog(java.awt.Frame parent, boolean modal) {
094: super (parent, modal);
095: initComponents();
096: }
097:
098: public void clear() {
099: JarEntryTreeNode root = new JarEntryTreeNode(
100: "Available Classes");
101: m_root = root;
102: m_tree.setModel(new DefaultTreeModel(m_root));
103: }
104:
105: public void addJar(JarFile jf) {
106: for (Enumeration e = jf.entries(); e.hasMoreElements();) {
107: JarEntry entry = (JarEntry) e.nextElement();
108: String[] res = entry.toString().split("/");
109: // System.out.println("JarEntry: " + entry);
110: if ((res.length > 0)
111: && (res[res.length - 1].toLowerCase()
112: .endsWith(".class"))) {
113: String rs = res[res.length - 1];
114: rs = rs.substring(0, rs.length() - 6);
115: res[res.length - 1] = rs;
116: m_root.add(res);
117: }
118: }
119: }
120:
121: public void setJar(JarFile jf) {
122: JarEntryTreeNode root = new JarEntryTreeNode(jf.getName());
123: m_root = root;
124:
125: m_tree.setModel(new DefaultTreeModel(root));
126: }
127:
128: public String getClassName() {
129: TreePath path = m_tree.getSelectionPath();
130: Object[] objs = path.getPath();
131: StringBuffer cname = new StringBuffer();
132: for (int i = 1; i < objs.length; i++) {
133: if (i > 1)
134: cname.append(".");
135: cname.append(((JarEntryTreeNode) objs[i]).getUserObject()
136: .toString());
137: }
138: return cname.toString();
139: }
140:
141: public void setClassName(String classname) {
142: if (m_root == null)
143: return;
144:
145: String[] res = classname.split("[.]");
146: JarEntryTreeNode node = m_root;
147: JarEntryTreeNode best = m_root;
148:
149: for (int i = 0; i < res.length; i++) {
150: String item = res[i];
151: JarEntryTreeNode next = null;
152: for (Enumeration e = node.children(); e.hasMoreElements();) {
153: JarEntryTreeNode opt = (JarEntryTreeNode) e
154: .nextElement();
155:
156: if (item.equals(opt.getUserObject()))
157: next = opt;
158: }
159:
160: node = next;
161: if (node != null)
162: best = node;
163: }
164:
165: TreePath tp = new TreePath(best.getPath());
166: m_tree.setSelectionPath(tp);
167: m_tree.makeVisible(tp);
168: }
169:
170: /** This method is called from within the constructor to
171: * initialize the form.
172: * WARNING: Do NOT modify this code. The content of this method is
173: * always regenerated by the Form Editor.
174: */
175: private void initComponents()//GEN-BEGIN:initComponents
176: {
177: jPanel1 = new javax.swing.JPanel();
178: jLabel1 = new javax.swing.JLabel();
179: jPanel3 = new javax.swing.JPanel();
180: jScrollPane1 = new javax.swing.JScrollPane();
181: m_tree = new javax.swing.JTree();
182: jPanel2 = new javax.swing.JPanel();
183: m_buttonSelect = new javax.swing.JButton();
184: jSeparator1 = new javax.swing.JSeparator();
185: m_buttonCancel = new javax.swing.JButton();
186:
187: setTitle("Class Selector");
188: addWindowListener(new java.awt.event.WindowAdapter() {
189: public void windowClosing(java.awt.event.WindowEvent evt) {
190: closeDialog(evt);
191: }
192: });
193:
194: jPanel1.setLayout(new java.awt.BorderLayout());
195:
196: jPanel1.setBorder(new javax.swing.border.EmptyBorder(
197: new java.awt.Insets(10, 10, 10, 10)));
198: jPanel1.setFocusable(false);
199: jLabel1.setText("Select a class...");
200: jLabel1.setFocusable(false);
201: jPanel1.add(jLabel1, java.awt.BorderLayout.NORTH);
202:
203: jPanel3.setLayout(new java.awt.BorderLayout());
204:
205: jPanel3.setBorder(new javax.swing.border.EmptyBorder(
206: new java.awt.Insets(1, 1, 1, 1)));
207: jPanel3.setFocusable(false);
208: jScrollPane1.setFocusable(false);
209: jScrollPane1.setViewportView(m_tree);
210:
211: jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
212:
213: jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);
214:
215: getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
216:
217: jPanel2.setFocusable(false);
218: m_buttonSelect.setIcon(new javax.swing.ImageIcon(getClass()
219: .getResource("/icons/stock_calc-accept-16.png")));
220: m_buttonSelect.setText("Select");
221: m_buttonSelect
222: .addActionListener(new java.awt.event.ActionListener() {
223: public void actionPerformed(
224: java.awt.event.ActionEvent evt) {
225: buttonSelectActionPerformed(evt);
226: }
227: });
228:
229: jPanel2.add(m_buttonSelect);
230:
231: jPanel2.add(jSeparator1);
232:
233: m_buttonCancel.setIcon(new javax.swing.ImageIcon(getClass()
234: .getResource("/icons/stock_calc-cancel-16.png")));
235: m_buttonCancel.setText("Cancel");
236: m_buttonCancel
237: .addActionListener(new java.awt.event.ActionListener() {
238: public void actionPerformed(
239: java.awt.event.ActionEvent evt) {
240: buttonCancelActionPerformed(evt);
241: }
242: });
243:
244: jPanel2.add(m_buttonCancel);
245:
246: getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
247:
248: java.awt.Dimension screenSize = java.awt.Toolkit
249: .getDefaultToolkit().getScreenSize();
250: setBounds((screenSize.width - 371) / 2,
251: (screenSize.height - 260) / 2, 371, 260);
252: }//GEN-END:initComponents
253:
254: private void buttonSelectActionPerformed(
255: java.awt.event.ActionEvent evt)//GEN-FIRST:event_buttonSelectActionPerformed
256: {//GEN-HEADEREND:event_buttonSelectActionPerformed
257: // Add your handling code here:
258: m_valid = true;
259: setVisible(false);
260: }//GEN-LAST:event_buttonSelectActionPerformed
261:
262: private void buttonCancelActionPerformed(
263: java.awt.event.ActionEvent evt)//GEN-FIRST:event_buttonCancelActionPerformed
264: {//GEN-HEADEREND:event_buttonCancelActionPerformed
265: // Add your handling code here:
266: m_valid = false;
267: setVisible(false);
268: }//GEN-LAST:event_buttonCancelActionPerformed
269:
270: /** Closes the dialog */
271: private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
272: m_valid = false;
273: setVisible(false);
274: dispose();
275: }//GEN-LAST:event_closeDialog
276:
277: /**
278: * @param args the command line arguments
279: */
280: public static void main(String args[]) {
281: new ClassChooserDialog(new javax.swing.JFrame(), true)
282: .setVisible(true);
283: }
284:
285: public boolean validated() {
286: return m_valid;
287: }
288:
289: // Variables declaration - do not modify//GEN-BEGIN:variables
290: private javax.swing.JLabel jLabel1;
291: private javax.swing.JPanel jPanel1;
292: private javax.swing.JPanel jPanel2;
293: private javax.swing.JPanel jPanel3;
294: private javax.swing.JScrollPane jScrollPane1;
295: private javax.swing.JSeparator jSeparator1;
296: private javax.swing.JButton m_buttonCancel;
297: private javax.swing.JButton m_buttonSelect;
298: private javax.swing.JTree m_tree;
299: // End of variables declaration//GEN-END:variables
300:
301: }
|