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

History | View | Annotate | Download (19.8 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 javax.swing.JOptionPane;
31
import javax.swing.JPanel;
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.filedialogchooser.FileDialogChooser;
96
import org.gvsig.filedialogchooser.FileDialogChooserManager;
97
import org.gvsig.gui.beans.swing.JFileChooser;
98
import org.gvsig.i18n.Messages;
99
import org.gvsig.layout.mapbox.MapBoxUtils;
100
import org.gvsig.layout.mapbox.panel.menu.AddColumnMapBoxMenuEntry;
101
import org.gvsig.layout.mapbox.panel.menu.AddRowMapBoxMenuEntry;
102
import org.gvsig.layout.mapbox.panel.menu.CombineCellsMapBoxMenuEntry;
103
import org.gvsig.layout.mapbox.panel.menu.InsertColumnMapBoxMenuEntry;
104
import org.gvsig.layout.mapbox.panel.menu.InsertRowMapBoxMenuEntry;
105
import org.gvsig.layout.mapbox.panel.menu.RemoveCellsMapBoxMenuEntry;
106
import org.gvsig.layout.mapbox.panel.menu.RemoveColumnMapBoxMenuEntry;
107
import org.gvsig.layout.mapbox.panel.menu.RemoveRowMapBoxMenuEntry;
108
import org.gvsig.layout.mapbox.panel.menu.SplitCellsHorizontallyMapBoxMenuEntry;
109
import org.gvsig.layout.mapbox.panel.menu.SplitCellsVerticallyMapBoxMenuEntry;
110
import org.gvsig.tools.ToolsLocator;
111
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
112
import org.gvsig.tools.i18n.I18nManager;
113
import org.gvsig.tools.persistence.PersistenceManager;
114
import org.gvsig.tools.persistence.PersistentState;
115
import org.gvsig.tools.persistence.exception.PersistenceException;
116
import org.gvsig.tools.util.ToolsUtilLocator;
117
import org.gvsig.utils.GenericFileFilter;
118
import org.slf4j.Logger;
119
import org.slf4j.LoggerFactory;
120

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

    
130
    private static final Logger logger = LoggerFactory
131
            .getLogger(FFrameView.class);
132

    
133
    private String templatesPath;
134

    
135
    public static final String LAYOUT_TEMPLATE_FILECHOOSER_ID = "LAYOUT_TEMPLATE_FILECHOOSER_ID";
136

    
137
    private void registerIcons() {
138

    
139
        IconThemeHelper.registerIcon("action",
140
                "application-layout-template-open", this);
141
        IconThemeHelper.registerIcon("action",
142
                "application-layout-new", this);
143
        
144
    }
145

    
146
    @Override
147
    public void initialize() {
148
        registerIcons();
149
    }
150

    
151
    @Override
152
    public void postInitialize() {
153
        super.postInitialize();
154
        initializeDocumentActionsExtensionPoint();
155
        registerDocuments();
156
        registerFFrames();
157
        registerContextMenuOptions();
158
    }
159

    
160

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

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

    
208
    private void openLayout() {
209
        LayoutPanel layout = null;
210

    
211
        I18nManager i18n = ToolsLocator.getI18nManager();
212
        FileDialogChooserManager manager = ToolsUtilLocator.getFileDialogChooserManager();
213
        FileDialogChooser fc = manager.create("_Last_layout_template_folder");
214
        GenericFileFilter layoutMainFilter = new GenericFileFilter(
215
        LayoutManager.TEMPLATE_FILE_POINTEXT,
216
        i18n.getTranslation("_Layout_template") + " (*"
217
                + LayoutManager.TEMPLATE_FILE_POINTEXT + ")");
218

    
219
        fc.addChoosableFileFilter(layoutMainFilter);
220
        fc.setFileFilter(layoutMainFilter);
221
        ApplicationManager application = ApplicationLocator.getApplicationManager();
222
        
223
        File projectfile = application.getCurrentProject().getFile();
224

    
225
        JPanel accesory = new LayoutExtension.AccesoryPanelForTemplates(projectfile, fc);
226
        fc.setAccessory(accesory);
227

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

    
250
                File xmlFile = new File(file.getAbsolutePath());
251
                FileInputStream is = new FileInputStream(xmlFile);
252

    
253
                PersistenceManager persistenceManager = ToolsLocator
254
                        .getPersistenceManager();
255
                PersistentState persistentState = persistenceManager
256
                        .loadState(is);
257
                layout = (LayoutPanel) persistenceManager
258
                        .create(persistentState);
259

    
260
            }
261
            catch (Exception e) {
262
                String tit = Messages.getText("_Layout_template");
263
                String msg;
264

    
265
                if (e instanceof PersistenceException) {
266
                    msg = Messages.getText("_Bad_format_in_layout_template");
267
                }
268
                else {
269
                    msg = Messages.getText("_Cant_load_layout_template");
270
                }
271

    
272
                msg = msg + ": " + file.getPath();
273
                msg = msg + "\n" + e.getMessage();
274
                logger.warn(msg, e);
275
                JOptionPane.showMessageDialog(ApplicationLocator.getManager()
276
                        .getRootComponent(), msg, tit,
277
                        JOptionPane.ERROR_MESSAGE);
278
            }
279

    
280
            if (layout == null) {
281
                return;
282
            }
283

    
284
            LayoutDocument layoutDocument = (LayoutDocument) layout
285
                    .getDocument();
286
            /*
287
             * (LayoutDocument) ProjectManager.getInstance().createDocument(
288
             * DefaultLayoutManager.TYPENAME, file.getName());
289
             */
290

    
291
            Project p = ApplicationLocator.getManager().getProjectManager()
292
                    .getCurrentProject();
293
            p.addDocument(layoutDocument);
294

    
295
            layout.getLayoutControl().setTool("layoutselect");
296

    
297
            LayoutManager lm = (LayoutManager) layoutDocument.getFactory();
298
            layout.setLayoutManager(lm);
299

    
300
            PluginServices.getMDIManager().addWindow(layout);
301

    
302
        }
