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 @ 145

History | View | Annotate | Download (19.9 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.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.JComponent;
49
import javax.swing.JOptionPane;
50
import javax.swing.JPanel;
51
import javax.swing.JSplitPane;
52
import javax.swing.filechooser.FileFilter;
53

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

    
82
/**
83
 * Graphic representation of the elements to print.
84
 * 
85
 * @author Vicente Caballero Navarro
86
 */
87
public class DefaultLayoutPanel extends LayoutPanel {
88

    
89
    private static final long serialVersionUID = 916830228032567624L;
90

    
91
    private static final int DEFAULT_HEIGHT = 450;
92

    
93
    private static final int DEFAULT_WIDTH = 700;
94

    
95
    public static final String PERSISTENCE_DEFINITION_NAME = "LayoutPanel";
96
    private static final String LAYOUTCONTEXT_OBJECT = "LayoutContext";
97
    private static final String LAYOUTDOCUMENT_OBJECT = "LayoutDocument";
98

    
99
    public static final String PDF_AND_PS_FILECHOOSER =
100
        "PDF_AND_PS_FILECHOOSER";
101

    
102
    public static File defaultPDFFolderPath;
103

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

    
120
        private UIManager uiManager;
121

    
122
        private JPanel leftPanel;
123

    
124
        private JSplitPane splitPane;
125
        private int lastPanePos = 0;
126

    
127
    /**
128
     * Creates a new Layout object.
129
     */
130
    public DefaultLayoutPanel() {
131
        DefaultLayoutControl defaultLayoutControl = new DefaultLayoutControl();
132
        this.layoutControl = defaultLayoutControl;
133
        defaultLayoutControl.initialize(this);       
134
        this.initComponents();
135
    }
136

    
137
    public void setDocument(Document document) {
138
        layoutDocument = (LayoutDocument) document;
139
        setLayoutContext(layoutDocument.getLayoutContext());
140
        this.setName(document.getName());
141
        layoutDocument.addPropertyChangeListener(new PropertyChangeListener() {
142

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

    
168
        add(splitPane, BorderLayout.CENTER);
169
        layoutControl.getComponent().setDoubleBuffered(true);
170
    }
171

    
172
    /**
173
     * Open the dialog of Layout properties.
174
     * 
175
     * @param job
176
     *            PrinterJob
177
     */
178
    public void showPagePropertiesWindow(PrinterJob job) {
179
        PageFormat pf1;
180

    
181
        pf1 = layoutContext.getAttributes().getPageFormat();
182
        pf1 = job.pageDialog(pf1);
183
        layoutContext.getAttributes().setPageFormat(pf1);
184
        layoutControl.refresh();
185
    }
186

    
187
    public void obtainRect(boolean isPrint) {
188
        layoutContext.getAttributes().obtainRect(isPrint,
189
            layoutControl.getRect(), getWidth(), getHeight());
190
    }
191
    
192
    public void showFConfig() {
193
        FConfigLayoutDialog m_configLayout = new FConfigLayoutDialog(this);
194
        PluginServices.getMDIManager().addWindow(m_configLayout);
195
    }
196

    
197
    public boolean showFProperties() {
198
        if (layoutDocument == null) {
199
            layoutDocument = new DefaultLayoutDocument();
200
            layoutDocument.setName(getName());
201
        }
202

    
203
        m_propertiesLayout = new MapProperties(layoutDocument);
204
        PluginServices.getMDIManager().addWindow(m_propertiesLayout);
205
        return m_propertiesLayout.isAccepted();
206
    }
207

    
208
    public void showPrintDialog(PrinterJob job) {
209
        if (job != null) {
210
            job.printDialog();
211

    
212
            try {
213
                job.setPrintable((Printable) PluginServices
214
                    .getExtension(org.gvsig.app.extension.Print.class));
215
                job.print();
216
            } catch (PrinterException e) {
217
                e.printStackTrace();
218
            }
219
        } else {
220
            // Actualizar attributes
221
            att = layoutContext.getAttributes().toPrintRequestAttributeSet();
222

    
223
            // ------------------ The Printing things --------------------- //
224
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
225

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

    
240
            PrintService defaultService = null;
241

    
242
            if (m_cachePrintService == null) {
243
                defaultService = PrintServiceLookup.lookupDefaultPrintService();
244
            }
245

    
246
            if ((defaultService == null) && (m_cachePrintService == null)) {
247
                JOptionPane.showMessageDialog((Component) PluginServices
248
                    .getMainFrame(), PluginServices.getText(this,
249
                    "ninguna_impresora_configurada"));
250

    
251
                return;
252
            }
253

    
254
            if (m_cachePrintService == null) {
255
                m_cachePrintService =
256
                    ServiceUI.printDialog(null, 200, 200, m_cachePrintServices,
257
                        defaultService, flavor, att);
258
            } else {
259
                m_cachePrintService =
260
                    ServiceUI.printDialog(null, 200, 200, m_cachePrintServices,
261
                        m_cachePrintService, flavor, att);
262
            }
263

    
264
            if (m_cachePrintService != null) {
265
                DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
266
                PrintJobListener pjlistener = new PrintJobAdapter() {
267

    
268
                    public void printDataTransferCompleted(PrintJobEvent e) {
269
                        // System.out.println("Fin de impresi?n");
270
                        layoutControl.fullRect();
271
                    }
272
                };
273

    
274
                jobNuevo.addPrintJobListener(pjlistener);
275

    
276
                Printable pb = null;
277
                Class repClass = null;
278
                try {
279
                     repClass = Class.forName(flavor.getRepresentationClassName());
280
                } catch (Throwable e) {
281
                    throw new IllegalArgumentException("unknown representation class");
282
                }
283

    
284
                Object printData = PluginServices.getExtension(org.gvsig.app.extension.Print.class);
285
                
286
                doc = new SimpleDoc(printData, flavor, null);
287

    
288
                try {
289
                    jobNuevo.print(doc, att);
290

    
291
                } catch (PrintException pe) {
292
                    NotificationManager.addError(pe);
293
                }
294
            }
295
        }
296
    }
297

    
298
    public IFFrameDialog createFFrameDialog(IFFrame fframe) {
299
        return layoutManager.createFFrameDialog(fframe, this);
300
    }
301

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

    
319
            m_viewInfo.setTitle(PluginServices.getText(this, "Mapa") + " : "
320
                + layoutDocument.getName());
321
        }
322
        return m_viewInfo;
323
    }
324

    
325
    public LayoutContext getLayoutContext() {
326
        return layoutContext;
327
    }
328

    
329
    public LayoutControl getLayoutControl() {
330
        return layoutControl;
331
    }
332

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

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

    
367
    public Document getDocument() {
368
        return layoutDocument;
369
    }
370

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

    
378
    /**
379
     * @see org.gvsig.andami.ui.mdiManager.IWindowListener#windowClosed()
380
     */
381
    public void windowClosed() {
382
        
383
    }
384

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

    
392
    public void layoutToPDF(String suggestedName) {
393
        FileFilter pdfFilter =
394
            new GenericFileFilter("pdf", PluginServices.getText(this, "pdf"));
395

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

    
404
        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
405
            File f = jfc.getSelectedFile();
406
            File faux = null;
407

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

    
414
            layoutControl.getLayoutDraw().toPDF(faux);
415
        }
416
    }
417

    
418
    public void layoutToPS(String suggestedName) {
419
        FileFilter pdfFilter =
420
            new GenericFileFilter("ps", PluginServices.getText(this, "ps"));
421

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

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

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

    
440
            layoutControl.getLayoutDraw().toPS(faux);
441
        }
442
    }
