Statistics
| Revision:

svn-gvsig-desktop / branches / gvSIG_CAD_Layout_version / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / FLayoutGraphics.java @ 1822

History | View | Annotate | Download (13.7 KB)

1
/*
2
 * Created on 27-jul-2004
3
 *
4
 */
5
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45
package com.iver.cit.gvsig.gui.layout;
46

    
47
import com.iver.andami.PluginServices;
48

    
49
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
50
import com.iver.cit.gvsig.gui.layout.dialogs.FAlignDialog;
51
import com.iver.cit.gvsig.gui.layout.dialogs.FBorderDialog;
52
import com.iver.cit.gvsig.gui.layout.dialogs.FPositionDialog;
53
import com.iver.cit.gvsig.gui.layout.fframes.FFrameGraphic;
54
import com.iver.cit.gvsig.gui.layout.fframes.FFrameGroup;
55
import com.iver.cit.gvsig.gui.layout.fframes.FFrameLegend;
56
import com.iver.cit.gvsig.gui.layout.fframes.FFramePicture;
57
import com.iver.cit.gvsig.gui.layout.fframes.FFrameScaleBar;
58
import com.iver.cit.gvsig.gui.layout.fframes.FFrameText;
59
import com.iver.cit.gvsig.gui.layout.fframes.FFrameView;
60
import com.iver.cit.gvsig.gui.layout.fframes.IFFrame;
61
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFrameGraphicsDialog;
62
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.IFFrameDialog;
63

    
64
import java.awt.geom.Rectangle2D;
65

    
66
import java.io.IOException;
67

    
68
import java.util.ArrayList;
69

    
70

    
71
/**
72
 * Operaciones realizadas sobre el conjunto de FFrames.
73
 *
74
 * @author Vicente Caballero Navarro
75
 */
