svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libIverUtiles / src / com / iver / utiles / PostProcessSupport.java @ 28465
History | View | Annotate | Download (6.86 KB)
1 | 2673 | fjp | /*
|
---|---|---|---|
2 | * Created on 24-ago-2005
|
||
3 | *
|
||
4 | * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
5 | 28465 | fdiaz | *
|
6 | 2673 | fjp | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 | 28465 | fdiaz | *
|
8 | 2673 | fjp | * This program is free software; you can redistribute it and/or
|
9 | * modify it under the terms of the GNU General Public License
|
||
10 | * as published by the Free Software Foundation; either version 2
|
||
11 | * of the License, or (at your option) any later version.
|
||
12 | 28465 | fdiaz | *
|
13 | 2673 | fjp | * This program is distributed in the hope that it will be useful,
|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
16 | * GNU General Public License for more details.
|
||
17 | 28465 | fdiaz | *
|
18 | 2673 | fjp | * You should have received a copy of the GNU General Public License
|
19 | * along with this program; if not, write to the Free Software
|
||
20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||
21 | 28465 | fdiaz | *
|
22 | 2673 | fjp | * For more information, contact:
|
23 | *
|
||
24 | * Generalitat Valenciana
|
||
25 | * Conselleria d'Infraestructures i Transport
|
||
26 | * Av. Blasco Ib??ez, 50
|
||
27 | * 46010 VALENCIA
|
||
28 | * SPAIN
|
||
29 | *
|
||
30 | * +34 963862235
|
||
31 | * gvsig@gva.es
|
||
32 | * www.gvsig.gva.es
|
||
33 | 28465 | fdiaz | *
|
34 | 2673 | fjp | * or
|
35 | 28465 | fdiaz | *
|
36 | 2673 | fjp | * IVER T.I. S.A
|
37 | * Salamanca 50
|
||
38 | * 46005 Valencia
|
||
39 | * Spain
|
||
40 | 28465 | fdiaz | *
|
41 | 2673 | fjp | * +34 963163400
|
42 | * dac@iver.es
|
||
43 | */
|
||
44 | package com.iver.utiles; |
||
45 | |||
46 | import java.lang.reflect.InvocationTargetException; |
||
47 | import java.lang.reflect.Method; |
||
48 | import java.util.ArrayList; |
||
49 | |||
50 | /**
|
||
51 | * @author fjp
|
||
52 | *
|
||
53 | * Clase que recibe las llamadas que tiene que hacer cuando termine
|
||
54 | * la lectura de proyecto, por ejemplo. Recibir?a algo as? como Clase,
|
||
55 | * Nombre de funci?n a invocar, Par?metros y Prioridad.
|
||
56 | * Lo guarda en un array, ordena por prioridad y empieza a invocar.
|
||
57 | */
|
||
58 | public class PostProcessSupport |
||
59 | { |
||
60 | private static ArrayList callList = new ArrayList(); |
||
61 | 28465 | fdiaz | private static class Call |
62 | 2673 | fjp | { |
63 | private Object obj; |
||
64 | private Method method; |
||
65 | private Object[] parameters; |
||
66 | private int priority; |
||
67 | public Call(Object obj, String strMethod, Object[] parameters, int priority) |
||
68 | { |
||
69 | this.obj = obj;
|
||
70 | this.parameters = parameters;
|
||
71 | Class[] classParams = new Class[parameters.length]; |
||
72 | for (int i=0; i< parameters.length; i++) |
||
73 | classParams[i] = parameters[i].getClass(); |
||
74 | try {
|
||
75 | method = obj.getClass().getMethod(strMethod, classParams); |
||
76 | } catch (SecurityException e) { |
||
77 | // TODO Auto-generated catch block
|
||
78 | e.printStackTrace(); |
||
79 | } catch (NoSuchMethodException e) { |
||
80 | 28465 | fdiaz | // Resulta que con getMethod no pilla bien el superinterface VectorialLegend,
|
81 | 2699 | fjp | // as? que lo hacemos manual
|
82 | 2673 | fjp | Method[] list = obj.getClass().getMethods(); |
83 | for (int i=0; i< list.length; i++) |
||
84 | { |
||
85 | 2699 | fjp | // System.err.println(list[i].toString());
|
86 | 2673 | fjp | if (list[i].getName().compareTo(strMethod) == 0) |
87 | { |
||
88 | 2699 | fjp | boolean encontrado = true; |
89 | 2673 | fjp | Class[] params = list[i].getParameterTypes(); |
90 | 28465 | fdiaz | // En cada par?metro del m?todo, miramos si
|
91 | 2699 | fjp | // el par?metro correspondiente implementa la interfaz
|
92 | // adecuada.
|
||
93 | for (int j=0; j < params.length; j++) // for de los par?metros del m?todo. |
||
94 | 2673 | fjp | { |
95 | 6620 | fjp | // Miramos primero si es de la clase que buscamos, y si no lo es, pasamos
|
96 | // a revisar sus interfaces.
|
||
97 | boolean bParamOK = false; |
||
98 | 28465 | fdiaz | // if (params[j].getName().compareTo(parameters[j].getClass().getName()) == 0)
|
99 | if ( params[j].isInstance(parameters[j]) )
|
||
100 | 6620 | fjp | { |
101 | bParamOK = true;
|
||
102 | continue;
|
||
103 | } |
||
104 | 2699 | fjp | // Interfaces que implementa el par?metro j-?simo que le pasamos.
|
105 | 2673 | fjp | Class[] theInterfaces = parameters[j].getClass().getInterfaces(); |
106 | 28465 | fdiaz | |
107 | 2673 | fjp | for (int k=0; k< theInterfaces.length; k++) |
108 | { |
||
109 | 2699 | fjp | // Si alguno de estos interfaces cuadra con lo que
|
110 | // espera el m?todo, podemos comprobar el siguiente par?metro.
|
||
111 | 28465 | fdiaz | // Si al salir del for externo, encontrado sigue siendo true,
|
112 | 2699 | fjp | // hemos encontrado el m?todo que busc?bamos.
|
113 | 2673 | fjp | if (theInterfaces[k].getName().compareTo(params[j].getName()) == 0) |
114 | { |
||
115 | 2699 | fjp | bParamOK = true;
|
116 | break;
|
||
117 | 2673 | fjp | } |
118 | } |
||
119 | 2699 | fjp | if (bParamOK == false) |
120 | { |
||
121 | encontrado = false;
|
||
122 | break; // Vamos a por otro m?todo |
||
123 | } |
||
124 | } |
||
125 | if (encontrado)
|
||
126 | { |
||
127 | method = list[i]; |
||
128 | 28465 | fdiaz | return;
|
129 | 2699 | fjp | } |
130 | 2673 | fjp | } |
131 | } |
||
132 | e.printStackTrace(); |
||
133 | } |
||
134 | } |
||
135 | 28465 | fdiaz | |
136 | 2673 | fjp | public Object executeMethod() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException |
137 | { |
||
138 | return method.invoke(obj,parameters);
|
||
139 | } |
||
140 | } |
||
141 | public static void addToPostProcess(Object obj, String strMethod, Object[] parameters, int priority) |
||
142 | { |
||
143 | 28465 | fdiaz | Call newCall = new Call(obj, strMethod, parameters, priority);
|
144 | 6620 | fjp | callList.add(newCall); |
145 | 2673 | fjp | } |
146 | public static void addToPostProcess(Object obj, String strMethod, Object parameter, int priority) |
||
147 | { |
||
148 | Object[] parameters = new Object[1]; |
||
149 | parameters[0] = parameter;
|
||
150 | Call newCall = new Call(obj, strMethod, parameters, priority);
|
||
151 | callList.add(newCall); |
||
152 | } |
||
153 | 28465 | fdiaz | |
154 | 2673 | fjp | private static void orderByPriority() |
155 | { |
||
156 | // TODO
|
||
157 | } |
||
158 | public static void executeCalls() |
||
159 | { |
||
160 | // TODO: Primero deber?amos ordenar por prioridad
|
||
161 | // por ahora no lo hago.
|
||
162 | orderByPriority(); |
||
163 | 28465 | fdiaz | |
164 | 2673 | fjp | for (int i=0; i < callList.size(); i++) |
165 | { |
||
166 | Call call = (Call) callList.get(i); |
||
167 | try {
|
||
168 | call.executeMethod(); |
||
169 | } catch (IllegalArgumentException e) { |
||
170 | // TODO Auto-generated catch block
|
||
171 | e.printStackTrace(); |
||
172 | } catch (IllegalAccessException e) { |
||
173 | // TODO Auto-generated catch block
|
||
174 | e.printStackTrace(); |
||
175 | } catch (InvocationTargetException e) { |
||
176 | // TODO Auto-generated catch block
|
||
177 | e.printStackTrace(); |
||
178 | } |
||
179 | } |
||
180 | clearList(); |
||
181 | } |
||
182 | public static void clearList() |
||
183 | { |
||
184 | callList.clear(); |
||
185 | } |
||
186 | } |