1 | /*
|
---|
2 | * Copyright 2005 Paul Hinds
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 | package org.tp23.antinstaller.renderer;
|
---|
17 |
|
---|
18 | import org.tp23.antinstaller.input.OutputField;
|
---|
19 | import org.tp23.antinstaller.page.Page;
|
---|
20 | import org.tp23.antinstaller.renderer.swing.SwingOutputFieldRenderer;
|
---|
21 | import org.tp23.antinstaller.renderer.swing.SwingPageRenderer;
|
---|
22 | import org.tp23.antinstaller.renderer.text.AbstractTextPageRenderer;
|
---|
23 | import org.tp23.antinstaller.renderer.text.TextOutputFieldRenderer;
|
---|
24 |
|
---|
25 |
|
---|
26 |
|
---|
27 | /**
|
---|
28 | *
|
---|
29 | * <p>Fetches an instance of a Renderers by using the class of the method parameter and
|
---|
30 | * a naming convention </p>
|
---|
31 | * <p> </p>
|
---|
32 | * <p>Copyright: Copyright (c) 2004</p>
|
---|
33 | * <p>Company: tp23</p>
|
---|
34 | * @author Paul Hinds
|
---|
35 | * @version $Id: RendererFactory.java,v 1.3 2006/12/21 00:03:30 teknopaul Exp $
|
---|
36 | */
|
---|
37 | public class RendererFactory {
|
---|
38 | public RendererFactory() {
|
---|
39 | }
|
---|
40 |
|
---|
41 | public static TextOutputFieldRenderer getTextRenderer(OutputField field) throws ClassNotFoundException {
|
---|
42 | String fullyQualified = field.getClass().getName();
|
---|
43 | int lastDot = fullyQualified.lastIndexOf('.');
|
---|
44 | if (lastDot == -1) {
|
---|
45 | throw new UnsupportedOperationException("InputField can not be a member of the default package");
|
---|
46 | }
|
---|
47 | int prevDot = fullyQualified.substring(0,lastDot-1).lastIndexOf('.', lastDot - 1);
|
---|
48 | if (prevDot == -1) {
|
---|
49 | throw new UnsupportedOperationException("InputField can not be a member of a single level package");
|
---|
50 | }
|
---|
51 | StringBuffer rendererClassName = new StringBuffer();
|
---|
52 | rendererClassName.append(fullyQualified.substring(0, prevDot));
|
---|
53 | rendererClassName.append(".renderer.text.");
|
---|
54 | rendererClassName.append(fullyQualified.substring(lastDot + 1));
|
---|
55 | rendererClassName.append("Renderer");
|
---|
56 | try {
|
---|
57 | Class clazz = Class.forName(rendererClassName.toString());
|
---|
58 | return (TextOutputFieldRenderer) clazz.newInstance();
|
---|
59 | }
|
---|
60 | catch (Exception ex) {
|
---|
61 | throw new ClassNotFoundException("Class does not meet the contract for TextInputFieldRenderer:" + rendererClassName);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public static AbstractTextPageRenderer getTextPageRenderer(Page page) throws ClassNotFoundException {
|
---|
66 | String fullyQualified = page.getClass().getName();
|
---|
67 | int lastDot = fullyQualified.lastIndexOf('.');
|
---|
68 | if (lastDot == -1) {
|
---|
69 | throw new UnsupportedOperationException("Pages can not be a member of the default package");
|
---|
70 | }
|
---|
71 | int prevDot = fullyQualified.substring(0,lastDot-1).lastIndexOf('.', lastDot - 1);
|
---|
72 | if (prevDot == -1) {
|
---|
73 | throw new UnsupportedOperationException("Pages can not be a member of a single level package");
|
---|
74 | }
|
---|
75 | StringBuffer rendererClassName = new StringBuffer();
|
---|
76 | rendererClassName.append(fullyQualified.substring(0, prevDot));
|
---|
77 | rendererClassName.append(".renderer.text.");
|
---|
78 | rendererClassName.append(fullyQualified.substring(lastDot + 1));
|
---|
79 | rendererClassName.append("Renderer");
|
---|
80 | try {
|
---|
81 | Class clazz = Class.forName(rendererClassName.toString());
|
---|
82 | return (AbstractTextPageRenderer) clazz.newInstance();
|
---|
83 | }
|
---|
84 | catch (Exception ex) {
|
---|
85 | throw new ClassNotFoundException("Class does not meet the contract for TextPageRenderer:" + rendererClassName);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | public static SwingPageRenderer getSwingPageRenderer(Page page) throws ClassNotFoundException {
|
---|
89 | String fullyQualified = page.getClass().getName();
|
---|
90 | int lastDot = fullyQualified.lastIndexOf('.');
|
---|
91 | if (lastDot == -1) {
|
---|
92 | throw new UnsupportedOperationException("Pages can not be a member of the default package");
|
---|
93 | }
|
---|
94 | int prevDot = fullyQualified.substring(0,lastDot-1).lastIndexOf('.', lastDot - 1);
|
---|
95 | if (prevDot == -1) {
|
---|
96 | throw new UnsupportedOperationException("Pages can not be a member of a single level package");
|
---|
97 | }
|
---|
98 | StringBuffer rendererClassName = new StringBuffer();
|
---|
99 | rendererClassName.append(fullyQualified.substring(0, prevDot));
|
---|
100 | rendererClassName.append(".renderer.swing.");
|
---|
101 | rendererClassName.append(fullyQualified.substring(lastDot + 1));
|
---|
102 | rendererClassName.append("Renderer");
|
---|
103 | try {
|
---|
104 | Class clazz = Class.forName(rendererClassName.toString());
|
---|
105 | return (SwingPageRenderer) clazz.newInstance();
|
---|
106 | }
|
---|
107 | catch (Exception ex) {
|
---|
108 | throw new ClassNotFoundException("Class does not meet the contract for SwingPageRenderer:" + rendererClassName);
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | public static SwingOutputFieldRenderer getSwingRenderer(OutputField field) throws ClassNotFoundException {
|
---|
113 | String fullyQualified = field.getClass().getName();
|
---|
114 | int lastDot = fullyQualified.lastIndexOf('.');
|
---|
115 | if (lastDot == -1) {
|
---|
116 | throw new UnsupportedOperationException("OutputField can not be a member of the default package");
|
---|
117 | }
|
---|
118 | int prevDot = fullyQualified.substring(0,lastDot-1).lastIndexOf('.', lastDot - 1);
|
---|
119 | if (prevDot == -1) {
|
---|
120 | throw new UnsupportedOperationException("OutputField can not be a member of a single level package");
|
---|
121 | }
|
---|
122 | StringBuffer rendererClassName = new StringBuffer();
|
---|
123 | rendererClassName.append(fullyQualified.substring(0, prevDot));
|
---|
124 | rendererClassName.append(".renderer.swing.");
|
---|
125 | rendererClassName.append(fullyQualified.substring(lastDot + 1));
|
---|
126 | rendererClassName.append("Renderer");
|
---|
127 | try {
|
---|
128 | Class clazz = Class.forName(rendererClassName.toString());
|
---|
129 | return (SwingOutputFieldRenderer) clazz.newInstance();
|
---|
130 | }
|
---|
131 | catch (Exception ex) {
|
---|
132 | ex.printStackTrace();
|
---|
133 | throw new ClassNotFoundException("Class does not meet the contract for SwingInputFieldRenderer:" + rendererClassName);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|