001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.isis.core.progmodel.facets.value.color;
021
022 import java.text.DecimalFormat;
023
024 import org.apache.isis.applib.adapters.EncoderDecoder;
025 import org.apache.isis.applib.adapters.Parser;
026 import org.apache.isis.applib.profiles.Localization;
027 import org.apache.isis.applib.value.Color;
028 import org.apache.isis.core.commons.config.IsisConfiguration;
029 import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
030 import org.apache.isis.core.metamodel.facetapi.Facet;
031 import org.apache.isis.core.metamodel.facetapi.FacetHolder;
032 import org.apache.isis.core.metamodel.facets.object.parseable.TextEntryParseException;
033 import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderAndFacetAbstract;
034 import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
035
036 public class ColorValueSemanticsProvider extends ValueSemanticsProviderAndFacetAbstract<Color> implements
037 ColorValueFacet {
038
039 public static Class<? extends Facet> type() {
040 return ColorValueFacet.class;
041 }
042
043 private static final Color DEFAULT_VALUE = Color.BLACK;
044 private static final int TYPICAL_LENGTH = 4;
045 private static final boolean IMMUTABLE = true;
046 private static final boolean EQUAL_BY_CONTENT = false;
047
048 /**
049 * Required because implementation of {@link Parser} and {@link EncoderDecoder}.
050 */
051 public ColorValueSemanticsProvider() {
052 this(null, null, null);
053 }
054
055 public ColorValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration,
056 final ValueSemanticsProviderContext context) {
057 super(type(), holder, Color.class, TYPICAL_LENGTH, IMMUTABLE, EQUAL_BY_CONTENT, DEFAULT_VALUE, configuration,
058 context);
059 }
060
061 // //////////////////////////////////////////////////////////////////
062 // Parser
063 // //////////////////////////////////////////////////////////////////
064
065 @Override
066 protected Color doParse(final Object context, final String text) {
067 try {
068 if (text.startsWith("0x")) {
069 return new Color(Integer.parseInt(text.substring(2), 16));
070 } else if (text.startsWith("#")) {
071 return new Color(Integer.parseInt(text.substring(1), 16));
072 } else {
073 return new Color(Integer.parseInt(text));
074 }
075 } catch (final NumberFormatException e) {
076 throw new TextEntryParseException("Not a number " + text, e);
077 }
078 }
079
080 @Override
081 public String titleString(final Object object, final Localization localization) {
082 final Color color = (Color) object;
083 return color.title();
084 }
085
086 @Override
087 public String titleStringWithMask(final Object object, final String usingMask) {
088 final Color color = (Color) object;
089 return titleString(new DecimalFormat(usingMask), color.intValue());
090 }
091
092 // //////////////////////////////////////////////////////////////////
093 // Encode, Decode
094 // //////////////////////////////////////////////////////////////////
095
096 @Override
097 protected String doEncode(final Object object) {
098 final Color color = (Color) object;
099 return Integer.toHexString(color.intValue());
100 }
101
102 @Override
103 protected Color doRestore(final String data) {
104 return new Color(Integer.parseInt(data, 16));
105 }
106
107 // //////////////////////////////////////////////////////////////////
108 // ColorValueFacet
109 // //////////////////////////////////////////////////////////////////
110
111 @Override
112 public int colorValue(final ObjectAdapter object) {
113 if (object == null) {
114 return 0;
115 }
116 final Color color = (Color) object.getObject();
117 return color.intValue();
118 }
119
120 @Override
121 public ObjectAdapter createValue(final ObjectAdapter object, final int colorAsInt) {
122 final Color color = new Color(colorAsInt);
123 return getAdapterMap().adapterFor(color);
124 }
125
126 @Override
127 public String toString() {
128 return "ColorValueSemanticsProvider";
129 }
130
131 }