Displaying HTML message with no extra beeps

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Displaying HTML message with no extra beeps

Michael Schmid
Hi to all Java Gurus out there:

Currently I am trying to write a simple plugin that can display a HTML
message, similar to the ImageJ-builtin HTMLDialog (and maybe for
eventually replacing it?), but with the possibility to show longer
messages with scrollbars, and possibly more bells & whistles (hyperlinks,
copy).

The usual way to do this seems to be a JEditorPane.

The Problem: A readonly (non-editable) JEditorPane always beeps if one
presses <DELETE> or <ENTER>; the latter is a nuisance: If the focus is in
the text (the JEditorPane), not the 'OK' button, and the user presses
<ENTER> to close the window, it beeps.
Tried with Java 1.5 and 1.6.

My code (including a test program) is below.
I tried various things, including adding a DocumentFilter that does
nothing on insert/replace/delete operations, to no avail.

Does anyone have an idea how to avoid those beeps?

Michael
______________________________________________________________________
<SNIP file HTML_Dialog.java>


import ij.IJ;
import ij.gui.*;//##
import ij.plugin.URLOpener;
import ij.macro.MacroRunner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkEvent.EventType;
import java.net.URL;

/** This is modal dialog box that displays HTML formated text. */
public class HTML_Dialog extends JDialog implements ActionListener,
KeyListener, HyperlinkListener {
        private boolean escapePressed;
        private JEditorPane editorPane;
        //private JTextPane editorPane;

        public HTML_Dialog(String title, String message) {
                super(ij.IJ.getInstance(), title, true);
                init(message);
        }

        public HTML_Dialog(Dialog parent, String title, String message) {
                super(parent, title, true);
                init(message);
        }

        private void init(String message) {
                ij.util.Java2.setSystemLookAndFeel();
                Container container = getContentPane();
                container.setLayout(new BorderLayout());
                if (message==null) message = "";
                editorPane = new JEditorPane("text/html","");
                //editorPane = new JTextPane(new HTMLDocument());
                editorPane.setEditable(false);
        HTMLEditorKit kit = new HTMLEditorKit();
        editorPane.setEditorKit(kit);
        StyleSheet styleSheet = kit.getStyleSheet();
        styleSheet.addRule("body{font-family:Verdana,sans-serif;
font-size:11.5pt; margin:5px 10px 5px 10px;}"); //top right bottom
left
        styleSheet.addRule("h1{font-size:18pt;}");
        styleSheet.addRule("h2{font-size:15pt;}");
        styleSheet.addRule("dl dt{font-face:bold;}");
        editorPane.setText(message);
                JScrollPane scrollPane = new JScrollPane(editorPane);
                container.add(scrollPane);
                JButton button = new JButton("OK");
                button.addActionListener(this);
                button.addKeyListener(this);
                editorPane.addKeyListener(this);
                editorPane.addHyperlinkListener(this);
                JPanel panel = new JPanel();
                panel.add(button);
                container.add(panel, "South");
                setForeground(Color.black);
                pack();
                GUI.center(this);
                show();
        }

        public void actionPerformed(ActionEvent e) {
                dispose();
        }

        public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                ij.IJ.setKeyDown(keyCode);
                escapePressed = keyCode==KeyEvent.VK_ESCAPE;
                if (keyCode==KeyEvent.VK_C) {
                    if (editorPane.getSelectedText()==null ||
editorPane.getSelectedText().length()==0)
                        editorPane.selectAll();
                    editorPane.copy();
                    editorPane.select(0,0);
                } else if (keyCode==KeyEvent.VK_ENTER || keyCode==KeyEvent.VK_W ||
escapePressed)
                        dispose();
        }

        public void keyReleased(KeyEvent e) {
                int keyCode = e.getKeyCode();
                ij.IJ.setKeyUp(keyCode);
        }

        public void keyTyped(KeyEvent e) {}

        public boolean escapePressed() {
                return escapePressed;
        }

    public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            String url = e.getDescription(); //getURL does not work for
relative links within document such as "#top"
            if (url==null) return;
            if (url.startsWith("#"))
                editorPane.scrollToReference(url.substring(1));
            else {
                String macro = "run('URL...', 'url="+url+"');";
                            new MacroRunner(macro);
                        }
        }
    }
}

<SNAP>
______________________________________________________________________
<SNIP file Test_HTMLDialog.java>

import ij.*;
import ij.plugin.*;

public class Test_HTMLDialog implements PlugIn {
    //Help texts (html, but without the <html> tags)
    private static final String SHORT_TEXT =
            "<html>first line: short<p>"+
            "This is the second paragraph of our short text."+
            "</html>";

    public void run(String arg) {
        new HTML_Dialog("Short dialog...", SHORT_TEXT);
    }

}

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Displaying HTML message with no extra beeps

Michael Schmid
Hi everyone,

in case someone else should need it, in the meanwhile I found a solution for suppressing the beep when typing <ENTER> in a readonly (non-editable) JEditorPane:

        editorPane.getActionMap().put("insert-break", new AbstractAction(){      
                        public void actionPerformed(ActionEvent e) {}
                }); //suppress beep on <ENTER> key

So the trick is entering an empty action for 'insert-break' (a funny name for pressing the ENTER key; the other actions have more meaningful names).  Having an empty 'beep' action did not help.

Michael
________________________________________________________________
On Jan 4, 2015, at 21:48, Michael Schmid wrote:

> Hi to all Java Gurus out there:
>
> Currently I am trying to write a simple plugin that can display a HTML
> message, similar to the ImageJ-builtin HTMLDialog (and maybe for
> eventually replacing it?), but with the possibility to show longer
> messages with scrollbars, and possibly more bells & whistles (hyperlinks,
> copy).
>
> The usual way to do this seems to be a JEditorPane.
>
> The Problem: A readonly (non-editable) JEditorPane always beeps if one
> presses <DELETE> or <ENTER>; the latter is a nuisance: If the focus is in
> the text (the JEditorPane), not the 'OK' button, and the user presses
> <ENTER> to close the window, it beeps.
> Tried with Java 1.5 and 1.6.
>
> My code (including a test program) is below.
> I tried various things, including adding a DocumentFilter that does
> nothing on insert/replace/delete operations, to no avail.
>
> Does anyone have an idea how to avoid those beeps?
>
> Michael

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html