Statistics
| Revision:

svn-document-layout / branches / usability_v2 / 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 @ 144

History | View | Annotate | Download (20.5 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.BorderLayout;
25
import java.awt.Component;
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.JComponent;
50
import javax.swing.JOptionPane;
51
import javax.swing.JPanel;
52
import javax.swing.JSplitPane;
53
import javax.swing.filechooser.FileFilter;
54

    
55
import org.cresques.cts.IProjection;
56
import org.gvsig.andami.PluginServices;
57
import org.gvsig.andami.messages.NotificationManager;
58
import org.gvsig.andami.ui.mdiFrame.NewStatusBar;
59
import org.gvsig.andami.ui.mdiManager.WindowInfo;
60
import org.gvsig.app.project.documents.Document;
61
import org.gvsig.app.project.documents.gui.WindowLayout;
62
import org.gvsig.app.project.documents.layout.DefaultLayoutControl;
63
import org.gvsig.app.project.documents.layout.DefaultLayoutDocument;
64
import org.gvsig.app.project.documents.layout.LayoutContext;
65
import org.gvsig.app.project.documents.layout.LayoutControl;
66
import org.gvsig.app.project.documents.layout.LayoutDocument;
67
import org.gvsig.app.project.documents.layout.LayoutManager;
68
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
69
import org.gvsig.app.project.documents.layout.fframes.IFFrameUseFMap;
70
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.IFFrameDialog;
71
import org.gvsig.app.project.documents.layout.gui.dialogs.FConfigLayoutDialog;
72
import org.gvsig.app.project.documents.layout.gui.panel.DefaultUIManager;
73
import org.gvsig.app.project.documents.layout.gui.panel.UIManager;
74
import org.gvsig.app.project.documents.layout.gui.panel.UIManager.Area;
75
import org.gvsig.fmap.mapcontext.MapContext;
76
import org.gvsig.fmap.mapcontext.events.ColorEvent;
77
import org.gvsig.fmap.mapcontext.events.ExtentEvent;
78
import org.gvsig.fmap.mapcontext.events.ProjectionEvent;
79
import org.gvsig.gui.beans.swing.JFileChooser;
80
import org.gvsig.tools.ToolsLocator;
81
import org.gvsig.tools.dynobject.DynStruct;
82
import org.gvsig.tools.observer.Observable;
83
import org.gvsig.tools.persistence.PersistenceManager;
84
import org.gvsig.tools.persistence.PersistentState;
85
import org.gvsig.tools.persistence.exception.PersistenceException;
86
import org.gvsig.utils.GenericFileFilter;
87

    
88
/**
89
 * Graphic representation of the elements to print.
90
 * 
91
 * @author Vicente Caballero Navarro
92
 */
93
public class DefaultLayoutPanel extends LayoutPanel {
94

    
95
    private static final long serialVersionUID = 916830228032567624L;
96

    
97
    private static final int DEFAULT_HEIGHT = 450;
98

    
99
    private static final int DEFAULT_WIDTH = 700;
100

    
101
    public static final String PERSISTENCE_DEFINITION_NAME = "LayoutPanel";
102
    private static final String LAYOUTCONTEXT_OBJECT = "LayoutContext";
103
    private static final String LAYOUTDOCUMENT_OBJECT = "LayoutDocument";
104

    
105
    public static final String PDF_AND_PS_FILECHOOSER =
106
        "PDF_AND_PS_FILECHOOSER";
107

    
108
    public static File defaultPDFFolderPath;
109

    
110
    private MapProperties m_propertiesLayout = null;
111
    private PrintService[] m_cachePrintServices = null;
112
    private PrintService m_cachePrintService = null;
113
    private LayoutDocument layoutDocument = null;
114
    private Doc doc = null;
115
    private PrintRequestAttributeSet att = null;
116
    private WindowInfo m_viewInfo = null;
117
    /**
118
     * We use it when we are doing a layout and assigning tags.
119
     * It is put in debug when we do a VIEW_TAGS
120
     */
121
    private boolean bShowIconTag = false;
122
    private LayoutControl layoutControl = null;
123
    private LayoutContext layoutContext = null;
124
    private LayoutManager layoutManager = null;
125

    
126
        private UIManager uiManager;
127

    
128
        private JPanel leftPanel;
129

    
130
        private JSplitPane splitPane;
131
        private int lastPanePos = 0;
132

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

    
143
    public void setDocument(Document document) {
144
        layoutDocument = (LayoutDocument) document;
145
        setLayoutContext(layoutDocument.getLayoutContext());
146
        this.setName(document.getName());
147
        layoutDocument.addPropertyChangeListener(new PropertyChangeListener() {
148

    
149
            public void propertyChange(PropertyChangeEvent evt) {
150
                if (evt.getPropertyName().equals("name")) {
151
                    PluginServices
152
                        .getMDIManager()
153
                        .getWindowInfo(DefaultLayoutPanel.this)
154
                        .setTitle(
155
                            PluginServices.getText(this, "Mapa") + " : "
156
                                + (String) evt.getNewValue());
157
                }
158
            }
159
        });
160
    }
161
  
162
    /**
163
     * It initializes the components.
164
     */
165
    private void initComponents() {
166
            this.setLayout(new BorderLayout());
167
        leftPanel = new JPanel(new BorderLayout());
168
        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
169
                        leftPanel, layoutControl.getComponent());
170
        splitPane.setResizeWeight(0.2);
171
        splitPane.setOneTouchExpandable(true);
172
        splitPane.setContinuousLayout(true);
173

    
174
        add(splitPane, BorderLayout.CENTER);
175
        layoutControl.getComponent().setDoubleBuffered(true);
176
    }
177

    
178
    /**
179
     * Open the dialog of Layout properties.
180
     * 
181
     * @param job
182
     *            PrinterJob
183
     */
184
    public void showPagePropertiesWindow(PrinterJob job) {
185
        PageFormat pf1;
186

    
187
        pf1 = layoutContext.getAttributes().getPageFormat();
188
        pf1 = job.pageDialog(pf1);
189
        layoutContext.getAttributes().setPageFormat(pf1);
190
        layoutControl.refresh();
191
    }
192

    
193
    public void obtainRect(boolean isPrint) {
194
        layoutContext.getAttributes().obtainRect(isPrint,
195
            layoutControl.getRect(), getWidth(), getHeight());
196
    }
197
    
198
    public void showFConfig() {
199
        FConfigLayoutDialog m_configLayout = new FConfigLayoutDialog(this);
200
        PluginServices.getMDIManager().addWindow(m_configLayout);
201
    }
202

    
203
    public boolean showFProperties() {
204
        if (layoutDocument == null) {
205
            layoutDocument = new DefaultLayoutDocument();
206
            layoutDocument.setName(getName());
207
        }
208

    
209
        m_propertiesLayout = new MapProperties(layoutDocument);
210
        PluginServices.getMDIManager().addWindow(m_propertiesLayout);
211
        return m_propertiesLayout.isAccepted();
212
    }
213

    
214
    public void showPrintDialog(PrinterJob job) {
215
        if (job != null) {
216
            job.printDialog();
217

    
218
            try {
219
                job.setPrintable((Printable) PluginServices
220
                    .getExtension(org.gvsig.app.extension.Print.class));
221
                job.print();
222
            } catch (PrinterException e) {
223
                e.printStackTrace();
224
            }
225
        } else {
226
            // Actualizar attributes
227
            att = layoutContext.getAttributes().toPrintRequestAttributeSet();
228

    
229
            // ------------------ The Printing things --------------------- //
230
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
231

    
232
            // returns the set of printers that support printing a specific
233
            // document type (such as GIF)
234
            // with a specific set of attributes (such as two sided).
235
            // PrintRequestAttributeSet pras = new
236
            // HashPrintRequestAttributeSet();
237
            // interestingly, the printer dialog's default behavior has changed
238
            // with the new API: by default the dialog is not shown.
239
            // So we must use the ServiceUI class to create a print dialog
240
            // returns the default print service.
241
            if (m_cachePrintServices == null) {
242
                m_cachePrintServices =
243
                    PrintServiceLookup.lookupPrintServices(flavor, null);
244
            }
245

    
246
            PrintService defaultService = null;
247

    
248
            if (m_cachePrintService == null) {
249
                defaultService = PrintServiceLookup.lookupDefaultPrintService();
250
            }
251

    
252
            if ((defaultService == null) && (m_cachePrintService == null)) {
253
                JOptionPane.showMessageDialog((Component) PluginServices
254
                    .getMainFrame(), PluginServices.getText(this,
255
                    "ninguna_impresora_configurada"));
256

    
257
                return;
258
            }
259

    
260
            if (m_cachePrintService == null) {
261
                m_cachePrintService =
262
                    ServiceUI.printDialog(null, 200, 200, m_cachePrintServices,
263
                        defaultService, flavor, att);
264
            } else {
265
                m_cachePrintService =
266
                    ServiceUI.printDialog(null, 200, 200, m_cachePrintServices,
267
                        m_cachePrintService, flavor, att);
268
            }
269

    
270
            if (m_cachePrintService != null) {
271
                DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
272
                PrintJobListener pjlistener = new PrintJobAdapter() {
273

    
274
                    public void printDataTransferCompleted(PrintJobEvent e) {
275
                        // System.out.println("Fin de impresi?n");
276
                        layoutControl.fullRect();
277
                    }
278
                };
279

    
280
                jobNuevo.addPrintJobListener(pjlistener);
281

    
282
                Printable pb = null;
283
                Class repClass = null;
284
                try {
285
                     repClass = Class.forName(flavor.getRepresentationClassName());
286
                } catch (Throwable e) {
287
                    throw new IllegalArgumentException("unknown representation class");
288
                }
289

    
290
                Object printData = PluginServices.getExtension(org.gvsig.app.extension.Print.class);
291
                
292
                doc = new SimpleDoc(printData, flavor, null);
293

    
294
                try {
295
                    jobNuevo.print(doc, att);
296

    
297
                } catch (PrintException pe) {
298
                    NotificationManager.addError(pe);
299
                }
300
            }
301
        }
302
    }
303

    
304
    public IFFrameDialog createFFrameDialog(IFFrame fframe) {
305
        return layoutManager.createFFrameDialog(fframe, this);
306
    }
307

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

    
325
            m_viewInfo.setTitle(PluginServices.getText(this, "Mapa") + " : "
326
                + layoutDocument.getName());
327
        }
