File: HTMLDialog.java

package info (click to toggle)
imagej 1.54g-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,520 kB
  • sloc: java: 132,209; sh: 286; xml: 255; makefile: 6
file content (154 lines) | stat: -rw-r--r-- 4,988 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package ij.gui;
import ij.*;
import ij.plugin.URLOpener;
import ij.macro.MacroRunner;
import ij.util.Java2;
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 or non-modal dialog box that displays HTML formated text. */
public class HTMLDialog extends JDialog implements ActionListener, KeyListener, HyperlinkListener, WindowListener {
	private boolean escapePressed;
	private JEditorPane editorPane;
	private boolean modal = true;

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

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

	public HTMLDialog(String title, String message, boolean modal) {
		super(ij.IJ.getInstance(), title, modal);
		this.modal = modal;
		init(message);
	}
	
	private void init(String message) {
		LookAndFeel saveLookAndFeel = Java2.getLookAndFeel();
		Java2.setSystemLookAndFeel();
		Container container = getContentPane();
		container.setLayout(new BorderLayout());
		if (message==null) message = "";
		editorPane = new JEditorPane("text/html","");
		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);    //display the html text with the above style
		editorPane.getActionMap().put("insert-break", new AbstractAction(){
				public void actionPerformed(ActionEvent e) {}		
		}); //suppress beep on <ENTER> key
		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);
		addWindowListener(this);
		pack();
		Dimension screenD = IJ.getScreenSize();
		Dimension dialogD = getSize();
		int maxWidth = (int)(Math.min(0.70*screenD.width, 800)); //max 70% of screen width, but not more than 800 pxl
		if (maxWidth>400 && dialogD.width>maxWidth)
			dialogD.width = maxWidth;
		if ("Channels".equals(getTitle()))
			dialogD.height = 1000;
		if (dialogD.height>0.80*screenD.height && screenD.height>400)  //max 80% of screen height
			dialogD.height = (int)(0.80*screenD.height);
		setSize(dialogD);
		GUI.centerOnImageJScreen(this);		
		if (!modal) {
			WindowManager.addWindow(this);
			show();
		}
		final JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar();
		if (verticalScrollBar!=null) {
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					verticalScrollBar.setValue(verticalScrollBar.getMinimum()); //start scrollbar at top
				}
			});
		}
		if (modal) show();
		Java2.setLookAndFeel(saveLookAndFeel);
	}

	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);
			}
		}
	}

	public void dispose() {
		super.dispose();
		if (!modal) WindowManager.removeWindow(this);
	}
	
	public void windowClosing(WindowEvent e) {
		dispose();
    }
    
    public void windowActivated(WindowEvent e) {}
    public void windowOpened(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
    
}