74 |
ilm |
1 |
package org.openconcerto.modules.extensionbuilder.menu.mainmenu;
|
|
|
2 |
|
|
|
3 |
import java.util.Enumeration;
|
|
|
4 |
import java.util.List;
|
|
|
5 |
|
|
|
6 |
import javax.swing.tree.DefaultMutableTreeNode;
|
|
|
7 |
import javax.swing.tree.DefaultTreeModel;
|
|
|
8 |
import javax.swing.tree.MutableTreeNode;
|
|
|
9 |
import javax.swing.tree.TreePath;
|
|
|
10 |
|
|
|
11 |
import org.openconcerto.erp.config.MenuManager;
|
|
|
12 |
import org.openconcerto.modules.extensionbuilder.Extension;
|
|
|
13 |
import org.openconcerto.modules.extensionbuilder.component.ActivableMutableTreeNode;
|
|
|
14 |
import org.openconcerto.ui.group.Group;
|
|
|
15 |
import org.openconcerto.ui.group.Item;
|
|
|
16 |
|
|
|
17 |
public class MenuItemTreeModel extends DefaultTreeModel {
|
|
|
18 |
|
|
|
19 |
private boolean showAll = true;
|
|
|
20 |
private Extension extension;
|
|
|
21 |
|
|
|
22 |
public MenuItemTreeModel() {
|
|
|
23 |
super(null, false);
|
|
|
24 |
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
public void setShowAll(boolean b) {
|
|
|
28 |
this.showAll = b;
|
|
|
29 |
System.err.println("MenuItemTreeModel.setShowAll(): " + b);
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
public void fillFromDescriptor(Extension extension) {
|
|
|
33 |
|
|
|
34 |
if (extension == null) {
|
|
|
35 |
throw new IllegalArgumentException("null extension");
|
|
|
36 |
}
|
|
|
37 |
if (this.extension != null) {
|
|
|
38 |
// this.extension.removeChangeListener(this);
|
|
|
39 |
}
|
|
|
40 |
this.extension = extension;
|
|
|
41 |
// this.extension.addChangeListener(this);
|
|
|
42 |
final Group currentMenuGroup = MenuManager.getInstance().getGroup();
|
|
|
43 |
Group menuGroup = Group.copy(currentMenuGroup, new Group(currentMenuGroup.getId()));
|
|
|
44 |
|
|
|
45 |
extension.initMenuGroup(menuGroup);
|
|
|
46 |
|
|
|
47 |
final ActivableMutableTreeNode root = new ActivableMutableTreeNode(null);
|
|
|
48 |
root.setActive(true);
|
|
|
49 |
if (menuGroup == null) {
|
|
|
50 |
return;
|
|
|
51 |
}
|
|
|
52 |
// FIXME manque des items...
|
|
|
53 |
System.out.println(MenuManager.getInstance().getGroup().printTree());
|
|
|
54 |
|
|
|
55 |
addToTreeNode(root, menuGroup, 0);
|
|
|
56 |
setRoot(root);
|
|
|
57 |
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
void addToTreeNode(DefaultMutableTreeNode node, Item item, int depth) {
|
|
|
61 |
if (depth > 50) {
|
|
|
62 |
return;
|
|
|
63 |
}
|
|
|
64 |
depth++;
|
|
|
65 |
final ActivableMutableTreeNode newChild = new ActivableMutableTreeNode(item);
|
|
|
66 |
newChild.setActive(isActive(item.getId()));
|
|
|
67 |
if (showAll || newChild.isActive()) {
|
|
|
68 |
node.add(newChild);
|
|
|
69 |
}
|
|
|
70 |
if (item instanceof Group) {
|
|
|
71 |
final Group gr = (Group) item;
|
|
|
72 |
final int childCount = gr.getSize();
|
|
|
73 |
for (int i = 0; i < childCount; i++) {
|
|
|
74 |
final Item it = gr.getItem(i);
|
|
|
75 |
addToTreeNode(newChild, it, depth);
|
|
|
76 |
}
|
|
|
77 |
newChild.setAllowsChildren(true);
|
|
|
78 |
} else {
|
|
|
79 |
newChild.setAllowsChildren(false);
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
private boolean isActive(String id) {
|
|
|
84 |
List<MenuDescriptor> l = extension.getRemoveMenuList();
|
|
|
85 |
for (MenuDescriptor menuDescriptor : l) {
|
|
|
86 |
if (menuDescriptor.getId().equals(id)) {
|
|
|
87 |
return false;
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
return true;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
public void toggleActive(TreePath selectionPath) {
|
|
|
94 |
if (selectionPath == null) {
|
|
|
95 |
return;
|
|
|
96 |
}
|
|
|
97 |
final ActivableMutableTreeNode n = (ActivableMutableTreeNode) selectionPath.getLastPathComponent();
|
|
|
98 |
|
|
|
99 |
final Item item = (Item) n.getUserObject();
|
|
|
100 |
if (item instanceof Group) {
|
|
|
101 |
// A Group is always active
|
|
|
102 |
return;
|
|
|
103 |
}
|
|
|
104 |
n.setActive(!n.isActive());
|
|
|
105 |
final String id = item.getId();
|
|
|
106 |
setActive(n.isActive(), id);
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
public void setActive(boolean active, String id) {
|
|
|
110 |
if (active) {
|
|
|
111 |
extension.removeRemoveMenuForId(id);
|
|
|
112 |
} else {
|
|
|
113 |
extension.addRemoveMenu(new MenuDescriptor(id));
|
|
|
114 |
}
|
|
|
115 |
extension.setChanged();
|
|
|
116 |
|
|
|
117 |
DefaultMutableTreeNode n = getNode(id);
|
|
|
118 |
if (n != null) {
|
|
|
119 |
if (n instanceof ActivableMutableTreeNode) {
|
|
|
120 |
((ActivableMutableTreeNode) n).setActive(active);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
reload(n);
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
@Override
|
|
|
129 |
public boolean isLeaf(Object node) {
|
|
|
130 |
final ActivableMutableTreeNode n = (ActivableMutableTreeNode) node;
|
|
|
131 |
if (n.getUserObject() == null)
|
|
|
132 |
return super.isLeaf(node);
|
|
|
133 |
return !(n.getUserObject() instanceof Group);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
@Override
|
|
|
137 |
public void insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index) {
|
|
|
138 |
Item it = (Item) ((DefaultMutableTreeNode) newChild).getUserObject();
|
|
|
139 |
Group g = (Group) ((DefaultMutableTreeNode) parent).getUserObject();
|
|
|
140 |
extension.moveMenuItem(it.getId(), g.getId());
|
|
|
141 |
super.insertNodeInto(newChild, parent, index);
|
|
|
142 |
extension.setChanged();
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
@SuppressWarnings("rawtypes")
|
|
|
146 |
public void renameMenuItem(String previousId, String newId) {
|
|
|
147 |
DefaultMutableTreeNode root = (DefaultMutableTreeNode) this.getRoot();
|
|
|
148 |
|
|
|
149 |
for (Enumeration e = root.breadthFirstEnumeration(); e.hasMoreElements();) {
|
|
|
150 |
final DefaultMutableTreeNode current = (DefaultMutableTreeNode) e.nextElement();
|
|
|
151 |
|
|
|
152 |
final String idFromNode = getIdFromNode(current);
|
|
|
153 |
if (idFromNode != null && idFromNode.equals(previousId)) {
|
|
|
154 |
setId(current, newId);
|
|
|
155 |
|
|
|
156 |
reload(current);
|
|
|
157 |
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
extension.renameMenuItem(previousId, newId);
|
|
|
162 |
extension.setChanged();
|
|
|
163 |
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
private void setId(DefaultMutableTreeNode node, String newId) {
|
|
|
167 |
Object o = node.getUserObject();
|
|
|
168 |
if (o == null)
|
|
|
169 |
return;
|
|
|
170 |
((Item) o).setId(newId);
|
|
|
171 |
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
private static String getIdFromNode(DefaultMutableTreeNode node) {
|
|
|
175 |
Object o = node.getUserObject();
|
|
|
176 |
if (o == null)
|
|
|
177 |
return null;
|
|
|
178 |
return ((Item) o).getId();
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
@SuppressWarnings("rawtypes")
|
|
|
182 |
public DefaultMutableTreeNode getNode(String id) {
|
|
|
183 |
DefaultMutableTreeNode root = (DefaultMutableTreeNode) this.getRoot();
|
|
|
184 |
|
|
|
185 |
for (Enumeration e = root.breadthFirstEnumeration(); e.hasMoreElements();) {
|
|
|
186 |
final DefaultMutableTreeNode current = (DefaultMutableTreeNode) e.nextElement();
|
|
|
187 |
|
|
|
188 |
final String idFromNode = getIdFromNode(current);
|
|
|
189 |
if (idFromNode != null && idFromNode.equals(id)) {
|
|
|
190 |
return current;
|
|
|
191 |
|
|
|
192 |
}
|
|
|
193 |
}
|
|
|
194 |
return null;
|
|
|
195 |
}
|
|
|
196 |
}
|