Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.app.document.layout.app / org.gvsig.app.document.layout.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / gui / DefaultLayoutPanel.java @ 37196

History | View | Annotate | Download (18.3 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.gui;
23

    
24
import java.awt.Component;
25
import java.awt.Cursor;
26
import java.awt.Graphics2D;
27
import java.awt.GridLayout;
28
import java.awt.geom.AffineTransform;
29
import java.awt.print.PageFormat;
30
import java.awt.print.Printable;
31
import java.awt.print.PrinterException;
32
import java.awt.print.PrinterJob;
33
import java.beans.PropertyChangeEvent;
34
import java.beans.PropertyChangeListener;
35
import java.io.File;
36

    
37
import javax.print.Doc;
38
import javax.print.DocFlavor;
39
import javax.print.DocPrintJob;
40
import javax.print.PrintException;
41
import javax.print.PrintService;
42
import javax.print.PrintServiceLookup;
43
import javax.print.ServiceUI;
44
import javax.print.SimpleDoc;
45
import javax.print.attribute.PrintRequestAttributeSet;
46
import javax.print.event.PrintJobAdapter;
47
import javax.print.event.PrintJobEvent;
48
import javax.print.event.PrintJobListener;
49
import javax.swing.JOptionPane;
50
import javax.swing.filechooser.FileFilter;
51

    
52
import org.gvsig.andami.PluginServices;
53
import org.gvsig.andami.messages.NotificationManager;
54
import org.gvsig.andami.ui.mdiManager.WindowInfo;
55
import org.gvsig.app.project.documents.Document;
56
import org.gvsig.app.project.documents.gui.WindowLayout;
57
import org.gvsig.app.project.documents.layout.DefaultLayoutControl;
58
import org.gvsig.app.project.documents.layout.DefaultLayoutDocument;
59
import org.gvsig.app.project.documents.layout.LayoutContext;
60
import org.gvsig.app.project.documents.layout.LayoutControl;
61
import org.gvsig.app.project.documents.layout.LayoutDocument;
62
import org.gvsig.app.project.documents.layout.LayoutManager;
63
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
64
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.IFFrameDialog;
65
import org.gvsig.app.project.documents.layout.gui.dialogs.FConfigLayoutDialog;
66
import org.gvsig.fmap.mapcontext.events.ColorEvent;
67
import org.gvsig.fmap.mapcontext.events.ExtentEvent;
68
import org.gvsig.fmap.mapcontext.events.ProjectionEvent;
69
import org.gvsig.gui.beans.swing.JFileChooser;
70
import org.gvsig.tools.ToolsLocator;
71
import org.gvsig.tools.dynobject.DynStruct;
72
import org.gvsig.tools.observer.Observable;
73
import org.gvsig.tools.persistence.PersistenceManager;
74
import org.gvsig.tools.persistence.PersistentState;
75
import org.gvsig.tools.persistence.exception.PersistenceException;
76
import org.gvsig.utils.GenericFileFilter;
77

    
78
/**
79
 * Graphic representation of the elements to print.
80
 * 
81
 * @author Vicente Caballero Navarro
82
 */
83
public class DefaultLayoutPanel extends LayoutPanel {
84

    
85
    private static final long serialVersionUID = 916830228032567624L;
86

    
87
    private static final int DEFAULT_HEIGHT = 450;
88

    
89
    private static final int DEFAULT_WIDTH = 700;
90

    
91
    public static final String PERSISTENCE_DEFINITION_NAME = "LayoutPanel";
92
    private static final String LAYOUTCONTEXT_OBJECT = "LayoutContext";
93
    private static final String LAYOUTDOCUMENT_OBJECT = "LayoutDocument";
94

    
95
    public static final String PDF_AND_PS_FILECHOOSER =
96
        "PDF_AND_PS_FILECHOOSER";
97

    
98
    public static File defaultPDFFolderPath;
99

    
100
    private IFFrameDialog fframedialog = null;
101
    private MapProperties m_propertiesLayout = null;
102
    private PrintService[] m_cachePrintServices = null;
103
    private PrintService m_cachePrintService = null;
104
    private LayoutDocument layoutDocument = null;
105
    private Doc doc = null;
106
    private PrintRequestAttributeSet att = null;
107
    private WindowInfo m_viewInfo = null;
108
    /**
109
     * We use it when we are doing a layout and assigning tags.
110
     * It is put in debug when we do a VIEW_TAGS
111
     */
112
    private boolean bShowIconTag = false;
113
    private LayoutControl layoutControl = null;
114
    private LayoutContext layoutContext = null;
115
    private LayoutManager layoutManager = null;
116

    
117
    /**
118
     * Creates a new Layout object.
119
     */
120
    public DefaultLayoutPanel() {
121
        DefaultLayoutControl defaultLayoutControl = new DefaultLayoutControl();
122
        this.layoutControl = defaultLayoutControl;
123
        defaultLayoutControl.initialize(this);       
124
        this.initComponents();
125
    }
126

    
127
    public void setDocument(Document document) {
128
        layoutDocument = (LayoutDocument) document;
129
        setLayoutContext(layoutDocument.getLayoutContext());
130
        this.setName(document.getName());
131
        layoutDocument.addPropertyChangeListener(new PropertyChangeListener() {
132

    
133
            public void propertyChange(PropertyChangeEvent evt) {
134
                if (evt.getPropertyName().equals("name")) {
135
                    PluginServices
136
                        .getMDIManager()
137
                        .getWindowInfo(DefaultLayoutPanel.this)
138
                        .setTitle(
139
                            PluginServices.getText(this, "Mapa") + " : "
140
                                + (String) evt.getNewValue());
141
                }
142
            }
143
        });
144
    }
145

    
146
    /**
147
     * Method to print the Layout without modify the Affinetransform.
148
     * 
149
     * @param g2
150
     *            Geaphics2D
151
     */
152
    public void drawLayoutPrint(Graphics2D g2) {
153
        layoutControl.setCancelDrawing(false);
154

    
155
        setCursor(Cursor.getDefaultCursor());
156

    
157
        double scale = 0;
158
        scale =
159
            layoutControl.getRect().height
160
                / layoutContext.getAttributes().m_sizePaper.getAlto() * 1;
161
        AffineTransform escalado = new AffineTransform();
162
        AffineTransform translacion = new AffineTransform();
163
        translacion.setToTranslation(layoutControl.getRect().getMinX(),
164
            layoutControl.getRect().getMinY());
165
        escalado.setToScale(scale, scale);
166
        layoutControl.getAT().setToIdentity();
167
        layoutControl.getAT().concatenate(translacion);
168
        layoutControl.getAT().concatenate(escalado);
169
        layoutContext.getAttributes().setDistanceUnitX(layoutControl.getRect());
170
        layoutContext.getAttributes().setDistanceUnitY(layoutControl.getRect());
171
        IFFrame[] fframes = layoutContext.getFFrames();
172
        for (int i = 0; i < fframes.length; i++) {
173
            fframes[i].print(g2, layoutControl.getAT(), null, layoutContext
174
                .getAttributes().toPrintAttributes());
175
        }
176
    }
177

    
178
    /**
179
     * It initializes the components.
180
     */
181
    private void initComponents() {
182
        this.setLayout(new GridLayout(1, 1));
183
        add(layoutControl.getComponent());
184
        setDoubleBuffered(true);
185
    }
186

    
187
    /**
188
     * Open the dialog of Layout properties.
189
     * 
190
     * @param job
191
     *            PrinterJob
192
     */
193
    public void showPagePropertiesWindow(PrinterJob job) {
194
        PageFormat pf1;
195

    
196
        pf1 = layoutContext.getAttributes().getPageFormat();
197
        pf1 = job.pageDialog(pf1);
198
        layoutContext.getAttributes().setPageFormat(pf1);
199
        layoutControl.refresh();
200
    }
201

    
202
    public void obtainRect(boolean isPrint) {
203
        layoutContext.getAttributes().obtainRect(isPrint,
204
            layoutControl.getRect(), getWidth(), getHeight());
205
    }
206
    
207
    public void showFConfig() {
208
        FConfigLayoutDialog m_configLayout = new FConfigLayoutDialog(this);
209
        PluginServices.getMDIManager().addWindow(m_configLayout);
210
    }
211

    
212
    public boolean showFProperties() {
213
        if (layoutDocument == null) {
214
            layoutDocument = new DefaultLayoutDocument();
215
            layoutDocument.setName(getName());
216
        }
217

    
218
        m_propertiesLayout = new MapProperties(layoutDocument);
219
        PluginServices.getMDIManager().addWindow(m_propertiesLayout);
220
        return m_propertiesLayout.isAccepted();
221
    }
222

    
223
    public void showPrintDialog(PrinterJob job) {
224
        if (job != null) {
225
            job.printDialog();
226

    
227
            try {
228
                job.setPrintable((Printable) PluginServices
229
                    .getExtension(org.gvsig.app.extension.Print.class));
230
                job.print();
231
            } catch (PrinterException e) {
232
                e.printStackTrace();
233
            }
234
        } else {
235
            // Actualizar attributes
236
            att = layoutContext.getAttributes().toPrintRequestAttributeSet();
237

    
238
            // ------------------ The Printing things --------------------- //
239
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
240

    
241
            // returns the set of printers that support printing a specific
242
            // document type (such as GIF)
243
            // with a specific set of attributes (such as two sided).
244
            // PrintRequestAttributeSet pras = new
245
            // HashPrintRequestAttributeSet();
246
            // interestingly, the printer dialog's default behavior has changed
247
            // with the new API: by default the dialog is not shown.
248
            // So we must use the ServiceUI class to create a print dialog
249
            // returns the default print service.
250
            if (m_cachePrintServices == null) {
251
                m_cachePrintServices =
252
                    PrintServiceLookup.lookupPrintServices(flavor, null);
253
            }
254

    
255
            PrintService defaultService = null;
256

    
257
            if (m_cachePrintService == null) {
258
                defaultService = PrintServiceLookup.lookupDefaultPrintService();
259
            }
260

    
261
            if ((defaultService == null) && (m_cachePrintService == null)) {
262
                JOptionPane.showMessageDialog((Component) PluginServices
263
                    .getMainFrame(), PluginServices.getText(this,
264
                    "ninguna_impresora_configurada"));
265

    
266
                return;
267
            }
268

    
269
            if (m_cachePrintService == null) {
270
                m_cachePrintService =
271
                    ServiceUI.printDialog(null, 200, 200, m_cachePrintServices,
272
                        defaultService, flavor, att);
273
            } else {
274
                m_cachePrintService =
275
                    ServiceUI.printDialog(null, 200, 200, m_cachePrintServices,
276
                        m_cachePrintService, flavor, att);
277
            }
278

    
279
            if (m_cachePrintService != null) {
280
                DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
281
                PrintJobListener pjlistener = new PrintJobAdapter() {
282

    
283
                    public void printDataTransferCompleted(PrintJobEvent e) {
284
                        // System.out.println("Fin de impresi?n");
285
                        layoutControl.fullRect();
286
                    }
287
                };
288

    
289
                jobNuevo.addPrintJobListener(pjlistener);
290

    
291
                doc =
292
                    new SimpleDoc(
293
                        PluginServices
294
                            .getExtension(org.gvsig.app.extension.Print.class),
295
                        flavor, null);
296

    
297
                try {
298
                    jobNuevo.print(doc, att);
299

    
300
                } catch (PrintException pe) {
301
                    NotificationManager.addError(pe);
302
                }
303
            }
304
        }
305
    }
306

    
307
    public IFFrameDialog createFFrameDialog(IFFrame fframe) {
308
        return layoutManager.createFFrameDialog(fframe, this);
309
    }
310

    
311
    /**
312
     * This method is used to get <strong>an initial</strong> ViewInfo object
313
     * for this Map. It is not intended to retrieve the ViewInfo object in a
314
     * later time. <strong>Use PluginServices.getMDIManager().getViewInfo(view)
315
     * to retrieve the ViewInfo object at any time after the creation of the
316
     * object.
317
     * 
318
     * @see com.iver.mdiApp.ui.MDIManager.IWindow#getWindowInfo()
319
     */
320
    public WindowInfo getWindowInfo() {
321
        if (m_viewInfo == null) {
322
            m_viewInfo =
323
                new WindowInfo(WindowInfo.ICONIFIABLE | WindowInfo.RESIZABLE
324
                    | WindowInfo.MAXIMIZABLE);
325
            m_viewInfo.setWidth(DEFAULT_WIDTH);
326
            m_viewInfo.setHeight(DEFAULT_HEIGHT);
327

    
328
            m_viewInfo.setTitle(PluginServices.getText(this, "Mapa") + " : "
329
                + layoutDocument.getName());
330
        }
331
        return m_viewInfo;
332
    }
333

    
334
    public LayoutContext getLayoutContext() {
335
        return layoutContext;
336
    }
337

    
338
    public LayoutControl getLayoutControl() {
339
        return layoutControl;
340
    }
341

    
342
    /**
343
     * @see com.iver.mdiApp.ui.MDIManager.IWindow#windowActivated()
344
     */
345
    public void windowActivated() {
346
        // fullRect();
347
        layoutControl.refresh();
348
        PluginServices
349
            .getMainFrame()
350
            .getStatusBar()
351
            .setMessage(
352
                "units",
353
                PluginServices.getText(this, layoutContext.getAttributes()
354
                    .getNameUnit()));
355
        // ensure requestFocus is enabled
356
        if (!layoutControl.getComponent().isRequestFocusEnabled()) {
357
            layoutControl.getComponent().setRequestFocusEnabled(true);
358
        }
359
        requestFocus();
360
        layoutControl.getComponent().requestFocus();
361
    }
362

    
363
    /**
364
     * @see com.iver.mdiApp.ui.MDIManager.SingletonWindow#getWindowModel()
365
     */
366
    public Object getWindowModel() {
367
        return layoutDocument;
368
    }
369

    
370
    public Document getDocument() {
371
        return layoutDocument;
372
    }
373

    
374
    /**
375
     * @see com.iver.cit.gvsig.fmap.ExtentListener#extentChanged(org.gvsig.fmap.mapcontext.events.ExtentEvent)
376
     */
377
    public void extentChanged(ExtentEvent e) {
378
        
379
    }
380

    
381
    /**
382
     * @see org.gvsig.andami.ui.mdiManager.IWindowListener#windowClosed()
383
     */
384
    public void windowClosed() {
385
        
386
    }
387

    
388
    /**
389
     * @see org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener#backColorChanged(org.gvsig.fmap.mapcontext.events.ColorEvent)
390
     */
391
    public void backColorChanged(ColorEvent e) {
392
        // refresh();
393
    }
394

    
395
    public void layoutToPDF(String suggestedName) {
396
        FileFilter pdfFilter =
397
            new GenericFileFilter("pdf", PluginServices.getText(this, "pdf"));
398

    
399
        JFileChooser jfc =
400
            new JFileChooser(PDF_AND_PS_FILECHOOSER, defaultPDFFolderPath);
401
        if (suggestedName != null) {
402
            jfc.setSelectedFile(new File(suggestedName));
403
        }
404
        jfc.addChoosableFileFilter(pdfFilter);
405
        jfc.setFileFilter(pdfFilter);
406

    
407
        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
408
            File f = jfc.getSelectedFile();
409
            File faux = null;
410

    
411
            if (f.getName().endsWith(".pdf") || f.getName().endsWith(".PDF")) {
412
                faux = f;
413
            } else {
414
                faux = new File(f.getPath() + ".pdf");
415
            }
416

    
417
            layoutControl.getLayoutDraw().toPDF(faux);
418
        }
419
    }
420

    
421
    public void layoutToPS(String suggestedName) {
422
        FileFilter pdfFilter =
423
            new GenericFileFilter("ps", PluginServices.getText(this, "ps"));
424

    
425
        JFileChooser jfc =
426
            new JFileChooser(PDF_AND_PS_FILECHOOSER, defaultPDFFolderPath);
427
        if (suggestedName != null) {
428
            jfc.setSelectedFile(new File(suggestedName));
429
        }
430
        jfc.addChoosableFileFilter(pdfFilter);
431
        jfc.setFileFilter(pdfFilter);
432

    
433
        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
434
            File f = jfc.getSelectedFile();
435
            File faux = null;
436

    
437
            if (f.getName().endsWith(".ps") || f.getName().endsWith(".PS")) {
438
                faux = f;
439
            } else {
440
                faux = new File(f.getPath() + ".ps");
441
            }
442

    
443
            layoutControl.getLayoutDraw().toPS(faux);
444
        }
445
    }