303
    }
304

    
305
    private void registerDocuments() {
306
        DefaultLayoutManager.register();
307
    }
308

    
309
    private void registerFFrames() {
310
        DefaultLayoutManager layoutManager = (DefaultLayoutManager) ProjectManager
311
                .getInstance()
312
                .getDocumentManager(DefaultLayoutManager.TYPENAME);
313

    
314
        // Register Frames
315
        layoutManager.registerFrameFactory(new FFrameBasicFactory());
316
        layoutManager.registerFrameFactory(new FFrameGraphicsFactory());
317
        layoutManager.registerFrameFactory(new FFrameGroupFactory());
318
        layoutManager.registerFrameFactory(new FFrameLegendFactory());
319
        layoutManager.registerFrameFactory(new FFrameNorthFactory());
320
        layoutManager.registerFrameFactory(new FFrameOverViewFactory());
321
        layoutManager.registerFrameFactory(new FFramePictureFactory());
322
        layoutManager.registerFrameFactory(new FFrameScaleBarFactory());
323
        layoutManager.registerFrameFactory(new FFrameSymbolFactory());
324
        layoutManager.registerFrameFactory(new FFrameTableFactory());
325
        layoutManager.registerFrameFactory(new FFrameTextFactory());
326
        layoutManager.registerFrameFactory(new FFrameViewFactory());
327
        layoutManager.registerFrameFactory(new FFrameGridFactory());
328

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

    
363
        MapBoxUtils.register(layoutManager);
364
        
365
        FFrame.initializeIcons();
366
        FFrameTable.initializeIcons();
367
    }
368

    
369
    private void registerContextMenuOptions() {
370
        LayoutManager layoutManager = (LayoutManager) ProjectManager
371
                .getInstance().getDocumentManager("project.document.layout");
372

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

    
417
    }
418

    
419
    private void initializeDocumentActionsExtensionPoint() {
420
        ExtensionPointManager epMan = ToolsLocator.getExtensionPointManager();
421
        epMan.add(
422
                "DocumentActions_Map",
423
                "Context menu options of the map document list"
424
                        + " in the project window "
425
                        + "(register instances of "
426
                        + "org.gvsig.app.project.AbstractDocumentContextMenuAction)");
427
    }
428

    
429
    @Override
430
    public boolean isEnabled() {
431
        return true;
432
    }
433

    
434
    @Override
435
    public boolean isVisible() {
436
        return true;
437
    }
438
}