1 | /**
|
---|
2 | * JSPanel.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.items;
|
---|
20 |
|
---|
21 | import java.awt.Graphics;
|
---|
22 | import java.awt.Graphics2D;
|
---|
23 |
|
---|
24 | import javax.script.Invocable;
|
---|
25 | import javax.swing.JPanel;
|
---|
26 |
|
---|
27 | public class JSPanel extends JPanel {
|
---|
28 |
|
---|
29 | private static final long serialVersionUID = 1L;
|
---|
30 |
|
---|
31 | private final Invocable invocable;
|
---|
32 | private final String functionName;
|
---|
33 | private boolean autoPaint;
|
---|
34 |
|
---|
35 | public JSPanel(Invocable invocable, String functionName, boolean autoPaint) {
|
---|
36 | super();
|
---|
37 | this.invocable = invocable;
|
---|
38 | this.functionName = functionName;
|
---|
39 | this.autoPaint = autoPaint;
|
---|
40 | this.setIgnoreRepaint(true);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public JSPanel(Invocable invocable, String functionName) {
|
---|
44 | this(invocable, functionName, true);
|
---|
45 | }
|
---|
46 |
|
---|
47 | private boolean paintNext = false;
|
---|
48 | @Override
|
---|
49 | public void paintComponent(Graphics g) {
|
---|
50 | if(!autoPaint && !paintNext)
|
---|
51 | return;
|
---|
52 | paintNext = false;
|
---|
53 | try {
|
---|
54 | invocable.invokeFunction(functionName, (Graphics2D) g);
|
---|
55 | } catch (Exception e) {
|
---|
56 | e.printStackTrace();
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Calls paintComponent()
|
---|
62 | * The only way to cause a repaint if autoPaint is false
|
---|
63 | */
|
---|
64 | public void doPaint() {
|
---|
65 | paintNext = true;
|
---|
66 | this.repaint();
|
---|
67 | }
|
---|
68 |
|
---|
69 | public void setAutoPaint(boolean autoPaint) {
|
---|
70 | this.autoPaint = autoPaint;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public boolean getAutoPaint() {
|
---|
74 | return autoPaint;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Make some padding around the drawable area so we don't fight with the item outline
|
---|
79 | */
|
---|
80 | @Override
|
---|
81 | public int getX() {
|
---|
82 | return super.getX() + 5;
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public int getY() {
|
---|
87 | return super.getY() + 5;
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | public int getWidth() {
|
---|
92 | return super.getWidth() - 10;
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public int getHeight() {
|
---|
97 | return super.getHeight() - 10;
|
---|
98 | }
|
---|
99 | }
|
---|