Statistics
| Revision:

svn-gvsig-desktop / 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 @ 36648

History | View | Annotate | Download (19.4 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
        layoutControl = new DefaultLayoutControl(this);
122
        this.initComponents();
123
    }
124

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

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

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

    
153
        setCursor(Cursor.getDefaultCursor());
154

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

    
177
        // TODO Esto es para ver el rect?ngulo que representa el folio en la
178
        // impresi?n.
179
        // g2.drawRect(0, 0, (int) rect.getWidth(), (int) rect.getHeight());
180
    }
181

    
182
    /**
183
     * It initializes the components.
184
     */
185
    private void initComponents() {
186
        this.setLayout(new GridLayout(1, 1));
187
        add(layoutControl.getComponent());
188
        setDoubleBuffered(true);
189
    }
190

    
191
    /**
192
     * Open the dialog of Layout properties.
193
     * 
194
     * @param job
195
     *            PrinterJob
196
     */
197
    public void showPagePropertiesWindow(PrinterJob job) {
198
        PageFormat pf1;
199

    
200
        pf1 = layoutContext.getAttributes().getPageFormat();
201
        pf1 = job.pageDialog(pf1);
202
        layoutContext.getAttributes().setPageFormat(pf1);
203
        layoutControl.refresh();
204
    }
205

    
206
    public void obtainRect(boolean isPrint) {
207
        layoutContext.getAttributes().obtainRect(isPrint,
208
            layoutControl.getRect(), getWidth(), getHeight());
209
        // layoutContext.getAtributes().obtainRect(layoutControl.getRect());
210
    }
211

    
212
    public void showFConfig() {
213
        FConfigLayoutDialog m_configLayout = new FConfigLayoutDialog(this);
214
        PluginServices.getMDIManager().addWindow(m_configLayout);
215
    }
216

    
217
    public boolean showFProperties() {
218
        if (layoutDocument == null) {
219
            layoutDocument = new DefaultLayoutDocument();
220
            layoutDocument.setName(getName());
221
        }
222

    
223
        m_propertiesLayout = new MapProperties(layoutDocument);
224
        PluginServices.getMDIManager().addWindow(m_propertiesLayout);
225
        return m_propertiesLayout.isAccepted();
226
    }
227

    
228
    public void showPrintDialog(PrinterJob job) {
229
        if (job != null) {
230
            job.printDialog();
231

    
232
            try {
233
                job.setPrintable((Printable) PluginServices
234
                    .getExtension(org.gvsig.app.extension.Print.class));
235
                job.print();
236
            } catch (PrinterException e) {
237
                e.printStackTrace();
238
            }
239
        } else {
240
            // Actualizar attributes
241
            att = layoutContext.getAttributes().toPrintRequestAttributeSet();
242

    
243
            // ------------------ The Printing things --------------------- //
244
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
245

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

    
260
            PrintService defaultService = null;
261

    
262
            if (m_cachePrintService == null) {
263
                defaultService = PrintServiceLookup.lookupDefaultPrintService();
264
            }
265

    
266
            if ((defaultService == null) && (m_cachePrintService == null)) {
267
                JOptionPane.showMessageDialog((Component) PluginServices
268
                    .getMainFrame(), PluginServices.getText(this,
269
                    "ninguna_impresora_configurada"));
270

    
271
                return;
272
            }
273

    
274
            if (m_cachePrintService == null) {
275
                m_cachePrintService =
276
                    ServiceUI.printDialog(null, 200, 200, m_cachePrintServices,
277
                        defaultService, flavor, att);
278

    
279
                // m_cachePrintRequestAtributeSet = new
280
                // HashPrintRequestAttributeSet();
281
                // m_cachePrintRequestAtributeSet.addAll(pras);
282
            } else {
283
                m_cachePrintService =
284
                    ServiceUI.printDialog(null, 200, 200, m_cachePrintServices,
285
                        m_cachePrintService, flavor, att);
286
            }
287

    
288
            if (m_cachePrintService != null) {
289
                DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
290
                PrintJobListener pjlistener = new PrintJobAdapter() {
291

    
292
                    public void printDataTransferCompleted(PrintJobEvent e) {
293
                        // System.out.println("Fin de impresi?n");
294
                        layoutControl.fullRect();
295
                    }
296
                };
297

    
298
                jobNuevo.addPrintJobListener(pjlistener);
299

    
300
                // DocAttributeSet das = new HashDocAttributeSet();
301
                doc =
302
                    new SimpleDoc(
303
                        PluginServices
304
                            .getExtension(org.gvsig.app.extension.Print.class),
305
                        flavor, null);
306

    
307
                try {
308
                    jobNuevo.print(doc, att);
309

    
310
                    // m_attributes.
311
                } catch (PrintException pe) {
312
                    NotificationManager.addError(pe);
313
                }
314
            }
315
        }
316

    
317
        /*
318
         * try { print = job.printDialog(att); } catch (Exception e) {
319
         * logger.error("Abriendo el Di?logo de imprimir");
320
         * //System.out.println("Excepci?n "+e); } if (print) {
321
         * job.setPrintable((Printable)
322
         * App.instance.getPc().getExtension(com.iver.cit.gvsig.Print.class));
323
         * try { job.print(att); } catch (Exception ex) { ex.printStackTrace();
324
         * } }
325
         */
326
    }
