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.Application;
020import org.apache.wicket.Component;
021
022/**
023 * A model that represents a localized resource string. This is a lightweight version of the
024 * {@link StringResourceModel}. It lacks parameter substitutions, but is generally easier to use.
025 * <p>
026 * If you don't use this model as primary component model (you don't specify it in component
027 * constructor and don't assign it to component using {@link Component#setDefaultModel(IModel)}),
028 * you will need to connect the model with a component using {@link #wrapOnAssignment(Component)}.
029 * 
030 * @author Igor Vaynberg (ivaynberg)
031 * 
032 */
033public class ResourceModel implements IComponentAssignedModel<String>
034{
035        private static final long serialVersionUID = 1L;
036
037        private final String resourceKey;
038
039        private final IModel<String> defaultValue;
040
041        /**
042         * Constructor
043         * 
044         * @param resourceKey
045         *            key of the resource this model represents
046         */
047        public ResourceModel(String resourceKey)
048        {
049                this(resourceKey, (IModel<String>)null);
050        }
051
052        /**
053         * Constructor
054         * 
055         * @param resourceKey
056         *            key of the resource this model represents
057         * @param defaultValue
058         *            value that will be returned if resource does not exist
059         * 
060         */
061        public ResourceModel(String resourceKey, String defaultValue)
062        {
063                this(resourceKey, Model.of(defaultValue));
064        }
065
066        public ResourceModel(String resourceKey, IModel<String> defaultValue)
067        {
068                this.resourceKey = resourceKey;
069                this.defaultValue = defaultValue;
070        }
071
072        @Override
073        public String getObject()
074        {
075                // this shouldn't be called always wrapped!
076                return Application.get()
077                        .getResourceSettings()
078                        .getLocalizer()
079                        .getString(resourceKey, null, null, null, null, defaultValue);
080        }
081
082        @Override
083        public final void setObject(String object)
084        {
085                IComponentAssignedModel.super.setObject(object);
086        }
087
088        @Override
089        public IWrapModel<String> wrapOnAssignment(final Component component)
090        {
091                return new AssignmentWrapper(component);
092        }
093
094        private class AssignmentWrapper extends LoadableDetachableModel<String>
095                implements
096                        IWrapModel<String>
097        {
098                private static final long serialVersionUID = 1L;
099
100                private final Component component;
101
102                /**
103                 * Construct.
104                 * 
105                 * @param component
106                 */
107                AssignmentWrapper(Component component)
108                {
109                        this.component = component;
110                }
111
112                @Override
113                public IModel<String> getWrappedModel()
114                {
115                        return ResourceModel.this;
116                }
117
118                @Override
119                protected String load()
120                {
121                        return Application.get()
122                                .getResourceSettings()
123                                .getLocalizer()
124                                .getString(resourceKey, component, null, null, null, defaultValue);
125                }
126
127                @Override
128                protected void onDetach()
129                {
130                        ResourceModel.this.detach();
131                }
132
133        }
134}