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