Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / layout / gui / DefaultLayoutPanel.java @ 34228

History | View | Annotate | Download (19.1 KB)

1
/*
2
 * Created on 20-feb-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 org.gvsig.app.project.documents.layout.gui;
46

    
47
import java.awt.Component;
48
import java.awt.Cursor;
49
import java.awt.Graphics2D;
50
import java.awt.GridLayout;
51
import java.awt.geom.AffineTransform;
52
import java.awt.print.PageFormat;
53
import java.awt.print.Printable;
54
import java.awt.print.PrinterException;
55
import java.awt.print.PrinterJob;
56
import java.beans.PropertyChangeEvent;
57
import java.beans.PropertyChangeListener;
58
import java.io.File;
59

    
60
import javax.print.Doc;
61
import javax.print.DocFlavor;
62
import javax.print.DocPrintJob;
63
import javax.print.PrintException;
64
import javax.print.PrintService;
65
import javax.print.PrintServiceLookup;
66
import javax.print.ServiceUI;
67
import javax.print.SimpleDoc;
68
import javax.print.attribute.PrintRequestAttributeSet;
69
import javax.print.event.PrintJobAdapter;
70
import javax.print.event.PrintJobEvent;
71
import javax.print.event.PrintJobListener;
72
import javax.swing.JOptionPane;
73
import javax.swing.filechooser.FileFilter;
74

    
75
import org.gvsig.andami.PluginServices;
76
import org.gvsig.andami.messages.NotificationManager;
77
import org.gvsig.andami.ui.mdiManager.WindowInfo;
78
import org.gvsig.app.project.documents.Document;
79
import org.gvsig.app.project.documents.gui.WindowLayout;
80
import org.gvsig.app.project.documents.layout.DefaultLayoutControl;
81
import org.gvsig.app.project.documents.layout.DefaultLayoutDocument;
82
import org.gvsig.app.project.documents.layout.LayoutContext;
83
import org.gvsig.app.project.documents.layout.LayoutControl;
84
import org.gvsig.app.project.documents.layout.LayoutDocument;
85
import org.gvsig.app.project.documents.layout.LayoutManager;
86
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
87
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.IFFrameDialog;
88
import org.gvsig.app.project.documents.layout.gui.dialogs.FConfigLayoutDialog;
89
import org.gvsig.fmap.mapcontext.events.ColorEvent;
90
import org.gvsig.fmap.mapcontext.events.ExtentEvent;
91
import org.gvsig.fmap.mapcontext.events.ProjectionEvent;
92
import org.gvsig.gui.beans.swing.JFileChooser;
93
import org.gvsig.tools.ToolsLocator;
94
import org.gvsig.tools.dynobject.DynStruct;
95
import org.gvsig.tools.observer.Observable;
96
import org.gvsig.tools.persistence.PersistenceManager;
97
import org.gvsig.tools.persistence.PersistentState;
98
import org.gvsig.tools.persistence.exception.PersistenceException;
99
import org.gvsig.utils.GenericFileFilter;
100

    
101

    
102
/**
103
 * Graphic representation of the elements to print.
104
 *
105
 * @author Vicente Caballero Navarro
106
 */
