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

History | View | Annotate | Download (17.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.tools.ToolsLocator;
100
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
101
import org.gvsig.tools.i18n.I18nManager;
102
import org.gvsig.tools.persistence.PersistenceManager;
103
import org.gvsig.tools.persistence.PersistentState;
104
import org.gvsig.tools.persistence.exception.PersistenceException;
105
import org.gvsig.tools.util.ToolsUtilLocator;
106
import org.gvsig.utils.GenericFileFilter;
107
import org.slf4j.Logger;
108
import org.slf4j.LoggerFactory;
109

    
110
/**
111
 * Extension que proporciona controles para crear proyectos nuevos, abrirlos y
112
 * guardarlos. Adem?s los tipos de tabla que soporta el proyecto son a?adidos en
113
 * esta clase.
114
 * 
115
 * @author gvSIG team
116
 */
117
public class LayoutMainExtension extends Extension {
118

    
119
    private static final Logger logger = LoggerFactory
120
            .getLogger(FFrameView.class);
121

    
122
    public static final String LAYOUT_TEMPLATE_FILECHOOSER_ID = "LAYOUT_TEMPLATE_FILECHOOSER_ID";
123

    
124
    private void registerIcons() {
125

    
126
        IconThemeHelper.registerIcon("action",
127
                "application-layout-template-open", this);
128
        IconThemeHelper.registerIcon("action",
129
                "application-layout-new", this);
130
        
131
    }
132

    
133
    @Override
134
    public void initialize() {
135
        registerIcons();
136
    }
137

    
138
    @Override
139
    public void postInitialize() {
140
        super.postInitialize();
141
        initializeDocumentActionsExtensionPoint();
142
        registerDocuments();
143
        registerFFrames();
144
        registerContextMenuOptions();
145
    }
146

    
147

    
148
    @Override
149
    public void execute(String actionCommand) {
150
        if ("application-layout-template-open".equals(actionCommand)) {
151
            openLayout();
152
        } else if ("application-layout-new".equals(actionCommand)) { 
153
            newLayout();   
154
        }
155
    }
156

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

    
195
    private void openLayout() {
196
        LayoutPanel layout = null;
197

    
198
        I18nManager i18n = ToolsLocator.getI18nManager();
199
        FileDialogChooserManager manager = ToolsUtilLocator.getFileDialogChooserManager();
200
        FileDialogChooser fc = manager.create("_Last_layout_template_folder");
201
        GenericFileFilter layoutMainFilter = new GenericFileFilter(
202
        LayoutManager.TEMPLATE_FILE_POINTEXT,
203
        i18n.getTranslation("_Layout_template") + " (*"
204
                + LayoutManager.TEMPLATE_FILE_POINTEXT + ")");
205

    
206
        fc.addChoosableFileFilter(layoutMainFilter);
207
        fc.setFileFilter(layoutMainFilter);
208
        ApplicationManager application = ApplicationLocator.getApplicationManager();
209
        
210
        File projectfile = application.getCurrentProject().getFile();
211

    
212
        JPanel accesory = new LayoutExtension.AccesoryPanelForTemplates(projectfile, fc);
213
        fc.setAccessory(accesory);
214

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

    
237
                File xmlFile = new File(file.getAbsolutePath());
238
                FileInputStream is = new FileInputStream(xmlFile);
239

    
240
                PersistenceManager persistenceManager = ToolsLocator
241
                        .getPersistenceManager();
242
                PersistentState persistentState = persistenceManager
243
                        .loadState(is);
244
                layout = (LayoutPanel) persistenceManager
245
                        .create(persistentState);
246

    
247
            }
248
            catch (Exception e) {
249
                String tit = Messages.getText("_Layout_template");
250
                String msg;
251

    
252
                if (e instanceof PersistenceException) {
253
                    msg = Messages.getText("_Bad_format_in_layout_template");
254
                }
255
                else {
256
                    msg = Messages.getText("_Cant_load_layout_template");
257
                }
258

    
259
                msg = msg + ": " + file.getPath();
260
                msg = msg + "\n" + e.getMessage();
261
                logger.warn(msg, e);
262
                JOptionPane.showMessageDialog(ApplicationLocator.getManager()
263
                        .getRootComponent(), msg, tit,
264
                        JOptionPane.ERROR_MESSAGE);
265
            }
266

    
267
            if (layout == null) {
268
                return;
269
            }
270

    
271
            LayoutDocument layoutDocument = (LayoutDocument) layout
272
                    .getDocument();
273
            /*
274
             * (LayoutDocument) ProjectManager.getInstance().createDocument(
275
             * DefaultLayoutManager.TYPENAME, file.getName());
276
             */
277

    
278
            Project p = ApplicationLocator.getManager().getProjectManager()
279
                    .getCurrentProject();
280
            p.addDocument(layoutDocument);
281

    
282
            layout.getLayoutControl().setTool("layoutselect");
283

    
284
            LayoutManager lm = (LayoutManager) layoutDocument.getFactory();
285
            layout.setLayoutManager(lm);
286

    
287
            PluginServices.getMDIManager().addWindow(layout);
288

    
289
        }
290
    }