446

    
447
    public void layoutToPDF() {
448
        layoutToPDF(null);
449
    }
450

    
451
    public void layoutToPS() {
452
        layoutToPS(null);
453
    }
454

    
455
    public boolean isShowIconTag() {
456
        return bShowIconTag;
457
    }
458

    
459
    public void setShowIconTag(boolean modeDebug) {
460
        bShowIconTag = modeDebug;
461
    }
462

    
463
    /**
464
     * Event to change the projection.
465
     */
466
    public void projectionChanged(ProjectionEvent e) {
467
        // TODO Auto-generated method stub
468

    
469
    }
470

    
471
    public void update(Observable observable, Object notification) {
472
        this.layoutContext.updateFFrames();
473
        this.layoutControl.refresh();
474
    }
475

    
476
    public Object getWindowProfile() {
477
        return WindowInfo.EDITOR_PROFILE;
478
    }
479

    
480
    public void saveToState(PersistentState state) throws PersistenceException {
481
        state.set(LAYOUTCONTEXT_OBJECT, layoutContext);
482
        state.set(LAYOUTDOCUMENT_OBJECT, layoutDocument);
483
    }
484

    
485
    public void loadFromState(PersistentState state)
486
        throws PersistenceException {
487
        this.layoutContext = (LayoutContext) state.get(LAYOUTCONTEXT_OBJECT);
488
        this.layoutDocument = (LayoutDocument) state.get(LAYOUTDOCUMENT_OBJECT);
489
        this.layoutControl.setLayoutContext(layoutContext);
490
    }
