Statistics
| Revision:

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
/*
2
 * Created on 24-ago-2005
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * 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
 *
13
 * 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
 *
18
 * 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
 *
22
 * 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
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +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
    private static class Call
62
    {
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
                // Resulta que con getMethod no pilla bien el superinterface VectorialLegend,
81
                // as? que lo hacemos manual
82
                Method[] list = obj.getClass().getMethods();
83
                for (int i=0; i< list.length; i++)
84
                {
85
                    // System.err.println(list[i].toString());
86
                    if (list[i].getName().compareTo(strMethod) == 0)
87
                    {
88
                        boolean encontrado = true;
89
                        Class[] params = list[i].getParameterTypes();
90
                        // En cada par?metro del m?todo, miramos si
91
                        // 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
                        {
95
                                // 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
//                                if (params[j].getName().compareTo(parameters[j].getClass().getName()) == 0)
99
                                if ( params[j].isInstance(parameters[j]) )
100
                                {
101
                                        bParamOK = true;
102
                                        continue;
103
                                }
104
                            // Interfaces que implementa el par?metro j-?simo que le pasamos.
105
                            Class[] theInterfaces = parameters[j].getClass().getInterfaces();
106

    
107
                            for (int k=0; k< theInterfaces.length; k++)
108
                            {
109
                                // Si alguno de estos interfaces cuadra con lo que
110
                                // espera el m?todo, podemos comprobar el siguiente par?metro.
111
                                // Si al salir del for externo, encontrado sigue siendo true,
112
                                // hemos encontrado el m?todo que busc?bamos.
113
                                if (theInterfaces[k].getName().compareTo(params[j].getName()) == 0)
114
                                {
115
                                    bParamOK = true;
116
                                    break;
117
                                }
118
                            }
119
                            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
                            return;
129
                        }
130
                    }
131
                }
132
                e.printStackTrace();
133
            }
134
        }
135

    
136
        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
        Call newCall = new Call(obj, strMethod, parameters, priority);
144
        callList.add(newCall);
145
    }
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

    
154
    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

    
164
        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
}