📄 manualviewer.java
字号:
// write the line printWriter.println(line); } printWriter.println("</BODY>"); printWriter.println("</HTML>"); printWriter.close(); } } /** * Method to edit the current page in the manual. * This is an advanced function that is not available to users. */ private void edit() { PageInfo pi = pageSequence.get(currentIndex); JFrame jf = null; if (TopLevel.isMDIMode()) jf = TopLevel.getCurrentJFrame(); EditHTML dialog = new EditHTML(jf, pi.url, this); dialog.setVisible(true); } private static class EditHTML extends EModelessDialog { private JTextArea textArea; private URL file; private ManualViewer world; private EditHTML(Frame parent, URL file, ManualViewer world) { super(parent, false); this.file = file; this.world = world; getContentPane().setLayout(new GridBagLayout()); setTitle("Edit HTML"); setName(""); Dimension maxScreenSize = Toolkit.getDefaultToolkit().getScreenSize(); setPreferredSize(new Dimension(maxScreenSize.width/2, maxScreenSize.height/2)); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { closeDialog(evt); } }); textArea = new JTextArea(); textArea.getDocument().putProperty( DefaultEditorKit.EndOfLineStringProperty, System.getProperty("line.separator") ); JScrollPane scrollPane = new JScrollPane(textArea); GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.weightx = 1; gridBagConstraints.weighty = 1; gridBagConstraints.anchor = GridBagConstraints.CENTER; gridBagConstraints.fill = GridBagConstraints.BOTH; gridBagConstraints.insets = new Insets(4, 4, 4, 4); getContentPane().add(scrollPane, gridBagConstraints); try { URLConnection con = file.openConnection(); InputStream stream = con.getInputStream(); InputStreamReader is = new InputStreamReader(stream); LineNumberReader ln = new LineNumberReader(is); StringBuffer sb = new StringBuffer(); for(;;) { String aLine = ln.readLine(); if (aLine == null) break; sb.append(aLine); sb.append('\n'); } ln.close(); textArea.setText(sb.toString()); textArea.setSelectionStart(0); textArea.setSelectionEnd(0); } catch (IOException e) { System.out.println("Could not find file: " + file.getFile()); return; } pack(); } private void closeDialog(WindowEvent evt) { String fileName = file.getFile(); try { PrintWriter printWriter = new PrintWriter(new BufferedWriter(new FileWriter(fileName))); printWriter.print(textArea.getText()); printWriter.close(); } catch (IOException e) { System.out.println("Could not save file: " + fileName); return; } /* * Because some IDEs keep class files in a separate directory, * the file that is being displayed is only a cache of the real one. * The real one is missing the "/bin/" part. */ int binPos = fileName.indexOf("/bin/"); if (binPos >= 0) { String otherFileName = fileName.substring(0, binPos) + "/srcj" + fileName.substring(binPos+4); try { PrintWriter printWriter = new PrintWriter(new BufferedWriter(new FileWriter(otherFileName))); printWriter.print(textArea.getText()); printWriter.close(); System.out.println("Also saved to "+otherFileName); } catch (IOException e) { System.out.println("Could not also save file: " + otherFileName); System.out.println(" but did save: " + fileName); return; } } setVisible(false); dispose(); world.loadPage(world.currentIndex); } } private static class Hyperactive implements HyperlinkListener { private ManualViewer dialog; Hyperactive(ManualViewer dialog) { this.dialog = dialog; } public void hyperlinkUpdate(HyperlinkEvent e) { if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { JEditorPane pane = (JEditorPane)e.getSource(); if (e instanceof HTMLFrameHyperlinkEvent) { HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent)e; HTMLDocument doc = (HTMLDocument)pane.getDocument(); doc.processHTMLFrameHyperlinkEvent(evt); } else { URL desiredURL = e.getURL(); // first see if it is one of the manual files, in which case it gets auto-generated String desiredFile = desiredURL.getFile(); for(int i=0; i<dialog.pageSequence.size(); i++) { PageInfo pi = dialog.pageSequence.get(i); if (pi.url.getFile().equals(desiredFile)) { dialog.loadPage(i); return; } } // external URL: fetch it try { pane.setPage(desiredURL); } catch (Throwable t) { System.out.println("Cannot find URL "+e.getURL()); } } } } } /** * Initialize list of all ToolTips and initilize components */ private void init() { // set up dialog GridBagConstraints gbc; getContentPane().setLayout(new GridBagLayout()); // setup tree pane for chapter selection (on the left) rootNode = new DefaultMutableTreeNode("Manual"); DefaultTreeModel treeModel = new DefaultTreeModel(rootNode); manualTree = new ManualTree(treeModel, this); TreeHandler handler = new TreeHandler(this); manualTree.addMouseListener(handler); JScrollPane scrolledTree = new JScrollPane(manualTree); // the left side of the options dialog: a tree JPanel leftHalf = new JPanel(); leftHalf.setLayout(new GridBagLayout()); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 4; gbc.gridwidth = 2; gbc.gridheight = 1; gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.insets = new Insets(0, 4, 4, 4); leftHalf.add(scrolledTree, gbc); // forward and backward buttons JButton backButton = new JButton("Back"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.insets = new Insets(4, 4, 4, 4); gbc.anchor = GridBagConstraints.CENTER; leftHalf.add(backButton, gbc); backButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { back(); } }); JButton menuButton = new JButton("Menu Help"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.insets = new Insets(4, 4, 4, 4); gbc.anchor = GridBagConstraints.CENTER; leftHalf.add(menuButton, gbc); menuButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { loadMenuBar(); } }); // Previous and Next buttons JButton nextButton = new JButton("Next"); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.insets = new Insets(4, 4, 4, 4); gbc.anchor = GridBagConstraints.CENTER; leftHalf.add(nextButton, gbc); nextButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { next(); } }); JButton prevButton = new JButton("Prev"); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 1; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.insets = new Insets(4, 4, 4, 4); gbc.anchor = GridBagConstraints.CENTER; leftHalf.add(prevButton, gbc); prevButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { prev(); } }); JSeparator sep = new JSeparator(); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 2; gbc.gridwidth = 2; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; gbc.fill = GridBagConstraints.HORIZONTAL; leftHalf.add(sep, gbc); // forward and backward buttons at the bottom of the left side searchField = new JTextField(); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 3; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.insets = new Insets(4, 4, 4, 4); gbc.weightx = 0.5; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.anchor = GridBagConstraints.CENTER; leftHalf.add(searchField, gbc); JButton searchButton = new JButton("Find"); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 3; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.insets = new Insets(4, 4, 4, 4); gbc.anchor = GridBagConstraints.CENTER; leftHalf.add(searchButton, gbc); searchButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { search(); } }); getRootPane().setDefaultButton(searchButton); if (Job.getDebug()) { // manual and edit buttons at the bottom of the left side JButton manualButton = new JButton("1-Page Man"); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 5; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.insets = new Insets(0, 4, 4, 4); gbc.anchor = GridBagConstraints.CENTER; leftHalf.add(manualButton, gbc); manualButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { manual1Page(); } }); // manual and edit buttons at the bottom of the left side JButton manualMultiButton = new JButton("Many-Page Man"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 5; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.insets = new Insets(0, 4, 4, 4); gbc.anchor = GridBagConstraints.CENTER; leftHalf.add(manualMultiButton, gbc); manualMultiButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { manualManyPages(); } }); JButton editButton = new JButton("Edit Page"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 6; gbc.gridwidth = 2; gbc.gridheight = 1; gbc.insets = new Insets(0, 4, 4, 4); leftHalf.add(editButton, gbc); editButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { edit(); } }); } // set up scroll pane for manual (on the right) editorPane = new JEditorPane(); editorPane.setEditorKit(new HTMLEditorKit()); editorPane.addHyperlinkListener(new Hyperactive(this)); editorPane.setEditable(false); rightHalf = new JScrollPane(editorPane); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); rightHalf.setPreferredSize(new Dimension(screenSize.width/2, screenSize.height*3/4)); rightHalf.setMinimumSize(new Dimension(screenSize.width/4, screenSize.height/3)); // build split pane with both halves splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); splitPane.setLeftComponent(leftHalf); splitPane.setRightComponent(rightHalf); splitPane.setDividerLocation(200); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 1.0; gbc.weighty = 1.0; getContentPane().add(splitPane, gbc); // close of dialog event addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { closeDialog(evt); } }); pack(); } private static class ManualTree extends JTree { private ManualViewer dialog; private ManualTree(DefaultTreeModel treeModel, ManualViewer dialog) { super(treeModel); this.dialog = dialog; // single selection as default getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); // do not show top-level setRootVisible(true); setShowsRootHandles(true); } public String convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { Object nodeInfo = ((DefaultMutableTreeNode)value).getUserObject(); if (nodeInfo instanceof Integer) { Integer index = (Integer)nodeInfo; PageInfo pi = dialog.pageSequence.get(index.intValue()); String ret = pi.title; if (pi.sectionNumber > 0) ret = pi.sectionNumber + ": " + ret; return ret; } return nodeInfo.toString(); } } private static class TreeHandler implements MouseListener { private ManualViewer dialog; TreeHandler(ManualViewer dialog) { this.dialog = dialog; } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) { TreePath currentPath = dialog.manualTree.getPathForLocation(e.getX(), e.getY()); if (currentPath == null) return; dialog.manualTree.expandPath(currentPath); dialog.manualTree.setSelectionPath(currentPath); DefaultMutableTreeNode node = (DefaultMutableTreeNode)currentPath.getLastPathComponent(); Object obj = node.getUserObject(); if (obj instanceof Integer) { int index = ((Integer)obj).intValue(); dialog.loadPage(index); } } } private void closeDialog(WindowEvent evt) { setVisible(false); dispose(); theManual = null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -