001: package com.silvermindsoftware.hitch.handlers;
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.*;
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022:
023: import javax.swing.*;
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: public class ComponentHandlerFactory {
028:
029: private static final Log log = LogFactory
030: .getLog(ComponentHandlerFactory.class);
031:
032: private Map<Object, ComponentHandler> componentHandler;
033:
034: public ComponentHandlerFactory() {
035:
036: this .componentHandler = new HashMap<Object, ComponentHandler>();
037:
038: ComponentHandler componentHandler = new TextComponentHandler();
039: register(JButton.class, componentHandler);
040: register(JTextField.class, componentHandler);
041: register(JTextArea.class, componentHandler);
042:
043: componentHandler = new JLabelComponentHandler();
044: register(JLabel.class, componentHandler);
045:
046: componentHandler = new ValueComponentHandler();
047: register(JFormattedTextField.class, componentHandler);
048:
049: componentHandler = new JComboBoxComponentHandler();
050: register(JComboBox.class, componentHandler);
051:
052: componentHandler = new JListComponentHandler();
053: register(JList.class, componentHandler);
054:
055: componentHandler = new ToggleComponentHandler();
056: register(JRadioButton.class, componentHandler);
057: register(JCheckBox.class, componentHandler);
058: register(JToggleButton.class, componentHandler);
059:
060: componentHandler = new JSpinnerComponentHandler();
061: register(JSpinner.class, componentHandler);
062:
063: componentHandler = new JSliderComponentHandler();
064: register(JSlider.class, componentHandler);
065:
066: //--- NEED TO BE ASSIGNED---
067: // register(JMenuItem.class, componentHandler);
068: // register(JRadioButtonMenuItem.class, componentHandler);
069: // register(JCheckBoxMenuItem.class, componentHandler);
070: // register(JMenu.class, componentHandler);
071: // register(JMenuItem.class, componentHandler);
072:
073: }
074:
075: /**
076: * Register a component handler instance for a key (generally a class)
077: *
078: * @param key the key
079: * @param componentHandler the component instance that will handle data mapping for the key
080: */
081: public void register(Object key, ComponentHandler componentHandler) {
082: if (log.isDebugEnabled()) {
083: log.debug("Adding '" + componentHandler.getClass()
084: + "' to registry under key '" + key + "'.");
085: }
086: this .componentHandler.put(key, componentHandler);
087: }
088:
089: /**
090: * Looks in the registry for a suitable handler instance based on the 'key'. If no exact match
091: * is found and key is of type Class, then we look for a superclass of 'key'. If we find a
092: * match, we return it, and register it's handler under the subclass so future lookups are fast.
093: *
094: * @param key - generally a class (will it ever not be a class?)
095: * @return the ComponentHandler for the key
096: */
097: public ComponentHandler getHandler(Object key) {
098:
099: // look for an exact match
100: ComponentHandler returnValue = componentHandler.get(key);
101:
102: if (null == returnValue) {
103: // Darn, we didn't find one.
104: if (log.isDebugEnabled()) {
105: log.debug("Could not find match for key '" + key
106: + "' in registry, searching for subclasses.");
107: }
108: if (key instanceof Class) {
109: // The search was for a class, so let's look to see if any of it's superclasses are
110: // registered...
111: Class keyClass = (Class) key;
112:
113: if (log.isDebugEnabled()) {
114: log.debug("Searching for superclasses of '" + key
115: + "' in registry.");
116: }
117:
118: keyClass = keyClass.getSuperclass();
119: while (keyClass != null) {
120: if (log.isDebugEnabled()) {
121: log
122: .debug("Looking for a match for "
123: + keyClass);
124: }
125: returnValue = componentHandler.get(keyClass);
126: if (null != returnValue) {
127: if (log.isDebugEnabled()) {
128: log.debug("Found a match for " + keyClass);
129: }
130: register(key, returnValue);
131: break;
132: }
133: keyClass = keyClass.getSuperclass();
134: }
135: }
136: }
137:
138: if (log.isDebugEnabled()) {
139: log.debug("Returning '" + returnValue + "' for key '" + key
140: + "'");
141: }
142:
143: return returnValue;
144: }
145:
146: /**
147: * Get the class that will be responsible for handling a particular component.
148: *
149: * @param componentType the component to look up
150: * @return the class that will handle this component
151: */
152: public Class getHandlerType(Class componentType) {
153:
154: Class retVal = null;
155: Object object = getHandler(componentType);
156: if (object != null) {
157: retVal = object.getClass();
158: }
159: return retVal;
160: }
161:
162: }
|