001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.renderer;
017:
018: import org.tp23.antinstaller.input.OutputField;
019: import org.tp23.antinstaller.page.Page;
020: import org.tp23.antinstaller.renderer.swing.SwingOutputFieldRenderer;
021: import org.tp23.antinstaller.renderer.swing.SwingPageRenderer;
022: import org.tp23.antinstaller.renderer.text.AbstractTextPageRenderer;
023: import org.tp23.antinstaller.renderer.text.TextOutputFieldRenderer;
024:
025: /**
026: *
027: * <p>Fetches an instance of a Renderers by using the class of the method parameter and
028: * a naming convention </p>
029: * <p> </p>
030: * <p>Copyright: Copyright (c) 2004</p>
031: * <p>Company: tp23</p>
032: * @author Paul Hinds
033: * @version $Id: RendererFactory.java,v 1.3 2006/12/21 00:03:30 teknopaul Exp $
034: */
035: public class RendererFactory {
036: public RendererFactory() {
037: }
038:
039: public static TextOutputFieldRenderer getTextRenderer(
040: OutputField field) throws ClassNotFoundException {
041: String fullyQualified = field.getClass().getName();
042: int lastDot = fullyQualified.lastIndexOf('.');
043: if (lastDot == -1) {
044: throw new UnsupportedOperationException(
045: "InputField can not be a member of the default package");
046: }
047: int prevDot = fullyQualified.substring(0, lastDot - 1)
048: .lastIndexOf('.', lastDot - 1);
049: if (prevDot == -1) {
050: throw new UnsupportedOperationException(
051: "InputField can not be a member of a single level package");
052: }
053: StringBuffer rendererClassName = new StringBuffer();
054: rendererClassName.append(fullyQualified.substring(0, prevDot));
055: rendererClassName.append(".renderer.text.");
056: rendererClassName.append(fullyQualified.substring(lastDot + 1));
057: rendererClassName.append("Renderer");
058: try {
059: Class clazz = Class.forName(rendererClassName.toString());
060: return (TextOutputFieldRenderer) clazz.newInstance();
061: } catch (Exception ex) {
062: throw new ClassNotFoundException(
063: "Class does not meet the contract for TextInputFieldRenderer:"
064: + rendererClassName);
065: }
066: }
067:
068: public static AbstractTextPageRenderer getTextPageRenderer(Page page)
069: throws ClassNotFoundException {
070: String fullyQualified = page.getClass().getName();
071: int lastDot = fullyQualified.lastIndexOf('.');
072: if (lastDot == -1) {
073: throw new UnsupportedOperationException(
074: "Pages can not be a member of the default package");
075: }
076: int prevDot = fullyQualified.substring(0, lastDot - 1)
077: .lastIndexOf('.', lastDot - 1);
078: if (prevDot == -1) {
079: throw new UnsupportedOperationException(
080: "Pages can not be a member of a single level package");
081: }
082: StringBuffer rendererClassName = new StringBuffer();
083: rendererClassName.append(fullyQualified.substring(0, prevDot));
084: rendererClassName.append(".renderer.text.");
085: rendererClassName.append(fullyQualified.substring(lastDot + 1));
086: rendererClassName.append("Renderer");
087: try {
088: Class clazz = Class.forName(rendererClassName.toString());
089: return (AbstractTextPageRenderer) clazz.newInstance();
090: } catch (Exception ex) {
091: throw new ClassNotFoundException(
092: "Class does not meet the contract for TextPageRenderer:"
093: + rendererClassName);
094: }
095: }
096:
097: public static SwingPageRenderer getSwingPageRenderer(Page page)
098: throws ClassNotFoundException {
099: String fullyQualified = page.getClass().getName();
100: int lastDot = fullyQualified.lastIndexOf('.');
101: if (lastDot == -1) {
102: throw new UnsupportedOperationException(
103: "Pages can not be a member of the default package");
104: }
105: int prevDot = fullyQualified.substring(0, lastDot - 1)
106: .lastIndexOf('.', lastDot - 1);
107: if (prevDot == -1) {
108: throw new UnsupportedOperationException(
109: "Pages can not be a member of a single level package");
110: }
111: StringBuffer rendererClassName = new StringBuffer();
112: rendererClassName.append(fullyQualified.substring(0, prevDot));
113: rendererClassName.append(".renderer.swing.");
114: rendererClassName.append(fullyQualified.substring(lastDot + 1));
115: rendererClassName.append("Renderer");
116: try {
117: Class clazz = Class.forName(rendererClassName.toString());
118: return (SwingPageRenderer) clazz.newInstance();
119: } catch (Exception ex) {
120: throw new ClassNotFoundException(
121: "Class does not meet the contract for SwingPageRenderer:"
122: + rendererClassName);
123: }
124: }
125:
126: public static SwingOutputFieldRenderer getSwingRenderer(
127: OutputField field) throws ClassNotFoundException {
128: String fullyQualified = field.getClass().getName();
129: int lastDot = fullyQualified.lastIndexOf('.');
130: if (lastDot == -1) {
131: throw new UnsupportedOperationException(
132: "OutputField can not be a member of the default package");
133: }
134: int prevDot = fullyQualified.substring(0, lastDot - 1)
135: .lastIndexOf('.', lastDot - 1);
136: if (prevDot == -1) {
137: throw new UnsupportedOperationException(
138: "OutputField can not be a member of a single level package");
139: }
140: StringBuffer rendererClassName = new StringBuffer();
141: rendererClassName.append(fullyQualified.substring(0, prevDot));
142: rendererClassName.append(".renderer.swing.");
143: rendererClassName.append(fullyQualified.substring(lastDot + 1));
144: rendererClassName.append("Renderer");
145: try {
146: Class clazz = Class.forName(rendererClassName.toString());
147: return (SwingOutputFieldRenderer) clazz.newInstance();
148: } catch (Exception ex) {
149: ex.printStackTrace();
150: throw new ClassNotFoundException(
151: "Class does not meet the contract for SwingInputFieldRenderer:"
152: + rendererClassName);
153: }
154: }
155: }
|