Statistics
| Revision:

svn-document-layout / trunk / 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 @ 30

History | View | Annotate | Download (19.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.Graphics2D;
26
import java.awt.GridLayout;
27
import java.awt.geom.AffineTransform;
28
import java.awt.print.PageFormat;
29
import java.awt.print.Printable;
30
import java.awt.print.PrinterException;
31
import java.awt.print.PrinterJob;
32
import java.beans.PropertyChangeEvent;
33
import java.beans.PropertyChangeListener;
34
import java.io.File;
35

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

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

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

    
84
    private static final long serialVersionUID = 916830228032567624L;
85

    
86
    private static final int DEFAULT_HEIGHT = 450;
87

    
88
    private static final int DEFAULT_WIDTH = 700;
89

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

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

    
97
    public static File defaultPDFFolderPath;
98

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

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

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

    
132
            public void propertyChange(PropertyChangeEvent evt) {
133
                if (evt.getPropertyName().equals("name")) {
134
                    PluginServices
135
                        .getMDIManager()
136
                        .getWindowInfo(DefaultLayoutPanel.this)
137
                        .setTitle(
138
                            PluginServices.getText(this, "Mapa") + " : "
139
                                + (String) evt.getNewValue());
140
                }
141
            }
142
        });
143
    }
144
  
145
    /**
146
     * It initializes the components.
147
     */
148
    private void initComponents() {
149
        this.setLayout(new GridLayout(1, 1));
150
        add(layoutControl.getComponent());
151
        setDoubleBuffered(true);
152
    }
153

    
154
    /**
155
     * Open the dialog of Layout properties.
156
     * 
157
     * @param job
158
     *            PrinterJob
159
     */
160
    public void showPagePropertiesWindow(PrinterJob job) {
161
        PageFormat pf1;
162

    
163
        pf1 = layoutContext.getAttributes().getPageFormat();
164
        pf1 = job.pageDialog(pf1);
165
        layoutContext.getAttributes().setPageFormat(pf1);
166
        layoutControl.refresh();
167
    }
168

    
169
    public void obtainRect(boolean isPrint) {
170
        layoutContext.getAttributes().obtainRect(isPrint,
171
            layoutControl.getRect(), getWidth(), getHeight());
172
    }
173
    
174
    public void showFConfig() {
175
        FConfigLayoutDialog m_configLayout = new FConfigLayoutDialog(this);
176
        PluginServices.getMDIManager().addWindow(m_configLayout);
177
    }
178

    
179
    public boolean showFProperties() {
180
        if (layoutDocument == null) {
181
            layoutDocument = new DefaultLayoutDocument();
182
            layoutDocument.setName(getName());
183
        }
184

    
185
        m_propertiesLayout = new MapProperties(layoutDocument);
186
        PluginServices.getMDIManager().addWindow(m_propertiesLayout);
187
        return m_propertiesLayout.isAccepted();
188
    }
