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.examples.unicodeconverter;
018:
019: import java.util.Arrays;
020: import java.util.List;
021:
022: import org.apache.wicket.Component;
023: import org.apache.wicket.examples.WicketExamplePage;
024: import org.apache.wicket.markup.html.form.DropDownChoice;
025: import org.apache.wicket.markup.html.form.Form;
026: import org.apache.wicket.markup.html.form.TextArea;
027: import org.apache.wicket.model.CompoundPropertyModel;
028: import org.apache.wicket.model.Model;
029: import org.apache.wicket.util.string.Strings;
030:
031: /**
032: * Converts between unescaped and escaped unicode and shows a custom model.
033: * Handy for message bundles.
034: *
035: * @author Eelco Hillenius
036: */
037: public class UnicodeConverter extends WicketExamplePage {
038: private static final String FROM_ESCAPED_UNICODE = "from escaped unicode";
039:
040: private static final String TO_ESCAPED_UNICODE = "to escaped unicode";
041:
042: private static List translationTypes = Arrays.asList(new String[] {
043: TO_ESCAPED_UNICODE, FROM_ESCAPED_UNICODE });
044:
045: private String source = "";
046:
047: private String translationType = (String) translationTypes.get(0);
048:
049: /**
050: * Model that does the conversion. Note that as we 'pull' the value every
051: * time we render (we get the current value of message), we don't need to
052: * update the model itself. The alternative strategy would be to have a
053: * model with it's own, translated, string representation of the source,
054: * which should be updated on every form post (e.g. by overriding
055: * {@link Form#onSubmit} and in that method explicitly setting the new
056: * value). But as you can see, this method is slighly easier, and if we
057: * wanted to use the translated value in e.g. a database, we could just
058: * query this model directly or indirectly by calling
059: * {@link Component#getModelObject()} on the component that holds it, and we
060: * would have a recent value.
061: */
062: private final class ConverterModel extends Model {
063: /**
064: * @see org.apache.wicket.model.IModel#getObject()
065: */
066: public Object getObject() {
067: String result;
068: if (TO_ESCAPED_UNICODE.equals(translationType)) {
069: result = Strings.toEscapedUnicode(source);
070: } else {
071: result = Strings.fromEscapedUnicode(source);
072: }
073: return result;
074: }
075:
076: /**
077: * @see org.apache.wicket.model.IModel#setObject(java.lang.Object)
078: */
079: public void setObject(Object object) {
080: // Ignore. We are not interested in updating any value,
081: // and we don't want to throw an exception like
082: // AbstractReadOnlyModel either. Alternatively, we
083: // could have overriden updateModel of FormInputComponent
084: // and ignore any input there.
085: }
086: }
087:
088: /**
089: * Constructor.
090: */
091: public UnicodeConverter() {
092: Form form = new Form("form", new CompoundPropertyModel(this ));
093: form.add(new TextArea("source"));
094: form
095: .add(new DropDownChoice("translationType",
096: translationTypes));
097: form.add(new TextArea("target", new ConverterModel()));
098: add(form);
099: }
100:
101: /**
102: * @return the source to translate
103: */
104: public String getSource() {
105: return source;
106: }
107:
108: /**
109: * @param source
110: * the source to set
111: */
112: public void setSource(String source) {
113: this .source = source;
114: }
115:
116: /**
117: * @return the selection
118: */
119: public String getTranslationType() {
120: return translationType;
121: }
122:
123: /**
124: * @param translationType
125: * the selection
126: */
127: public void setTranslationType(String translationType) {
128: this.translationType = translationType;
129: }
130: }
|