Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / PostProcessSupport.java @ 40561

History | View | Annotate | Download (6.58 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils;
25

    
26
import java.lang.reflect.InvocationTargetException;
27
import java.lang.reflect.Method;
28
import java.util.ArrayList;
29

    
30
/**
31
 * @author fjp
32
 *
33
 * Clase que recibe las llamadas que tiene que hacer cuando termine
34
 * la lectura de proyecto, por ejemplo. Recibir?a algo as? como Clase,
35
 * Nombre de funci?n a invocar, Par?metros y Prioridad.
36
 * Lo guarda en un array, ordena por prioridad y empieza a invocar.
37
 */
38
public class PostProcessSupport
39
{
40
    private static ArrayList callList = new ArrayList();
41
    private static class Call
42
    {
43
        private Object obj;
44
        private Method method;
45
        private Object[] parameters;
46
        private int priority;
47
        public Call(Object obj, String strMethod, Object[] parameters, int priority)
48
        {
49
            this.obj = obj;
50
            this.parameters = parameters;
51
            Class[] classParams = new Class[parameters.length];
52
            for (int i=0; i< parameters.length; i++)
53
                classParams[i] = parameters[i].getClass();
54
            try {
55
                method = obj.getClass().getMethod(strMethod, classParams);
56
            } catch (SecurityException e) {
57
                // TODO Auto-generated catch block
58
                e.printStackTrace();
59
            } catch (NoSuchMethodException e) {
60
                // Resulta que con getMethod no pilla bien el superinterface VectorialLegend,
61
                // as? que lo hacemos manual
62
                Method[] list = obj.getClass().getMethods();
63
                for (int i=0; i< list.length; i++)
64
                {
65
                    // System.err.println(list[i].toString());
66
                    if (list[i].getName().compareTo(strMethod) == 0)
67
                    {
68
                        boolean encontrado = true;
69
                        Class[] params = list[i].getParameterTypes();
70
                        // En cada par?metro del m?todo, miramos si
71
                        // el par?metro correspondiente implementa la interfaz
72
                        // adecuada.
73
                        for (int j=0; j < params.length; j++) // for de los par?metros del m?todo.
74
                        {
75
                                // Miramos primero si es de la clase que buscamos, y si no lo es, pasamos
76
                                // a revisar sus interfaces.
77
                                boolean bParamOK = false;
78
//                                if (params[j].getName().compareTo(parameters[j].getClass().getName()) == 0)
79
                                if ( params[j].isInstance(parameters[j]) )
80
                                {
81
                                        bParamOK = true;
82
                                        continue;
83
                                }
84
                            // Interfaces que implementa el par?metro j-?simo que le pasamos.
85
                            Class[] theInterfaces = parameters[j].getClass().getInterfaces();
86

    
87
                            for (int k=0; k< theInterfaces.length; k++)
88
                            {
89
                                // Si alguno de estos interfaces cuadra con lo que
90
                                // espera el m?todo, podemos comprobar el siguiente par?metro.
91
                                // Si al salir del for externo, encontrado sigue siendo true,
92
                                // hemos encontrado el m?todo que busc?bamos.
93
                                if (theInterfaces[k].getName().compareTo(params[j].getName()) == 0)
94
                                {
95
                                    bParamOK = true;
96
                                    break;
97
                                }
98
                            }
99
                            if (bParamOK == false)
100
                            {
101
                                encontrado = false;
102
                                break; // Vamos a por otro m?todo
103
                            }
104
                        }
105
                        if (encontrado)
106
                        {
107
                            method = list[i];
108
                            return;
109
                        }
110
                    }
111
                }
112
                e.printStackTrace();
113
            }
114
        }
115

    
116
        public Object executeMethod() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
117
        {
118
            return method.invoke(obj,parameters);
119
        }
120
    }
121
    public static void addToPostProcess(Object obj, String strMethod, Object[] parameters, int priority)
122
    {
123
        Call newCall = new Call(obj, strMethod, parameters, priority);
124
        callList.add(newCall);
125
    }
126
    public static void addToPostProcess(Object obj, String strMethod, Object parameter, int priority)
127
    {
128
        Object[] parameters = new Object[1];
129
        parameters[0] = parameter;
130
        Call newCall = new Call(obj, strMethod, parameters, priority);
131
        callList.add(newCall);
132
    }
133

    
134
    private static void orderByPriority()
135
    {
136
        // TODO
137
    }
138
    public static void executeCalls()
139
    {
140
        // TODO: Primero deber?amos ordenar por prioridad
141
        // por ahora no lo hago.
142
        orderByPriority();
143

    
144
        for (int i=0; i < callList.size(); i++)
145
        {
146
            Call call = (Call) callList.get(i);
147
            try {
148
                call.executeMethod();
149
            } catch (IllegalArgumentException e) {
150
                // TODO Auto-generated catch block
151
                e.printStackTrace();
152
            } catch (IllegalAccessException e) {
153
                // TODO Auto-generated catch block
154
                e.printStackTrace();
155
            } catch (InvocationTargetException e) {
156
                // TODO Auto-generated catch block
157
                e.printStackTrace();
158
            }
159
        }
160
        clearList();
161
    }
162
    public static void clearList()
163
    {
164
        callList.clear();
165
    }
166
}