491

    
492
    public static void registerPersistent() {
493
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
494
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
495
            DynStruct definition =
496
                manager.addDefinition(DefaultLayoutPanel.class,
497
                    PERSISTENCE_DEFINITION_NAME,
498
                    "Layout panel persistence definition", null, null);
499

    
500
            definition.addDynFieldObject(LAYOUTCONTEXT_OBJECT)
501
                .setClassOfValue(LayoutContext.class).setMandatory(true);
502
            definition.addDynFieldObject(LAYOUTDOCUMENT_OBJECT)
503
                .setClassOfValue(LayoutDocument.class).setMandatory(true);
504
        }
505
    }
506

    
507
    public WindowLayout getWindowLayout() {
508
        return null;
509
    }
510

    
511
    public void setWindowLayout(WindowLayout layout) {
512

    
513
    }
514

    
515
    private void setLayoutContext(LayoutContext theLayoutcontext) {
516
        this.layoutContext = theLayoutcontext;
517
        this.layoutControl.setLayoutContext(theLayoutcontext);
518
        layoutContext.getAttributes().setDistanceUnitX(layoutControl.getRect());
519
        layoutContext.getAttributes().setDistanceUnitY(layoutControl.getRect());
520

    
521
        layoutControl.setTool("layoutzoomin");
522
    }
523

    
524
    @Override
525
    public void setLayoutManager(LayoutManager layoutManager) {
526
        this.layoutManager = layoutManager;
527
    }
528

    
529
}