Statistics
| Revision:

root / trunk / extensions / extQuickPrint / src / es / iver / quickPrint / ModelTemplatePanel.java @ 28282

History | View | Annotate | Download (9.69 KB)

1
package es.iver.quickPrint;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.FileNotFoundException;
7
import java.io.Reader;
8
import java.net.URL;
9

    
10
import org.exolab.castor.xml.MarshalException;
11
import org.exolab.castor.xml.ValidationException;
12

    
13
import com.iver.andami.PluginServices;
14
import com.iver.andami.messages.NotificationManager;
15
import com.iver.cit.gvsig.Print;
16
import com.iver.cit.gvsig.ProjectExtension;
17
import com.iver.cit.gvsig.project.Project;
18
import com.iver.cit.gvsig.project.ProjectFactory;
19
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
20
import com.iver.cit.gvsig.project.documents.layout.FLayoutDraw;
21
import com.iver.cit.gvsig.project.documents.layout.ProjectMap;
22
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameGrid;
23
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameLegend;
24
import com.iver.cit.gvsig.project.documents.layout.fframes.FFramePicture;
25
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameScaleBar;
26
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameText;
27
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameView;
28
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
29
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
30
import com.iver.cit.gvsig.project.documents.view.ProjectView;
31
import com.iver.cit.gvsig.project.documents.view.gui.IView;
32
import com.iver.cit.gvsig.project.documents.view.gui.View;
33
import com.iver.utiles.XMLEntity;
34
import com.iver.utiles.xml.XMLEncodingUtils;
35
import com.iver.utiles.xmlEntity.generate.XmlTag;
36

    
37
public class ModelTemplatePanel {
38
        public static int DEFAULT=0;
39
        public static int WITHOUTLOGO=1;
40
        public static int IMAGE=2;
41

    
42
        public static final String[] formats=new String[]{"A4","A3","A2","A1","A0"};
43

    
44
        private String format="A4";
45
        private int copies=1;
46
        private String orientation=PluginServices.getText(ModelTemplatePanel.class,"horizontal");
47
        private String title="";
48
        private double scale=50000;
49
        private double grid=5000;
50
        private int legend=10;
51
        private int logo=DEFAULT;
52
        private String image="";
53
        private IView view;
54
        public static Integer[] sizeFont=new Integer[]{new Integer(4),new Integer(6),new Integer(8),new Integer(10),new Integer(12),new Integer(14),new Integer(16),new Integer(18),new Integer(20),new Integer(22),new Integer(24),new Integer(26),new Integer(28),new Integer(30)};
55
        private boolean isLegend;
56
        private boolean isGrid;
57
        private boolean forceScale=false;
58

    
59
        public ModelTemplatePanel(IView view) {
60
                this.view=view;
61
        }
62
        public int getCopies() {
63
                return copies;
64
        }
65
        public void setCopies(int copies) {
66
                this.copies = copies;
67
        }
68
        public String getFormat() {
69
                return format;
70
        }
71
        public void setFormat(String format) {
72
                this.format = format;
73
        }
74
        public double getGrid() {
75
                return grid;
76
        }
77
        public void setGrid(double grid) {
78
                this.grid = grid;
79
        }
80
        public String getImage() {
81
                return image;
82
        }
83
        public void setImage(String image) {
84
                this.image = image;
85
        }
86
        public int getLegend() {
87
                return legend;
88
        }
89
        public void setLegend(int legend) {
90
                this.legend = legend;
91
        }
92
        public int getLogo() {
93
                return logo;
94
        }
95
        public void setLogo(int logo) {
96
                this.logo = logo;
97
        }
98
        public String getOrientation() {
99
                return orientation;
100
        }
101
        public void setOrientation(String orientation) {
102
                this.orientation = orientation;
103
        }
104
        public double getScale() {
105
                return scale;
106
        }
107
        public void setScale(double scale) {
108
                this.scale = scale;
109
        }
110
        public String getTitle() {
111
                return title;
112
        }
113
        public void setTitle(String title) {
114
                this.title = title;
115
        }
116

    
117
         /**
118
     * M?todo que abre una ventana nueva con el informe ya confeccionado.
119
     */
120
    public void openReport() {
121
//        Project p = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
122
        Layout layout = openTemplate();
123
        updateReport(layout);
124

    
125
        ProjectMap pmap = ProjectFactory.createMap(getTitle());
126
        pmap.setModel(layout);
127
        pmap.getModel().setProjectMap(pmap);
128
//        p.addDocument(pmap);
129
        PluginServices.getMDIManager().addWindow(layout);
130
    }
131

    
132
    /**
133
     * M?todo que exporta directamente el informe a PDF.
134
     */
135
    public void toPDFReport() {
136
        Layout layout = openTemplate();
137
        updateReport(layout);
138

    
139
        FLayoutDraw layoutDraw = new FLayoutDraw(layout);
140
        String generatePDF="";
141
        layoutDraw.toPDF(new File(generatePDF));
142
    }
143

    
144
    /**
145
     * M?todo que imprime directamente el informe.
146
     */
147
    public void printReport() {
148
        Layout layout = openTemplate();
149
        updateReport(layout);
150

    
151
        Print printExtension = (Print) PluginServices.getExtension(Print.class);
152
        int num=getCopies();
153
        for (int i = 0; i < num; i++) {
154
                 printExtension.doPrint(layout);
155
                }
156

    
157
    }
158

    
159
         /**
160
     * Carga la plantilla.
161
     *
162
     * @return Layout con el contenido de la plantilla ya cargado.
163
     */
164
    private Layout openTemplate() {
165
        Project p = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
166
        Layout layout = null;
167

    
168
        try {
169
                String path=getPath();
170
            File xmlFile = new File(path);
171
            FileInputStream is = new FileInputStream(xmlFile);
172
            Reader reader = XMLEncodingUtils.getReader(is);
173
            XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
174

    
175
            try {
176
                XMLEntity xml = new XMLEntity(tag);
177
                layout = Layout.createLayout(xml, p);
178
            } catch (OpenException e) {
179
                e.showError();
180
            }
181
        } catch (FileNotFoundException e) {
182
            NotificationManager.addError(PluginServices.getText(this,
183
                    "Al_leer_la_leyenda"), e);
184
        } catch (MarshalException e) {
185
            NotificationManager.addError(PluginServices.getText(this,
186
                    "Al_leer_la_leyenda"), e);
187
        } catch (ValidationException e) {
188
            NotificationManager.addError(PluginServices.getText(this,
189
                    "Al_leer_la_leyenda"), e);
190
        }
191

    
192
        return layout;
193
    }
194

    
195
    private String getPath() {
196
            return TemplateExtension.templatesDir + File.separator + getTemplateName();
197
        }
198

    
199
    private String getTemplateName() {
200
            String format=getFormat();
201
                String orientation=getOrientation();
202
                String path="";
203
                if (orientation.equals(PluginServices.getText(ModelTemplatePanel.class,"horizontal"))){
204
                        path=format+"H"+".gvt";
205
                }else{
206
                        path=format+"V"+".gvt";
207
                }
208
                return path;
209
    }
210
        /**
211
     * Actualiza los datos del informe que queremos confeccionar.
212
     *
213
     * @param layout Layout sobre el que modificaremos. En nuestro caso es una plantilla.
214
     */
215
    private void updateReport(Layout layout) {
216
        IFFrame[] frames = layout.getLayoutContext().getFFrames();
217
        FFrameView fview = null;
218

    
219
        for (int i = 0; i < frames.length; i++) {
220
            IFFrame frame = frames[i];
221
            if (frame.getTag()==null)
222
                    continue;
223
            if (frame.getTag().equals("view")) {
224
                fview = (FFrameView) frame;
225

    
226
                fview.setView((ProjectView) ((View)view).getModel());
227
                if (isForceScale()){
228
                        fview.setScale(getScale());
229
                        fview.setTypeScale(FFrameView.MANUAL);
230
                        fview.refresh();
231
                }else{
232
                        fview.setTypeScale(FFrameView.AUTOMATICO);
233
                }
234
            }
235
        }
236

    
237
        if (isGrid()){
238
                FFrameGrid fgrid=new FFrameGrid();
239
                fgrid.setIsLine(true);
240
                fgrid.setIntervalX(getGrid());
241
                fgrid.setIntervalY(getGrid());
242
                fgrid.setLayout(layout);
243
                fgrid.setFFrameDependence(fview);
244
                fview.setGrid(fgrid);
245
                fview.showGrid(isGrid());
246
        }
247

    
248
        for (int i = 0; i < frames.length; i++) {
249
            IFFrame frame = frames[i];
250
            if (frame.getTag()==null)
251
                    continue;
252
            if (frame.getTag().equals("tittle")) {
253
                FFrameText ftext = (FFrameText) frame;
254
                ftext.clearText();
255
                String[] s=getTitle().split("\n");
256
                for (int j = 0; j < s.length; j++) {
257
                                        ftext.addText(s[j]);
258
                                }
259
            } else if (frame.getTag().equals("scale")) {
260
                FFrameScaleBar fscale = (FFrameScaleBar) frame;
261
                fscale.setFFrameDependence(fview);
262
            } else if (frame.getTag().equals("legend")) {
263
                    FFrameLegend flegend = (FFrameLegend) frame;
264
                    if (isLegend()){
265
                            flegend.setFont(flegend.getFont().deriveFont(getLegend()));
266
                            flegend.setFFrameDependence(fview);
267
                    }else{
268
                            flegend.setBoundBox(new Rectangle2D.Double(0,0,0,0));
269
                    }
270
            } else if (frame.getTag().equals("image")) {
271
                FFramePicture fpicture = (FFramePicture) frame;
272
                if (getLogo()==DEFAULT){
273
                        fpicture.load(TemplateExtension.templatesDir+File.separator+"defaultLogo.png");
274
                }else if (getLogo()==IMAGE){
275
                        fpicture.load(getImage());
276
                }else{
277
                        fpicture.setBoundBox(new Rectangle2D.Double(0,0,0,0));
278
                }
279
            }
280
        }
281
    }
282
        public boolean isGrid() {
283
                return isGrid;
284
        }
285
        public void setGrid(boolean isGrid) {
286
                this.isGrid = isGrid;
287
        }
288
        public boolean isLegend() {
289
                return isLegend;
290
        }
291
        public void setLegend(boolean isLegend) {
292
                this.isLegend = isLegend;
293
        }
294
        public boolean isForceScale() {
295
                return forceScale;
296
        }
297
        public void forceScale(boolean b) {
298
                forceScale=b;
299
        }
300

    
301
}