1 19 package org.lucane.applications.helpbrowser; 20 21 import org.lucane.client.*; 22 import org.lucane.client.widgets.ManagedWindow; 23 import org.lucane.common.*; 24 25 import java.awt.*; 26 import java.awt.event.*; 27 import java.util.Iterator ; 28 29 import javax.swing.*; 30 import javax.swing.event.*; 31 import javax.swing.tree.*; 32 33 36 public class HelpBrowser extends StandalonePlugin 37 implements TreeSelectionListener, ActionListener 38 { 39 private ManagedWindow window; 41 private JTree sections; 42 private JEditorPane bighelp; 43 private JTextArea minihelp; 44 private HelpParser parser; 45 private JComboBox plugins; 46 47 50 public HelpBrowser() 51 { 52 } 53 54 57 public Plugin newInstance(ConnectInfo[] friends) 58 { 59 return new HelpBrowser(); 60 } 61 62 65 public void start() 66 { 67 window = new ManagedWindow(this, getTitle()); 68 window.getContentPane().setLayout(new BorderLayout()); 69 window.setName("frame"); 70 71 bighelp = new JEditorPane(); 72 bighelp.setEditable(false); 73 bighelp.setContentType("text/html"); 74 minihelp = new JTextArea(""); 75 minihelp.setEditable(false); 76 bighelp.addHyperlinkListener(new LinkListener(this, bighelp, minihelp)); 77 78 JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, 79 new JScrollPane(bighelp), 80 new JScrollPane(minihelp)); 81 split.setOneTouchExpandable(true); 82 split.setName("split"); 83 window.getContentPane().add(split, BorderLayout.CENTER); 84 sections = new JTree(new DefaultMutableTreeNode()); 85 sections.setRootVisible(false); 86 sections.setEditable(false); 87 sections.setShowsRootHandles(true); 88 sections.addTreeSelectionListener(this); 89 window.getContentPane().add(sections, BorderLayout.WEST); 90 plugins = new JComboBox(); 91 plugins.addActionListener(this); 92 93 Iterator i = PluginManager.getInstance().getAvailablePlugins(); 94 while(i.hasNext()) 95 plugins.addItem(i.next()); 96 97 window.getContentPane().add(plugins, BorderLayout.NORTH); 98 window.setPreferredSize(new Dimension(600, 400)); 99 window.show(); 100 101 split.setDividerLocation(310); 102 } 103 104 105 107 public void actionPerformed(ActionEvent ae) 108 { 109 Plugin p = (Plugin)plugins.getSelectedItem(); 110 if(p != null) 111 showHelp(p); 112 } 113 114 public void valueChanged(TreeSelectionEvent tse) 115 { 116 TreePath tp = sections.getSelectionPath(); 117 118 if(tp != null) 119 showHelpSection(tp); 120 } 121 122 124 127 public void showHelp(Plugin p) 128 { 129 try 130 { 131 parser = new HelpParser(p.getDirectory() + "help/", tr("file")); 132 sections.setModel(new DefaultTreeModel(parser.getSections())); 133 expandSections(); 134 135 bighelp.setText(""); 136 137 TreeNode root = (TreeNode)sections.getModel().getRoot(); 139 TreePath path = new TreePath(root); 140 path = path.pathByAddingChild(((DefaultMutableTreeNode)root).getFirstChild()); 141 sections.setSelectionPath(path); 142 valueChanged(null); 143 } 144 catch(Exception e) 145 { 146 sections.setModel(new DefaultTreeModel(null)); 147 bighelp.setText("<html><head></head><body>" + tr("nohelp") + "</body></html>"); 148 } 149 150 minihelp.setText(""); 151 } 152 153 public void hidePluginList() 154 { 155 this.plugins.setVisible(false); 156 } 157 158 161 public void gotoSection(String section) 162 { 163 TreeNode root = (TreeNode)sections.getModel().getRoot(); 164 TreePath path = new TreePath(root); 165 path = findSection(path, section); 166 167 if(path != null) 168 { 169 sections.setSelectionPath(path); 170 showHelpSection(path); 171 } 172 } 173 174 177 private void showHelpSection(TreePath tp) 178 { 179 bighelp.setText(parser.htmlForSection(tp)); 180 bighelp.setCaretPosition(0); 181 } 182 183 186 private TreePath findSection(TreePath path, String section) 187 { 188 TreePath result = null; 189 DefaultMutableTreeNode n = (DefaultMutableTreeNode)path.getLastPathComponent(); 190 191 if(section.equals(n.getUserObject())) 192 return path; 193 194 TreePath newpath; 195 196 for(int i = 0; i < n.getChildCount(); i++) 197 { 198 newpath = path.pathByAddingChild(n.getChildAt(i)); 199 result = findSection(newpath, section); 200 201 if(result != null) 202 break; 203 } 204 205 return result; 206 } 207 208 211 private void expandSections() 212 { 213 for(int i=0;i<this.sections.getRowCount();i++) 214 { 215 this.sections.expandRow(i); 216 } 217 } 218 } | Popular Tags |