001: package examples.swingdemos;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.io.*;
006: import java.net.*;
007:
008: import javax.swing.*;
009: import javax.swing.event.*;
010: import javax.swing.tree.*;
011:
012: public class TreeIconDemo extends JFrame {
013: private JEditorPane htmlPane;
014: static boolean DEBUG = false;
015: URL helpURL;
016:
017: public TreeIconDemo() {
018: super ("TreeIconDemo");
019:
020: //Create the nodes.
021: DefaultMutableTreeNode top = new DefaultMutableTreeNode(
022: "The Java Series");
023: createNodes(top);
024:
025: //Create a tree that allows one selection at a time.
026: final JTree tree = new JTree(top);
027: tree.getSelectionModel().setSelectionMode(
028: TreeSelectionModel.SINGLE_TREE_SELECTION);
029:
030: /*
031: * Set the icon for leaf nodes.
032: * Note: In the Swing 1.0.x release, we used
033: * swing.plaf.basic.BasicTreeCellRenderer.
034: */
035: DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
036: renderer.setLeafIcon(new ImageIcon("images/middle.gif"));
037: tree.setCellRenderer(renderer);
038:
039: //Listen for when the selection changes.
040: tree.addTreeSelectionListener(new TreeSelectionListener() {
041: public void valueChanged(TreeSelectionEvent e) {
042: DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
043: .getLastSelectedPathComponent();
044:
045: if (node == null)
046: return;
047:
048: Object nodeInfo = node.getUserObject();
049: if (node.isLeaf()) {
050: BookInfo book = (BookInfo) nodeInfo;
051: displayURL(book.bookURL);
052: if (DEBUG) {
053: System.out.print(book.bookURL + ": \n ");
054: }
055: } else {
056: displayURL(helpURL);
057: }
058: if (DEBUG) {
059: System.out.println(nodeInfo.toString());
060: }
061: }
062: });
063:
064: //Create the scroll pane and add the tree to it.
065: JScrollPane treeView = new JScrollPane(tree);
066:
067: //Create the HTML viewing pane.
068: htmlPane = new JEditorPane();
069: htmlPane.setEditable(false);
070: initHelp();
071: JScrollPane htmlView = new JScrollPane(htmlPane);
072:
073: //Add the scroll panes to a split pane.
074: JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
075: splitPane.setTopComponent(treeView);
076: splitPane.setBottomComponent(htmlView);
077: Dimension minimumSize = new Dimension(100, 50);
078: htmlView.setMinimumSize(minimumSize);
079: treeView.setMinimumSize(minimumSize);
080: splitPane.setDividerLocation(100); //XXX: ignored in some releases
081: //of Swing. bug 4101306
082: //workaround for bug 4101306:
083: //treeView.setPreferredSize(new Dimension(100, 100));
084:
085: splitPane.setPreferredSize(new Dimension(500, 300));
086:
087: //Add the split pane to this frame.
088: getContentPane().add(splitPane, BorderLayout.CENTER);
089: }
090:
091: private class BookInfo {
092: public String bookName;
093: public URL bookURL;
094: public String prefix = "file:" + System.getProperty("user.dir")
095: + System.getProperty("file.separator");
096:
097: public BookInfo(String book, String filename) {
098: bookName = book;
099: try {
100: bookURL = new URL(prefix + filename);
101: } catch (java.net.MalformedURLException exc) {
102: System.err.println("Attempted to create a BookInfo "
103: + "with a bad URL: " + bookURL);
104: bookURL = null;
105: }
106: }
107:
108: public String toString() {
109: return bookName;
110: }
111: }
112:
113: private void initHelp() {
114: String s = null;
115: try {
116: s = "file:" + System.getProperty("user.dir")
117: + System.getProperty("file.separator")
118: + "TreeDemoHelp.html";
119: if (DEBUG) {
120: System.out.println("Help URL is " + s);
121: }
122: helpURL = new URL(s);
123: displayURL(helpURL);
124: } catch (Exception e) {
125: System.err.println("Couldn't create help URL: " + s);
126: }
127: }
128:
129: void displayURL(URL url) {
130: try {
131: htmlPane.setPage(url);
132: } catch (IOException e) {
133: System.err.println("Attempted to read a bad URL: " + url);
134: }
135: }
136:
137: private void createNodes(DefaultMutableTreeNode top) {
138: DefaultMutableTreeNode category = null;
139: DefaultMutableTreeNode book = null;
140:
141: category = new DefaultMutableTreeNode(
142: "Books for Java Programmers");
143: top.add(category);
144:
145: //original Tutorial
146: book = new DefaultMutableTreeNode(
147: new BookInfo(
148: "The Java Tutorial: Object-Oriented Programming for the Internet",
149: "tutorial.html"));
150: category.add(book);
151:
152: //Tutorial Continued
153: book = new DefaultMutableTreeNode(new BookInfo(
154: "The Java Tutorial Continued: The Rest of the JDK",
155: "tutorialcont.html"));
156: category.add(book);
157:
158: //JFC Swing Tutorial
159: book = new DefaultMutableTreeNode(new BookInfo(
160: "The JFC Swing Tutorial: A Guide to Constructing GUIs",
161: "swingtutorial.html"));
162: category.add(book);
163:
164: //Arnold/Gosling
165: book = new DefaultMutableTreeNode(new BookInfo(
166: "The Java Programming Language", "arnold.html"));
167: category.add(book);
168:
169: //FAQ
170: book = new DefaultMutableTreeNode(new BookInfo("The Java FAQ",
171: "faq.html"));
172: category.add(book);
173:
174: //Chan/Lee
175: book = new DefaultMutableTreeNode(new BookInfo(
176: "The Java Class Libraries: An Annotated Reference",
177: "chanlee.html"));
178: category.add(book);
179:
180: //Threads
181: book = new DefaultMutableTreeNode(
182: new BookInfo(
183: "Concurrent Programming in Java: Design Principles and Patterns",
184: "thread.html"));
185: category.add(book);
186:
187: category = new DefaultMutableTreeNode(
188: "Books for Java Implementers");
189: top.add(category);
190:
191: //VM
192: book = new DefaultMutableTreeNode(new BookInfo(
193: "The Java Virtual Machine Specification", "vm.html"));
194: category.add(book);
195:
196: //Language Spec
197: book = new DefaultMutableTreeNode(new BookInfo(
198: "The Java Language Specification", "jls.html"));
199: category.add(book);
200: }
201:
202: public static void main(String[] args) {
203: JFrame frame = new TreeIconDemo();
204:
205: frame.addWindowListener(new WindowAdapter() {
206: public void windowClosing(WindowEvent e) {
207: System.exit(0);
208: }
209: });
210:
211: frame.pack();
212: frame.setVisible(true);
213: }
214: }
|