001: package com.silvermindsoftware.hitch.sample.handler;
002:
003: /**
004: * Copyright 2007 Brandon Goodin
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * 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, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import com.silvermindsoftware.hitch.handlers.component.AbstractComponentHandler;
020: import com.silvermindsoftware.hitch.handlers.component.ComponentHandler;
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: import javax.swing.*;
025: import java.awt.*;
026: import java.lang.reflect.Field;
027:
028: public class FavoriteColorComponentHandler extends
029: AbstractComponentHandler<JComponent, Object, Object> implements
030: ComponentHandler<JComponent, Object, Object> {
031:
032: private static final Log log = LogFactory
033: .getLog(FavoriteColorComponentHandler.class);
034:
035: private Color colorA = Color.BLUE;
036: private Color colorB = Color.GREEN;
037:
038: protected Class getSetterParameterType() {
039: return String.class;
040: }
041:
042: protected String getGetterName() {
043: return "getText";
044: }
045:
046: protected String getSetterName() {
047: return "setText";
048: }
049:
050: public Object preProcessPopulate(JComponent component,
051: Object modelFieldValue) {
052: if (modelFieldValue == null) {
053: return Color.BLUE;
054: }
055: colorA = getColor(((String) modelFieldValue).toUpperCase());
056: return modelFieldValue;
057: }
058:
059: public Object preProcessUpdate(JComponent component,
060: Object componentFieldValue) {
061: colorB = getColor(((String) componentFieldValue).toUpperCase());
062: return componentFieldValue;
063: }
064:
065: public void postProcessPopulate(JComponent component) {
066: component.setBackground(colorA);
067: }
068:
069: public void postProcessUpdate(JComponent component) {
070: component.setBackground(colorB);
071: }
072:
073: public void setColorA(String colorA) {
074: this .colorA = getColor(colorA);
075: }
076:
077: public void setColorB(String colorB) {
078: this .colorB = getColor(colorB);
079: }
080:
081: private Color getColor(String color) {
082: Class colorClass = Color.class;
083: Color colorObject = null;
084: try {
085: colorObject = Color.decode(color);
086: } catch (NumberFormatException e) {
087: log.info("Could not decode color value " + color
088: + " attempting reflection lookup.");
089: }
090:
091: if (colorObject == null) {
092: try {
093: Field colorField = colorClass.getField(color);
094: try {
095: colorObject = (Color) colorField.get(Class.class);
096: } catch (IllegalAccessException e) {
097: log.info("Unable to access field " + color
098: + " on Color.class.");
099: }
100: } catch (NoSuchFieldException e) {
101: log.info("Field " + color
102: + " not found on Color.class.");
103: }
104: }
105:
106: return colorObject;
107: }
108: }
|