328
        return m_viewInfo;
329
    }
330

    
331
    public LayoutContext getLayoutContext() {
332
        return layoutContext;
333
    }
334

    
335
    public LayoutControl getLayoutControl() {
336
        return layoutControl;
337
    }
338

    
339
    /**
340
     * @see com.iver.mdiApp.ui.MDIManager.IWindow#windowActivated()
341
     */
342
    public void windowActivated() {
343
        // fullRect();
344
        layoutControl.refresh();
345
        PluginServices
346
            .getMainFrame()
347
            .getStatusBar()
348
            .setMessage(
349
                "units",
350
                PluginServices.getText(this, layoutContext.getAttributes()
351
                    .getNameUnit()));
352
        // ensure requestFocus is enabled
353
        if (!layoutControl.getComponent().isRequestFocusEnabled()) {
354
            layoutControl.getComponent().setRequestFocusEnabled(true);
355
        }
356
        IFFrameUseFMap[] selectedViews = layoutContext.getSelectedFFrames(IFFrameUseFMap.class);
357
        if (selectedViews.length==1) {
358
                IFFrameUseFMap ffview = selectedViews[0];
359
                // FIXME CMI: we should do sth more specific to update the ScaleControl
360
                ffview.refresh();
361
        }
362
        requestFocus();
363
        layoutControl.getComponent().requestFocus();
364
    }
