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 super(target, enabled, type);
51
52 if (target.getWidget() instanceof Viewer) {
53 this.viewer = target.<StructuredViewer> getCastWidget();
54 } else {
55 throw new UnsupportedClassException(target.getWidgetClass(),
56 StructuredViewer.class);
57 }
58 }
59
60
61
62
63 @Override
64 protected void setupListener() {
65 ISelectionChangedListener listener = new ISelectionChangedListener() {
66 public void selectionChanged(SelectionChangedEvent event) {
67 updateEnableState();
68 }
69 };
70 viewer.addSelectionChangedListener(listener);
71 }
72
73
74
75
76 @Override
77 protected void updateEnableState() {
78 if (!viewer.getControl().isDisposed()) {
79 Object enabledWidget = enabled.getWidget();
80 if (enabledWidget instanceof Widget
81 && ((Widget) enabledWidget).isDisposed()) {
82 return;
83 }
84 boolean enableState = resolveEnabledState();
85 enabledProperty.setValue(enabledWidget, Boolean
86 .valueOf(enableState));
87 }
88 }
89
90 protected boolean resolveEnabledState() {
91 boolean result = false;
92 int selectionCount = ((StructuredSelection) viewer.getSelection())
93 .size();
94 if (type == EnablesForType.SELECTION) {
95 result = (selectionCount > 0);
96 } else if (type == EnablesForType.SINGLE) {
97 result = (selectionCount == 1);
98 } else if (type == EnablesForType.PAIR) {
99 result = (selectionCount == 2);
100 } else if (type == EnablesForType.NONE) {
101 result = (selectionCount == 0);
102 } else if (type == EnablesForType.MULTI) {
103 result = (selectionCount >= 2);
104 } else {
105 throw new EnablesDependingException(viewer.getClass(), type);
106 }
107 return result;
108 }
109 }