View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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   * {@link Viewer} に対する {@link EnablesDependingListener} です。<br />
32   * 
33   * @author y-komori
34   */
35  public class ViewerEnablesDependingListener extends EnablesDependingListener {
36      protected StructuredViewer viewer;
37  
38      /**
39       * {@link ViewerEnablesDependingListener} を構築します。<br />
40       * 
41       * @param target
42       *            ターゲットの {@link WidgetHandle}
43       * @param enabled
44       *            イネーブル状態を変更するウィジットの {@link WidgetHandle}
45       * @param type
46       *            選択条件を表す {@link EnablesForType}
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       * @see org.seasar.uruma.binding.enables.EnablesDependingListener#setupListener()
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       * @see org.seasar.uruma.binding.enables.EnablesDependingListener#updateEnableState()
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 }