|
1 | | -/* |
2 | | - * Copyright 2017 Chris2011. |
3 | | - * |
4 | | - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | - * you may not use this file except in compliance with the License. |
6 | | - * You may obtain a copy of the License at |
7 | | - * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | - * |
10 | | - * Unless required by applicable law or agreed to in writing, software |
11 | | - * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | - * See the License for the specific language governing permissions and |
14 | | - * limitations under the License. |
15 | | - */ |
16 | | -package org.chrisle.netbeans.plugins.utils; |
17 | | - |
18 | | -import java.awt.Color; |
19 | | -import java.awt.Component; |
20 | | -import java.awt.KeyboardFocusManager; |
21 | | -import java.awt.event.ActionEvent; |
22 | | -import java.awt.event.KeyEvent; |
23 | | -import java.awt.event.WindowEvent; |
24 | | -import java.awt.event.WindowFocusListener; |
25 | | -import javax.swing.JComponent; |
26 | | -import javax.swing.JDialog; |
27 | | -import javax.swing.KeyStroke; |
28 | | -import net.java.html.js.JavaScriptBody; |
29 | | -import org.w3c.dom.Element; |
30 | | -import org.w3c.dom.NodeList; |
31 | | -import org.w3c.dom.events.Event; |
32 | | -import org.w3c.dom.events.EventTarget; |
33 | | - |
34 | | -/** |
35 | | - * |
36 | | - * @author Chris2011 |
37 | | - */ |
38 | | -public class WebViewDialog extends JDialog { |
39 | | - private static final long serialVersionUID = 5885621373197292877L; |
40 | | - |
41 | | - public void init(JComponent jfxPanel) { |
42 | | - super.add(jfxPanel); |
43 | | - super.setSize(550, 436); |
44 | | - super.setResizable(false); |
45 | | - super.setAlwaysOnTop(true); |
46 | | - super.setUndecorated(true); |
47 | | - super.getRootPane().setOpaque(false); |
48 | | - super.getContentPane().setBackground(new Color(0, 0, 0, 0)); |
49 | | - super.setBackground(new Color(0, 0, 0, 0)); |
50 | | - |
51 | | - super.addWindowFocusListener(new WindowFocusListener() { |
52 | | - @Override |
53 | | - public void windowGainedFocus(WindowEvent e) { |
54 | | - } |
55 | | - |
56 | | - @Override |
57 | | - public void windowLostFocus(WindowEvent e) { |
58 | | - WebViewDialog.this.setVisible(false); |
59 | | - } |
60 | | - }); |
61 | | - |
62 | | - super.getRootPane().registerKeyboardAction((ActionEvent e1) -> { |
63 | | - super.setVisible(false); |
64 | | - }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); |
65 | | - } |
66 | | - |
67 | | - @JavaScriptBody(args = { "id", "tag", "css" }, body = "" |
68 | | - + "var sourceElem = id ? document.getElementById(id) : document.getElementsByTagName(tag)[0];" |
69 | | - + "sourceElem.setAttribute('style', css);") |
70 | | - public native void colorizeElement(String id, String tag, String css); |
71 | | - |
72 | | - private void addHoverEffectToElement(Element sourceElem, String newCss, String oldCss) { |
73 | | - ((EventTarget) sourceElem).addEventListener("mouseover", (Event elem) -> { |
74 | | - sourceElem.setAttribute("style", newCss); |
75 | | - }, false); |
76 | | - |
77 | | - ((EventTarget) sourceElem).addEventListener("mouseout", (Event elem) -> { |
78 | | - sourceElem.setAttribute("style", oldCss); |
79 | | - }, false); |
80 | | - } |
81 | | - |
82 | | - public void addHoverEffectToElements(NodeList sourceElements, String newCss, String oldCss) { |
83 | | - for (int i = 0; i < sourceElements.getLength(); i++) { |
84 | | - addHoverEffectToElement((Element) sourceElements.item(i), newCss, oldCss); |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - public void showDialog() { |
89 | | - // try to use monitor, where the input focus is |
90 | | - // therefor get the topmost component based on the input focus |
91 | | - Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); |
92 | | - |
93 | | - if (null != focusOwner) { |
94 | | - while (focusOwner.getParent() != null) { |
95 | | - focusOwner = focusOwner.getParent(); |
96 | | - } |
97 | | - } |
98 | | - |
99 | | - super.setLocationRelativeTo(focusOwner); |
100 | | - super.setVisible(true); |
101 | | - } |
102 | | -} |
| 1 | +/* |
| 2 | + * Copyright 2017 Chris2011. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.chrisle.netbeans.plugins.utils; |
| 17 | + |
| 18 | +import java.awt.Color; |
| 19 | +import java.awt.Component; |
| 20 | +import java.awt.KeyboardFocusManager; |
| 21 | +import java.awt.event.ActionEvent; |
| 22 | +import java.awt.event.KeyEvent; |
| 23 | +import java.awt.event.WindowEvent; |
| 24 | +import java.awt.event.WindowFocusListener; |
| 25 | +import javax.swing.JComponent; |
| 26 | +import javax.swing.JDialog; |
| 27 | +import javax.swing.KeyStroke; |
| 28 | +import net.java.html.js.JavaScriptBody; |
| 29 | +import org.w3c.dom.Element; |
| 30 | +import org.w3c.dom.NodeList; |
| 31 | +import org.w3c.dom.events.Event; |
| 32 | +import org.w3c.dom.events.EventTarget; |
| 33 | + |
| 34 | +/** |
| 35 | + * |
| 36 | + * @author Chris2011 |
| 37 | + */ |
| 38 | +public class WebViewDialog extends JDialog { |
| 39 | + private static final long serialVersionUID = 5885621373197292877L; |
| 40 | + |
| 41 | + public void init(JComponent jfxPanel) { |
| 42 | + super.add(jfxPanel); |
| 43 | + super.setSize(550, 438); |
| 44 | + super.setResizable(false); |
| 45 | + super.setAlwaysOnTop(true); |
| 46 | + super.setUndecorated(true); |
| 47 | + super.getRootPane().setOpaque(false); |
| 48 | + super.getContentPane().setBackground(new Color(0, 0, 0, 0)); |
| 49 | + super.setBackground(new Color(0, 0, 0, 0)); |
| 50 | + |
| 51 | + super.addWindowFocusListener(new WindowFocusListener() { |
| 52 | + @Override |
| 53 | + public void windowGainedFocus(WindowEvent e) { |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void windowLostFocus(WindowEvent e) { |
| 58 | + WebViewDialog.this.setVisible(false); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + super.getRootPane().registerKeyboardAction((ActionEvent e1) -> { |
| 63 | + super.setVisible(false); |
| 64 | + }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); |
| 65 | + } |
| 66 | + |
| 67 | + @JavaScriptBody(args = { "id", "tag", "css" }, body = "" |
| 68 | + + "var sourceElem = id ? document.getElementById(id) : document.getElementsByTagName(tag)[0];" |
| 69 | + + "sourceElem.setAttribute('style', css);") |
| 70 | + public native void colorizeElement(String id, String tag, String css); |
| 71 | + |
| 72 | + private void addHoverEffectToElement(Element sourceElem, String newCss, String oldCss) { |
| 73 | + ((EventTarget) sourceElem).addEventListener("mouseover", (Event elem) -> { |
| 74 | + sourceElem.setAttribute("style", newCss); |
| 75 | + }, false); |
| 76 | + |
| 77 | + ((EventTarget) sourceElem).addEventListener("mouseout", (Event elem) -> { |
| 78 | + sourceElem.setAttribute("style", oldCss); |
| 79 | + }, false); |
| 80 | + } |
| 81 | + |
| 82 | + public void addHoverEffectToElements(NodeList sourceElements, String newCss, String oldCss) { |
| 83 | + for (int i = 0; i < sourceElements.getLength(); i++) { |
| 84 | + addHoverEffectToElement((Element) sourceElements.item(i), newCss, oldCss); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public void showDialog() { |
| 89 | + // try to use monitor, where the input focus is |
| 90 | + // therefor get the topmost component based on the input focus |
| 91 | + Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); |
| 92 | + |
| 93 | + if (null != focusOwner) { |
| 94 | + while (focusOwner.getParent() != null) { |
| 95 | + focusOwner = focusOwner.getParent(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + super.setLocationRelativeTo(focusOwner); |
| 100 | + super.setVisible(true); |
| 101 | + } |
| 102 | +} |
0 commit comments