189

    
190
    public void showPrintDialog(PrinterJob job) {
191
        if (job != null) {
192
            job.printDialog();
193

    
194
            try {
195
                job.setPrintable((Printable) PluginServices
196
                    .getExtension(org.gvsig.app.extension.Print.class));
197
                job.print();
198
            } catch (PrinterException e) {
199
                e.printStackTrace();
200
            }
201
        } else {
202
            // Actualizar attributes
203
            att = layoutContext.getAttributes().toPrintRequestAttributeSet();
204

    
205
            // ------------------ The Printing things --------------------- //
206
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
207

    
208
            // returns the set of printers that support printing a specific
209
            // document type (such as GIF)
210
            // with a specific set of attributes (such as two sided).
211
            // PrintRequestAttributeSet pras = new
212
            // HashPrintRequestAttributeSet();
213
            // interestingly, the printer dialog's default behavior has changed
214
            // with the new API: by default the dialog is not shown.
215
            // So we must use the ServiceUI class to create a print dialog
216
            // returns the default print service.
217
            if (m_cachePrintServices == null) {
218
                m_cachePrintServices =
219
                    PrintServiceLookup.lookupPrintServices(flavor, null);
220
            }
221

    
222
            PrintService defaultService = null;
223

    
224
            if (m_cachePrintService == null) {
225
                defaultService = PrintServiceLookup.lookupDefaultPrintService();
226
            }
227

    
228
            if ((defaultService == null) && (m_cachePrintService == null)) {
229
                JOptionPane.showMessageDialog((Component) PluginServices
230
                    .getMainFrame(), PluginServices.getText(this,
231
                    "ninguna_impresora_configurada"));
232

    
233
                return;
234
            }
235

    
236
            if (m_cachePrintService == null) {
237
                m_cachePrintService =
238
                    ServiceUI.printDialog(null, 200, 200, m_cachePrintServices,
239
                        defaultService, flavor, att);
240
            } else {
241
                m_cachePrintService =
242
                    ServiceUI.printDialog(null, 200, 200, m_cachePrintServices,
243
                        m_cachePrintService, flavor, att);
244
            }
245

    
246
            if (m_cachePrintService != null) {
247
                DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
248
                PrintJobListener pjlistener = new PrintJobAdapter() {
249

    
250
                    public void printDataTransferCompleted(PrintJobEvent e) {
251
                        // System.out.println("Fin de impresi?n");
252
                        layoutControl.fullRect();
253
                    }
254
                };
255

    
256
                jobNuevo.addPrintJobListener(pjlistener);
257
                
258
                /*
259
       Class repClass = null;
260
       try {
261
            repClass = Class.forName(flavor.getRepresentationClassName());
262
       } catch (Throwable e) {
263
           throw new IllegalArgumentException("unknown representation class");
264
       }
265
  
266
       if (!repClass.isInstance(printData)) {
267
           throw new IllegalArgumentException("data is not of declared type");
268
       }                 */
269

    
270
                Printable pb = null;
271
                Class repClass = null;
272
                try {
273
                     repClass = Class.forName(flavor.getRepresentationClassName());
274
                } catch (Throwable e) {
275
                    throw new IllegalArgumentException("unknown representation class");
276
                }
277

    
278
                Object printData = PluginServices.getExtension(org.gvsig.app.extension.Print.class);
279
                
280
                doc = new SimpleDoc(printData, flavor, null);
281

    
282
                try {
283
                    jobNuevo.print(doc, att);
284

    
285
                } catch (PrintException pe) {
286
                    NotificationManager.addError(pe);
287
                }
288
            }
289
        }
290
    }
291

    
292
    public IFFrameDialog createFFrameDialog(IFFrame fframe) {
293
        return layoutManager.createFFrameDialog(fframe, this);
294
    }
295

    
296
    /**
297
     * This method is used to get <strong>an initial</strong> ViewInfo object
298
     * for this Map. It is not intended to retrieve the ViewInfo object in a
299
     * later time. <strong>Use PluginServices.getMDIManager().getViewInfo(view)
300
     * to retrieve the ViewInfo object at any time after the creation of the
301
     * object.
302
     * 
303
     * @see com.iver.mdiApp.ui.MDIManager.IWindow#getWindowInfo()
304
     */
305
    public WindowInfo getWindowInfo() {
306
        if (m_viewInfo == null) {
307
            m_viewInfo =
308
                new WindowInfo(WindowInfo.ICONIFIABLE | WindowInfo.RESIZABLE
309
                    | WindowInfo.MAXIMIZABLE);
310
            m_viewInfo.setWidth(DEFAULT_WIDTH);
311
            m_viewInfo.setHeight(DEFAULT_HEIGHT);
312

    
313
            m_viewInfo.setTitle(PluginServices.getText(this, "Mapa") + " : "
314
                + layoutDocument.getName());
315
        }
316
        return m_viewInfo;
317
    }
318

    
319
    public LayoutContext getLayoutContext() {
320
        return layoutContext;
321
    }
322

    
323
    public LayoutControl getLayoutControl() {
324
        return layoutControl;
325
    }
326

    
327
    /**
328
     * @see com.iver.mdiApp.ui.MDIManager.IWindow#windowActivated()
329
     */
