Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / extension / LayoutMainExtension.java @ 1714

History | View | Annotate | Download (19.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.extension;
23

    
24
import java.awt.Component;
25
import java.beans.PropertyVetoException;
26
import java.io.File;
27
import java.io.FileInputStream;
28
import java.io.IOException;
29
import java.util.Iterator;
30
import java.util.prefs.Preferences;
31
import javax.swing.JOptionPane;
32
import org.gvsig.andami.IconThemeHelper;
33
import org.gvsig.andami.PluginServices;
34
import org.gvsig.andami.plugins.Extension;
35
import org.gvsig.andami.ui.mdiManager.IWindow;
36
import org.gvsig.andami.ui.mdiManager.MDIManager;
37
import org.gvsig.app.ApplicationLocator;
38
import org.gvsig.app.ApplicationManager;
39
import org.gvsig.app.project.Project;
40
import org.gvsig.app.project.ProjectManager;
41
import org.gvsig.app.project.documents.Document;
42
import org.gvsig.app.project.documents.layout.DefaultLayoutManager;
43
import org.gvsig.app.project.documents.layout.LayoutDocument;
44
import org.gvsig.app.project.documents.layout.LayoutManager;
45
import org.gvsig.app.project.documents.layout.contextmenu.gui.BeforeLayoutMenuEntry;
46
import org.gvsig.app.project.documents.layout.contextmenu.gui.BehindLayoutMenuEntry;
47
import org.gvsig.app.project.documents.layout.contextmenu.gui.CancelLayoutMenuEntry;
48
import org.gvsig.app.project.documents.layout.contextmenu.gui.CopyLayoutMenuEntry;
49
import org.gvsig.app.project.documents.layout.contextmenu.gui.CutLayoutMenuEntry;
50
import org.gvsig.app.project.documents.layout.contextmenu.gui.PasteLayoutMenuEntry;
51
import org.gvsig.app.project.documents.layout.contextmenu.gui.PositionLayoutMenuEntry;
52
import org.gvsig.app.project.documents.layout.contextmenu.gui.PropertyLayoutMenuEntry;
53
import org.gvsig.app.project.documents.layout.contextmenu.gui.RefreshLayoutMenuEntry;
54
import org.gvsig.app.project.documents.layout.contextmenu.gui.SelectAllLayoutMenuEntry;
55
import org.gvsig.app.project.documents.layout.contextmenu.gui.SimplifyLayoutMenuEntry;
56
import org.gvsig.app.project.documents.layout.contextmenu.gui.TerminateLayoutMenuEntry;
57
import org.gvsig.app.project.documents.layout.fframes.FFrame;
58
import org.gvsig.app.project.documents.layout.fframes.FFrameBasicFactory;
59
import org.gvsig.app.project.documents.layout.fframes.FFrameGraphics;
60
import org.gvsig.app.project.documents.layout.fframes.FFrameGraphicsFactory;
61
import org.gvsig.app.project.documents.layout.fframes.FFrameGrid;
62
import org.gvsig.app.project.documents.layout.fframes.FFrameGridFactory;
63
import org.gvsig.app.project.documents.layout.fframes.FFrameGroup;
64
import org.gvsig.app.project.documents.layout.fframes.FFrameGroupFactory;
65
import org.gvsig.app.project.documents.layout.fframes.FFrameLegend;
66
import org.gvsig.app.project.documents.layout.fframes.FFrameLegendFactory;
67
import org.gvsig.app.project.documents.layout.fframes.FFrameNorth;
68
import org.gvsig.app.project.documents.layout.fframes.FFrameNorthFactory;
69
import org.gvsig.app.project.documents.layout.fframes.FFrameOverView;
70
import org.gvsig.app.project.documents.layout.fframes.FFrameOverViewFactory;
71
import org.gvsig.app.project.documents.layout.fframes.FFramePicture;
72
import org.gvsig.app.project.documents.layout.fframes.FFramePictureFactory;
73
import org.gvsig.app.project.documents.layout.fframes.FFrameScaleBar;
74
import org.gvsig.app.project.documents.layout.fframes.FFrameScaleBarFactory;
75
import org.gvsig.app.project.documents.layout.fframes.FFrameSymbol;
76
import org.gvsig.app.project.documents.layout.fframes.FFrameSymbolFactory;
77
import org.gvsig.app.project.documents.layout.fframes.FFrameTable;
78
import org.gvsig.app.project.documents.layout.fframes.FFrameTableFactory;
79
import org.gvsig.app.project.documents.layout.fframes.FFrameText;
80
import org.gvsig.app.project.documents.layout.fframes.FFrameTextFactory;
81
import org.gvsig.app.project.documents.layout.fframes.FFrameView;
82
import org.gvsig.app.project.documents.layout.fframes.FFrameViewFactory;
83
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameBoxDialog;
84
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameGraphicsDialog;
85
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameGridDialog;
86
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameGroupDialog;
87
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameLegendDialog;
88
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameNorthDialog;
89
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameOverViewDialog;
90
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFramePictureDialog;
91
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameScaleBarDialog;
92
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameTextDialog;
93
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameViewDialog;
94
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
95
import org.gvsig.gui.beans.swing.JFileChooser;
96
import org.gvsig.i18n.Messages;
97
import org.gvsig.layout.mapbox.MapBoxUtils;
98
import org.gvsig.layout.mapbox.panel.menu.AddColumnMapBoxMenuEntry;
99
import org.gvsig.layout.mapbox.panel.menu.AddRowMapBoxMenuEntry;
100
import org.gvsig.layout.mapbox.panel.menu.CombineCellsMapBoxMenuEntry;
101
import org.gvsig.layout.mapbox.panel.menu.InsertColumnMapBoxMenuEntry;
102
import org.gvsig.layout.mapbox.panel.menu.InsertRowMapBoxMenuEntry;
103
import org.gvsig.layout.mapbox.panel.menu.RemoveCellsMapBoxMenuEntry;
104
import org.gvsig.layout.mapbox.panel.menu.RemoveColumnMapBoxMenuEntry;
105
import org.gvsig.layout.mapbox.panel.menu.RemoveRowMapBoxMenuEntry;
106
import org.gvsig.layout.mapbox.panel.menu.SplitCellsHorizontallyMapBoxMenuEntry;
107
import org.gvsig.layout.mapbox.panel.menu.SplitCellsVerticallyMapBoxMenuEntry;
108
import org.gvsig.tools.ToolsLocator;
109
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
110
import org.gvsig.tools.i18n.I18nManager;
111
import org.gvsig.tools.persistence.PersistenceManager;
112
import org.gvsig.tools.persistence.PersistentState;
113
import org.gvsig.tools.persistence.exception.PersistenceException;
114
import org.gvsig.utils.GenericFileFilter;
115
import org.slf4j.Logger;
116
import org.slf4j.LoggerFactory;
117

    
118
/**
119
 * Extension que proporciona controles para crear proyectos nuevos, abrirlos y
120
 * guardarlos. Adem?s los tipos de tabla que soporta el proyecto son a?adidos en
121
 * esta clase.
122
 * 
123
 * @author gvSIG team
124
 */
125
public class LayoutMainExtension extends Extension {
126

    
127
    private static final Logger logger = LoggerFactory
128
            .getLogger(FFrameView.class);
129

    
130
    private String templatesPath;
131

    
132
    public static final String LAYOUT_TEMPLATE_FILECHOOSER_ID = "LAYOUT_TEMPLATE_FILECHOOSER_ID";
133

    
134
    private void registerIcons() {
135

    
136
        IconThemeHelper.registerIcon("action",
137
                "application-layout-template-open", this);
138
        IconThemeHelper.registerIcon("action",
139
                "application-layout-new", this);
140
        
141
    }
142

    
143
    @Override
144
    public void initialize() {
145
        registerIcons();
146
    }
147

    
148
    @Override
149
    public void postInitialize() {
150
        super.postInitialize();
151
        initializeDocumentActionsExtensionPoint();
152
        registerDocuments();
153
        registerFFrames();
154
        registerContextMenuOptions();
155
    }
156

    
157

    
158
    @Override
159
    public void execute(String actionCommand) {
160
        if ("application-layout-template-open".equals(actionCommand)) {
161
            openLayout();
162
        } else if ("application-layout-new".equals(actionCommand)) { 
163
            newLayout();   
164
        }
165
    }
166

    
167
    private void newLayout() {
168
        ApplicationManager application = ApplicationLocator.getManager();
169
        ProjectManager projectManager = ApplicationLocator.getProjectManager();
170
        I18nManager i18nManager = ToolsLocator.getI18nManager();
171
        Project project = projectManager.getCurrentProject();
172
        
173
        Iterator<? extends Document> documents =
174
            projectManager.createDocumentsByUser(LayoutManager.TYPENAME);
175
        for (; (documents != null) && (documents.hasNext());) {
176
            try {
177
                Document document = documents.next();
178
                project.addDocument(document);
179
                IWindow window = document.getFactory().getMainWindow(document);
180
                if (window == null) {
181
                    application.messageDialog(
182
                            i18nManager.getTranslation("error_opening_the_document"), 
183
                            i18nManager.getTranslation("warning"), 
184
                            JOptionPane.WARNING_MESSAGE
185
                    );
186
                    continue;
187
                }
188
                application.getUIManager().addWindow(
189
                    window,
190
                    MDIManager.ALIGN_FIRST_LINE_END_CASCADE
191
                );
192
                try {
193
                    application.getUIManager().setMaximum(window, true);
194
                } catch (PropertyVetoException e) {
195
                    logger.info("Can't maximize layout.", e);
196
                }
197
                
198
            } catch (Exception e) {
199
                logger.warn("Problems creating a Layout document.",e);
200
            }
201
        }        
202
    }
203
    
204

    
205
    private void openLayout() {
206
        LayoutPanel layout = null;
207

    
208
        if (templatesPath == null) {
209
            Preferences prefs = Preferences.userRoot().node("gvsig.foldering");
210
            templatesPath = prefs.get("TemplatesFolder", null);
211
        }
212

    
213
        JFileChooser jfc = new JFileChooser(LAYOUT_TEMPLATE_FILECHOOSER_ID,
214
                templatesPath);
215
        GenericFileFilter layoutMainExtensionFilter = new GenericFileFilter(
216
                LayoutManager.TEMPLATE_FILE_POINTEXT,
217
                Messages.getText("_Layout_template") + " (*"
218
                        + LayoutManager.TEMPLATE_FILE_POINTEXT + ")");
219
        jfc.addChoosableFileFilter(layoutMainExtensionFilter);
220
        jfc.setFileFilter(layoutMainExtensionFilter);
221

    
222
        if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
223
            File file = jfc.getSelectedFile();
224
            try {
225
                if (!file.exists()) {
226
                    /*
227
                     * The user has written a file name instead of clicking on
228
                     * one and it does not exist
229
                     */
230
                    File auxfile = new File(file.getPath()
231
                            + LayoutManager.TEMPLATE_FILE_POINTEXT);
232
                    if (!auxfile.exists()) {
233
                        /*
234
                         * Not even this one exists: error
235
                         */
236
                        throw new IOException("File does not exist: "
237
                                + file.getName());
238
                    }
239
                    else {
240
                        file = auxfile;
241
                    }
242
                }
243

    
244
                File xmlFile = new File(file.getAbsolutePath());
245
                FileInputStream is = new FileInputStream(xmlFile);
246

    
247
                PersistenceManager persistenceManager = ToolsLocator
248
                        .getPersistenceManager();
249
                PersistentState persistentState = persistenceManager
250
                        .loadState(is);
251
                layout = (LayoutPanel) persistenceManager
252
                        .create(persistentState);
253

    
254
            }
255
            catch (Exception e) {
256
                String tit = Messages.getText("_Layout_template");
257
                String msg;
258

    
259
                if (e instanceof PersistenceException) {
260
                    msg = Messages.getText("_Bad_format_in_layout_template");
261
                }
262
                else {
263
                    msg = Messages.getText("_Cant_load_layout_template");
264
                }
265

    
266
                msg = msg + ": " + file.getPath();
267
                msg = msg + "\n" + e.getMessage();
268
                logger.warn(msg, e);
269
                JOptionPane.showMessageDialog(ApplicationLocator.getManager()
270
                        .getRootComponent(), msg, tit,
271
                        JOptionPane.ERROR_MESSAGE);
272
            }
273

    
274
            if (layout == null) {
275
                return;
276
            }
277

    
278
            LayoutDocument layoutDocument = (LayoutDocument) layout
279
                    .getDocument();
280
            /*
281
             * (LayoutDocument) ProjectManager.getInstance().createDocument(
282
             * DefaultLayoutManager.TYPENAME, file.getName());
283
             */
284

    
285
            Project p = ApplicationLocator.getManager().getProjectManager()
286
                    .getCurrentProject();
287
            p.addDocument(layoutDocument);
288

    
289
            layout.getLayoutControl().setTool("layoutselect");
290

    
291
            LayoutManager lm = (LayoutManager) layoutDocument.getFactory();
292
            layout.setLayoutManager(lm);
293

    
294
            PluginServices.getMDIManager().addWindow(layout);
295

    
296
        }
297
    }
298

    
299
    private void registerDocuments() {
300
        DefaultLayoutManager.register();
301
    }
302

    
303
    private void registerFFrames() {
304
        DefaultLayoutManager layoutManager = (DefaultLayoutManager) ProjectManager
305
                .getInstance()
306
                .getDocumentManager(DefaultLayoutManager.TYPENAME);
307

    
308
        // Register Frames
309
        layoutManager.registerFrameFactory(new FFrameBasicFactory());
310
        layoutManager.registerFrameFactory(new FFrameGraphicsFactory());
311
        layoutManager.registerFrameFactory(new FFrameGroupFactory());
312
        layoutManager.registerFrameFactory(new FFrameLegendFactory());
313
        layoutManager.registerFrameFactory(new FFrameNorthFactory());
314
        layoutManager.registerFrameFactory(new FFrameOverViewFactory());
315
        layoutManager.registerFrameFactory(new FFramePictureFactory());
316
        layoutManager.registerFrameFactory(new FFrameScaleBarFactory());
317
        layoutManager.registerFrameFactory(new FFrameSymbolFactory());
318
        layoutManager.registerFrameFactory(new FFrameTableFactory());
319
        layoutManager.registerFrameFactory(new FFrameTextFactory());
320
        layoutManager.registerFrameFactory(new FFrameViewFactory());
321
        layoutManager.registerFrameFactory(new FFrameGridFactory());
322

    
323
        // Register FFrameDialogs forms
324
        layoutManager.registerFFrameDialog(
325
                FFrameGraphics.PERSISTENCE_DEFINITION_NAME,
326
                FFrameGraphicsDialog.class);
327
        layoutManager.registerFFrameDialog(
328
                FFrameSymbol.PERSISTENCE_DEFINITION_NAME,
329
                FFrameGraphicsDialog.class);
330
        layoutManager.registerFFrameDialog(
331
                FFrameGroup.PERSISTENCE_DEFINITION_NAME,
332
                FFrameGroupDialog.class);
333
        layoutManager.registerFFrameDialog(
334
                FFrameTable.PERSISTENCE_DEFINITION_NAME, FFrameBoxDialog.class);
335
        layoutManager.registerFFrameDialog(
336
                FFrameLegend.PERSISTENCE_DEFINITION_NAME,
337
                FFrameLegendDialog.class);
338
        layoutManager.registerFFrameDialog(
339
                FFramePicture.PERSISTENCE_DEFINITION_NAME,
340
                FFramePictureDialog.class);
341
        layoutManager.registerFFrameDialog(
342
                FFrameNorth.PERSISTENCE_DEFINITION_NAME,
343
                FFrameNorthDialog.class);
344
        layoutManager.registerFFrameDialog(
345
                FFrameScaleBar.PERSISTENCE_DEFINITION_NAME,
346
                FFrameScaleBarDialog.class);
347
        layoutManager.registerFFrameDialog(
348
                FFrameText.PERSISTENCE_DEFINITION_NAME, FFrameTextDialog.class);
349
        layoutManager.registerFFrameDialog(
350
                FFrameView.PERSISTENCE_DEFINITION_NAME, FFrameViewDialog.class);
351
        layoutManager.registerFFrameDialog(
352
                FFrameOverView.PERSISTENCE_DEFINITION_NAME,
353
                FFrameOverViewDialog.class);
354
        layoutManager.registerFFrameDialog(
355
                FFrameGrid.PERSISTENCE_DEFINITION_NAME, FFrameGridDialog.class);
356

    
357
        MapBoxUtils.register(layoutManager);
358
        
359
        FFrame.initializeIcons();
360
        FFrameTable.initializeIcons();
361
    }
362

    
363
    private void registerContextMenuOptions() {
364
        LayoutManager layoutManager = (LayoutManager) ProjectManager
365
                .getInstance().getDocumentManager("project.document.layout");
366

    
367
        layoutManager.registerLayoutMenuAction("Terminate",
368
                TerminateLayoutMenuEntry.class);
369
        layoutManager.registerLayoutMenuAction("Cancel",
370
                CancelLayoutMenuEntry.class);
371
        layoutManager.registerLayoutMenuAction("Copy",
372
                CopyLayoutMenuEntry.class);
373
        layoutManager.registerLayoutMenuAction("Cut", CutLayoutMenuEntry.class);
374
        layoutManager.registerLayoutMenuAction("Paste",
375
                PasteLayoutMenuEntry.class);
376
        layoutManager.registerLayoutMenuAction("Simplify",
377
                SimplifyLayoutMenuEntry.class);
378
        layoutManager.registerLayoutMenuAction("Property",
379
                PropertyLayoutMenuEntry.class);
380
        layoutManager.registerLayoutMenuAction("SelectAll",
381
                SelectAllLayoutMenuEntry.class);
382
        layoutManager.registerLayoutMenuAction("Behind",
383
                BehindLayoutMenuEntry.class);
384
        layoutManager.registerLayoutMenuAction("Before",
385
                BeforeLayoutMenuEntry.class);
386
        layoutManager.registerLayoutMenuAction("Position",
387
                PositionLayoutMenuEntry.class);
388
        layoutManager.registerLayoutMenuAction("Refresh",
389
                RefreshLayoutMenuEntry.class);
390
        layoutManager.registerLayoutMenuAction("CombineCells",
391
                CombineCellsMapBoxMenuEntry.class);
392
        layoutManager.registerLayoutMenuAction("SplitCellsVertically",
393
                SplitCellsVerticallyMapBoxMenuEntry.class);
394
        layoutManager.registerLayoutMenuAction("SplitCellsHorizontally",
395
                SplitCellsHorizontallyMapBoxMenuEntry.class);
396
        layoutManager.registerLayoutMenuAction("InsertColumnMapBoxMenuEntry",
397
                InsertColumnMapBoxMenuEntry.class);
398
        layoutManager.registerLayoutMenuAction("AddColumnMapBoxMenuEntry",
399
                AddColumnMapBoxMenuEntry.class);
400
        layoutManager.registerLayoutMenuAction("InsertRowMapBoxMenuEntry",
401
                InsertRowMapBoxMenuEntry.class);
402
        layoutManager.registerLayoutMenuAction("AddRowMapBoxMenuEntry",
403
                AddRowMapBoxMenuEntry.class);
404
        layoutManager.registerLayoutMenuAction("RemoveColumnMapBoxMenuEntry",
405
                RemoveColumnMapBoxMenuEntry.class);
406
        layoutManager.registerLayoutMenuAction("RemoveRowMapBoxMenuEntry",
407
                RemoveRowMapBoxMenuEntry.class);
408
        layoutManager.registerLayoutMenuAction("RemoveCellsMapBoxMenuEntry",
409
                RemoveCellsMapBoxMenuEntry.class);
410

    
411
    }
412

    
413
    private void initializeDocumentActionsExtensionPoint() {
414
        ExtensionPointManager epMan = ToolsLocator.getExtensionPointManager();
415
        epMan.add(
416
                "DocumentActions_Map",
417
                "Context menu options of the map document list"
418
                        + " in the project window "
419
                        + "(register instances of "
420
                        + "org.gvsig.app.project.AbstractDocumentContextMenuAction)");
421
    }
422

    
423
    @Override
424
    public boolean isEnabled() {
425
        return true;
426
    }
427

    
428
    @Override
429
    public boolean isVisible() {
430
        return true;
431
    }
432
}