365

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

    
373
    public Document getDocument() {
374
        return layoutDocument;
375
    }
376

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

    
384
    /**
385
     * @see org.gvsig.andami.ui.mdiManager.IWindowListener#windowClosed()
386
     */
387
    public void windowClosed() {
388
        
389
    }
390

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

    
398
    public void layoutToPDF(String suggestedName) {
399
        FileFilter pdfFilter =
400
            new GenericFileFilter("pdf", PluginServices.getText(this, "pdf"));
401

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

    
410
        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
411
            File f = jfc.getSelectedFile();
412
            File faux = null;
413

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

    
420
            layoutControl.getLayoutDraw().toPDF(faux);
421
        }
422
    }
423

    
424
    public void layoutToPS(String suggestedName) {
425
        FileFilter pdfFilter =
426
            new GenericFileFilter("ps", PluginServices.getText(this, "ps"));
427

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

    
436
        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
437
            File f = jfc.getSelectedFile();
438
            File faux = null;
439

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

    
446
            layoutControl.getLayoutDraw().toPS(faux);
447
        }
448
    }
449

    
450
    public void layoutToPDF() {
451
        layoutToPDF(null);
452
    }
453

    
454
    public void layoutToPS() {
455
        layoutToPS(null);
456
    }
457

    
458
    public boolean isShowIconTag() {
459
        return bShowIconTag;
460
    }