107
public class DefaultLayoutPanel extends LayoutPanel{
108
        private static final long serialVersionUID = 916830228032567624L;
109

    
110
        public static final String PERSISTENCE_DEFINITION_NAME = "LayoutPanel";
111
        private static final String LAYOUTCONTEXT_OBJECT = "LayoutContext";
112
        private static final String LAYOUTDOCUMENT_OBJECT = "LayoutDocument";
113
        
114
        public static final String PDF_AND_PS_FILECHOOSER = "PDF_AND_PS_FILECHOOSER";
115

    
116
        public static File defaultPDFFolderPath;
117

    
118
    private IFFrameDialog fframedialog = null;
119
    private MapProperties m_propertiesLayout = null;
120
    private PrintService[] m_cachePrintServices = null;
121
    private PrintService m_cachePrintService = null;
122
    private LayoutDocument layoutDocument = null;
123
    private Doc doc = null;
124
    private PrintRequestAttributeSet att = null;
125
    private WindowInfo m_viewInfo = null;
126
    /**
127
     * We use it when we are doing a layout and assigning tags.
128
     * It is put in debug when we do a VIEW_TAGS
129
     */
130
    private boolean bShowIconTag = false;
131
          private LayoutControl layoutControl = null;
132
        private LayoutContext layoutContext = null;
133
        private LayoutManager layoutManager = null;
134

    
135
    /**
136
     * Creates a new Layout object.
137
     */
138
    public DefaultLayoutPanel() {       
139
        layoutControl = new DefaultLayoutControl(this);
140
        this.initComponents();
141
    }
142

    
143

    
144
    public void setDocument(Document document){
145
        layoutDocument = (LayoutDocument)document; 
146
        setLayoutContext(layoutDocument.getLayoutContext());
147
        this.setName(document.getName());      
148
        layoutDocument.addPropertyChangeListener(new PropertyChangeListener() {
149
            public void propertyChange(PropertyChangeEvent evt) {
150
                if (evt.getPropertyName().equals("name")) {
151
                    PluginServices.getMDIManager().getWindowInfo(DefaultLayoutPanel.this)
152
                            .setTitle(
153
                                    PluginServices.getText(this, "Mapa")
154
                                            + " : "
155
                                            + (String) evt.getNewValue());
156
                }
157
            }
158
        });
159
    }
160

    
161

    
162

    
163

    
164

    
165

    
166
    /**
167
         * Method to print the Layout without modify the Affinetransform.
168
         *
169
         * @param g2 Geaphics2D
170
         */
171
    public void drawLayoutPrint(Graphics2D g2) {
172
        layoutControl.setCancelDrawing(false);
173

    
174
        setCursor(Cursor.getDefaultCursor());
175

    
176
        double scale = 0;
177
        scale = layoutControl.getRect().height / layoutContext.getAttributes().m_sizePaper.getAlto() * 1;
178
        AffineTransform escalado = new AffineTransform();
179
        AffineTransform translacion = new AffineTransform();
180
        translacion.setToTranslation(layoutControl.getRect().getMinX(), layoutControl.getRect().getMinY());
181
        escalado.setToScale(scale, scale);
182
        layoutControl.getAT().setToIdentity();
183
        layoutControl.getAT().concatenate(translacion);
184
        layoutControl.getAT().concatenate(escalado);
185
        layoutContext.getAttributes().setDistanceUnitX(layoutControl.getRect());
186
        layoutContext.getAttributes().setDistanceUnitY(layoutControl.getRect());
187
        IFFrame[] fframes=layoutContext.getFFrames();
188
        for (int i = 0; i < fframes.length; i++) {
189
//                    fframes[i].setPrintingProperties(this.att);
190
                   fframes[i].print(g2, layoutControl.getAT(),null, layoutContext.getAttributes().toPrintAttributes());
191
//                    fframes[i].setPrintingProperties(null);
192
        }
193

    
194
        // TODO Esto es para ver el rect?ngulo que representa el folio en la
195
        // impresi?n.
196
        // g2.drawRect(0, 0, (int) rect.getWidth(), (int) rect.getHeight());
197
    }
198

    
199

    
200

    
201
    /**
202
         * It initializes the components.
203
         */
204
    private void initComponents() {
205
            this.setLayout(new GridLayout(1,1));
206
        add(layoutControl.getComponent());
207
            setDoubleBuffered(true);
208
    }
209

    
210
    /**
211
         * Open the dialog of Layout properties.
212
         *
213
         * @param job PrinterJob
214
         */
215
    public void showPagePropertiesWindow(PrinterJob job) {
216
        PageFormat pf1;
217

    
218
        pf1 = layoutContext.getAttributes().getPageFormat();
219
        pf1 = job.pageDialog(pf1);
220
        layoutContext.getAttributes().setPageFormat(pf1);
221
        layoutControl.refresh();
222
    }
223

    
224
    public void obtainRect(boolean isPrint) {
225
            layoutContext.getAttributes().obtainRect(isPrint,layoutControl.getRect(), getWidth(), getHeight());
226
//            layoutContext.getAtributes().obtainRect(layoutControl.getRect());
227
    }
228

    
229
    public void showFConfig() {
230
        FConfigLayoutDialog m_configLayout = new FConfigLayoutDialog(this);
231
        PluginServices.getMDIManager().addWindow(m_configLayout);
232
    }
233

    
234
    public boolean showFProperties() {
235
        if (layoutDocument == null) {
236
            layoutDocument = new DefaultLayoutDocument();           
237
            layoutDocument.setName(getName());
238
        }
239

    
240
        m_propertiesLayout = new MapProperties(layoutDocument);
241
        PluginServices.getMDIManager().addWindow(m_propertiesLayout);
242
        return m_propertiesLayout.isAccepted();
243
    }
244

    
245
    public void showPrintDialog(PrinterJob job) {
246
        if (job != null) {
247
            job.printDialog();
248

    
249
            try {
250
                job.setPrintable((Printable) PluginServices
251
                        .getExtension(org.gvsig.app.extension.Print.class));
252
                job.print();
253
            } catch (PrinterException e) {
254
                e.printStackTrace();
255
            }
256
        } else {
257
            // Actualizar attributes
258
                        att = layoutContext.getAttributes().toPrintRequestAttributeSet();
259

    
260
            // ------------------ The Printing things --------------------- //
261
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
262

    
263
            // returns the set of printers that support printing a specific
264
            // document type (such as GIF)
265
            // with a specific set of attributes (such as two sided).
266
            // PrintRequestAttributeSet pras = new
267
            // HashPrintRequestAttributeSet();
268
            // interestingly, the printer dialog's default behavior has changed
269
            // with the new API: by default the dialog is not shown.
270
            // So we must use the ServiceUI class to create a print dialog
271
            // returns the default print service.
272
            if (m_cachePrintServices == null) {
273
                m_cachePrintServices = PrintServiceLookup.lookupPrintServices(
274
                        flavor, null);
275
            }
276

    
277
            PrintService defaultService = null;
278

    
279
            if (m_cachePrintService == null) {
280
                defaultService = PrintServiceLookup.lookupDefaultPrintService();
281
            }
282

    
283
            if ((defaultService == null) && (m_cachePrintService == null)) {
284
                JOptionPane.showMessageDialog((Component) PluginServices
285
                        .getMainFrame(),PluginServices.getText(this,"ninguna_impresora_configurada"));
286

    
287
                return;
288
            }
289

    
290
            if (m_cachePrintService == null) {
291
                m_cachePrintService = ServiceUI.printDialog(null, 200, 200,
292
                        m_cachePrintServices, defaultService, flavor, att);
293

    
294
                // m_cachePrintRequestAtributeSet = new
295
                // HashPrintRequestAttributeSet();
296
                // m_cachePrintRequestAtributeSet.addAll(pras);
297
            } else {
298
                m_cachePrintService = ServiceUI.printDialog(null, 200, 200,
299
                        m_cachePrintServices, m_cachePrintService, flavor, att);
300
            }
301

    
302
            if (m_cachePrintService != null) {
303
                DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
304
                PrintJobListener pjlistener = new PrintJobAdapter() {
305
                    public void printDataTransferCompleted(PrintJobEvent e) {
306
//                        System.out.println("Fin de impresi?n");
307
                        layoutControl.fullRect();
308
                    }
309
                };
310

    
311
                jobNuevo.addPrintJobListener(pjlistener);
312

    
313
                // DocAttributeSet das = new HashDocAttributeSet();
314
                doc = new SimpleDoc(PluginServices
315
                        .getExtension(org.gvsig.app.extension.Print.class), flavor,
316
                        null);
317

    
318
                try {
319
                    jobNuevo.print(doc, att);
320

    
321
                    // m_attributes.
322
                } catch (PrintException pe) {
323
                    NotificationManager.addError(pe);
324
                }
325
            }
326
        }
327

    
328
        /*
329
         * try { print = job.printDialog(att); } catch (Exception e) {
330
         * logger.error("Abriendo el Di?logo de imprimir");
331
         * //System.out.println("Excepci?n "+e); } if (print) {
332
         * job.setPrintable((Printable)
333
         * App.instance.getPc().getExtension(com.iver.cit.gvsig.Print.class));
334
         * try { job.print(att); } catch (Exception ex) { ex.printStackTrace(); } }
335
         */
336
    }
337

    
338
    public IFFrameDialog createFFrameDialog(IFFrame fframe) {
339
        return layoutManager.createFFrameDialog(fframe, this);
340
    }
341
       
342

    
343
    /**
344
     * This method is used to get <strong>an initial</strong> ViewInfo object
345
     * for this Map. It is not intended to retrieve the ViewInfo object in a
346
     * later time. <strong>Use PluginServices.getMDIManager().getViewInfo(view)
347
     * to retrieve the ViewInfo object at any time after the creation of the
348
     * object.
349
     *
350
     * @see com.iver.mdiApp.ui.MDIManager.IWindow#getWindowInfo()
351
     */
352
    public WindowInfo getWindowInfo() {
353
        if (m_viewInfo == null) {
354
            m_viewInfo = new WindowInfo(WindowInfo.ICONIFIABLE | WindowInfo.RESIZABLE
355
                    | WindowInfo.MAXIMIZABLE);
356
            m_viewInfo.setWidth(500);
357
            m_viewInfo.setHeight(400);
358

    
359
            m_viewInfo.setTitle(PluginServices.getText(this, "Mapa") + " : "
360
                    + layoutDocument.getName());
361
        }
362
        return m_viewInfo;
363
    }
364

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

    
389
    /**
390
     * @see com.iver.mdiApp.ui.MDIManager.SingletonWindow#getWindowModel()
391
     */
392
    public Object getWindowModel() {
393
        return layoutDocument;
394
    }
395
 
396
    public Document getDocument(){
397
            return layoutDocument;
398
    }
399
    /**
400
     * @see com.iver.cit.gvsig.fmap.ExtentListener#extentChanged(org.gvsig.fmap.mapcontext.events.ExtentEvent)
401
     */
402
    public void extentChanged(ExtentEvent e) {
403
            // do nothing
404
    }
405

    
406
    /**
407
     * @see org.gvsig.andami.ui.mdiManager.IWindowListener#windowClosed()
408
     */
409
    public void windowClosed() {
410
        // /PluginServices.getMainFrame().getStatusBar().setMessage("1","");
411
    }
412

    
413
    /**
414
     * @see org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener#backColorChanged(org.gvsig.fmap.mapcontext.events.ColorEvent)
415
     */
416
    public void backColorChanged(ColorEvent e) {
417
        // refresh();
418
    }
419

    
420
    public void layoutToPDF(String suggestedName) {
421
        FileFilter pdfFilter = new GenericFileFilter("pdf", PluginServices
422
                .getText(this, "pdf"));
423

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

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

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

    
441
            layoutControl.getLayoutDraw().toPDF(faux);
442
        }
443
    }
