|
1 | 1 | package cc.arduino.view;
|
2 | 2 |
|
3 | 3 | import java.awt.Component;
|
4 |
| -import java.io.File; |
5 | 4 | import java.util.LinkedList;
|
6 | 5 |
|
7 |
| -import javax.swing.Icon; |
8 |
| -import javax.swing.ImageIcon; |
9 | 6 | import javax.swing.JMenu;
|
10 | 7 | import javax.swing.JMenuItem;
|
11 | 8 | import javax.swing.JPopupMenu;
|
12 | 9 | import javax.swing.JSeparator;
|
13 | 10 | import javax.swing.event.MenuEvent;
|
14 | 11 | import javax.swing.event.MenuListener;
|
15 | 12 |
|
16 |
| -import processing.app.BaseNoGui; |
17 |
| - |
18 | 13 | /**
|
19 |
| - * Avoid break rendering of menu while load full menu tree in background |
| 14 | + * Avoid slow rendering of menu while load full menu tree in background |
| 15 | + * |
20 | 16 | * @author Ricardo JL Rufino - ([email protected])
|
21 | 17 | * @date 13 de mai de 2020
|
22 | 18 | */
|
23 | 19 | public class JMenuLazy extends JMenu {
|
24 |
| - |
| 20 | + |
25 | 21 | private LinkedList<Component> components = new LinkedList<>();
|
26 |
| - |
| 22 | + |
27 | 23 | private boolean loading = false;
|
28 |
| - |
| 24 | + |
29 | 25 | public JMenuLazy(String s) {
|
30 | 26 | super(s);
|
31 |
| - addMenuListener(new MenuListenerLazy()); |
| 27 | + addMenuListener(menuListener); |
32 | 28 | }
|
33 | 29 |
|
34 | 30 | public void setLoading(boolean loading) {
|
35 | 31 | this.loading = loading;
|
36 | 32 | this.setEnabled(!loading);
|
37 | 33 | }
|
38 |
| - |
| 34 | + |
39 | 35 | public boolean isLoading() {
|
40 | 36 | return loading;
|
41 | 37 | }
|
42 |
| - |
| 38 | + |
43 | 39 | @Override
|
44 | 40 | public Component add(Component c) {
|
45 |
| - if(isLoading()) { |
| 41 | + if (isLoading()) { |
46 | 42 | components.add(c);
|
47 | 43 | return c;
|
48 | 44 | }
|
49 | 45 | return super.add(c);
|
50 | 46 | }
|
51 |
| - |
| 47 | + |
52 | 48 | @Override
|
53 | 49 | public JMenuItem add(JMenuItem c) {
|
54 |
| - if(isLoading()) { |
| 50 | + if (isLoading()) { |
55 | 51 | components.add(c);
|
56 | 52 | return c;
|
57 | 53 | }
|
58 | 54 | return super.add(c);
|
59 | 55 | }
|
60 |
| - |
| 56 | + |
61 | 57 | @Override
|
62 | 58 | public void addSeparator() {
|
63 |
| - if(isLoading()) { |
| 59 | + if (isLoading()) { |
64 | 60 | components.add(new JPopupMenu.Separator());
|
65 | 61 | return;
|
66 | 62 | }
|
67 | 63 | super.addSeparator();
|
68 | 64 | }
|
69 |
| - |
70 |
| - private class MenuListenerLazy implements MenuListener{ |
| 65 | + |
| 66 | + public void removeAll() { |
| 67 | + super.removeAll(); |
| 68 | + synchronized (components) { |
| 69 | + components.clear(); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + private MenuListener menuListener = new MenuListener() { |
71 | 74 |
|
72 | 75 | @Override
|
73 | 76 | public void menuSelected(MenuEvent e) {
|
74 |
| - if(!isLoading()) { |
75 |
| - if(loading == false) { |
76 |
| - if(!components.isEmpty()) { |
| 77 | + if (!isLoading()) { |
| 78 | + if (loading == false) { |
| 79 | + if (!components.isEmpty()) { |
77 | 80 | for (Component component : components) {
|
78 |
| - if(component instanceof JSeparator) { |
| 81 | + if (component instanceof JSeparator) { |
79 | 82 | JMenuLazy.super.addSeparator();
|
80 |
| - }else { |
| 83 | + } else { |
81 | 84 | JMenuLazy.super.add(component);
|
82 |
| - |
| 85 | + |
83 | 86 | }
|
84 | 87 | }
|
85 | 88 | components.clear();
|
86 | 89 | }
|
87 | 90 | }
|
88 | 91 | }
|
89 | 92 | }
|
90 |
| - |
| 93 | + |
91 | 94 | @Override
|
92 | 95 | public void menuDeselected(MenuEvent e) {
|
93 |
| - |
| 96 | + |
94 | 97 | }
|
95 |
| - |
| 98 | + |
96 | 99 | @Override
|
97 | 100 | public void menuCanceled(MenuEvent e) {
|
98 |
| - |
| 101 | + |
99 | 102 | }
|
100 |
| - } |
| 103 | + }; |
101 | 104 |
|
102 | 105 | }
|
0 commit comments