Skip to content

Commit df26745

Browse files
authored
Merge pull request #11 from vykio/drawing-zone
Drawing zone -> develop
2 parents 47b4c02 + 5e12f53 commit df26745

19 files changed

+1038
-86
lines changed

.idea/java-rdp.iml

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

res/icons/clear.png

485 Bytes
Loading

src/com/tech/app/App.java

+28-22
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,69 @@
55

66
package com.tech.app;
77

8+
import com.tech.app.functions.CheckThreadViolationRepaintManager;
89
import com.tech.app.models.Arc;
910
import com.tech.app.models.Place;
10-
import com.tech.app.models.System;
11+
import com.tech.app.models.Model;
1112
import com.tech.app.models.Transition;
1213
import com.tech.app.windows.MainWindow;
1314

14-
import java.awt.*;
15+
import javax.swing.*;
1516

1617
public class App implements Runnable {
1718
@Override
1819
public void run() {
19-
MainWindow mainWindow = new MainWindow(500,500);
20+
try {
21+
MainWindow mainWindow = new MainWindow(900,500);
22+
} catch (UnsupportedLookAndFeelException e) {
23+
e.printStackTrace();
24+
}
2025

2126
/* Instanciation du système */
22-
System system = new System();
27+
/*Model model = new Model();*/
2328

2429
/* Définition des places et des transitions */
25-
Place p = new Place("P1", 0, 0, 1);
30+
/*Place p = new Place("P1", 0, 0, 1);
2631
Place p2 = new Place("P2", 0, 0, 0);
2732
Place p3 = new Place("P3", 0,0, 0);
2833
Transition t = new Transition("t1", 0, 0);
2934
Transition t2 = new Transition("t2", 0, 0);
30-
35+
*/
3136
/* Ajout des places d'entrées et de sorties */
32-
t.addChildren(p);
37+
/*t.addChildren(p);
3338
t.addParent(new Arc(p, 2));
3439
t.addParent(p3);
3540
t2.addChildren(p2);
3641
t2.addChildren(p3);
37-
t2.addParent(p);
42+
t2.addParent(p);*/
3843

3944
/* Ajout des places et des transitions au système */
40-
system.addPlace(p);
41-
system.addPlace(p2);
42-
system.addPlace(p3);
43-
system.addTransition(t);
44-
system.addTransition(t2);
45+
/* model.addPlace(p);
46+
model.addPlace(p2);
47+
model.addPlace(p3);
48+
model.addTransition(t);
49+
model.addTransition(t2);*/
4550

4651
/* Experimentation */
47-
Place p4 = new Place("P4", 0, 0, 1);
48-
system.addPlace(p4);
49-
Transition t4 = new Transition("t4", 0, 0);
50-
t4.addChildren(new Arc(p, 3));
51-
t4.addParent(p4);
52-
system.addTransition(t4);
52+
//Place p4 = new Place("P4", 0, 0, 1);
53+
//model.addPlace(p4);
54+
//Transition t4 = new Transition("t4", 0, 0);
55+
// t4.addChildren(new Arc(p, 3));
56+
//t4.addParent(p4);
57+
// model.addTransition(t4);
5358

5459
// Doit retourner une petite erreur car "p4" != "P4"
55-
system.removePlace("p4");
60+
// model.removePlace("p4");
5661

5762
/* Affichage du système */
58-
java.lang.System.out.println(system);
63+
//java.lang.System.out.println(model);
5964

6065
}
6166

6267

6368
public synchronized void start() {
64-
new Thread(this).start();
69+
SwingUtilities.invokeLater(this);
70+
RepaintManager.setCurrentManager(new CheckThreadViolationRepaintManager());
6571
}
6672

6773
public static void main(String[] args) {new App().start();}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* This library is free software; you can redistribute it and/or
3+
* modify it under the terms of the GNU Lesser General Public
4+
* License as published by the Free Software Foundation; either
5+
* version 2.1 of the License, or (at your option) any later version.
6+
*
7+
* This library is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10+
* Lesser General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU Lesser General Public
13+
* License along with this library; if not, write to the Free Software
14+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15+
*/
16+
17+
package com.tech.app.functions;
18+
19+
import java.lang.ref.WeakReference;
20+
21+
import javax.swing.JButton;
22+
import javax.swing.JComponent;
23+
import javax.swing.JEditorPane;
24+
import javax.swing.JFrame;
25+
import javax.swing.RepaintManager;
26+
import javax.swing.SwingUtilities;
27+
28+
/**
29+
* <p>
30+
* This class is used to detect Event Dispatch Thread rule violations<br>
31+
* See <a
32+
* href="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How
33+
* to Use Threads</a> for more info
34+
* </p>
35+
* <p/>
36+
* <p>
37+
* This is a modification of original idea of Scott Delap<br>
38+
* Initial version of ThreadCheckingRepaintManager can be found here<br>
39+
* <a href="http://www.clientjava.com/blog/2004/08/20/1093059428000.html">Easily
40+
* Find Swing Threading Mistakes</a>
41+
* </p>
42+
*
43+
* @author Scott Delap
44+
* @author Alexander Potochkin
45+
*
46+
* https://swinghelper.dev.java.net/
47+
*/
48+
public class CheckThreadViolationRepaintManager extends RepaintManager {
49+
// it is recommended to pass the complete check
50+
private boolean completeCheck = true;
51+
52+
private WeakReference<JComponent> lastComponent;
53+
54+
public CheckThreadViolationRepaintManager(boolean completeCheck) {
55+
this.completeCheck = completeCheck;
56+
}
57+
58+
public CheckThreadViolationRepaintManager() {
59+
this(true);
60+
}
61+
62+
public boolean isCompleteCheck() {
63+
return completeCheck;
64+
}
65+
66+
public void setCompleteCheck(boolean completeCheck) {
67+
this.completeCheck = completeCheck;
68+
}
69+
70+
public synchronized void addInvalidComponent(JComponent component) {
71+
checkThreadViolations(component);
72+
super.addInvalidComponent(component);
73+
}
74+
75+
public void addDirtyRegion(JComponent component, int x, int y, int w, int h) {
76+
checkThreadViolations(component);
77+
super.addDirtyRegion(component, x, y, w, h);
78+
}
79+
80+
private void checkThreadViolations(JComponent c) {
81+
if (!SwingUtilities.isEventDispatchThread() && (completeCheck || c.isShowing())) {
82+
boolean repaint = false;
83+
boolean fromSwing = false;
84+
boolean imageUpdate = false;
85+
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
86+
for (StackTraceElement st : stackTrace) {
87+
if (repaint && st.getClassName().startsWith("javax.swing.")) {
88+
fromSwing = true;
89+
}
90+
if (repaint && "imageUpdate".equals(st.getMethodName())) {
91+
imageUpdate = true;
92+
}
93+
if ("repaint".equals(st.getMethodName())) {
94+
repaint = true;
95+
fromSwing = false;
96+
}
97+
}
98+
if (imageUpdate) {
99+
// assuming it is java.awt.image.ImageObserver.imageUpdate(...)
100+
// image was asynchronously updated, that's ok
101+
return;
102+
}
103+
if (repaint && !fromSwing) {
104+
// no problems here, since repaint() is thread safe
105+
return;
106+
}
107+
// ignore the last processed component
108+
if (lastComponent != null && c == lastComponent.get()) {
109+
return;
110+
}
111+
lastComponent = new WeakReference<JComponent>(c);
112+
violationFound(c, stackTrace);
113+
}
114+
}
115+
116+
protected void violationFound(JComponent c, StackTraceElement[] stackTrace) {
117+
System.out.println();
118+
System.out.println("EDT violation detected");
119+
System.out.println(c);
120+
for (StackTraceElement st : stackTrace) {
121+
System.out.println("\tat " + st);
122+
}
123+
}
124+
125+
public static void main(String[] args) throws Exception {
126+
// set CheckThreadViolationRepaintManager
127+
RepaintManager.setCurrentManager(new CheckThreadViolationRepaintManager());
128+
// Valid code
129+
SwingUtilities.invokeAndWait(new Runnable() {
130+
public void run() {
131+
test();
132+
}
133+
});
134+
System.out.println("Valid code passed...");
135+
repaintTest();
136+
System.out.println("Repaint test - correct code");
137+
// Invalide code (stack trace expected)
138+
test();
139+
}
140+
141+
static void test() {
142+
JFrame frame = new JFrame("Am I on EDT?");
143+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
144+
frame.add(new JButton("JButton"));
145+
frame.pack();
146+
frame.setVisible(true);
147+
frame.dispose();
148+
}
149+
150+
// this test must pass
151+
static void imageUpdateTest() {
152+
JFrame frame = new JFrame();
153+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
154+
JEditorPane editor = new JEditorPane();
155+
frame.setContentPane(editor);
156+
editor.setContentType("text/html");
157+
// it works with no valid image as well
158+
editor.setText("<html><img src=\"file:\\lala.png\"></html>");
159+
frame.setSize(300, 200);
160+
frame.setVisible(true);
161+
}
162+
163+
private static JButton test;
164+
165+
static void repaintTest() {
166+
try {
167+
SwingUtilities.invokeAndWait(new Runnable() {
168+
public void run() {
169+
test = new JButton();
170+
test.setSize(100, 100);
171+
}
172+
});
173+
} catch (Exception e) {
174+
e.printStackTrace();
175+
}
176+
// repaint(Rectangle) should be ok
177+
test.repaint(test.getBounds());
178+
test.repaint(0, 0, 100, 100);
179+
test.repaint();
180+
}
181+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.tech.app.functions;
2+
3+
public class FMaths {
4+
5+
public static double round(double number, int precision) {
6+
double scale = Math.pow(10, precision);
7+
return Math.round(number * scale) / scale;
8+
}
9+
10+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.tech.app.functions;
2+
3+
import java.util.Locale;
4+
5+
public final class FUtils {
6+
7+
public static class OS {
8+
9+
public static String getOs() {
10+
return System.getProperty("os.name");
11+
}
12+
13+
public static boolean isMacOs() {
14+
return (getOs().toLowerCase(Locale.ROOT).contains("max"));
15+
}
16+
17+
}
18+
19+
20+
21+
}

0 commit comments

Comments
 (0)