View Javadoc

1   // Copyright 2006 Daniel Gredler
2   //
3   // Licensed under the Apache License, Version 2.0 (the "License");
4   // you may not use this file except in compliance with the License.
5   // You may obtain a copy of the License at
6   //
7   //     http://www.apache.org/licenses/LICENSE-2.0
8   //
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  
15  package net.sf.beanform;
16  
17  import java.util.Map;
18  
19  import net.sf.beanform.binding.DualBinding;
20  import net.sf.beanform.util.TapestryCompat;
21  
22  import org.apache.tapestry.IActionListener;
23  import org.apache.tapestry.IBinding;
24  import org.apache.tapestry.IComponent;
25  import org.apache.tapestry.IMarkupWriter;
26  import org.apache.tapestry.IRequestCycle;
27  import org.apache.tapestry.components.Block;
28  
29  /***
30   * A low level BeanForm component that renders the save, cancel, refresh and delete buttons.
31   * This component must be wrapped by a {@link BeanForm} component.
32   *
33   * @author Daniel Gredler
34   */
35  public abstract class BeanFormButtons extends BeanFormComponent {
36  
37      final static String CUSTOM_BUTTON_BLOCK_SUFFIX = "_Buttons";
38      final static String STANDARD_BUTTON_BLOCK_ID = "standardButtonBlock";
39  
40      final static String SAVE_BUTTON_ID = "save";
41      final static String CANCEL_BUTTON_ID = "cancel";
42      final static String REFRESH_BUTTON_ID = "refresh";
43      final static String DELETE_BUTTON_ID = "delete";
44  
45      Map<String, IBinding> saveBindings;
46      Map<String, IBinding> cancelBindings;
47      Map<String, IBinding> refreshBindings;
48      Map<String, IBinding> deleteBindings;
49  
50      @Override
51      protected void renderComponent( IMarkupWriter writer, IRequestCycle cycle ) {
52          this.initButtonBindings();
53          DualBinding.addCustomBindings( this.getComponent( SAVE_BUTTON_ID ), this.saveBindings, true );
54          DualBinding.addCustomBindings( this.getComponent( CANCEL_BUTTON_ID ), this.cancelBindings, true );
55          DualBinding.addCustomBindings( this.getComponent( REFRESH_BUTTON_ID ), this.refreshBindings, true );
56          DualBinding.addCustomBindings( this.getComponent( DELETE_BUTTON_ID ), this.deleteBindings, true );
57          super.renderComponent( writer, cycle );
58      }
59  
60      private synchronized void initButtonBindings() {
61          if( this.saveBindings != null ) return;
62          BeanForm bf = this.getBeanForm();
63          this.saveBindings = bf.extractBindingOverrides( SAVE_BUTTON_ID + BINDING_OVERRIDE_SEPARATOR );
64          this.cancelBindings = bf.extractBindingOverrides( CANCEL_BUTTON_ID + BINDING_OVERRIDE_SEPARATOR );
65          this.refreshBindings = bf.extractBindingOverrides( REFRESH_BUTTON_ID + BINDING_OVERRIDE_SEPARATOR );
66          this.deleteBindings = bf.extractBindingOverrides( DELETE_BUTTON_ID + BINDING_OVERRIDE_SEPARATOR );
67      }
68  
69      public IComponent getButtonBlock() {
70          String customButtonsId = this.getBeanForm().getId() + CUSTOM_BUTTON_BLOCK_SUFFIX;
71          Block customButtons = this.getSiblingComponent( customButtonsId, Block.class );
72          if( customButtons != null ) return customButtons;
73          else return this.getComponent( STANDARD_BUTTON_BLOCK_ID );
74      }
75  
76      public String getCancelJavaScript() {
77          return TapestryCompat.getCancelJavaScript();
78      }
79  
80      public String getRefreshJavaScript() {
81          return TapestryCompat.getRefreshJavaScript();
82      }
83  
84      public boolean getHasSave() {
85          return this.getBeanForm().getSave() != null;
86      }
87  
88      public boolean getHasCancel() {
89          return this.getBeanForm().getCancel() != null;
90      }
91  
92      public boolean getHasRefresh() {
93          return this.getBeanForm().getRefresh() != null;
94      }
95  
96      public boolean getHasDelete() {
97          return this.getBeanForm().getDelete() != null;
98      }
99  
100     public IActionListener getSaveListener() {
101         return this.getBeanForm().getSave();
102     }
103 
104     public IActionListener getDeleteListener() {
105         return this.getBeanForm().getDelete();
106     }
107 
108 }