001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.model; 018 019import org.apache.wicket.Component; 020 021/** 022 * Quick model that is implements the IComponentAssignedModel and the IModel interfaces. Its a quick 023 * replacement for the current setObject(Component,Object) and getObject(Component) methods when the 024 * component is needed in the model. 025 * 026 * @author jcompagner 027 * @param <T> 028 * type of model object 029 */ 030public class ComponentModel<T> implements IModel<T>, IComponentAssignedModel<T> 031{ 032 private static final long serialVersionUID = 1L; 033 034 /** 035 * This getObject throws an exception. 036 * 037 * @see org.apache.wicket.model.IModel#getObject() 038 */ 039 @Override 040 public final T getObject() 041 { 042 throw new RuntimeException("get object call not expected on a IComponentAssignedModel"); 043 } 044 045 @Override 046 public final void setObject(T object) 047 { 048 throw new RuntimeException("set object call not expected on a IComponentAssignedModel"); 049 } 050 051 /** 052 * Returns the object from the model with the use of the component where it is attached to. 053 * 054 * @param component 055 * The component which has this model. 056 * @return The object of the model. 057 */ 058 protected T getObject(Component component) 059 { 060 return null; 061 } 062 063 /** 064 * Sets the model object for this model. 065 * 066 * @param component 067 * The component which has this model. 068 * @param object 069 * The object that will be set in the model. 070 */ 071 protected void setObject(Component component, T object) 072 { 073 } 074 075 @Override 076 public IWrapModel<T> wrapOnAssignment(Component comp) 077 { 078 return new WrapModel(comp); 079 } 080 081 private class WrapModel implements IWrapModel<T> 082 { 083 private static final long serialVersionUID = 1L; 084 085 private final Component component; 086 087 /** 088 * @param comp 089 */ 090 public WrapModel(Component comp) 091 { 092 component = comp; 093 } 094 095 @Override 096 public IModel<T> getWrappedModel() 097 { 098 return ComponentModel.this; 099 } 100 101 @Override 102 public T getObject() 103 { 104 return ComponentModel.this.getObject(component); 105 } 106 107 @Override 108 public void setObject(T object) 109 { 110 ComponentModel.this.setObject(component, object); 111 } 112 113 @Override 114 public void detach() 115 { 116 ComponentModel.this.detach(); 117 } 118 } 119}