76
public class FLayoutGraphics {
77
        private Layout layout;
78
        private FAlignDialog m_alignLayout = null;
79
        private FBorderDialog borderdialog = null;
80
        private FPositionDialog positiondialog = null;
81

    
82
        /**
83
         * Crea un nuevo FLayoutGraphics.
84
         *
85
         * @param l Referencia al Layout.
86
         */
87
        public FLayoutGraphics(Layout l) {
88
                layout = l;
89
        }
90

    
91
        /**
92
         * Transforma un FFrameLegend a FFrames de tipo FFrameSymbol y FFrameText.
93
         *
94
         * @throws DriverIOException
95
         * @throws IOException
96
         */
97
        public void simplify() throws DriverIOException, IOException {
98
                ArrayList frames = layout.getAllFFrames();
99
                startComplex();
100

    
101
                for (int i = frames.size() - 1; i >= 0; i--) {
102
                        IFFrame fframe = (IFFrame) frames.get(i);
103

    
104
                        if (fframe instanceof FFrameLegend) {
105
                                if (fframe.getSelected() != IFFrame.NOSELECT) {
106
                                        ((FFrameLegend) fframe).toFFrames(layout, i);
107
                                }
108
                        }
109
                }
110

    
111
                endComplex();
112
                layout.setStatus(Layout.DESACTUALIZADO);
113
                layout.repaint();
114
        }
115

    
116
        /**
117
         * Agrupar en un FFrameGroup todos los FFrames seleccionados.
118
         *
119
         * @throws DriverIOException
120
         * @throws IOException
121
         */
122
        public void grouping() throws DriverIOException, IOException {
123
                //Rectangle2D rex = new Rectangle2D.Double();
124
                //                Se debe controlar de alguna forma si hay varios seleccionados.
125
                IFFrame fframegroup = new FFrameGroup();
126
                ArrayList frames = layout.getAllFFrames();
127

    
128
                if (frames.size() > 1) {
129
                        ArrayList selecList = new ArrayList();
130
                        startComplex();
131

    
132
                        for (int i = frames.size() - 1; i >= 0; i--) {
133
                                IFFrame fframe = (IFFrame) frames.get(i);
134

    
135
                                if ((fframe.getSelected() != IFFrame.NOSELECT) &&
136
                                                !layout.isInvalidate(i)) {
137
                                        selecList.add(fframe);
138
                                        getEFS().removeFFrame(fframe, i);
139

    
140
                                        //layout.setInvalidate(i,true);//getFFrames().remove(fframe);
141
                                }
142
                        }
143

    
144
                        for (int i = selecList.size() - 1; i >= 0; i--) {
145
                                ((FFrameGroup) fframegroup).addFFrame((IFFrame) selecList.get(i));
146
                        }
147

    
148
                        ((FFrameGroup) fframegroup).setAt(layout.getAT());
149

    
150
                        Rectangle2D.Double rd = ((FFrameGroup) fframegroup).getRectangle(layout.getAT());
151

    
152
                        //Rectangle re=new Rectangle((int)rd.getMinX(),(int)rd.getMinY(),(int)rd.getWidth(),(int)rd.getHeight());
153
                        Rectangle2D.Double rd1 = FLayoutUtilities.toSheetRect(rd,
154
                                        layout.getAT());
155

    
156
                        fframegroup.setBoundBox(rd1);
157
                        fframegroup.setSelected(true);
158
                        getEFS().addFFrame(fframegroup);
159

    
160
                        //layout.addFFrame(fframegroup, true);
161
                        layout.setStatus(Layout.DESACTUALIZADO);
162
                        layout.repaint();
163
                        endComplex();
164
                }
165
        }
166

    
167
        /**
168
         * Desagrupar los FFrames que estan contenidos dentro del FFrameGroup en
169
         * FFrames individuales.
170
         *
171
         * @throws DriverIOException
172
         * @throws IOException
173
         */
174
        public void ungrouping() throws DriverIOException, IOException {
175
                ArrayList frames = layout.getAllFFrames();
176
                startComplex();
177

    
178
                for (int i = frames.size() - 1; i >= 0; i--) {
179
                        IFFrame fframe = (IFFrame) frames.get(i);
180

    
181
                        if ((fframe.getSelected() != IFFrame.NOSELECT) &&
182
                                        !layout.isInvalidate(i)) {
183
                                if (fframe instanceof FFrameGroup) {
184
                                        FFrameGroup fframegroup = (FFrameGroup) fframe;
185
                                        ArrayList selecList = new ArrayList();
186

    
187
                                        for (int j = fframegroup.getFFrames().size() - 1; j >= 0;
188
                                                        j--) {
189
                                                selecList.add(fframegroup.getFFrames().get(j));
190

    
191
                                                fframegroup.getFFrames().remove(j);
192
                                        }
193

    
194
                                        for (int j = selecList.size() - 1; j >= 0; j--) {
195
                                                //layout.addFFrame((IFFrame) selecList.get(j), false);
196
                                                getEFS().addFFrame((IFFrame) selecList.get(j));
197
                                        }
198

    
199
                                        if (fframegroup.getFFrames().isEmpty()) {
200
                                                getEFS().removeFFrame(fframegroup, i);
201

    
202
                                                //layout.setInvalidate(i,true);
203
                                        }
204
                                }
205
                        }
206
                }
207

    
208
                endComplex();
209
                layout.setStatus(Layout.DESACTUALIZADO);
210
                layout.repaint();
211
        }
212

    
213
        /**
214
         * Abre el di?logo para alinear los FFrames.
215
         */
216
        public void aligning() {
217
                m_alignLayout = new FAlignDialog(layout);
218
                PluginServices.getMDIManager().addView(m_alignLayout);
219
        }
220

    
221
        /**
222
         * Posiciona los FFrames seleccionados delante de los no seleccionados.
223
         *
224
         * @throws DriverIOException
225
         * @throws IOException
226
         */
227
        public void before() throws DriverIOException, IOException {
228
                ArrayList auxListBefore = new ArrayList();
229
                ArrayList auxListBehind = new ArrayList();
230
                int numBefore = 0;
231
                int numBehind = 0;
232
                ArrayList frames = layout.getAllFFrames();
233
                startComplex();
234

    
235
                for (int i = frames.size() - 1; i >= 0; i--) {
236
                        IFFrame fframe = (IFFrame) frames.get(i);
237

    
238
                        if (!layout.isInvalidate(i)) {
239
                                if (fframe.getSelected() != IFFrame.NOSELECT) {
240
                                        auxListBefore.add(fframe);
241
                                        getEFS().removeFFrame(fframe, i);
242

    
243
                                        //layout.setInvalidate(i,true);
244
                                        numBefore++;
245
                                } else {
246
                                        auxListBehind.add(fframe);
247
                                        getEFS().removeFFrame(fframe, i);
248

    
249
                                        //layout.setInvalidate(i,true);
250
                                        numBehind++;
251
                                }
252
                        }
253
                }
254

    
255
                for (int i = numBehind - 1; i >= 0; i--) {
256
                        getEFS().addFFrame((IFFrame) auxListBehind.get(i));
257

    
258
                        //layout.getAllFFrames().add((IFFrame) auxList.get(i));
259
                }
260

    
261
                for (int i = numBefore - 1; i >= 0; i--) {
262
                        getEFS().addFFrame((IFFrame) auxListBefore.get(i));
263

    
264
                        //layout.getAllFFrames().add((IFFrame) auxList.get(i));
265
                }
266

    
267
                endComplex();
268
                layout.setStatus(Layout.DESACTUALIZADO);
269
                layout.repaint();
270
        }
271

    
272
        /**
273
         * Posiciona los FFrames seleccionados detr?s de los FFrames no
274
         * seleccionados.
275
         *
276
         * @throws DriverIOException
277
         * @throws IOException
278
         */
279
        public void behind() throws DriverIOException, IOException {
280
                ArrayList auxListBehind = new ArrayList();
281
                ArrayList auxListBefore = new ArrayList();
282
                int numBehind = 0;
283
                int numBefore = 0;
284
                ArrayList frames = layout.getAllFFrames();
285
                startComplex();
286

    
287
                for (int i = frames.size() - 1; i >= 0; i--) {
288
                        IFFrame fframe = (IFFrame) frames.get(i);
289

    
290
                        if (!layout.isInvalidate(i)) {
291
                                if (fframe.getSelected() != IFFrame.NOSELECT) {
292
                                        auxListBehind.add(fframe);
293
                                        getEFS().removeFFrame(fframe, i);
294

    
295
                                        //layout.setInvalidate(i,true);
296
                                        numBehind++;
297
                                } else {
298
                                        auxListBefore.add(fframe);
299
                                        getEFS().removeFFrame(fframe, i);
300

    
301
                                        //layout.setInvalidate(i,true);
302
                                        numBefore++;
303
                                }
304
                        }
305
                }
306

    
307
                for (int i = numBehind - 1; i >= 0; i--) {
308
                        getEFS().addFFrame((IFFrame) auxListBehind.get(i));
309

    
310
                        //layout.getAllFFrames().add(0, (IFFrame) auxList.get(i));
311
                }
312

    
313
                for (int i = numBefore - 1; i >= 0; i--) {
314
                        getEFS().addFFrame((IFFrame) auxListBefore.get(i));
315

    
316
                        //layout.getAllFFrames().add(0, (IFFrame) auxList.get(i));
317
                }
318

    
319
                endComplex();
320
                layout.setStatus(Layout.DESACTUALIZADO);
321
                layout.repaint();
322
        }
323

    
324
        /**
325
         * Abre el di?logo adecuadao a las propiedades del FFrame seleccionado,
326
         * ahora mismo solo se abre cuando hay un solo FFrame seleccionado.
327
         */
328
        public void openFFrameDialog() {
329
                //TODO falta que implementar con la nueva forma de geometrias en el Layout.
330

    
331
                /*FBitSet geomSel = layout.getCadToolAdapter().getSelection();
332
                   ArrayList geometries = new ArrayList();
333
                   for (int i = geomSel.nextSetBit(0); i >= 0;
334
                                   i = geomSel.nextSetBit(i + 1)) {
335
                           try {
336
                                   IGeometry geo = layout.getCadToolAdapter()
337
                                                                             .getEditableFeatureSource().getGeometry(i);
338
                                   geometries.add(geo);
339
                           } catch (IOException e) {
340
                                   e.printStackTrace();
341
                           } catch (DriverIOException e) {
342
                                   e.printStackTrace();
343
                           }
344
                   }
345
                 */
346
                ArrayList selected = new ArrayList();
347
                ArrayList frames = layout.getAllFFrames();
348

    
349
                for (int i = 0; i < frames.size(); i++) {
350
                        if (frames.get(i) instanceof FFrameGraphic &&
351
                                        (((IFFrame) frames.get(i)).getSelected() != IFFrame.NOSELECT) &&
352
                                        !layout.isInvalidate(i)) {
353
                                selected.add(frames.get(i));
354
                        }
355
                }
356

    
357
                if (!(selected.size() == 0)) {
358
                        IFFrameDialog fframedialog = new FFrameGraphicsDialog(layout,
359
                                        (FFrameGraphic) selected.get(0));
360
                        PluginServices.getMDIManager().addView(fframedialog);
361
                } else {
362
                        ArrayList selecList = new ArrayList();
363

    
364
                        //IFFrame[] frames = layout.getFFrames1();
365
                        for (int i = 0; i < frames.size(); i++) {
366
                                //Abrir el di?logo de cada uno de los fframe seleccionados o 
367
                                //uno que sea general como en arcView.
368
                                if ((((IFFrame) frames.get(i)).getSelected() != IFFrame.NOSELECT) &&
369
                                                layout.isInvalidate(i)) {
370
                                        selecList.add(frames.get(i));
371
                                }
372
                        }
373

    
374
                        if (selecList.size() == 1) {
375
                                int toolaux = layout.getTool();
376
                                layout.setTool(getType((IFFrame) selecList.get(0)));
377
                                layout.openFFrameDialog(((IFFrame) selecList.get(0)));
378
                                layout.setTool(toolaux);
379
                        }
380
                }
381
        }
382

    
383
        /**
384
         * Devuelve un entero que representa el tipo de FFrame  que se le pasa como
385
         * par?metro.
386
         *
387
         * @param fframe FFrame que se pasa como par?metro, para saber de que tipo
388
         *                   es.
389
         *
390
         * @return entero.
391
         */
392
        private int getType(IFFrame fframe) {
393
                if (fframe instanceof FFrameView) {
394
                        return Layout.RECTANGLEVIEW;
395
                } else if (fframe instanceof FFrameText) {
396
                        return Layout.RECTANGLETEXT;
397
                } else if (fframe instanceof FFrameScaleBar) {
398
                        return Layout.RECTANGLESCALEBAR;
399
                } else if (fframe instanceof FFramePicture) {
400
                        return Layout.RECTANGLEPICTURE;
401
                } else if (fframe instanceof FFrameLegend) {
402
                        return Layout.RECTANGLELEGEND;
403
                } else if (fframe instanceof FFrameGraphic) {
404
                        return Layout.GRAPHICS;
405
                } /*else if (fframe instanceof FFrameGraphics) {
406
                   return Layout.GRAPHICS;
407
                   }*/
408
                return Layout.SELECT;
409
        }
410

    
411
        /**
412
         * Selecci?n de todos los FFrames del Layout.
413
         */
414
        public void selecAll() {
415
                IFFrame[] frames = layout.getFFrames1();
416

    
417
                for (int i = frames.length - 1; i >= 0; i--) {
418
                        IFFrame fframe = frames[i]; //(IFFrame) layout.getFFrames().get(i);
419
                        fframe.setSelected(true);
420
                }
421

    
422
                layout.setStatus(Layout.DESACTUALIZADO);
423
                layout.repaint();
424
        }
425

    
426
        /**
427
         * Abre el di?logo para a?adir un borde a los fframes.
428
         */
429
        public void border() {
430
                borderdialog = new FBorderDialog(layout);
431
                PluginServices.getMDIManager().addView(borderdialog);
432
        }
433

    
434
        /**
435
         * Abre el di?logo de cambio de posici?n y tama?o del FFrame.
436
         */
437
        public void position() {
438
                IFFrame[] fframes = layout.getFFrameSelected();
439

    
440
                if (fframes.length != 0) {
441
                        for (int i = 0; i < fframes.length; i++) {
442
                                positiondialog = new FPositionDialog(layout, fframes[i]);
443
                                PluginServices.getMDIManager().addView(positiondialog);
444
                        }
445
                }
446
        }
447

    
448
        /**
449
         * Comienza la edici?n compleja para que todas las operaciones de edici?n
450
         * desde esta instrucci?n hasta el endComplex() se pueda deshacer y
451
         * rehacer todo junto.
452
         */
453
        private void startComplex() {
454
                ((LayoutEditableFeature) layout.getCadToolAdapter()
455
                                                                           .getEditableFeatureSource()).startComplexGeometry();
456
        }
457

    
458
        /**
459
         * Finaliza la edici?n compleja para almacenar todos los comandos
460
         */
461
        private void endComplex() {
462
                try {
463
                        ((LayoutEditableFeature) layout.getCadToolAdapter()
464
                                                                                   .getEditableFeatureSource()).endComplexGeometry();
465
                } catch (IOException e) {
466
                        e.printStackTrace();
467
                } catch (DriverIOException e) {
468
                        e.printStackTrace();
469
                }
470
        }
471

    
472
        /**
473
         * Modifica el FFrame a partir del ?ndice y el fframe que se pasa como
474
         * par?metro.
475
         *
476
         * @param frame FFrame a insertar.
477
         * @param index ?ndice del fframe a modificar.
478
         */
479
        private void modifyFFrame(IFFrame frame, int index) {
480
                try {
481
                        ((LayoutEditableFeature) layout.getCadToolAdapter()
482
                                                                                   .getEditableFeatureSource()).modifyFFrame(frame,
483
                                index);
484
                } catch (IOException e) {
485
                        e.printStackTrace();
486
                } catch (DriverIOException e) {
487
                        e.printStackTrace();
488
                }
489
        }
490

    
491
        /**
492
         * M?todo que devuelve el LayoutEditableFeature sobre el que editamos.
493
         *
494
         * @return LayoutEditableFeature.
495
         */
496
        private LayoutEditableFeature getEFS() {
497
                return (LayoutEditableFeature) layout.getCadToolAdapter()
498
                                                                                         .getEditableFeatureSource();
499
        }
500
}