1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.uruma.binding.enables.impl; |
17 | |
|
18 | |
import org.eclipse.jface.viewers.ISelectionChangedListener; |
19 | |
import org.eclipse.jface.viewers.SelectionChangedEvent; |
20 | |
import org.eclipse.jface.viewers.StructuredSelection; |
21 | |
import org.eclipse.jface.viewers.StructuredViewer; |
22 | |
import org.eclipse.jface.viewers.Viewer; |
23 | |
import org.eclipse.swt.widgets.Widget; |
24 | |
import org.seasar.uruma.binding.enables.EnablesDependingListener; |
25 | |
import org.seasar.uruma.binding.enables.EnablesForType; |
26 | |
import org.seasar.uruma.context.WidgetHandle; |
27 | |
import org.seasar.uruma.exception.EnablesDependingException; |
28 | |
import org.seasar.uruma.exception.UnsupportedClassException; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public class ViewerEnablesDependingListener extends EnablesDependingListener { |
36 | |
protected StructuredViewer viewer; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public ViewerEnablesDependingListener(final WidgetHandle target, |
49 | |
final WidgetHandle enabled, final EnablesForType type) { |
50 | 40 | super(target, enabled, type); |
51 | |
|
52 | 40 | if (target.getWidget() instanceof Viewer) { |
53 | 40 | this.viewer = target.<StructuredViewer> getCastWidget(); |
54 | |
} else { |
55 | 0 | throw new UnsupportedClassException(target.getWidgetClass(), |
56 | |
StructuredViewer.class); |
57 | |
} |
58 | 40 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
@Override |
64 | |
protected void setupListener() { |
65 | 40 | ISelectionChangedListener listener = new ISelectionChangedListener() { |
66 | 40 | public void selectionChanged(SelectionChangedEvent event) { |
67 | 0 | updateEnableState(); |
68 | 0 | } |
69 | |
}; |
70 | 40 | viewer.addSelectionChangedListener(listener); |
71 | 40 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
@Override |
77 | |
protected void updateEnableState() { |
78 | 40 | if (!viewer.getControl().isDisposed()) { |
79 | 40 | Object enabledWidget = enabled.getWidget(); |
80 | 40 | if (enabledWidget instanceof Widget |
81 | |
&& ((Widget) enabledWidget).isDisposed()) { |
82 | 0 | return; |
83 | |
} |
84 | 40 | boolean enableState = resolveEnabledState(); |
85 | 40 | enabledProperty.setValue(enabledWidget, Boolean |
86 | |
.valueOf(enableState)); |
87 | |
} |
88 | 40 | } |
89 | |
|
90 | |
protected boolean resolveEnabledState() { |
91 | 40 | boolean result = false; |
92 | 40 | int selectionCount = ((StructuredSelection) viewer.getSelection()) |
93 | |
.size(); |
94 | 40 | if (type == EnablesForType.SELECTION) { |
95 | 8 | result = (selectionCount > 0); |
96 | 32 | } else if (type == EnablesForType.SINGLE) { |
97 | 8 | result = (selectionCount == 1); |
98 | 24 | } else if (type == EnablesForType.PAIR) { |
99 | 8 | result = (selectionCount == 2); |
100 | 16 | } else if (type == EnablesForType.NONE) { |
101 | 8 | result = (selectionCount == 0); |
102 | 8 | } else if (type == EnablesForType.MULTI) { |
103 | 8 | result = (selectionCount >= 2); |
104 | |
} else { |
105 | 0 | throw new EnablesDependingException(viewer.getClass(), type); |
106 | |
} |
107 | 40 | return result; |
108 | |
} |
109 | |
} |