Statistics
| Revision:

root / trunk / examples / exaTemplateLayout / src / com / iver / templateLayout / TemplateExtension.java @ 9591

History | View | Annotate | Download (10.6 KB)

1
package com.iver.templateLayout;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.util.ArrayList;
5
import java.util.Hashtable;
6

    
7
import com.iver.andami.PluginServices;
8
import com.iver.andami.plugins.Extension;
9
import com.iver.andami.ui.mdiManager.IWindow;
10
import com.iver.cit.gvsig.Print;
11
import com.iver.cit.gvsig.fmap.DriverException;
12
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
13
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
14
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
15
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
16
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameText;
17
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameView;
18
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
19
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
20

    
21

    
22
/**
23
 * Extensi?n de ejemplo que genera una ficha por cada shape seleccionado,
24
 * cambiando los tag por su valor en la tabla.
25
 *
26
 * @author Vicente Caballero Navarro
27
 */
28
public class TemplateExtension extends Extension {
29
        //Indica si es la primera ficha lanzada a imprimir.
30
        private boolean isFirst = true;
31

    
32
        //Layout que modificamos para la creaci?n de las fichas.
33
        private Layout layout = null;
34

    
35
        //Array de booleanos que contiene en la posici?n:
36
        //"0"-->Si se selecciona la impresi?n de todas las fichas generadas se pone a true.
37
        //"1"-->Si se selecciona cancelar de todas las fichas generadas se pone a false.
38
        private boolean[] printAll = { false, true };
39

    
40
        //Otra extensi?n, que es utlizada para lanzar el Layout a impresi?n.
41
        private Print printExtension = new Print();
42

    
43
        //Almacena el texto de todos los FFrameText del Layout.
44
        private Hashtable hashtext = new Hashtable();
45

    
46
        //ArrayList que almacena todos los FFrames que contiene el Layout.
47
        private IFFrame[] fframes = null;
48

    
49
        /**
50
         * @see com.iver.mdiApp.plugins.Extension#isEnabled()
51
         */
52
        public boolean isEnabled() {
53
                //Devuelvo true, y de esta forma siempre estar?n activos.
54
                return true;
55
        }
56

    
57
        /**
58
         * @see com.iver.mdiApp.plugins.Extension#isVisible()
59
         */
60
        public boolean isVisible() {
61
                //Se obtiene la vista activada.
62
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
63

    
64
                //Se comprueba si la vista es null y se devuelve un false para no hacer visible la extensi?n.
65
                if (f == null) {
66
                        return false;
67
                }
68

    
69
                //Se comprueba si la vista activa contiene un Layout y 
70
                //si es as? se devuelve true como visible o false si no contiene un Layout.
71
                if (f instanceof Layout) {
72
                        return true;
73
                } else {
74
                        return false;
75
                }
76
        }
77

    
78
        /**
79
         * Guarda el contenido de todos los FFrameText para poder  recuperarlos al
80
         * final.
81
         */
82
        private void initFFrameText() {
83
                //Recorro todos los FFrames que contiene el Layout y compruebo que son una instancia de FFrameText, 
84
                //para guardar en hastext el texto y su FFrameText.
85
                for (int ft = 0; ft < fframes.length; ft++) {
86
                        if (fframes[ft] instanceof FFrameText) {
87
                                ArrayList al = new ArrayList();
88

    
89
                                for (int i = 0;
90
                                                i < ((FFrameText) fframes[ft]).getText().size();
91
                                                i++) {
92
                                        al.add(((FFrameText) fframes[ft]).getText().get(i));
93
                                }
94

    
95
                                hashtext.put(fframes[ft], al);
96
                        }
97
                }
98
        }
99

    
100
        /**
101
         * Recupera el texto que inicialmente ten?an todos FFrameText.
102
         */
103
        private void endFFrameText() {
104
                //Recorro todos los FFrames que contiene el Layout y compruebo que son una instancia de FFrameText, 
105
                //para inicializarlo con su texto original almacenado en hashtext.
106
                for (int ft = 0; ft < fframes.length; ft++) {
107
                        if (fframes[ft] instanceof FFrameText) {
108
                                ArrayList text = (ArrayList) hashtext.get(fframes[ft]);
109
                                ((FFrameText) fframes[ft]).getText().clear();
110

    
111
                                for (int i = 0; i < text.size(); i++) {
112
                                        ((FFrameText) fframes[ft]).addText((String) text.get(i));
113
                                }
114
                        }
115
                }
116
        }
117

    
118
        /**
119
         * Inicia la creaci?n de todas las platillas posibles a partir de un
120
         * FFrameView.
121
         *
122
         * @param fframeview FFrameView a modificar.
123
         *
124
         * @throws DriverException
125
         * @throws com.hardcode.gdbms.engine.data.DriverException
126
         * @throws DriverIOException
127
         * @throws com.hardcode.gdbms.engine.data.driver.DriverException 
128
         */
129
        private void initTemplates(FFrameView fframeview)
130
                throws DriverException, 
131
                        DriverIOException, com.hardcode.gdbms.engine.data.driver.DriverException {
132
                //Inicio la opci?n de cancelar la impresi?n de todas las fichas a true.
133
                printAll[1] = true;
134

    
135
                //Rect?ngulo del extent inicial de la vista contenida en el FFrameView.
136
                Rectangle2D.Double rectIni = new Rectangle2D.Double();
137
                rectIni.setRect(fframeview.getMapContext().getViewPort().getExtent());
138

    
139
                //Entero que representa el tipo de escala que tiene seleccionada el FFrameView.
140
                int typeScaleIni = fframeview.getTypeScale();
141

    
142
                //Guarda los textos iniciales de todos los FFrameText
143
                initFFrameText();
144

    
145
                //Recorro todas las capas de la vista.
146
                for (int i = 0; i < fframeview.getMapContext().getLayers().getLayersCount();
147
                                i++) {
148
                        //Compruebo que la capa este activada.
149
                        if (fframeview.getMapContext().getLayers().getLayer(i).isActive()) {
150
                                //Obtengo su recordSet
151
                                if (fframeview.getMapContext().getLayers().getLayer(i) instanceof AlphanumericData) {
152
                                        SelectableDataSource dataSource = ((AlphanumericData) fframeview.getMapContext()
153
                                                                                                                                                                        .getLayers()
154
                                                                                                                                                                        .getLayer(i)).getRecordset();
155

    
156
                                        //Recorro todos los registros.
157
                                        for (long k = 0; k < dataSource.getRowCount(); k++) {
158
                                                //Compruebo que est? seleccionado.
159
                                                if (dataSource.isSelected((int) k)) {
160
                                                        //Actualizo la posici?n del puntero del recordSet.
161
                                                        ////dataSource.moveTo(k);
162
                                                        //Obtengo el rect?ngulo del shape para despu?s ponerlo como extent del FFrameView
163
                                                        Rectangle2D rec = ((SingleLayer) fframeview
164
                                                                                           .getMapContext().getLayers().getLayer(i)).getSource()
165
                                                                                           .getShape((int) k).getBounds2D();
166

    
167
                                                        //fframeview.setLinked(false);
168
                                                        //Cambio el extent de la vista que contiene fframeview, 
169
                                                        //del que ten?a al extent del shape seleccionado.
170
                                                        fframeview.setNewExtent(rec);
171

    
172
                                                        //Cambio el tipo de escala de fframeview a Escala especificada por el usuario.
173
                                                        fframeview.setTypeScale(FFrameView.MANUAL);
174

    
175
                                                        //Recorro todos los FFrames que contiene el Layout y compruebo que son una instancia de FFrameText, 
176
                                                        //para inicializarlo con el valor correspondiente.
177
                                                        for (int ft = 0; ft < fframes.length; ft++) {
178
                                                                if (fframes[ft] instanceof FFrameText) {
179
                                                                        if (fframeview != null) {
180
                                                                                initFFrameText((FFrameText) fframes[ft], dataSource, k);
181
                                                                        }
182
                                                                }
183
                                                        }
184

    
185
                                                        if (isFirst) {
186
                                                                //Si es el primer registro seleccionado se abre el di?logo 
187
                                                                //para preguntar si se quiere imprimir todas las fichas.
188
                                                                askAll();
189
                                                                isFirst = false;
190
                                                        }
191

    
192
                                                        if (!printAll[1]) {
193
                                                                //Si se cancela la impresi?n desde el di?logo askAll se termina la creaci?n de fichas.
194
                                                                //Pongo isFirst a true para que la pr?xima vez que se llame 
195
                                                                //a este m?todo se pregunte si se quiere imprimir todas las fichas.
196
                                                                isFirst = true;
197

    
198
                                                                return;
199
                                                        }
200

    
201
                                                        if (printAll[0]) {
202
                                                                //Imprime la ficha actual.
203
                                                                printExtension.printLayout(layout);
204
                                                        } else {
205
                                                                //Refrescar el Layout para ver la ficha actual en pantalla.
206
                                                                layout.getLayoutControl().refresh();
207
                                                                //Preguntar si se quiere imprimir la ficha actual.
208
                                                                askOne();
209
                                                        }
210
                                                }
211
                                        }
212
                                }
213
                        }
214
                }
215

    
216
                //Recupera el Extent inicial del FFrameView.
217
                fframeview.getMapContext().getViewPort().setExtent(rectIni);
218

    
219
                //Recupera el tipo de escala que ten?a seleccionada el FFrameView.
220
                fframeview.setTypeScale(typeScaleIni);
221

    
222
                //Recupera el texto de todos los FFrameText del Layout.
223
                endFFrameText();
224

    
225
                //Refresca el Layout en pantalla.
226
                layout.getLayoutControl().refresh();
227

    
228
                //Volvemos a inicializar el boolean que sirve para saber si estamos en la primera ficha a imprimir.
229
                isFirst = true;
230
        }
231

    
232
        /**
233
         * Modifica el FFrameText que se le pasa como par?metro cambiando su texto
234
         * por el valor del campo que representa el tag.
235
         *
236
         * @param fframetext FFrameText a modificar.
237
         * @param dataSource DataSource para extraer el valor.
238
         * @param k indice del registro.
239
         * @throws com.hardcode.gdbms.engine.data.driver.DriverException 
240
         *
241
         * @throws com.hardcode.gdbms.engine.data.DriverException
242
         */
243
        private void initFFrameText(FFrameText fframetext,
244
                SelectableDataSource dataSource, long k) throws com.hardcode.gdbms.engine.data.driver.DriverException
245
                 {
246
                //Recorro todos los campos.
247
                dataSource.start();
248
                for (int j = 0; j < dataSource.getFieldCount(); j++) {
249
                        //Compruebo si el tag es igual al campo, y si es as?, 
250
                        //borro el texto que contiene y le a?ado el valor.
251
                        if (dataSource.getFieldName(j).equals(fframetext.getTag())) {
252
                                fframetext.getText().clear();
253
                                fframetext.addText(dataSource.getFieldValue(k, j)
254
                                                                                         .toString());
255
                        }
256
                }
257
                dataSource.stop();
258
        }
259

    
260
        /**
261
         * Abre el di?logo que pregunta si se quieren imprimir todas las fichas que
262
         * se generan.
263
         */
264
        public void askAll() {
265
                AskAll all = new AskAll(printAll);
266

    
267
                //A?ade el objeto all que extiende JPanel 
268
                //e implementa com.iver.mdiApp.ui.MDIManager.View como una 
269
                //ventana m?s de la aplicaci?n.
270
                PluginServices.getMDIManager().addWindow(all);
271
        }
272

    
273
        /**
274
         * Abre el di?logo que pregunta si la ficha que se esta visualizando se
275
         * quiere imprimir.
276
         */
277
        public void askOne() {
278
                AskOne one = new AskOne(layout);
279

    
280
                //A?ade el objeto one que extiende JPanel 
281
                //e implementa com.iver.mdiApp.ui.MDIManager.View como una 
282
                //ventana m?s de la aplicaci?n.
283
                PluginServices.getMDIManager().addWindow(one);
284
        }
285

    
286
        /**
287
         * @see com.iver.andami.plugins.Extension#inicializar()
288
         */
289
        public void initialize() {
290
        }
291

    
292
        /**
293
         * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
294
         */
295
        public void execute(String actionCommand) {
296
                //Obtengo el Layout que contiene la vista actualmente seleccionada en ANDAMI.
297
                layout = (Layout) PluginServices.getMDIManager().getActiveWindow();
298

    
299
                //Obtengo todos los FFrames que contiene el Layout.
300
                fframes = layout.getLayoutContext().getFFrames();
301

    
302
                //Recorro todos los FFrames comprobando si es una instacia de FFrameView 
303
                //para ejecutar initTemplates en el caso de que as? sea.
304
                for (int i = 0; i < fframes.length; i++) {
305
                        if (fframes[i] instanceof FFrameView) {
306
                                try {
307
                                        //Inicia la creaci?n de todas las platillas posibles a partir de fframeview.
308
                                        initTemplates((FFrameView) fframes[i]);
309
                                } catch (DriverException e) {
310
                                        e.printStackTrace();
311
                                } catch (DriverIOException e) {
312
                                        e.printStackTrace();
313
                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
314
                    // TODO Auto-generated catch block
315
                    e.printStackTrace();
316
                }
317
                        }
318
                }
319
        }
320
}