forked from kiegroup/kie-wb-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractToolboxControl.java
179 lines (149 loc) · 6.4 KB
/
AbstractToolboxControl.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright 2017 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.workbench.common.stunner.core.client.canvas.controls.toolbox;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler;
import org.kie.workbench.common.stunner.core.client.canvas.CanvasHandler;
import org.kie.workbench.common.stunner.core.client.canvas.controls.ToolboxControl;
import org.kie.workbench.common.stunner.core.client.canvas.event.AbstractCanvasHandlerEvent;
import org.kie.workbench.common.stunner.core.client.canvas.event.registration.AbstractCanvasShapeEvent;
import org.kie.workbench.common.stunner.core.client.canvas.event.registration.CanvasShapeRemovedEvent;
import org.kie.workbench.common.stunner.core.client.canvas.event.selection.CanvasClearSelectionEvent;
import org.kie.workbench.common.stunner.core.client.canvas.event.selection.CanvasSelectionEvent;
import org.kie.workbench.common.stunner.core.client.components.toolbox.Toolbox;
import org.kie.workbench.common.stunner.core.client.components.toolbox.actions.ActionsToolboxFactory;
import org.kie.workbench.common.stunner.core.graph.Element;
import static org.kie.soup.commons.validation.PortablePreconditions.checkNotNull;
public abstract class AbstractToolboxControl
implements ToolboxControl<AbstractCanvasHandler, Element> {
private final ToolboxControlImpl<ActionsToolboxFactory> toolboxControl;
private final SingleItemSelectedShowPredicate toolboxShowPredicate;
// It makes the toolbox appear only if single selection.
private static class SingleItemSelectedShowPredicate implements Predicate<String> {
private String id;
private int count;
@Override
public boolean test(String s) {
return (null == id && count == 0) || (null != id && count == 1 && id.equals(s));
}
}
protected abstract List<ActionsToolboxFactory> getFactories();
@Inject
public AbstractToolboxControl() {
this.toolboxShowPredicate = new SingleItemSelectedShowPredicate();
this.toolboxControl = new ToolboxControlImpl<>(this::getFactories,
toolboxShowPredicate);
}
AbstractToolboxControl(final ToolboxControlImpl<ActionsToolboxFactory> toolboxControl) {
this.toolboxShowPredicate = new SingleItemSelectedShowPredicate();
this.toolboxControl = toolboxControl;
}
@Override
public void init(final AbstractCanvasHandler context) {
toolboxControl.init(context);
}
@Override
@SuppressWarnings("unchecked")
public void register(final Element element) {
toolboxControl.register(element);
}
@Override
public void deregister(final Element element) {
toolboxControl.deregister(element);
}
@Override
public Iterator<Toolbox<?>> getToolboxes(final Element element) {
return toolboxControl.getToolboxes(element);
}
void onCanvasSelectionEvent(final @Observes CanvasSelectionEvent event) {
checkNotNull("event",
event);
handleCanvasSelectionEvent(event);
}
void onCanvasClearSelectionEvent(final @Observes CanvasClearSelectionEvent event) {
checkNotNull("event",
event);
handleCanvasClearSelectionEvent(event);
}
void onCanvasShapeRemovedEvent(final @Observes CanvasShapeRemovedEvent event) {
checkNotNull("event",
event);
handleCanvasShapeRemovedEvent(event);
}
private String lastSelected = "";
protected void handleCanvasSelectionEvent(final CanvasSelectionEvent event) {
if (checkEventContext(event)) {
if (1 == event.getIdentifiers().size()) {
final String uuid = event.getIdentifiers().iterator().next();
if (lastSelected.equals(uuid)) {
return;
}
lastSelected = uuid;
show(uuid);
} else {
showMultiple(event.getIdentifiers());
}
}
}
protected void handleCanvasClearSelectionEvent(final CanvasClearSelectionEvent event) {
if (checkEventContext(event)) {
toolboxControl.destroyToolboxes();
lastSelected = "";
clear();
}
}
protected void handleCanvasShapeRemovedEvent(final CanvasShapeRemovedEvent event) {
if (checkEventContext(event)) {
lastSelected = "";
clear();
}
}
private void show(final String uuid) {
clear();
toolboxShowPredicate.id = uuid;
toolboxShowPredicate.count = 1;
toolboxControl.show(uuid);
}
private void showMultiple(final Collection<String> ids) {
clear();
toolboxShowPredicate.id = ids.iterator().next();
toolboxShowPredicate.count = ids.size();
}
public void clear() {
toolboxControl.hideAndDestroyToolboxes();
toolboxShowPredicate.id = null;
toolboxShowPredicate.count = 0;
}
public void destroy() {
clear();
toolboxControl.destroy();
}
private boolean checkEventContext(final AbstractCanvasHandlerEvent canvasHandlerEvent) {
final CanvasHandler _canvasHandler = canvasHandlerEvent.getCanvasHandler();
return toolboxControl.getCanvasHandler() != null
&& toolboxControl.getCanvasHandler().equals(_canvasHandler);
}
private boolean checkEventContext(final AbstractCanvasShapeEvent canvasShapeEvent) {
return toolboxControl.getCanvasHandler() != null
&& toolboxControl.getCanvasHandler().getCanvas().equals(canvasShapeEvent.getCanvas())
&& toolboxControl.isActive(canvasShapeEvent.getShape().getUUID());
}
}