443

    
444
    public void layoutToPDF() {
445
        layoutToPDF(null);
446
    }
447

    
448
    public void layoutToPS() {
449
        layoutToPS(null);
450
    }
451

    
452
    public boolean isShowIconTag() {
453
        return bShowIconTag;
454
    }
455

    
456
    public void setShowIconTag(boolean modeDebug) {
457
        bShowIconTag = modeDebug;
458
    }
459

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

    
466
    }
467

    
468
    public void update(Observable observable, Object notification) {
469
        this.layoutContext.updateFFrames();
470
        this.layoutControl.refresh();
471
    }
472

    
473
    public Object getWindowProfile() {
474
        return WindowInfo.EDITOR_PROFILE;
475
    }
476

    
477
    public void saveToState(PersistentState state) throws PersistenceException {
478
        state.set(LAYOUTCONTEXT_OBJECT, layoutContext);
479
        state.set(LAYOUTDOCUMENT_OBJECT, layoutDocument);
480
    }
481

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

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

    
497
            definition.addDynFieldObject(LAYOUTCONTEXT_OBJECT)
498
                .setClassOfValue(LayoutContext.class).setMandatory(true);
499
            definition.addDynFieldObject(LAYOUTDOCUMENT_OBJECT)
500
                .setClassOfValue(LayoutDocument.class).setMandatory(true);