327

    
328
    public IFFrameDialog createFFrameDialog(IFFrame fframe) {
329
        return layoutManager.createFFrameDialog(fframe, this);
330
    }
331

    
332
    /**
333
     * This method is used to get <strong>an initial</strong> ViewInfo object
334
     * for this Map. It is not intended to retrieve the ViewInfo object in a
335
     * later time. <strong>Use PluginServices.getMDIManager().getViewInfo(view)
336
     * to retrieve the ViewInfo object at any time after the creation of the
337
     * object.
338
     * 
339
     * @see com.iver.mdiApp.ui.MDIManager.IWindow#getWindowInfo()
340
     */
341
    public WindowInfo getWindowInfo() {
342
        if (m_viewInfo == null) {
343
            m_viewInfo =
344
                new WindowInfo(WindowInfo.ICONIFIABLE | WindowInfo.RESIZABLE
345
                    | WindowInfo.MAXIMIZABLE);
346
            m_viewInfo.setWidth(DEFAULT_WIDTH);
347
            m_viewInfo.setHeight(DEFAULT_HEIGHT);
348

    
349
            m_viewInfo.setTitle(PluginServices.getText(this, "Mapa") + " : "
350
                + layoutDocument.getName());
351
        }
352
        return m_viewInfo;
353
    }
354

    
355
    public LayoutContext getLayoutContext() {
356
        return layoutContext;
357
    }
358

    
359
    public LayoutControl getLayoutControl() {
360
        return layoutControl;
361
    }
362

    
363
    /**
364
     * @see com.iver.mdiApp.ui.MDIManager.IWindow#windowActivated()
365
     */
366
    public void windowActivated() {
367
        // fullRect();
368
        layoutControl.refresh();
369
        PluginServices
370
            .getMainFrame()
371
            .getStatusBar()
372
            .setMessage(
373
                "units",
374
                PluginServices.getText(this, layoutContext.getAttributes()
375
                    .getNameUnit()));
376
        // ensure requestFocus is enabled
377
        if (!layoutControl.getComponent().isRequestFocusEnabled()) {
378
            layoutControl.getComponent().setRequestFocusEnabled(true);
379
        }
380
        requestFocus();
381
        layoutControl.getComponent().requestFocus();
382
    }
383

    
384
    /**
385
     * @see com.iver.mdiApp.ui.MDIManager.SingletonWindow#getWindowModel()
386
     */
387
    public Object getWindowModel() {
388
        return layoutDocument;
389
    }
390

    
391
    public Document getDocument() {
392
        return layoutDocument;
393
    }
394

    
395
    /**
396
     * @see com.iver.cit.gvsig.fmap.ExtentListener#extentChanged(org.gvsig.fmap.mapcontext.events.ExtentEvent)
397
     */
398
    public void extentChanged(ExtentEvent e) {
399
        // do nothing
400
    }
401

    
402
    /**
403
     * @see org.gvsig.andami.ui.mdiManager.IWindowListener#windowClosed()
404
     */
405
    public void windowClosed() {
406
        // /PluginServices.getMainFrame().getStatusBar().setMessage("1","");
407
    }
408

    
409
    /**
410
     * @see org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener#backColorChanged(org.gvsig.fmap.mapcontext.events.ColorEvent)
411
     */
412
    public void backColorChanged(ColorEvent e) {
413
        // refresh();
414
    }