461

    
462
    public void setShowIconTag(boolean modeDebug) {
463
        bShowIconTag = modeDebug;
464
    }
465

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

    
472
    }
473

    
474
    public void update(Observable observable, Object notification) {
475
        this.layoutContext.updateFFrames();
476
        this.layoutControl.refresh();
477
    }
478

    
479
    public Object getWindowProfile() {
480
        return WindowInfo.EDITOR_PROFILE;
481
    }
482

    
483
    public void saveToState(PersistentState state) throws PersistenceException {
484
        state.set(LAYOUTCONTEXT_OBJECT, layoutContext);
485
        state.set(LAYOUTDOCUMENT_OBJECT, layoutDocument);
486
    }
487

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

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

    
503
            definition.addDynFieldObject(LAYOUTCONTEXT_OBJECT)
504
                .setClassOfValue(LayoutContext.class).setMandatory(true);
505
            definition.addDynFieldObject(LAYOUTDOCUMENT_OBJECT)
506
                .setClassOfValue(LayoutDocument.class).setMandatory(true);
507
        }
508
    }
509

    
510
    public WindowLayout getWindowLayout() {
511
        return null;
512
    }
513

    
514
    public void setWindowLayout(WindowLayout layout) {
515

    
516
    }
517

    
518
    private void setLayoutContext(LayoutContext theLayoutcontext) {
519
        this.layoutContext = theLayoutcontext;
520
        this.layoutControl.setLayoutContext(theLayoutcontext);
521
        layoutContext.getAttributes().setDistanceUnitX(layoutControl.getRect());
522
        layoutContext.getAttributes().setDistanceUnitY(layoutControl.getRect());
523

    
524
        layoutControl.setTool("layoutzoomin");
525
    }
526

    
527
    @Override
528
    public void setLayoutManager(LayoutManager layoutManager) {
529
        this.layoutManager = layoutManager;
530
    }
531
    
532
    
533
    /**
534
     * Method to print the Layout without modify the Affinetransform.
535
     *
536
     * @param g2 Geaphics2D
537
     */
538
    public void drawLayoutPrint(Graphics2D g2) {
539
        layoutControl.setCancelDrawing(false);
540

    
541
        // setCursor(Cursor.getDefaultCursor());
542

    
543
        double scale = 0;
544
        scale = layoutControl.getRect().height / layoutContext.getAttributes().m_sizePaper.getAlto() * 1;
545
        AffineTransform escalado = new AffineTransform();
546
        AffineTransform translacion = new AffineTransform();
547
        translacion.setToTranslation(layoutControl.getRect().getMinX(), layoutControl.getRect().getMinY());
548
        escalado.setToScale(scale, scale);
549
        layoutControl.getAT().setToIdentity();
550
        layoutControl.getAT().concatenate(translacion);
551
        layoutControl.getAT().concatenate(escalado);
552
        layoutContext.getAttributes().setDistanceUnitX(layoutControl.getRect());
553
        layoutContext.getAttributes().setDistanceUnitY(layoutControl.getRect());
554
        IFFrame[] fframes=layoutContext.getFFrames();
555
        
556
        for (int i = 0; i < fframes.length; i++) {
557
//              fframes[i].setPrintingProperties(this.att);
558
            fframes[i].print(
559
                g2,
560
                layoutControl.getAT(),
561
                null,
562
                layoutContext.getAttributes().toPrintAttributes());
563
//              fframes[i].setPrintingProperties(null);
564
        }
565

    
566
        // TODO Esto es para ver el rect?ngulo que representa el folio en la
567
        // impresi?n.
568
        // g2.drawRect(0, 0, (int) rect.getWidth(), (int) rect.getHeight());
569
    }
570
        
571
        public void addLateralComponent(JComponent c) {
572
                leftPanel.removeAll();
573
                leftPanel.add(c, BorderLayout.CENTER);
574
        }
575
        
576
        public void removeLateralComponent() {
577
                if (leftPanel.getComponentCount()>0) {
578
                        leftPanel.remove(0);
579
                }
580
        }
581
        
582
        public void hideLateralComponent(JComponent c) {
583
                lastPanePos = splitPane.getDividerLocation();
584
                splitPane.setDividerLocation(0);
585
        }
586

    
587
        public void showLateralComponent(JComponent c) {
588
                splitPane.setDividerLocation(lastPanePos);
589
                
590
        }
591

    
592
}