501
        }
502
    }
503

    
504
    public WindowLayout getWindowLayout() {
505
        return null;
506
    }
507

    
508
    public void setWindowLayout(WindowLayout layout) {
509

    
510
    }
511

    
512
    private void setLayoutContext(LayoutContext theLayoutcontext) {
513
        this.layoutContext = theLayoutcontext;
514
        this.layoutControl.setLayoutContext(theLayoutcontext);
515
        layoutContext.getAttributes().setDistanceUnitX(layoutControl.getRect());
516
        layoutContext.getAttributes().setDistanceUnitY(layoutControl.getRect());
517

    
518
        layoutControl.setTool("layoutzoomin");
519
    }
520

    
521
    @Override
522
    public void setLayoutManager(LayoutManager layoutManager) {
523
        this.layoutManager = layoutManager;
524
    }
525
    
526
    
527
    /**
528
     * Method to print the Layout without modify the Affinetransform.
529
     *
530
     * @param g2 Geaphics2D
531
     */
532
    public void drawLayoutPrint(Graphics2D g2) {
533
        layoutControl.setCancelDrawing(false);
534

    
535
        double scale = 0;
536
        scale = layoutControl.getRect().height / layoutContext.getAttributes().m_sizePaper.getAlto() * 1;
537
        AffineTransform escalado = new AffineTransform();
538
        AffineTransform translacion = new AffineTransform();
539
        translacion.setToTranslation(layoutControl.getRect().getMinX(), layoutControl.getRect().getMinY());
540
        escalado.setToScale(scale, scale);
541
        layoutControl.getAT().setToIdentity();
542
        layoutControl.getAT().concatenate(translacion);
543
        layoutControl.getAT().concatenate(escalado);
544
        layoutContext.getAttributes().setDistanceUnitX(layoutControl.getRect());
545
        layoutContext.getAttributes().setDistanceUnitY(layoutControl.getRect());
546
        IFFrame[] fframes=layoutContext.getFFrames();
547
        
548
        for (int i = 0; i < fframes.length; i++) {
549
            fframes[i].print(
550
                g2,
551
                layoutControl.getAT(),
552
                null,
553
                layoutContext.getAttributes().toPrintAttributes());
554
        }
555
    }
556
        
557
        public void addLateralComponent(JComponent c) {
558
                leftPanel.removeAll();
559
                leftPanel.add(c, BorderLayout.CENTER);
560
        }
561
        
562
        public void removeLateralComponent() {
563
                if (leftPanel.getComponentCount()>0) {
564
                        leftPanel.remove(0);
565
                }
566
        }
567
        
568
        public void hideLateralComponent(JComponent c) {
569
                lastPanePos = splitPane.getDividerLocation();
570
                splitPane.setDividerLocation(0);
571
        }
572

    
573
        public void showLateralComponent(JComponent c) {
574
                splitPane.setDividerLocation(lastPanePos);
575
                
576
        }
577

    
578
}