415

    
416
    public void layoutToPDF(String suggestedName) {
417
        FileFilter pdfFilter =
418
            new GenericFileFilter("pdf", PluginServices.getText(this, "pdf"));
419

    
420
        JFileChooser jfc =
421
            new JFileChooser(PDF_AND_PS_FILECHOOSER, defaultPDFFolderPath);
422
        if (suggestedName != null) {
423
            jfc.setSelectedFile(new File(suggestedName));
424
        }
425
        jfc.addChoosableFileFilter(pdfFilter);
426
        jfc.setFileFilter(pdfFilter);
427

    
428
        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
429
            File f = jfc.getSelectedFile();
430
            File faux = null;
431

    
432
            if (f.getName().endsWith(".pdf") || f.getName().endsWith(".PDF")) {
433
                faux = f;
434
            } else {
435
                faux = new File(f.getPath() + ".pdf");
436
            }
437

    
438
            layoutControl.getLayoutDraw().toPDF(faux);
439
        }
440
    }
441

    
442
    public void layoutToPS(String suggestedName) {
443
        FileFilter pdfFilter =
444
            new GenericFileFilter("ps", PluginServices.getText(this, "ps"));
445

    
446
        JFileChooser jfc =
447
            new JFileChooser(PDF_AND_PS_FILECHOOSER, defaultPDFFolderPath);
448
        if (suggestedName != null) {
449
            jfc.setSelectedFile(new File(suggestedName));
450
        }
451
        jfc.addChoosableFileFilter(pdfFilter);
452
        jfc.setFileFilter(pdfFilter);
453

    
454
        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
455
            File f = jfc.getSelectedFile();
456
            File faux = null;
457

    
458
            if (f.getName().endsWith(".ps") || f.getName().endsWith(".PS")) {
459
                faux = f;
460
            } else {
461
                faux = new File(f.getPath() + ".ps");
462
            }
463

    
464
            layoutControl.getLayoutDraw().toPS(faux);
465
        }
466
    }
467

    
468
    public void layoutToPDF() {
469
        layoutToPDF(null);
470
    }
471

    
472
    public void layoutToPS() {
473
        layoutToPS(null);
474
    }
475

    
476
    public boolean isShowIconTag() {
477
        return bShowIconTag;
478
    }
479

    
480
    public void setShowIconTag(boolean modeDebug) {
481
        bShowIconTag = modeDebug;
482
    }
483

    
484
    /**
485
     * Event to change the projection.
486
     */
487
    public void projectionChanged(ProjectionEvent e) {
488
        // TODO Auto-generated method stub
489

    
490
    }
491

    
492
    public void update(Observable observable, Object notification) {
493
        this.layoutContext.updateFFrames();
494
        this.layoutControl.refresh();
495
    }
496

    
497
    public Object getWindowProfile() {
498
        return WindowInfo.EDITOR_PROFILE;
499
    }
500

    
501
    public void saveToState(PersistentState state) throws PersistenceException {
502
        state.set(LAYOUTCONTEXT_OBJECT, layoutContext);
503
        state.set(LAYOUTDOCUMENT_OBJECT, layoutDocument);
504
    }
505

    
506
    public void loadFromState(PersistentState state)
507
        throws PersistenceException {
508
        this.layoutContext = (LayoutContext) state.get(LAYOUTCONTEXT_OBJECT);
509
        this.layoutDocument = (LayoutDocument) state.get(LAYOUTDOCUMENT_OBJECT);
510
        this.layoutControl.setLayoutContext(layoutContext);
511
    }
512

    
513
    public static void registerPersistent() {
514
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
515
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
516
            DynStruct definition =
517
                manager.addDefinition(DefaultLayoutPanel.class,
518
                    PERSISTENCE_DEFINITION_NAME,
519
                    "Layout panel persistence definition", null, null);
520

    
521
            definition.addDynFieldObject(LAYOUTCONTEXT_OBJECT)
522
                .setClassOfValue(LayoutContext.class).setMandatory(true);
523
            definition.addDynFieldObject(LAYOUTDOCUMENT_OBJECT)
524
                .setClassOfValue(LayoutDocument.class).setMandatory(true);
525
        }
526
    }
527

    
528
    public WindowLayout getWindowLayout() {
529
        // TODO Auto-generated method stub
530
        return null;
531
    }
532

    
533
    public void setWindowLayout(WindowLayout layout) {
534
        // TODO Auto-generated method stub
535

    
536
    }
537

    
538
    private void setLayoutContext(LayoutContext theLayoutcontext) {
539
        this.layoutContext = theLayoutcontext;
540
        this.layoutControl.setLayoutContext(theLayoutcontext);
541
        layoutContext.getAttributes().setDistanceUnitX(layoutControl.getRect());
542
        layoutContext.getAttributes().setDistanceUnitY(layoutControl.getRect());
543

    
544
        layoutControl.setTool("layoutzoomin");
545
    }
546

    
547
    @Override
548
    public void setLayoutManager(LayoutManager layoutManager) {
549
        this.layoutManager = layoutManager;
550
    }
551

    
552
}