291

    
292
    private void registerDocuments() {
293
        DefaultLayoutManager.register();
294
    }
295

    
296
    private void registerFFrames() {
297
        DefaultLayoutManager layoutManager = (DefaultLayoutManager) ProjectManager
298
                .getInstance()
299
                .getDocumentManager(DefaultLayoutManager.TYPENAME);
300

    
301
        // Register Frames
302
        layoutManager.registerFrameFactory(new FFrameBasicFactory());
303
        layoutManager.registerFrameFactory(new FFrameGraphicsFactory());
304
        layoutManager.registerFrameFactory(new FFrameGroupFactory());
305
        layoutManager.registerFrameFactory(new FFrameLegendFactory());
306
        layoutManager.registerFrameFactory(new FFrameNorthFactory());
307
        layoutManager.registerFrameFactory(new FFrameOverViewFactory());
308
        layoutManager.registerFrameFactory(new FFramePictureFactory());
309
        layoutManager.registerFrameFactory(new FFrameScaleBarFactory());
310
        layoutManager.registerFrameFactory(new FFrameSymbolFactory());
311
        layoutManager.registerFrameFactory(new FFrameTableFactory());
312
        layoutManager.registerFrameFactory(new FFrameTextFactory());
313
        layoutManager.registerFrameFactory(new FFrameViewFactory());
314
        layoutManager.registerFrameFactory(new FFrameGridFactory());
315

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

    
350
        FFrame.initializeIcons();
351
        FFrameTable.initializeIcons();
352
    }
353

    
354
    private void registerContextMenuOptions() {
355
        LayoutManager layoutManager = (LayoutManager) ProjectManager
356
                .getInstance().getDocumentManager("project.document.layout");
357

    
358
        layoutManager.registerLayoutMenuAction("Terminate",
359
                TerminateLayoutMenuEntry.class);
360
        layoutManager.registerLayoutMenuAction("Cancel",
361
                CancelLayoutMenuEntry.class);
362
        layoutManager.registerLayoutMenuAction("Copy",
363
                CopyLayoutMenuEntry.class);
364
        layoutManager.registerLayoutMenuAction("Cut", CutLayoutMenuEntry.class);
365
        layoutManager.registerLayoutMenuAction("Paste",
366
                PasteLayoutMenuEntry.class);
367
        layoutManager.registerLayoutMenuAction("Simplify",
368
                SimplifyLayoutMenuEntry.class);
369
        layoutManager.registerLayoutMenuAction("Property",
370
                PropertyLayoutMenuEntry.class);
371
        layoutManager.registerLayoutMenuAction("SelectAll",
372
                SelectAllLayoutMenuEntry.class);
373
        layoutManager.registerLayoutMenuAction("Behind",
374
                BehindLayoutMenuEntry.class);
375
        layoutManager.registerLayoutMenuAction("Before",
376
                BeforeLayoutMenuEntry.class);
377
        layoutManager.registerLayoutMenuAction("Position",
378
                PositionLayoutMenuEntry.class);
379
        layoutManager.registerLayoutMenuAction("Refresh",
380
                RefreshLayoutMenuEntry.class);
381

    
382
    }
383

    
384
    private void initializeDocumentActionsExtensionPoint() {
385
        ExtensionPointManager epMan = ToolsLocator.getExtensionPointManager();
386
        epMan.add(
387
                "DocumentActions_Map",
388
                "Context menu options of the map document list"
389
                        + " in the project window "
390
                        + "(register instances of "
391
                        + "org.gvsig.app.project.AbstractDocumentContextMenuAction)");
392
    }
393

    
394
    @Override
395
    public boolean isEnabled() {
396
        return true;
397
    }
398

    
399
    @Override
400
    public boolean isVisible() {
401
        return true;
402
    }
403
}