330
    public void windowActivated() {
331
        // fullRect();
332
        layoutControl.refresh();
333
        PluginServices
334
            .getMainFrame()
335
            .getStatusBar()
336
            .setMessage(
337
                "units",
338
                PluginServices.getText(this, layoutContext.getAttributes()
339
                    .getNameUnit()));
340
        // ensure requestFocus is enabled
341
        if (!layoutControl.getComponent().isRequestFocusEnabled()) {
342
            layoutControl.getComponent().setRequestFocusEnabled(true);
343
        }
344
        requestFocus();
345
        layoutControl.getComponent().requestFocus();
346
    }
347

    
348
    /**
349
     * @see com.iver.mdiApp.ui.MDIManager.SingletonWindow#getWindowModel()
350
     */
351
    public Object getWindowModel() {
352
        return layoutDocument;
353
    }
354

    
355
    public Document getDocument() {
356
        return layoutDocument;
357
    }
358

    
359
    /**
360
     * @see com.iver.cit.gvsig.fmap.ExtentListener#extentChanged(org.gvsig.fmap.mapcontext.events.ExtentEvent)
361
     */
362
    public void extentChanged(ExtentEvent e) {
363
        
364
    }
365

    
366
    /**
367
     * @see org.gvsig.andami.ui.mdiManager.IWindowListener#windowClosed()
368
     */
369
    public void windowClosed() {
370
        
371
    }
372

    
373
    /**
374
     * @see org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener#backColorChanged(org.gvsig.fmap.mapcontext.events.ColorEvent)
375
     */
376
    public void backColorChanged(ColorEvent e) {
377
        // refresh();
378
    }
379

    
380
    public void layoutToPDF(String suggestedName) {
381
        FileFilter pdfFilter =
382
            new GenericFileFilter("pdf", PluginServices.getText(this, "pdf"));
383

    
384
        JFileChooser jfc =
385
            new JFileChooser(PDF_AND_PS_FILECHOOSER, defaultPDFFolderPath);
386
        if (suggestedName != null) {
387
            jfc.setSelectedFile(new File(suggestedName));
388
        }
389
        jfc.addChoosableFileFilter(pdfFilter);
390
        jfc.setFileFilter(pdfFilter);
391

    
392
        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
393
            File f = jfc.getSelectedFile();
394
            File faux = null;
395

    
396
            if (f.getName().endsWith(".pdf") || f.getName().endsWith(".PDF")) {
397
                faux = f;
398
            } else {
399
                faux = new File(f.getPath() + ".pdf");
400
            }
401

    
402
            layoutControl.getLayoutDraw().toPDF(faux);
403
        }
404
    }
405

    
406
    public void layoutToPS(String suggestedName) {
407
        FileFilter pdfFilter =
408
            new GenericFileFilter("ps", PluginServices.getText(this, "ps"));
409

    
410
        JFileChooser jfc =
411
            new JFileChooser(PDF_AND_PS_FILECHOOSER, defaultPDFFolderPath);
412
        if (suggestedName != null) {
413
            jfc.setSelectedFile(new File(suggestedName));
414
        }
415
        jfc.addChoosableFileFilter(pdfFilter);
416
        jfc.setFileFilter(pdfFilter);
417

    
418
        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
419
            File f = jfc.getSelectedFile();
420
            File faux = null;
421

    
422
            if (f.getName().endsWith(".ps") || f.getName().endsWith(".PS")) {
423
                faux = f;
424
            } else {
425
                faux = new File(f.getPath() + ".ps");
426
            }
427

    
428
            layoutControl.getLayoutDraw().toPS(faux);
429
        }
430
    }
431

    
432
    public void layoutToPDF() {
433
        layoutToPDF(null);
434
    }
435

    
436
    public void layoutToPS() {
437
        layoutToPS(null);
438
    }
439

    
440
    public boolean isShowIconTag() {
441
        return bShowIconTag;
442
    }
443

    
444
    public void setShowIconTag(boolean modeDebug) {
445
        bShowIconTag = modeDebug;
446
    }
447

    
448
    /**
449
     * Event to change the projection.
450
     */
451
    public void projectionChanged(ProjectionEvent e) {
452
        // TODO Auto-generated method stub
453

    
454
    }
