Skip to content

Commit 6621f62

Browse files
Helper class to load menus in backgroud.
1 parent 60f19a8 commit 6621f62

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed
+33-30
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,105 @@
11
package cc.arduino.view;
22

33
import java.awt.Component;
4-
import java.io.File;
54
import java.util.LinkedList;
65

7-
import javax.swing.Icon;
8-
import javax.swing.ImageIcon;
96
import javax.swing.JMenu;
107
import javax.swing.JMenuItem;
118
import javax.swing.JPopupMenu;
129
import javax.swing.JSeparator;
1310
import javax.swing.event.MenuEvent;
1411
import javax.swing.event.MenuListener;
1512

16-
import processing.app.BaseNoGui;
17-
1813
/**
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+
*
2016
* @author Ricardo JL Rufino - ([email protected])
2117
* @date 13 de mai de 2020
2218
*/
2319
public class JMenuLazy extends JMenu {
24-
20+
2521
private LinkedList<Component> components = new LinkedList<>();
26-
22+
2723
private boolean loading = false;
28-
24+
2925
public JMenuLazy(String s) {
3026
super(s);
31-
addMenuListener(new MenuListenerLazy());
27+
addMenuListener(menuListener);
3228
}
3329

3430
public void setLoading(boolean loading) {
3531
this.loading = loading;
3632
this.setEnabled(!loading);
3733
}
38-
34+
3935
public boolean isLoading() {
4036
return loading;
4137
}
42-
38+
4339
@Override
4440
public Component add(Component c) {
45-
if(isLoading()) {
41+
if (isLoading()) {
4642
components.add(c);
4743
return c;
4844
}
4945
return super.add(c);
5046
}
51-
47+
5248
@Override
5349
public JMenuItem add(JMenuItem c) {
54-
if(isLoading()) {
50+
if (isLoading()) {
5551
components.add(c);
5652
return c;
5753
}
5854
return super.add(c);
5955
}
60-
56+
6157
@Override
6258
public void addSeparator() {
63-
if(isLoading()) {
59+
if (isLoading()) {
6460
components.add(new JPopupMenu.Separator());
6561
return;
6662
}
6763
super.addSeparator();
6864
}
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() {
7174

7275
@Override
7376
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()) {
7780
for (Component component : components) {
78-
if(component instanceof JSeparator) {
81+
if (component instanceof JSeparator) {
7982
JMenuLazy.super.addSeparator();
80-
}else {
83+
} else {
8184
JMenuLazy.super.add(component);
82-
85+
8386
}
8487
}
8588
components.clear();
8689
}
8790
}
8891
}
8992
}
90-
93+
9194
@Override
9295
public void menuDeselected(MenuEvent e) {
93-
96+
9497
}
95-
98+
9699
@Override
97100
public void menuCanceled(MenuEvent e) {
98-
101+
99102
}
100-
}
103+
};
101104

102105
}

0 commit comments

Comments
 (0)