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 TreeIconDemo2 extends JFrame {
013: private JEditorPane htmlPane;
014: static boolean DEBUG = false;
015: URL helpURL;
016:
017: public TreeIconDemo2() {
018: super ("TreeIconDemo2");
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: //Enable tool tips.
031: ToolTipManager.sharedInstance().registerComponent(tree);
032:
033: /*
034: * Set the icon for leaf nodes.
035: * Note: In the Swing 1.0.x release, we used
036: * swing.plaf.basic.BasicTreeCellRenderer.
037: */
038: tree.setCellRenderer(new MyRenderer());
039:
040: //Listen for when the selection changes.
041: tree.addTreeSelectionListener(new TreeSelectionListener() {
042: public void valueChanged(TreeSelectionEvent e) {
043: DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
044: .getLastSelectedPathComponent();
045:
046: if (node == null)
047: return;
048:
049: Object nodeInfo = node.getUserObject();
050: if (node.isLeaf()) {
051: BookInfo book = (BookInfo) nodeInfo;
052: displayURL(book.bookURL);
053: if (DEBUG) {
054: System.out.print(book.bookURL + ": \n ");
055: }
056: } else {
057: displayURL(helpURL);
058: }
059: if (DEBUG) {
060: System.out.println(nodeInfo.toString());
061: }
062: }
063: });
064:
065: //Create the scroll pane and add the tree to it.
066: JScrollPane treeView = new JScrollPane(tree);
067:
068: //Create the HTML viewing pane.
069: htmlPane = new JEditorPane();
070: htmlPane.setEditable(false);
071: initHelp();
072: JScrollPane htmlView = new JScrollPane(htmlPane);
073:
074: //Add the scroll panes to a split pane.
075: JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
076: splitPane.setTopComponent(treeView);
077: splitPane.setBottomComponent(htmlView);
078: Dimension minimumSize = new Dimension(100, 50);
079: htmlView.setMinimumSize(minimumSize);
080: treeView.setMinimumSize(minimumSize);
081: splitPane.setDividerLocation(100); //XXX: ignored in some releases
082: //of Swing. bug 4101306
083: //workaround for bug 4101306:
084: //treeView.setPreferredSize(new Dimension(100, 100));
085:
086: splitPane.setPreferredSize(new Dimension(500, 300));
087:
088: //Add the split pane to this frame.
089: getContentPane().add(splitPane, BorderLayout.CENTER);
090: }
091:
092: private class BookInfo {
093: public String bookName;
094: public URL bookURL;
095: public String prefix = "file:" + System.getProperty("user.dir")
096: + System.getProperty("file.separator");
097:
098: public BookInfo(String book, String filename) {
099: bookName = book;
100: try {
101: bookURL = new URL(prefix + filename);
102: } catch (java.net.MalformedURLException exc) {
103: System.err.println("Attempted to create a BookInfo "
104: + "with a bad URL: " + bookURL);
105: bookURL = null;
106: }
107: }
108:
109: public String toString() {
110: return bookName;
111: }
112: }
113:
114: private void initHelp() {
115: String s = null;
116: try {
117: s = "file:" + System.getProperty("user.dir")
118: + System.getProperty("file.separator")
119: + "TreeDemoHelp.html";
120: if (DEBUG) {
121: System.out.println("Help URL is " + s);
122: }
123: helpURL = new URL(s);
124: displayURL(helpURL);
125: } catch (Exception e) {
126: System.err.println("Couldn't create help URL: " + s);
127: }
128: }
129:
130: void displayURL(URL url) {
131: try {
132: htmlPane.setPage(url);
133: } catch (IOException e) {
134: System.err.println("Attempted to read a bad URL: " + url);
135: }
136: }
137:
138: private void createNodes(DefaultMutableTreeNode top) {
139: DefaultMutableTreeNode category = null;
140: DefaultMutableTreeNode book = null;
141:
142: category = new DefaultMutableTreeNode(
143: "Books for Java Programmers");
144: top.add(category);
145:
146: //original Tutorial
147: book = new DefaultMutableTreeNode(
148: new BookInfo(
149: "The Java Tutorial: Object-Oriented Programming for the Internet",
150: "tutorial.html"));
151: category.add(book);
152:
153: //Tutorial Continued
154: book = new DefaultMutableTreeNode(new BookInfo(
155: "The Java Tutorial Continued: The Rest of the JDK",
156: "tutorialcont.html"));
157: category.add(book);
158:
159: //JFC Swing Tutorial
160: book = new DefaultMutableTreeNode(new BookInfo(
161: "The JFC Swing Tutorial: A Guide to Constructing GUIs",
162: "swingtutorial.html"));
163: category.add(book);
164:
165: //Arnold/Gosling
166: book = new DefaultMutableTreeNode(new BookInfo(
167: "The Java Programming Language", "arnold.html"));
168: category.add(book);
169:
170: //FAQ
171: book = new DefaultMutableTreeNode(new BookInfo("The Java FAQ",
172: "faq.html"));
173: category.add(book);
174:
175: //Chan/Lee
176: book = new DefaultMutableTreeNode(new BookInfo(
177: "The Java Class Libraries: An Annotated Reference",
178: "chanlee.html"));
179: category.add(book);
180:
181: //Threads
182: book = new DefaultMutableTreeNode(
183: new BookInfo(
184: "Concurrent Programming in Java: Design Principles and Patterns",
185: "thread.html"));
186: category.add(book);
187:
188: category = new DefaultMutableTreeNode(
189: "Books for Java Implementers");
190: top.add(category);
191:
192: //VM
193: book = new DefaultMutableTreeNode(new BookInfo(
194: "The Java Virtual Machine Specification", "vm.html"));
195: category.add(book);
196:
197: //Language Spec
198: book = new DefaultMutableTreeNode(new BookInfo(
199: "The Java Language Specification", "jls.html"));
200: category.add(book);
201: }
202:
203: public static void main(String[] args) {
204: JFrame frame = new TreeIconDemo2();
205:
206: frame.addWindowListener(new WindowAdapter() {
207: public void windowClosing(WindowEvent e) {
208: System.exit(0);
209: }
210: });
211:
212: frame.pack();
213: frame.setVisible(true);
214: }
215:
216: private class MyRenderer extends DefaultTreeCellRenderer {
217: ImageIcon tutorialIcon;
218:
219: public MyRenderer() {
220: tutorialIcon = new ImageIcon("images/middle.gif");
221: }
222:
223: public Component getTreeCellRendererComponent(JTree tree,
224: Object value, boolean sel, boolean expanded,
225: boolean leaf, int row, boolean focus) {
226:
227: super .getTreeCellRendererComponent(tree, value, sel,
228: expanded, leaf, row, hasFocus);
229: if (leaf && isTutorialBook(value)) {
230: setIcon(tutorialIcon);
231: setToolTipText("This book is in the Tutorial series.");
232: } else {
233: setToolTipText(null);
234: }
235:
236: return this ;
237: }
238:
239: protected boolean isTutorialBook(Object value) {
240: DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
241: BookInfo nodeInfo = (BookInfo) (node.getUserObject());
242: String title = nodeInfo.bookName;
243: if (title.indexOf("Tutorial") >= 0) {
244: return true;
245: }
246:
247: return false;
248: }
249: }
250: }
|