001 // Copyright 2006-2007 Daniel Gredler 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package net.sf.beanform; 016 017 import java.util.Map; 018 019 import net.sf.beanform.binding.DualBinding; 020 import net.sf.beanform.util.TapestryCompat; 021 022 import org.apache.tapestry.IActionListener; 023 import org.apache.tapestry.IBinding; 024 import org.apache.tapestry.IComponent; 025 import org.apache.tapestry.IMarkupWriter; 026 import org.apache.tapestry.IRequestCycle; 027 import org.apache.tapestry.components.Block; 028 029 /** 030 * A low level BeanForm component that renders the save, cancel, refresh and delete buttons. 031 * This component must be wrapped by a {@link BeanForm} component. 032 * 033 * @author Daniel Gredler 034 */ 035 public abstract class BeanFormButtons extends BeanFormComponent { 036 037 final static String CUSTOM_BUTTON_BLOCK_SUFFIX = "_Buttons"; 038 final static String STANDARD_BUTTON_BLOCK_ID = "standardButtonBlock"; 039 040 final static String SAVE_BUTTON_ID = "save"; 041 final static String CANCEL_BUTTON_ID = "cancel"; 042 final static String REFRESH_BUTTON_ID = "refresh"; 043 final static String DELETE_BUTTON_ID = "delete"; 044 045 Map<String, IBinding> saveBindings; 046 Map<String, IBinding> cancelBindings; 047 Map<String, IBinding> refreshBindings; 048 Map<String, IBinding> deleteBindings; 049 050 @Override 051 protected void renderComponent( IMarkupWriter writer, IRequestCycle cycle ) { 052 this.initButtonBindings(); 053 DualBinding.addCustomBindings( this.getComponent( SAVE_BUTTON_ID ), this.saveBindings, true ); 054 DualBinding.addCustomBindings( this.getComponent( CANCEL_BUTTON_ID ), this.cancelBindings, true ); 055 DualBinding.addCustomBindings( this.getComponent( REFRESH_BUTTON_ID ), this.refreshBindings, true ); 056 DualBinding.addCustomBindings( this.getComponent( DELETE_BUTTON_ID ), this.deleteBindings, true ); 057 super.renderComponent( writer, cycle ); 058 } 059 060 private synchronized void initButtonBindings() { 061 if( this.saveBindings != null ) return; 062 BeanForm bf = this.getBeanForm(); 063 this.saveBindings = bf.extractBindingOverrides( SAVE_BUTTON_ID + BINDING_OVERRIDE_SEPARATOR ); 064 this.cancelBindings = bf.extractBindingOverrides( CANCEL_BUTTON_ID + BINDING_OVERRIDE_SEPARATOR ); 065 this.refreshBindings = bf.extractBindingOverrides( REFRESH_BUTTON_ID + BINDING_OVERRIDE_SEPARATOR ); 066 this.deleteBindings = bf.extractBindingOverrides( DELETE_BUTTON_ID + BINDING_OVERRIDE_SEPARATOR ); 067 } 068 069 public IComponent getButtonBlock() { 070 String customButtonsId = this.getBeanForm().getId() + CUSTOM_BUTTON_BLOCK_SUFFIX; 071 Block customButtons = this.getSiblingComponent( customButtonsId, Block.class ); 072 if( customButtons != null ) return customButtons; 073 else return this.getComponent( STANDARD_BUTTON_BLOCK_ID ); 074 } 075 076 public String getCancelJavaScript() { 077 return TapestryCompat.getCancelJavaScript(); 078 } 079 080 public String getRefreshJavaScript() { 081 return TapestryCompat.getRefreshJavaScript(); 082 } 083 084 public boolean getHasSave() { 085 return this.getBeanForm().getSave() != null; 086 } 087 088 public boolean getHasCancel() { 089 return this.getBeanForm().getCancel() != null; 090 } 091 092 public boolean getHasRefresh() { 093 return this.getBeanForm().getRefresh() != null; 094 } 095 096 public boolean getHasDelete() { 097 return this.getBeanForm().getDelete() != null; 098 } 099 100 public IActionListener getSaveListener() { 101 return this.getBeanForm().getSave(); 102 } 103 104 public IActionListener getDeleteListener() { 105 return this.getBeanForm().getDelete(); 106 } 107 108 }