public MainPanel() { super(new BorderLayout()); JEditorPane editor = new JEditorPane("text/html", String.format("<html><a href='%s'>%s</a>", MYSITE, MYSITE)); editor.setOpaque(false); editor.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE); editor.setEditable(false); editor.addHyperlinkListener( new HyperlinkListener() { @Override public void hyperlinkUpdate(HyperlinkEvent e) { if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED && Desktop.isDesktopSupported()) { try { Desktop.getDesktop().browse(new URI(MYSITE)); } catch (IOException | URISyntaxException ex) { ex.printStackTrace(); } textArea.setText(e.toString()); } } }); JPanel p = new JPanel(); p.add(editor); p.setBorder(BorderFactory.createTitledBorder("Desktop.getDesktop().browse(URI)")); add(p, BorderLayout.NORTH); add(new JScrollPane(textArea)); setPreferredSize(new Dimension(320, 240)); }
protected void initContent() { browserPane = new JEditorPane(); browserPane.setEditable(false); browserPane.setSize(1, 1); browserPane.addHyperlinkListener(this); // this frame is the listener getContentPane().add(new JScrollPane(browserPane), BorderLayout.CENTER); }
public EditorPaneHTMLHelp(String htmlFile) { if (GlobalValues.useSystemBrowserForHelp) { Desktop d = GlobalValues.desktop; try { // create a temp file GlobalValues.forHTMLHelptempFile = new File("tempHTMLHelpSynthetic.html"); FileWriter fw = new FileWriter(GlobalValues.forHTMLHelptempFile); java.util.List<String> list = readTextFromJar(htmlFile); Iterator<String> it = list.iterator(); while (it.hasNext()) { fw.write(it.next() + "\n"); } String canonicalPathOfFile = GlobalValues.forHTMLHelptempFile.getCanonicalPath(); URL urlFile = new URL("file://" + canonicalPathOfFile); URI uriOfFile = urlFile.toURI(); fw.close(); d.browse(uriOfFile); } catch (Exception e) { e.printStackTrace(); } } else { URL initialURL = getClass().getResource(htmlFile); font = GlobalValues.htmlfont; String title = "HTML Help"; setTitle(title); final Stack<String> urlStack = new Stack<String>(); final JEditorPane editorPane; editorPane = new JEditorPane(new HTMLEditorKit().getContentType(), " "); editorPane.setOpaque(false); editorPane.setBorder(null); editorPane.setEditable(false); JPanel magPanel = new JPanel(); magnificationFactor = GlobalValues.helpMagnificationFactor; magFactor = new JTextField(Double.toString(magnificationFactor)); JButton magButton = new JButton("Set Magnification: "); magButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { magnificationFactor = Double.parseDouble(magFactor.getText()); GlobalValues.helpMagnificationFactor = magnificationFactor; } }); magPanel.setLayout(new GridLayout(1, 2)); magPanel.add(magButton); magPanel.add(magFactor); final JTextField url = new JTextField(initialURL.toString()); // set up hyperlink listener editorPane.setEditable(false); try { // remember URL for back button urlStack.push(initialURL.toString()); // show URL in text field url.setText(initialURL.toString()); // add a CSS rule to force body tags to use the default label font // instead of the value in javax.swing.text.html.default.csss String bodyRule = "body { font-family: " + font.getFamily() + "; " + "font-size: " + font.getSize() * GlobalValues.helpMagnificationFactor + "pt; }"; ((HTMLDocument) editorPane.getDocument()).getStyleSheet().addRule(bodyRule); editorPane.setPage(initialURL); editorPane.firePropertyChange("dummyProp", true, false); } catch (IOException e) { editorPane.setText("Exception: " + e); } editorPane.addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { String bodyRule = "body { font-family: " + font.getFamily() + "; " + "font-size: " + font.getSize() * GlobalValues.helpMagnificationFactor + "pt; }"; ((HTMLDocument) editorPane.getDocument()).getStyleSheet().addRule(bodyRule); } }); editorPane.addHyperlinkListener( new HyperlinkListener() { public void hyperlinkUpdate(HyperlinkEvent event) { if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { try { // remember URL for back button urlStack.push(event.getURL().toString()); // show URL in text field url.setText(event.getURL().toString()); editorPane.setPage(event.getURL()); editorPane.firePropertyChange("dummyProp", true, false); } catch (IOException e) { editorPane.setText("Exception: " + e); } } } }); // set up checkbox for toggling edit mode final JCheckBox editable = new JCheckBox(); editable.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { editorPane.setEditable(editable.isSelected()); } }); // set up load button for loading URL ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent event) { try { // remember URL for back button urlStack.push(url.getText()); editorPane.setPage(url.getText()); editorPane.firePropertyChange("dummyProp", true, false); } catch (IOException e) { editorPane.setText("Exception: " + e); } } }; JButton loadButton = new JButton("Load/Magnify"); loadButton.addActionListener(listener); url.addActionListener(listener); // set up back button and button action JButton backButton = new JButton("Back"); backButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { if (urlStack.size() <= 1) return; try { // get URL from back button urlStack.pop(); // show URL in text field String urlString = urlStack.peek(); url.setText(urlString); editorPane.setPage(urlString); editorPane.firePropertyChange("dummyProp", true, false); } catch (IOException e) { editorPane.setText("Exception: " + e); } } }); JPanel allPanel = new JPanel(new BorderLayout()); // put all control components in a panel JPanel ctrlPanel = new JPanel(new BorderLayout()); JPanel urlPanel = new JPanel(); urlPanel.add(new JLabel("URL")); urlPanel.add(url); JPanel buttonPanel = new JPanel(); buttonPanel.add(loadButton); buttonPanel.add(backButton); buttonPanel.add(new JLabel("Editable")); buttonPanel.add(magPanel); buttonPanel.add(editable); ctrlPanel.add(buttonPanel, BorderLayout.NORTH); ctrlPanel.add(urlPanel, BorderLayout.CENTER); allPanel.add(ctrlPanel, BorderLayout.NORTH); allPanel.add(new JScrollPane(editorPane), BorderLayout.CENTER); add(allPanel); } }
/** Construct a new Help panel */ public HelpPanel() { // Try to build our help tree, keeping an eye out for malformed URLs try { // root node top = new DefaultMutableTreeNode(new HelpItem("MIDIMatrix", "top")); // matrices node category = new DefaultMutableTreeNode(new HelpItem("Matrices", "matrices")); top.add(category); category.add(new DefaultMutableTreeNode(new HelpItem("Note Entry", "matrices-noteentry"))); category.add( new DefaultMutableTreeNode( new HelpItem("Pitches, volume, and instruments", "matrices-metadata"))); category.add(new DefaultMutableTreeNode(new HelpItem("Playback", "matrices-playback"))); // sequences node category = new DefaultMutableTreeNode(new HelpItem("Sequences", "sequences")); top.add(category); category.add( new DefaultMutableTreeNode(new HelpItem("Parts of a sequence", "sequences-parts"))); category.add( new DefaultMutableTreeNode( new HelpItem("Constructing a sequence", "sequences-construction"))); category.add(new DefaultMutableTreeNode(new HelpItem("Playback", "sequences-playback"))); // controls node category = new DefaultMutableTreeNode(new HelpItem("Controls", "controls")); top.add(category); category.add( new DefaultMutableTreeNode(new HelpItem("MIDI devices", "controls-mididevices"))); category.add(new DefaultMutableTreeNode(new HelpItem("Saving", "controls-saving"))); // about node category = new DefaultMutableTreeNode(new HelpItem("About", "about")); top.add(category); category.add(new DefaultMutableTreeNode(new HelpItem("Author", "about-author"))); category.add(new DefaultMutableTreeNode(new HelpItem("License", "about-license"))); // javadoc node // category = new DefaultMutableTreeNode(new HelpItem("JavaDoc", base + "javadoc/")); // top.add(category); } catch (MalformedURLException e) { top = new DefaultMutableTreeNode("ERROR"); } helpTree = new JTree(top); helpTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); helpTree.addTreeSelectionListener( new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) helpTree.getLastSelectedPathComponent(); if (node == null) { return; } // try { helpPane.scrollToReference(((HelpItem) node.getUserObject()).getHash()); /*} catch (IOException exc) { JOptionPane.showMessageDialog( null, "There was a problem fetching the help page " + exc.getMessage() + "\n" + "Make sure you're connected to the internet before continuing", "Help Oops!", JOptionPane.ERROR_MESSAGE, null); }*/ } }); treePane = new JScrollPane(helpTree); treePane.setPreferredSize(new Dimension(200, 750)); helpPane = new JEditorPane(); scroller = new JScrollPane(helpPane); helpPane.setEditable(false); helpPane.setPreferredSize(new Dimension(550, 750)); helpPane.addHyperlinkListener( new HyperlinkListener() { 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); if (e.getDescription().indexOf("#") != -1) { System.err.println("Found anchor"); pane.scrollToReference( e.getDescription().substring(e.getDescription().indexOf("#"))); } } else { try { pane.setPage(e.getURL()); } catch (Throwable t) { t.printStackTrace(); } } } } }); try { helpPane.setPage(HelpPanel.class.getResource("help.xhtml")); } catch (IOException e) { helpPane.setText("Couldn't fetch help page, sorry! " + e.getMessage()); } content = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); content.add(treePane); content.add(scroller); add(content); }