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 020package org.apache.isis.core.progmodel.facets.value.color; 021 022import java.text.DecimalFormat; 023 024import org.apache.isis.applib.adapters.EncoderDecoder; 025import org.apache.isis.applib.adapters.Parser; 026import org.apache.isis.applib.profiles.Localization; 027import org.apache.isis.applib.value.Color; 028import org.apache.isis.core.commons.config.IsisConfiguration; 029import org.apache.isis.core.metamodel.adapter.ObjectAdapter; 030import org.apache.isis.core.metamodel.facetapi.Facet; 031import org.apache.isis.core.metamodel.facetapi.FacetHolder; 032import org.apache.isis.core.metamodel.facets.object.parseable.TextEntryParseException; 033import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderAndFacetAbstract; 034import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext; 035 036public class ColorValueSemanticsProvider extends ValueSemanticsProviderAndFacetAbstract<Color> implements ColorValueFacet { 037 038 public static Class<? extends Facet> type() { 039 return ColorValueFacet.class; 040 } 041 042 private static final Color DEFAULT_VALUE = Color.BLACK; 043 private static final int TYPICAL_LENGTH = 4; 044 045 /** 046 * Required because implementation of {@link Parser} and 047 * {@link EncoderDecoder}. 048 */ 049 public ColorValueSemanticsProvider() { 050 this(null, null, null); 051 } 052 053 public ColorValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) { 054 super(type(), holder, Color.class, TYPICAL_LENGTH, Immutability.IMMUTABLE, EqualByContent.NOT_HONOURED, DEFAULT_VALUE, configuration, context); 055 } 056 057 // ////////////////////////////////////////////////////////////////// 058 // Parser 059 // ////////////////////////////////////////////////////////////////// 060 061 @Override 062 protected Color doParse(final Object context, final String text) { 063 try { 064 if (text.startsWith("0x")) { 065 return new Color(Integer.parseInt(text.substring(2), 16)); 066 } else if (text.startsWith("#")) { 067 return new Color(Integer.parseInt(text.substring(1), 16)); 068 } else { 069 return new Color(Integer.parseInt(text)); 070 } 071 } catch (final NumberFormatException e) { 072 throw new TextEntryParseException("Not a number " + text, e); 073 } 074 } 075 076 @Override 077 public String titleString(final Object object, final Localization localization) { 078 final Color color = (Color) object; 079 return color.title(); 080 } 081 082 @Override 083 public String titleStringWithMask(final Object object, final String usingMask) { 084 final Color color = (Color) object; 085 return titleString(new DecimalFormat(usingMask), color.intValue()); 086 } 087 088 // ////////////////////////////////////////////////////////////////// 089 // Encode, Decode 090 // ////////////////////////////////////////////////////////////////// 091 092 @Override 093 protected String doEncode(final Object object) { 094 final Color color = (Color) object; 095 return Integer.toHexString(color.intValue()); 096 } 097 098 @Override 099 protected Color doRestore(final String data) { 100 return new Color(Integer.parseInt(data, 16)); 101 } 102 103 // ////////////////////////////////////////////////////////////////// 104 // ColorValueFacet 105 // ////////////////////////////////////////////////////////////////// 106 107 @Override 108 public int colorValue(final ObjectAdapter object) { 109 if (object == null) { 110 return 0; 111 } 112 final Color color = (Color) object.getObject(); 113 return color.intValue(); 114 } 115 116 @Override 117 public ObjectAdapter createValue(final ObjectAdapter object, final int colorAsInt) { 118 final Color color = new Color(colorAsInt); 119 return getAdapterManager().adapterFor(color); 120 } 121 122 @Override 123 public String toString() { 124 return "ColorValueSemanticsProvider"; 125 } 126 127}