Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / FLayoutGraphics.java @ 250

History | View | Annotate | Download (12.8 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.app.project.documents.layout;
23

    
24
import java.awt.geom.Rectangle2D;
25
import java.util.ArrayList;
26

    
27
import org.slf4j.Logger;
28
import org.slf4j.LoggerFactory;
29
import org.gvsig.andami.PluginServices;
30
import org.gvsig.app.project.ProjectManager;
31
import org.gvsig.app.project.documents.layout.fframes.FFrameGroup;
32
import org.gvsig.app.project.documents.layout.fframes.FFrameLegend;
33
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
34
import org.gvsig.app.project.documents.layout.fframes.IFFrameUseFMap;
35
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameDialogNotification;
36
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameViewDialog;
37
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.IFFrameDialog;
38
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
39
import org.gvsig.app.project.documents.layout.gui.dialogs.FAlignDialog;
40
import org.gvsig.app.project.documents.layout.gui.dialogs.FBorderDialog;
41
import org.gvsig.app.project.documents.layout.gui.dialogs.FPositionDialog;
42
import org.gvsig.tools.observer.Observable;
43
import org.gvsig.tools.observer.Observer;
44

    
45
/**
46
 * Operaciones realizadas sobre el conjunto de FFrames.
47
 * 
48
 * @author Vicente Caballero Navarro
49
 */
50
public class FLayoutGraphics {
51

    
52
    protected static final Logger LOG = LoggerFactory
53
        .getLogger(FLayoutGraphics.class);
54
    private LayoutPanel layout;
55
    private FAlignDialog m_alignLayout = null;
56
    private FBorderDialog borderdialog = null;
57
    private FPositionDialog positiondialog = null;
58
    private LayoutManager layoutManager = null;
59

    
60
    /**
61
     * Crea un nuevo FLayoutGraphics.
62
     * 
63
     * @param l
64
     *            Referencia al Layout.
65
     */
66
    public FLayoutGraphics(LayoutPanel layoutPanel) {
67
        layout = layoutPanel;
68
        layoutManager =
69
            (LayoutManager) ProjectManager.getInstance().getDocumentManager(
70
                DefaultLayoutManager.TYPENAME);
71
    }
72

    
73
    /**
74
     * Transforma un FFrameLegend a FFrames de tipo FFrameSymbol y FFrameText.
75
     */
76
    public void simplify() {
77
        layout.getLayoutContext().getFrameCommandsRecord()
78
            .startComplex(PluginServices.getText(this, "simplify"));
79
        IFFrame[] fframes = layout.getLayoutContext().getFFrames();
80
        for (int i = fframes.length - 1; i >= 0; i--) {
81
            IFFrame fframe = fframes[i];
82

    
83
            if (fframe instanceof FFrameLegend) {
84
                if (fframe.getSelected() != IFFrame.NOSELECT) {
85
                    ((FFrameLegend) fframe)
86
                        .toFFrames(layout.getLayoutContext());
87
                }
88
            }
89
        }
90
        layout.getLayoutContext().getFrameCommandsRecord().endComplex();
91
        layout.getLayoutControl().refresh();
92
    }
93

    
94
    /**
95
     * Agrupar en un FFrameGroup todos los FFrames seleccionados.
96
     */
97
    public void grouping() {
98
        // Se debe controlar de alguna forma si hay varios seleccionados.
99
        FFrameGroup fframegroup =
100
            (FFrameGroup) layoutManager
101
                .createFrame(FFrameGroup.PERSISTENCE_DEFINITION_NAME);
102

    
103
        // fframegroup.setLayout(layout);
104
        IFFrame[] fframes = layout.getLayoutContext().getFFrames();
105
        if (fframes.length > 1) {
106
            ArrayList selecList = new ArrayList();
107

    
108
            for (int i = fframes.length - 1; i >= 0; i--) {
109
                IFFrame fframe = fframes[i];
110

    
111
                if (fframe.getSelected() != IFFrame.NOSELECT) {
112
                    selecList.add(fframe);
113
                    layout.getLayoutContext().delFFrame(fframe);
114
                }
115
            }
116

    
117
            for (int i = selecList.size() - 1; i >= 0; i--) {
118
                fframegroup.addFFrame((IFFrame) selecList.get(i));
119
            }
120

    
121
            fframegroup.setAt(layout.getLayoutControl().getAT());
122

    
123
            Rectangle2D.Double rd =
124
                fframegroup.getRectangle(layout.getLayoutControl().getAT());
125

    
126
            Rectangle2D.Double rd1 =
127
                FLayoutUtilities.toSheetRect(rd, layout.getLayoutControl()
128
                    .getAT());
129

    
130
            fframegroup.setBoundBox(rd1);
131
            fframegroup.setSelected(true);
132
            layout.getLayoutContext().addFFrame(fframegroup, true, true);
133
            layout.getLayoutControl().refresh();
134
        }
135
    }
136

    
137
    /**
138
     * Desagrupar los FFrames que estan contenidos dentro del FFrameGroup en
139
     * FFrames individuales.
140
     */
141
    public void ungrouping() {
142
        layout.getLayoutContext().getFrameCommandsRecord()
143
            .startComplex(PluginServices.getText(this, "ungroup"));
144
        IFFrame[] fframes = layout.getLayoutContext().getFFrames();
145
        for (int i = fframes.length - 1; i >= 0; i--) {
146
            IFFrame fframe = fframes[i];
147

    
148
            if (fframe.getSelected() != IFFrame.NOSELECT) {
149
                if (fframe instanceof FFrameGroup) {
150
                    FFrameGroup fframegroup = (FFrameGroup) fframe;
151
                    ArrayList selecList = new ArrayList();
152

    
153
                    for (int j = fframegroup.getFFrames().length - 1; j >= 0; j--) {
154
                        selecList.add(fframegroup.getFFrames()[j]);
155
                    }
156

    
157
                    for (int j = selecList.size() - 1; j >= 0; j--) {
158
                        IFFrame frame = (IFFrame) selecList.get(j);
159
                        frame.setRotation(frame.getRotation()
160
                            + fframe.getRotation());
161
                        layout.getLayoutContext()
162
                            .addFFrameSameProperties(frame);
163
                    }
164
                    layout.getLayoutContext().delFFrame(fframegroup);
165
                }
166
            }
167
        }
168
        layout.getLayoutContext().getFrameCommandsRecord().endComplex();
169
        layout.getLayoutControl().refresh();
170
    }
171

    
172
    /**
173
     * Abre el di?logo para alinear los FFrames.
174
     */
175
    public void aligning() {
176
        m_alignLayout = new FAlignDialog(layout);
177
        PluginServices.getMDIManager().addWindow(m_alignLayout);
178
    }
179

    
180
    /**
181
     * Posiciona los FFrames seleccionados delante de los no seleccionados.
182
     */
183
    public void before() {
184
        layout.getLayoutContext().getFrameCommandsRecord()
185
            .startComplex(PluginServices.getText(this, "change_before"));
186
        IFFrame[] fframes = layout.getLayoutContext().getFFrames();
187
        for (int i = fframes.length - 1; i >= 0; i--) {
188
            IFFrame fframe = fframes[i];
189
            if (fframe.getSelected() != IFFrame.NOSELECT) {
190
                if (fframe instanceof FFrameGroup) {
191
                    ((FFrameGroup) fframe).setAt(layout.getLayoutControl()
192
                        .getAT());
193
                }
194

    
195
                IFFrame fframeAux;
196
                try {
197
                    fframeAux = (IFFrame) fframe.clone();
198
                    fframeAux
199
                        .setLevel(layout.getLayoutContext().getNumBefore());
200
                    layout.getLayoutContext().getFrameCommandsRecord()
201
                        .update(fframe, fframeAux);
202
                    fframeAux.getBoundingBox(layout.getLayoutControl().getAT());
203
                } catch (CloneNotSupportedException e) {
204
                    LOG.error("It is not possible clonate the object", e);
205
                }
206
            }
207
        }
208
        layout.getLayoutContext().getFrameCommandsRecord().endComplex();
209
        layout.getLayoutContext().updateFFrames();
210
        layout.getLayoutControl().refresh();
211
    }
212

    
213
    /**
214
     * Posiciona los FFrames seleccionados detr?s de los FFrames no
215
     * seleccionados.
216
     */
217
    public void behind() {
218
        layout.getLayoutContext().getFrameCommandsRecord()
219
            .startComplex(PluginServices.getText(this, "change_behind"));
220
        IFFrame[] fframes = layout.getLayoutContext().getFFrames();
221
        for (int i = fframes.length - 1; i >= 0; i--) {
222
            IFFrame fframe = fframes[i];
223
            if (fframe.getSelected() != IFFrame.NOSELECT) {
224
                if (fframe instanceof FFrameGroup) {
225
                    ((FFrameGroup) fframe).setAt(layout.getLayoutControl()
226
                        .getAT());
227
                }
228

    
229
                IFFrame fframeAux;
230
                try {
231
                    fframeAux = (IFFrame) fframe.clone();
232
                    fframeAux
233
                        .setLevel(layout.getLayoutContext().getNumBehind());
234
                    layout.getLayoutContext().getFrameCommandsRecord()
235
                        .update(fframe, fframeAux);
236
                    fframeAux.getBoundingBox(layout.getLayoutControl().getAT());
237
                } catch (CloneNotSupportedException e) {
238
                    LOG.error("It is not possible clonate the object", e);
239
                }
240
            }
241
        }
242
        layout.getLayoutContext().getFrameCommandsRecord().endComplex();
243
        layout.getLayoutContext().updateFFrames();
244
        layout.getLayoutControl().refresh();
245
    }
246

    
247
    /**
248
     * Abre el di?logo adecuadao a las propiedades del FFrame seleccionado,
249
     * ahora mismo solo se abre cuando hay un solo FFrame seleccionado.
250
     */
251
    public void openFrameDialog(final Observer observer) {
252
        IFFrame[] selecList = layout.getLayoutContext().getSelectedFFrames();
253

    
254
        if (selecList.length == 1) {
255
            final IFFrame frame = selecList[0];
256
            final IFFrameDialog fframeDialog = layout.createFFrameDialog(frame);
257
            if (fframeDialog != null) {
258
                    Observer onPanelClosedObserver = new Observer() {
259
                                        public void update(Observable observable,
260
                                                        Object notification) {
261
                                                if (notification instanceof FFrameDialogNotification &&
262
                                                        ((FFrameDialogNotification)notification).getType()==FFrameDialogNotification.DIALOG_CLOSED) {
263
                                                        onDialogClosed(fframeDialog, frame, observer);
264
                                                }
265
                                        }
266
                    };
267
                    fframeDialog.addObserver(onPanelClosedObserver);
268
                    if (observer!=null) {
269
                            fframeDialog.addObserver(observer);
270
                    }
271
                PluginServices.getMDIManager().addWindow(fframeDialog);
272
            }
273
        }
274
    }
275
    
276
    public void openFrameDialog() {
277
            openFrameDialog(null);
278
    }
279
    
280
    /**
281
     * @Deprecated Use {@link #openFrameDialog()}
282
     *                          and {@link #openFrameDialog(Observer)}
283
     * @return
284
     */
285
    @Deprecated
286
    public boolean openFFrameDialog() {
287
            openFrameDialog(null);
288
            return true;
289
    }
290
    
291
    protected void onDialogClosed(IFFrameDialog dialog, IFFrame oldFrame, Observer observer) {
292
            IFFrame fframeAux = dialog.getFFrame();
293
            if (fframeAux != null) {
294
                    // if user has pressed cancel, then fframeAux is null 
295
                    if (fframeAux instanceof IFFrameUseFMap)
296
                            ((IFFrameUseFMap) fframeAux).refresh();
297
                    layout.getLayoutContext().getFrameCommandsRecord()
298
                    .update(oldFrame, fframeAux);
299
                    fframeAux.getBoundingBox(layout.getLayoutControl().getAT());
300
                    layout.getLayoutContext().updateFFrames();
301
                    layout.getLayoutControl().setIsReSel(true);
302
                    layout.getLayoutControl().refresh();
303
                    if (observer!=null) {
304
                            observer.update(dialog, new FFrameDialogNotification(FFrameDialogNotification.FRAME_CREATED));
305
                    }
306
            }
307
    }
308

    
309

    
310
    /**
311
     * Selecci?n de todos los FFrames del Layout.
312
     */
313
    public void selecAll() {
314
        IFFrame[] fframes = layout.getLayoutContext().getFFrames();
315
        for (int i = fframes.length - 1; i >= 0; i--) {
316
            IFFrame fframe = fframes[i];
317
            fframe.setSelected(true);
318
        }
319
        layout.getLayoutControl().refresh();
320
    }
321

    
322
    /**
323
     * Abre el di?logo para a?adir un borde a los fframes.
324
     */
325
    public boolean border() {
326
        borderdialog = new FBorderDialog(layout);
327
        PluginServices.getMDIManager().addWindow(borderdialog);
328
        return borderdialog.isAccepted();
329
    }
330

    
331
    /**
332
     * Abre el di?logo de cambio de posici?n y tama?o del FFrame.
333
     */
334
    public void position() {
335

    
336
        IFFrame[] fframes = layout.getLayoutContext().getSelectedFFrames();
337
        if (fframes.length != 0) {
338
            for (int i = 0; i < fframes.length; i++) {
339
                positiondialog = new FPositionDialog(layout, fframes[i]);
340
                PluginServices.getMDIManager().addWindow(positiondialog);
341
            }
342
        }
343
    }
344
}