001 /* 002 * Copyright 2002-2006 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.richclient.list; 017 018 import java.beans.PropertyChangeEvent; 019 import java.beans.PropertyChangeListener; 020 021 import org.springframework.binding.value.ValueModel; 022 import org.springframework.richclient.core.Guarded; 023 024 /** 025 * This class applies a guard to a {@link Guarded} object that enables the 026 * guarded object bsaed on the contents of the selection model value. Concrete 027 * subclasses must provide an implementation for {@link #shouldEnable(int[])}. 028 * 029 * @author Larry Streepy 030 */ 031 public abstract class AbstractListSelectionGuard implements PropertyChangeListener { 032 033 private ValueModel selectionHolder; 034 private Guarded guarded; 035 036 /** 037 * Constructor. 038 * 039 * @param selectionHolder ValueModel holding the list selection (value must 040 * be an array of int (<code>int[]</code). 041 * @param guarded Object to guard 042 */ 043 public AbstractListSelectionGuard( ValueModel selectionHolder, Guarded guarded ) { 044 this.selectionHolder = selectionHolder; 045 this.selectionHolder.addValueChangeListener(this); 046 this.guarded = guarded; 047 propertyChange(null); 048 } 049 050 /** 051 * Handle a change in the selectionHolder value. 052 */ 053 public void propertyChange( PropertyChangeEvent evt ) { 054 int[] selected = (int[]) selectionHolder.getValue(); 055 guarded.setEnabled(shouldEnable(selected)); 056 } 057 058 /** 059 * Get the guarded object. 060 * 061 * @return guarded object 062 */ 063 public Guarded getGuarded() { 064 return guarded; 065 } 066 067 /** 068 * Get the selection value holder. The value of this value model will be an 069 * int array (<code>int[]</code). 070 * @return selection value holder 071 */ 072 public ValueModel getSelectionHolder() { 073 return selectionHolder; 074 } 075 076 /** 077 * Determine if the guarded object should be enabled based on the contents 078 * of the current selection model value. 079 * 080 * @param selected The array of selected rows 081 * @return boolean true if the guarded object should be enabled 082 */ 083 protected abstract boolean shouldEnable( int[] selected ); 084 }