public void showItem() { if (!this.editorPanel.getViewer().isLanguageFunctionAvailable()) { return; } this.highlight = this.editorPanel .getEditor() .addHighlight(this.position, this.position + this.word.length(), null, true); final FindSynonymsActionHandler _this = this; QTextEditor editor = this.editorPanel.getEditor(); Rectangle r = null; try { r = editor.modelToView(this.position); } catch (Exception e) { // BadLocationException! Environment.logError("Location: " + this.position + " is not valid", e); UIUtils.showErrorMessage(this.editorPanel, "Unable to display synonyms."); return; } int y = r.y; // Show a panel of all the items. final QPopup p = this.popup; p.setOpaque(false); Synonyms syns = null; try { syns = this.projectViewer.getSynonymProvider().getSynonyms(this.word); } catch (Exception e) { UIUtils.showErrorMessage(this.editorPanel, "Unable to display synonyms."); Environment.logError("Unable to lookup synonyms for: " + word, e); return; } if ((syns.words.size() == 0) && (this.word.toLowerCase().endsWith("ed"))) { // Trim off the ed and try again. try { syns = this.projectViewer.getSynonyms(this.word.substring(0, this.word.length() - 2)); } catch (Exception e) { UIUtils.showErrorMessage(this.editorPanel, "Unable to display synonyms."); Environment.logError("Unable to lookup synonyms for: " + word, e); return; } } if ((syns.words.size() == 0) && (this.word.toLowerCase().endsWith("s"))) { // Trim off the ed and try again. try { syns = this.projectViewer.getSynonyms(this.word.substring(0, this.word.length() - 1)); } catch (Exception e) { UIUtils.showErrorMessage(this.editorPanel, "Unable to display synonyms."); Environment.logError("Unable to lookup synonyms for: " + word, e); return; } } StringBuilder sb = new StringBuilder(); if (syns.words.size() > 0) { sb.append("6px"); for (int i = 0; i < syns.words.size(); i++) { if (sb.length() > 0) { sb.append(", "); } sb.append("p, 3px, [p,90px], 5px"); } /* if (syns.words.size () > 0) { sb.append (",5px"); } */ } else { sb.append("6px, p, 6px"); } FormLayout summOnly = new FormLayout("3px, fill:380px:grow, 3px", sb.toString()); PanelBuilder pb = new PanelBuilder(summOnly); CellConstraints cc = new CellConstraints(); int ind = 2; Map<String, String> names = new HashMap(); names.put(Synonyms.ADJECTIVE + "", "Adjectives"); names.put(Synonyms.NOUN + "", "Nouns"); names.put(Synonyms.VERB + "", "Verbs"); names.put(Synonyms.ADVERB + "", "Adverbs"); names.put(Synonyms.OTHER + "", "Other"); if (syns.words.size() == 0) { JLabel l = new JLabel("No synonyms found."); l.setFont(l.getFont().deriveFont(Font.ITALIC)); pb.add(l, cc.xy(2, 2)); } // Determine what type of word we are looking for. for (Synonyms.Part i : syns.words) { JLabel l = new JLabel(names.get(i.type + "")); l.setFont(l.getFont().deriveFont(Font.ITALIC)); l.setFont(l.getFont().deriveFont((float) UIUtils.getEditorFontSize(10))); l.setBorder( new CompoundBorder( new MatteBorder(0, 0, 1, 0, Environment.getBorderColor()), new EmptyBorder(0, 0, 3, 0))); pb.add(l, cc.xy(2, ind)); ind += 2; HTMLEditorKit kit = new HTMLEditorKit(); HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument(); JTextPane t = new JTextPane(doc); t.setEditorKit(kit); t.setEditable(false); t.setOpaque(false); StringBuilder buf = new StringBuilder( "<style>a { text-decoration: none; } a:hover { text-decoration: underline; }</style><span style='color: #000000; font-size: " + ((int) UIUtils.getEditorFontSize(10) /*t.getFont ().getSize () + 2*/) + "pt; font-family: " + t.getFont().getFontName() + ";'>"); for (int x = 0; x < i.words.size(); x++) { String w = (String) i.words.get(x); buf.append("<a class='x' href='http://" + w + "'>" + w + "</a>"); if (x < (i.words.size() - 1)) { buf.append(", "); } } buf.append("</span>"); t.setText(buf.toString()); t.addHyperlinkListener( new HyperlinkAdapter() { public void hyperlinkUpdate(HyperlinkEvent ev) { if (ev.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { QTextEditor ed = _this.editorPanel.getEditor(); ed.replaceText( _this.position, _this.position + _this.word.length(), ev.getURL().getHost()); ed.removeHighlight(_this.highlight); _this.popup.setVisible(false); _this.projectViewer.fireProjectEvent( ProjectEvent.SYNONYM, ProjectEvent.REPLACE, ev.getURL().getHost()); } } }); // Annoying that we have to do this but it prevents the text from being too small. t.setSize(new Dimension(380, Short.MAX_VALUE)); JScrollPane sp = new JScrollPane(t); t.setCaretPosition(0); sp.setOpaque(false); sp.getVerticalScrollBar().setValue(0); /* sp.setPreferredSize (t.getPreferredSize ()); sp.setMaximumSize (new Dimension (380, 75)); */ sp.getViewport().setOpaque(false); sp.setOpaque(false); sp.setBorder(null); sp.getViewport().setBackground(Color.WHITE); sp.setAlignmentX(Component.LEFT_ALIGNMENT); pb.add(sp, cc.xy(2, ind)); ind += 2; } JPanel pan = pb.getPanel(); pan.setOpaque(true); pan.setBackground(Color.WHITE); this.popup.setContent(pan); // r.y -= this.editorPanel.getScrollPane ().getVerticalScrollBar ().getValue (); Point po = SwingUtilities.convertPoint(editor, r.x, r.y, this.editorPanel); r.x = po.x; r.y = po.y; // Subtract the insets of the editorPanel. Insets ins = this.editorPanel.getInsets(); r.x -= ins.left; r.y -= ins.top; this.editorPanel.showPopupAt(this.popup, r, "above", true); }
private void checkClickOnHyperlink(String html, String link, String expectedTarget) throws Exception { JTextPane textPane = createTextPane(html); DummyHyperlinkListener listener = new DummyHyperlinkListener(); textPane.addHyperlinkListener(listener); TextBox textComponent = new TextBox(textPane); textComponent.clickOnHyperlink(link); assertEquals(1, listener.getCallCount()); assertEquals(expectedTarget, listener.getLastEvent().getDescription()); listener.reset(); textComponent.triggerClickOnHyperlink(link).run(); assertEquals(1, listener.getCallCount()); assertEquals(expectedTarget, listener.getLastEvent().getDescription()); }
/** Enables hyperlinks. Hyperlink anchors load in the text pane. */ public void enableHyperlinks() { if (hyperlinkListener != null) { // remove old listener textPane.removeHyperlinkListener(hyperlinkListener); } hyperlinkListener = new HyperlinkListener() { public void hyperlinkUpdate(HyperlinkEvent e) { if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { try { textPane.setPage(e.getURL()); } catch (IOException ex) { } } } }; textPane.addHyperlinkListener(hyperlinkListener); }
public TOSDialog() { super(null, false, IdeModalityType.IDE); init(); setTitle("eddy - Logging preferences"); setCrossClosesWindow(false); // get TOS page out of our jar or directory final String pathname = PathUtil.getJarPathForClass(TOSDialog.class); final File path = new File(pathname); try { final URL url; if (path.isDirectory()) { url = new File(path, "intro.html").toURI().toURL(); } else { url = ResourceUtil.getResource(TOSDialog.class, "", "intro.html"); } TOSTextPane.setPage(url); } catch (MalformedURLException e) { throw new RuntimeException("Cannot load intro text. Please try reinstalling.", e); } catch (IOException e) { throw new RuntimeException("Cannot load intro text. Please try reinstalling.", e); } HyperlinkListener l = new HyperlinkListener() { @Override public void hyperlinkUpdate(HyperlinkEvent e) { log("got link event: " + e); if (HyperlinkEvent.EventType.ACTIVATED == e.getEventType()) { try { java.awt.Desktop.getDesktop().browse(e.getURL().toURI()); } catch (IOException e1) { // wtf, do nothing log("exception: " + e1); } catch (URISyntaxException e2) { // broken link, do nothing log("exception: " + e2); } } } }; TOSTextPane.addHyperlinkListener(l); }
/** Enables hyperlinks. Hyperlink anchors load in the native browser. */ public void enableDesktopHyperlinks() { if (hyperlinkListener != null) { // remove old listener textPane.removeHyperlinkListener(hyperlinkListener); } hyperlinkListener = new HyperlinkListener() { public void hyperlinkUpdate(HyperlinkEvent e) { if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { try { if (!org.opensourcephysics.desktop.OSPDesktop.browse(e.getURL().toURI())) { // try the old way org.opensourcephysics.desktop.ostermiller.Browser.init(); org.opensourcephysics.desktop.ostermiller.Browser.displayURL( e.getURL().toString()); } } catch (Exception e1) { } } } }; textPane.addHyperlinkListener(hyperlinkListener); }
public GithubLoginPanel(final GithubLoginDialog dialog) { DocumentListener listener = new DocumentAdapter() { @Override protected void textChanged(DocumentEvent e) { dialog.clearErrors(); } }; myLoginTextField.getDocument().addDocumentListener(listener); myPasswordField.getDocument().addDocumentListener(listener); mySignupTextField.setText( "<html>Do not have an account at github.com? <a href=\"https://github.com\">Sign up</a>.</html>"); mySignupTextField.setMargin(new Insets(5, 0, 0, 0)); mySignupTextField.addHyperlinkListener( new HyperlinkAdapter() { @Override protected void hyperlinkActivated(final HyperlinkEvent e) { BrowserUtil.browse(e.getURL()); } }); mySignupTextField.setBackground(UIUtil.TRANSPARENT_COLOR); mySignupTextField.setCursor(new Cursor(Cursor.HAND_CURSOR)); }
About() { window = new JWindow(); this.setPreferredSize(new Dimension(650, 550)); this.setVisible(true); this.setLayout(null); JTextPane text = new JTextPane(); text.setBounds(5, 150, 625, 525); text.setVisible(true); text.setEditable(false); text.setOpaque(false); HTMLEditorKit htmlKit = new HTMLEditorKit() { public Parser getParser() { return super.getParser(); } }; HTMLDocument htmlDoc = new HTMLDocument(); text.addHyperlinkListener( new HyperlinkListener() { public void hyperlinkUpdate(HyperlinkEvent e) { if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { if (Desktop.isDesktopSupported()) { try { Desktop.getDesktop().browse(e.getURL().toURI()); return; } catch (IOException e1) { } catch (URISyntaxException e1) { } } try { Runtime.getRuntime().exec("xdg-open ".concat(e.getURL().toString())); } catch (IOException ioex) { ControlRoom.log( Level.WARNING, "Failed to show file: '" + e.getURL().toString() + "'. Possible unsupported File Manager...", null); } } } }); text.setEditorKit(htmlKit); text.setDocument(htmlDoc); try { BufferedReader reader = new BufferedReader( new InputStreamReader( this.getClass().getResourceAsStream("/resources/about.html"), "UTF-8")); htmlKit.read(reader, htmlDoc, htmlDoc.getLength()); } catch (IOException ioex) { ControlRoom.log(Level.SEVERE, "Failed to read about.html", ioex); } catch (BadLocationException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } text.addMouseListener(this); window.getContentPane().add(text); window.getContentPane().add(this); window.addMouseListener(this); window.pack(); ControlRoom.setWindowRelativeToCentral(window); window.setVisible(true); window.setAlwaysOnTop(true); }