/* * Copyright 2005-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wamblee.wicket.behavior; import org.apache.wicket.Component; import org.apache.wicket.behavior.IBehavior; import org.apache.wicket.model.Model; import org.apache.wicket.model.StringResourceModel; /** * Composite behavior that deals with (optional) localization of * the behavior. It does this by creating localized behaviors in the bind callback. * This is required because the component is need in order to obtain the resources to use * for localization. */ public abstract class LocalizedCompositeBehavior extends CompositeBehavior { private boolean localize; public LocalizedCompositeBehavior(IBehavior[] aBehaviors, boolean aLocalize) { super(aBehaviors); localize = aLocalize; } @Override public void bind(Component aComponent) { super.bind(aComponent); for (IBehavior behavior: createLocalizedBehaviors(aComponent) ) { behavior.bind(aComponent); add(behavior); } } public boolean isLocalized() { return localize; } /** * Utility method to obtain a localized message for a given component. * @param aComponent Component. * @param aMessage Message or message key in case localization is used. * @return Localized message. */ protected String getLocalizedMessage(Component aComponent, String aMessage) { if ( !localize ) { return aMessage; } return new StringResourceModel(aMessage, aComponent, new Model(aMessage)).toString(); } /** * Callback to create further localized components. * @param aLocalize True iff localization must be used. * @return Array of behaviors. */ protected abstract IBehavior[] createLocalizedBehaviors(Component aComponent); }