444

    
445
    public void layoutToPS(String suggestedName) {
446
        FileFilter pdfFilter = new GenericFileFilter("ps", PluginServices
447
                .getText(this, "ps"));
448

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

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

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

    
466
            layoutControl.getLayoutDraw().toPS(faux);
467
        }
468
    }
469

    
470
    public void layoutToPDF() {
471
        layoutToPDF(null);
472
    }
473

    
474
    public void layoutToPS() {
475
        layoutToPS(null);
476
    }
477

    
478
    public boolean isShowIconTag() {
479
        return bShowIconTag;
480
    }
481

    
482
   public void setShowIconTag(boolean modeDebug) {
483
        bShowIconTag = modeDebug;
484
    }
485

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

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

    
499
        public Object getWindowProfile() {
500
                return WindowInfo.EDITOR_PROFILE;
501
        }
502

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

    
508
        public void loadFromState(PersistentState state)
509
                        throws PersistenceException {
510
                this.layoutContext = (LayoutContext)state.get(LAYOUTCONTEXT_OBJECT);        
511
                this.layoutDocument = (LayoutDocument)state.get(LAYOUTDOCUMENT_OBJECT); 
512
                this.layoutControl.setLayoutContext(layoutContext);
513
        }
514
        
515
        public static void registerPersistent() {
516
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
517
                if( manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null ) {
518
                        DynStruct definition = manager.addDefinition(
519
                                        DefaultLayoutPanel.class,
520
                                        PERSISTENCE_DEFINITION_NAME,
521
                                        "Layout panel persistence definition",
522
                                        null, 
523
                                        null
524
                        );         
525

    
526
                        definition.addDynFieldObject(LAYOUTCONTEXT_OBJECT).setClassOfValue(LayoutContext.class).setMandatory(true);                
527
                        definition.addDynFieldObject(LAYOUTDOCUMENT_OBJECT).setClassOfValue(LayoutDocument.class).setMandatory(true);                              
528
                }
529
        }
530

    
531

    
532
        public WindowLayout getWindowLayout() {
533
                // TODO Auto-generated method stub
534
                return null;
535
        }
536

    
537

    
538
        public void setWindowLayout(WindowLayout layout) {
539
                // TODO Auto-generated method stub
540
                
541
        }
542

    
543
    private void setLayoutContext(LayoutContext theLayoutcontext) {
544
       this.layoutContext = theLayoutcontext;     
545
       this.layoutControl.setLayoutContext(theLayoutcontext);
546
       layoutContext.getAttributes().setDistanceUnitX(layoutControl.getRect());
547
       layoutContext.getAttributes().setDistanceUnitY(layoutControl.getRect());
548
       
549
       layoutControl.setTool("layoutzoomin");
550
    }
551

    
552
    @Override
553
    public void setLayoutManager(LayoutManager layoutManager) {
554
        this.layoutManager = layoutManager;        
555
    }
556

    
557
}