1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.ui.workbench.tree;
35
36 import info.magnolia.ui.workbench.event.ItemEditedEvent;
37 import info.magnolia.ui.workbench.list.ListViewImpl;
38
39 import java.util.List;
40
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import com.vaadin.event.Action;
45 import com.vaadin.event.Action.Container;
46 import com.vaadin.event.Action.Handler;
47 import com.vaadin.event.ShortcutAction;
48 import com.vaadin.event.dd.DropHandler;
49 import com.vaadin.ui.Field;
50 import com.vaadin.ui.Table.TableDragMode;
51 import com.vaadin.ui.TreeTable;
52
53
54
55
56 public class TreeViewImpl extends ListViewImpl implements TreeView {
57
58 private static final Logger log = LoggerFactory.getLogger(TreeViewImpl.class);
59
60 private final TreeTable treeTable;
61
62 private Container shortcutActionManager;
63
64 private Handler editingKeyboardHandler;
65
66 private TreeRowScroller rowScroller;
67
68 private final ItemEditedEvent.Handler itemEditedListener = new ItemEditedEvent.Handler() {
69
70 @Override
71 public void onItemEdited(ItemEditedEvent event) {
72 if (getListener() != null) {
73 getListener().onItemEdited(event.getItem());
74 }
75 }
76 };
77
78 public TreeViewImpl() {
79 this(new InplaceEditingTreeTable());
80 }
81
82 public TreeViewImpl(TreeTable tree) {
83 super(tree);
84 tree.setSortEnabled(false);
85
86 tree.setCollapsed(tree.firstItemId(), false);
87 rowScroller = new TreeRowScroller(tree);
88 this.treeTable = tree;
89 }
90
91 @Override
92 public void setActionManager(Container shortcutActionManager) {
93 this.shortcutActionManager = shortcutActionManager;
94 bindKeyboardShortcuts();
95 }
96
97 @Override
98 public void setEditable(boolean editable) {
99 treeTable.setEditable(editable);
100 bindKeyboardShortcuts();
101 }
102
103 private void bindKeyboardShortcuts() {
104 if (treeTable.isEditable() && treeTable instanceof InplaceEditingTreeTable) {
105 ((InplaceEditingTreeTable) treeTable).addItemEditedListener(itemEditedListener);
106 if (shortcutActionManager != null) {
107 if (editingKeyboardHandler == null) {
108 editingKeyboardHandler = new EditingKeyboardHandler((InplaceEditingTreeTable) treeTable);
109 }
110 shortcutActionManager.addActionHandler(editingKeyboardHandler);
111 }
112 } else {
113 ((InplaceEditingTreeTable) treeTable).removeItemEditedListener(itemEditedListener);
114 if (shortcutActionManager != null) {
115 shortcutActionManager.removeActionHandler(editingKeyboardHandler);
116 }
117 }
118 }
119
120 @Override
121 public void setEditableColumns(Object... propertyIds) {
122 ((InplaceEditingTreeTable) treeTable).setEditableColumns(propertyIds);
123 }
124
125 @Override
126 public void setDragAndDropHandler(DropHandler dropHandler) {
127 if (dropHandler != null) {
128 treeTable.setDragMode(TableDragMode.ROW);
129 treeTable.setDropHandler(dropHandler);
130 } else {
131 treeTable.setDragMode(TableDragMode.NONE);
132 treeTable.setDropHandler(null);
133 }
134 }
135
136 @Override
137 public void select(List<String> itemIds) {
138 String firstItemId = itemIds == null || itemIds.isEmpty() ? null : itemIds.get(0);
139 if (firstItemId == null) {
140 return;
141 }
142
143 treeTable.setValue(null);
144 for (String id : itemIds) {
145 treeTable.select(id);
146 }
147 rowScroller.bringRowIntoView(firstItemId);
148 }
149
150 @Override
151 public void expand(String itemId) {
152 rowScroller.expandTreeToNode(itemId, true);
153 }
154
155 @Override
156 protected TreeView.Listener getListener() {
157 return (TreeView.Listener) super.getListener();
158 }
159
160 @Override
161 public TreeTable asVaadinComponent() {
162 return treeTable;
163 }
164
165
166
167
168
169
170 private class EditingKeyboardHandler implements Handler {
171
172 private final ShortcutAction enter = new ShortcutAction("Enter", ShortcutAction.KeyCode.ENTER, null);
173
174 private final ShortcutAction tabNext = new ShortcutAction("Tab", ShortcutAction.KeyCode.TAB, null);
175
176 private final ShortcutAction tabPrev = new ShortcutAction("Shift+Tab", ShortcutAction.KeyCode.TAB, new int[] { ShortcutAction.ModifierKey.SHIFT });
177
178 private final ShortcutAction escape = new ShortcutAction("Esc", ShortcutAction.KeyCode.ESCAPE, null);
179
180 private final InplaceEditingTreeTable tree;
181
182 public EditingKeyboardHandler(InplaceEditingTreeTable tree) {
183 this.tree = tree;
184 }
185
186 @Override
187 public Action[] getActions(Object target, Object sender) {
188
189 return new Action[] { enter, tabNext, tabPrev, escape };
190 }
191
192 @Override
193 public void handleAction(Action action, Object sender, Object target) {
194
195
196
197
198
199 if (!(action instanceof ShortcutAction)) {
200 return;
201 }
202 ShortcutAction shortcut = (ShortcutAction) action;
203
204 if (target != tree && target instanceof Field) {
205 Field<?> field = (Field<?>) target;
206
207 if (shortcut == enter || shortcut.getKeyCode() == enter.getKeyCode()) {
208 tree.fireItemEditedEvent(field.getPropertyDataSource());
209 tree.setEditing(null, null);
210
211 } else if (action == tabNext) {
212 tree.editNextCell(field);
213
214 } else if (action == tabPrev) {
215 tree.editPreviousCell(field);
216
217 } else if (action == escape) {
218 tree.setEditing(null, null);
219 }
220 } else if (target == treeTable) {
221 if (tree.getValue() == null) {
222 return;
223 }
224
225 if (shortcut == enter || shortcut.getKeyCode() == enter.getKeyCode()) {
226 tree.editFirstCellofFirstSelectedRow();
227
228 }
229 }
230 }
231 }
232
233 }