X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=wicket%2Fcomponents%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fwicket%2Fbehavior%2FLocalizedCompositeBehavior.java;fp=wicket%2Fcomponents%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fwicket%2Fbehavior%2FLocalizedCompositeBehavior.java;h=e43e48ed1366540b9b291dc3037f8bf75c4fb454;hb=cf8230a84b46dbe64184b87f1743a5c7c770f338;hp=0000000000000000000000000000000000000000;hpb=0e762a5431af2bd621462013ee7a3ed4a66f7282;p=utils diff --git a/wicket/components/src/main/java/org/wamblee/wicket/behavior/LocalizedCompositeBehavior.java b/wicket/components/src/main/java/org/wamblee/wicket/behavior/LocalizedCompositeBehavior.java new file mode 100644 index 00000000..e43e48ed --- /dev/null +++ b/wicket/components/src/main/java/org/wamblee/wicket/behavior/LocalizedCompositeBehavior.java @@ -0,0 +1,70 @@ +/* + * 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); +}