455

    
456
    public void update(Observable observable, Object notification) {
457
        this.layoutContext.updateFFrames();
458
        this.layoutControl.refresh();
459
    }
460

    
461
    public Object getWindowProfile() {
462
        return WindowInfo.EDITOR_PROFILE;
463
    }
464

    
465
    public void saveToState(PersistentState state) throws PersistenceException {
466
        state.set(LAYOUTCONTEXT_OBJECT, layoutContext);
467
        state.set(LAYOUTDOCUMENT_OBJECT, layoutDocument);
468
    }
469

    
470
    public void loadFromState(PersistentState state)
471
        throws PersistenceException {
472
        this.layoutContext = (LayoutContext) state.get(LAYOUTCONTEXT_OBJECT);
473
        this.layoutDocument = (LayoutDocument) state.get(LAYOUTDOCUMENT_OBJECT);
474
        this.layoutControl.setLayoutContext(layoutContext);
475
    }
476

    
477
    public static void registerPersistent() {
478
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
479
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
480
            DynStruct definition =
481
                manager.addDefinition(DefaultLayoutPanel.class,
482
                    PERSISTENCE_DEFINITION_NAME,
483
                    "Layout panel persistence definition", null, null);
484

    
485
            definition.addDynFieldObject(LAYOUTCONTEXT_OBJECT)
486
                .setClassOfValue(LayoutContext.class).setMandatory(true);
487
            definition.addDynFieldObject(LAYOUTDOCUMENT_OBJECT)
488
                .setClassOfValue(LayoutDocument.class).setMandatory(true);
489
        }
490
    }
491

    
492
    public WindowLayout getWindowLayout() {
493
        return null;
494
    }
495

    
496
    public void setWindowLayout(WindowLayout layout) {
497

    
498
    }
499

    
500
    private void setLayoutContext(LayoutContext theLayoutcontext) {
501
        this.layoutContext = theLayoutcontext;
502
        this.layoutControl.setLayoutContext(theLayoutcontext);
503
        layoutContext.getAttributes().setDistanceUnitX(layoutControl.getRect());
504
        layoutContext.getAttributes().setDistanceUnitY(layoutControl.getRect());
505

    
506
        layoutControl.setTool("layoutzoomin");
507
    }
508

    
509
    @Override
510
    public void setLayoutManager(LayoutManager layoutManager) {
511
        this.layoutManager = layoutManager;
512
    }
513
    
514
    
515
    /**
516
     * Method to print the Layout without modify the Affinetransform.
517
     *
518
     * @param g2 Geaphics2D
519
     */
520
    public void drawLayoutPrint(Graphics2D g2) {
521
        layoutControl.setCancelDrawing(false);
522

    
523
        // setCursor(Cursor.getDefaultCursor());
524

    
525
        double scale = 0;
526
        scale = layoutControl.getRect().height / layoutContext.getAttributes().m_sizePaper.getAlto() * 1;
527
        AffineTransform escalado = new AffineTransform();
528
        AffineTransform translacion = new AffineTransform();
529
        translacion.setToTranslation(layoutControl.getRect().getMinX(), layoutControl.getRect().getMinY());
530
        escalado.setToScale(scale, scale);
531
        layoutControl.getAT().setToIdentity();
532
        layoutControl.getAT().concatenate(translacion);
533
        layoutControl.getAT().concatenate(escalado);
534
        layoutContext.getAttributes().setDistanceUnitX(layoutControl.getRect());
535
        layoutContext.getAttributes().setDistanceUnitY(layoutControl.getRect());
536
        IFFrame[] fframes=layoutContext.getFFrames();
537
        
538
        for (int i = 0; i < fframes.length; i++) {
539
//              fframes[i].setPrintingProperties(this.att);
540
            fframes[i].print(
541
                g2,
542
                layoutControl.getAT(),
543
                null,
544
                layoutContext.getAttributes().toPrintAttributes());
545
//              fframes[i].setPrintingProperties(null);
546
        }
547

    
548
        // TODO Esto es para ver el rect?ngulo que representa el folio en la
549
        // impresi?n.
550
        // g2.drawRect(0, 0, (int) rect.getWidth(), (int) rect.getHeight());
551
    }
552

    
553
}