Coverage Report - org.seasar.uruma.binding.enables.impl.ViewerEnablesDependingListener
 
Classes in this File Line Coverage Branch Coverage Complexity
ViewerEnablesDependingListener
90%
26/29
68%
19/28
0
ViewerEnablesDependingListener$1
33%
1/3
N/A
0
 
 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  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  
      * @see org.seasar.uruma.binding.enables.EnablesDependingListener#setupListener()
 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  
      * @see org.seasar.uruma.binding.enables.EnablesDependingListener#updateEnableState()
 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  
 }