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

History | View | Annotate | Download (15 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.io.File;
26
import java.io.FileInputStream;
27
import java.io.FileNotFoundException;
28
import java.io.IOException;
29
import java.text.MessageFormat;
30
import java.util.prefs.Preferences;
31

    
32
import javax.swing.JOptionPane;
33

    
34
import org.gvsig.andami.IconThemeHelper;
35
import org.gvsig.andami.PluginServices;
36
import org.gvsig.andami.messages.NotificationManager;
37
import org.gvsig.andami.plugins.Extension;
38
import org.gvsig.app.ApplicationLocator;
39
import org.gvsig.app.project.Project;
40
import org.gvsig.app.project.ProjectManager;
41
import org.gvsig.app.project.documents.layout.DefaultLayoutManager;
42
import org.gvsig.app.project.documents.layout.LayoutDocument;
43
import org.gvsig.app.project.documents.layout.LayoutManager;
44
import org.gvsig.app.project.documents.layout.contextmenu.gui.BeforeLayoutMenuEntry;
45
import org.gvsig.app.project.documents.layout.contextmenu.gui.BehindLayoutMenuEntry;
46
import org.gvsig.app.project.documents.layout.contextmenu.gui.CancelLayoutMenuEntry;
47
import org.gvsig.app.project.documents.layout.contextmenu.gui.CopyLayoutMenuEntry;
48
import org.gvsig.app.project.documents.layout.contextmenu.gui.CutLayoutMenuEntry;
49
import org.gvsig.app.project.documents.layout.contextmenu.gui.PasteLayoutMenuEntry;
50
import org.gvsig.app.project.documents.layout.contextmenu.gui.PositionLayoutMenuEntry;
51
import org.gvsig.app.project.documents.layout.contextmenu.gui.PropertyLayoutMenuEntry;
52
import org.gvsig.app.project.documents.layout.contextmenu.gui.RefreshLayoutMenuEntry;
53
import org.gvsig.app.project.documents.layout.contextmenu.gui.SelectAllLayoutMenuEntry;
54
import org.gvsig.app.project.documents.layout.contextmenu.gui.SimplifyLayoutMenuEntry;
55
import org.gvsig.app.project.documents.layout.contextmenu.gui.TerminateLayoutMenuEntry;
56
import org.gvsig.app.project.documents.layout.fframes.FFrame;
57
import org.gvsig.app.project.documents.layout.fframes.FFrameBasicFactory;
58
import org.gvsig.app.project.documents.layout.fframes.FFrameGraphics;
59
import org.gvsig.app.project.documents.layout.fframes.FFrameGraphicsFactory;
60
import org.gvsig.app.project.documents.layout.fframes.FFrameGrid;
61
import org.gvsig.app.project.documents.layout.fframes.FFrameGridFactory;
62
import org.gvsig.app.project.documents.layout.fframes.FFrameGroup;
63
import org.gvsig.app.project.documents.layout.fframes.FFrameGroupFactory;
64
import org.gvsig.app.project.documents.layout.fframes.FFrameLegend;
65
import org.gvsig.app.project.documents.layout.fframes.FFrameLegendFactory;
66
import org.gvsig.app.project.documents.layout.fframes.FFrameNorth;
67
import org.gvsig.app.project.documents.layout.fframes.FFrameNorthFactory;
68
import org.gvsig.app.project.documents.layout.fframes.FFrameOverView;
69
import org.gvsig.app.project.documents.layout.fframes.FFrameOverViewFactory;
70
import org.gvsig.app.project.documents.layout.fframes.FFramePicture;
71
import org.gvsig.app.project.documents.layout.fframes.FFramePictureFactory;
72
import org.gvsig.app.project.documents.layout.fframes.FFrameScaleBar;
73
import org.gvsig.app.project.documents.layout.fframes.FFrameScaleBarFactory;
74
import org.gvsig.app.project.documents.layout.fframes.FFrameSymbol;
75
import org.gvsig.app.project.documents.layout.fframes.FFrameSymbolFactory;
76
import org.gvsig.app.project.documents.layout.fframes.FFrameTable;
77
import org.gvsig.app.project.documents.layout.fframes.FFrameTableFactory;
78
import org.gvsig.app.project.documents.layout.fframes.FFrameText;
79
import org.gvsig.app.project.documents.layout.fframes.FFrameTextFactory;
80
import org.gvsig.app.project.documents.layout.fframes.FFrameView;
81
import org.gvsig.app.project.documents.layout.fframes.FFrameViewFactory;
82
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameBoxDialog;
83
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameGraphicsDialog;
84
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameGridDialog;
85
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameGroupDialog;
86
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameLegendDialog;
87
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameNorthDialog;
88
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameOverViewDialog;
89
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFramePictureDialog;
90
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameScaleBarDialog;
91
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameTextDialog;
92
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameViewDialog;
93
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
94
import org.gvsig.gui.beans.swing.JFileChooser;
95
import org.gvsig.i18n.Messages;
96
import org.gvsig.tools.ToolsLocator;
97
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
98
import org.gvsig.tools.persistence.PersistenceManager;
99
import org.gvsig.tools.persistence.PersistentState;
100
import org.gvsig.tools.persistence.exception.PersistenceException;
101
import org.gvsig.utils.GenericFileFilter;
102
import org.slf4j.Logger;
103
import org.slf4j.LoggerFactory;
104

    
105
/**
106
 * Extension que proporciona controles para crear proyectos nuevos, abrirlos y
107
 * guardarlos. Adem?s los tipos de tabla que soporta el proyecto son a?adidos
108
 * en esta clase.
109
 * 
110
 * @author Fernando Gonz?lez Cort?s
111
 */
112
public class LayoutMainExtension extends Extension {
113
    private static final Logger logger = LoggerFactory
114
            .getLogger(FFrameView.class);
115
    private String templatesPath;
116
    public static final String LAYOUT_TEMPLATE_FILECHOOSER_ID =
117
        "LAYOUT_TEMPLATE_FILECHOOSER_ID";
118

    
119
    private void registerIcons() {
120
        
121
        IconThemeHelper.registerIcon("action",
122
            "application-layout-template-open", this);
123
    }
124

    
125
    public void initialize() {
126
        registerIcons();
127
    }
128

    
129
    public void postInitialize() {
130
        super.postInitialize();
131
        initializeDocumentActionsExtensionPoint();
132
        registerDocuments();
133
        registerFFrames();
134
        registerContextMenuOptions();
135
    }
136

    
137
    /**
138
     * @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
139
     */
140
    public void execute(String actionCommand) {
141
        if ("application-layout-template-open".equals(actionCommand)) {
142
            openLayout();
143
        }
144
    }
145

    
146
    private void openLayout() {
147
        LayoutPanel layout = null;
148

    
149
        if (templatesPath == null) {
150
            Preferences prefs = Preferences.userRoot().node("gvsig.foldering");
151
            templatesPath = prefs.get("TemplatesFolder", null);
152
        }
153

    
154
        JFileChooser jfc =
155
            new JFileChooser(LAYOUT_TEMPLATE_FILECHOOSER_ID, templatesPath);
156
        jfc.addChoosableFileFilter(new GenericFileFilter(
157
            LayoutManager.TEMPLATE_FILE_POINTEXT,
158
            Messages.getText("_Layout_template") + " (*"
159
            + LayoutManager.TEMPLATE_FILE_POINTEXT + ")"
160
            ));
161

    
162
        if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
163
            File file = jfc.getSelectedFile();
164
            try {
165
                if (!file.exists()) {
166
                    /*
167
                     * The user has written a file name instead of clicking on one
168
                     * and it does not exist
169
                     */
170
                    File auxfile = new File(file.getPath() + LayoutManager.TEMPLATE_FILE_POINTEXT);
171
                    if (!auxfile.exists()) {
172
                        /*
173
                         * Not even this one exists: error
174
                         */
175
                        throw new IOException("File does not exist: " + file.getName());
176
                    } else {
177
                        file = auxfile;
178
                    }
179
                }
180

    
181
                File xmlFile = new File(file.getAbsolutePath());
182
                FileInputStream is = new FileInputStream(xmlFile);
183

    
184
                PersistenceManager persistenceManager =
185
                    ToolsLocator.getPersistenceManager();
186
                PersistentState persistentState =
187
                    persistenceManager.loadState(is);
188
                layout =
189
                    (LayoutPanel) persistenceManager.create(persistentState);
190

    
191
            } catch (Exception e) {
192
                String tit = Messages.getText("_Layout_template");
193
                String msg = null;
194
                
195
                if (e instanceof PersistenceException) {
196
                    msg = Messages.getText("_Bad_format_in_layout_template");
197
                } else {
198
                    msg = Messages.getText("_Cant_load_layout_template");
199
                }
200
                
201
                msg = msg + ": " + file.getPath();
202
                msg = msg + "\n" + e.getMessage();
203
                logger.warn(msg, e);
204
                JOptionPane.showMessageDialog(
205
                    ApplicationLocator.getManager().getRootComponent(),
206
                    msg, tit, JOptionPane.ERROR_MESSAGE);
207
            }
208
            
209
            if (layout == null) {
210
                return;
211
            }
212

    
213
            LayoutDocument layoutDocument = (LayoutDocument) layout.getDocument();
214
            /*
215
                (LayoutDocument) ProjectManager.getInstance().createDocument(
216
                    DefaultLayoutManager.TYPENAME, file.getName());
217
                    */
218
                
219
            Project p = ApplicationLocator.getManager().getProjectManager()
220
                    .getCurrentProject();
221
            p.addDocument(layoutDocument);
222
            
223
            layout.getLayoutControl().setTool("layoutselect");
224
            
225
            LayoutManager lm = (LayoutManager) layoutDocument.getFactory();
226
            layout.setLayoutManager(lm);
227
            
228
            PluginServices.getMDIManager().addWindow(layout);
229

    
230
        }
231
    }
232

    
233
    private void registerDocuments() {
234
        DefaultLayoutManager.register();
235
    }
236
    
237
    private void registerFFrames() {
238
        DefaultLayoutManager layoutManager =
239
            (DefaultLayoutManager) ProjectManager.getInstance()
240
                .getDocumentManager(DefaultLayoutManager.TYPENAME);
241
        
242
        // Register Frames
243
        layoutManager.registerFrameFactory(new FFrameBasicFactory());
244
        layoutManager.registerFrameFactory(new FFrameGraphicsFactory());
245
        layoutManager.registerFrameFactory(new FFrameGroupFactory());
246
        layoutManager.registerFrameFactory(new FFrameLegendFactory());
247
        layoutManager.registerFrameFactory(new FFrameNorthFactory());
248
        layoutManager.registerFrameFactory(new FFrameOverViewFactory());
249
        layoutManager.registerFrameFactory(new FFramePictureFactory());
250
        layoutManager.registerFrameFactory(new FFrameScaleBarFactory());
251
        layoutManager.registerFrameFactory(new FFrameSymbolFactory());
252
        layoutManager.registerFrameFactory(new FFrameTableFactory());
253
        layoutManager.registerFrameFactory(new FFrameTextFactory());
254
        layoutManager.registerFrameFactory(new FFrameViewFactory());
255
        layoutManager.registerFrameFactory(new FFrameGridFactory());
256

    
257
        // Register FFrameDialogs forms
258
        layoutManager.registerFFrameDialog(
259
            FFrameGraphics.PERSISTENCE_DEFINITION_NAME, FFrameGraphicsDialog.class);
260
        layoutManager.registerFFrameDialog(
261
            FFrameSymbol.PERSISTENCE_DEFINITION_NAME, FFrameGraphicsDialog.class);
262
        layoutManager.registerFFrameDialog(
263
            FFrameGroup.PERSISTENCE_DEFINITION_NAME, FFrameGroupDialog.class);
264
        layoutManager.registerFFrameDialog(
265
            FFrameTable.PERSISTENCE_DEFINITION_NAME, FFrameBoxDialog.class);
266
        layoutManager.registerFFrameDialog(
267
            FFrameLegend.PERSISTENCE_DEFINITION_NAME, FFrameLegendDialog.class);
268
        layoutManager.registerFFrameDialog(
269
            FFramePicture.PERSISTENCE_DEFINITION_NAME, FFramePictureDialog.class);
270
        layoutManager.registerFFrameDialog(
271
            FFrameNorth.PERSISTENCE_DEFINITION_NAME, FFrameNorthDialog.class);
272
        layoutManager.registerFFrameDialog(
273
            FFrameScaleBar.PERSISTENCE_DEFINITION_NAME, FFrameScaleBarDialog.class);
274
        layoutManager.registerFFrameDialog(
275
            FFrameText.PERSISTENCE_DEFINITION_NAME, FFrameTextDialog.class);
276
        layoutManager.registerFFrameDialog(
277
            FFrameView.PERSISTENCE_DEFINITION_NAME, FFrameViewDialog.class);
278
        layoutManager.registerFFrameDialog(
279
            FFrameOverView.PERSISTENCE_DEFINITION_NAME, FFrameOverViewDialog.class);
280
        layoutManager.registerFFrameDialog(
281
            FFrameGrid.PERSISTENCE_DEFINITION_NAME, FFrameGridDialog.class);
282
        
283
        FFrame.initializeIcons();
284
        FFrameTable.initializeIcons();
285
    }
286
        
287
    private void registerContextMenuOptions() {
288
        LayoutManager layoutManager =
289
            (LayoutManager) ProjectManager.getInstance()
290
                .getDocumentManager("project.document.layout");
291
        
292
        layoutManager.registerLayoutMenuAction("Terminate", TerminateLayoutMenuEntry.class);
293
        layoutManager.registerLayoutMenuAction("Cancel", CancelLayoutMenuEntry.class);
294
        layoutManager.registerLayoutMenuAction("Copy", CopyLayoutMenuEntry.class);
295
        layoutManager.registerLayoutMenuAction("Cut", CutLayoutMenuEntry.class);
296
        layoutManager.registerLayoutMenuAction("Paste", PasteLayoutMenuEntry.class);
297
        layoutManager.registerLayoutMenuAction("Simplify", SimplifyLayoutMenuEntry.class);
298
        layoutManager.registerLayoutMenuAction("Property",PropertyLayoutMenuEntry.class);
299
        layoutManager.registerLayoutMenuAction("SelectAll", SelectAllLayoutMenuEntry.class);
300
        layoutManager.registerLayoutMenuAction("Behind", BehindLayoutMenuEntry.class);
301
        layoutManager.registerLayoutMenuAction("Before", BeforeLayoutMenuEntry.class);
302
        layoutManager.registerLayoutMenuAction("Position", PositionLayoutMenuEntry.class);
303
        layoutManager.registerLayoutMenuAction("Refresh", RefreshLayoutMenuEntry.class);
304
        
305
    }
306

    
307
    private void initializeDocumentActionsExtensionPoint() {
308
        ExtensionPointManager epMan = ToolsLocator.getExtensionPointManager();
309
        epMan.add("DocumentActions_Map",
310
            "Context menu options of the map document list"
311
                + " in the project window " + "(register instances of "
312
                + "org.gvsig.app.project.AbstractDocumentContextMenuAction)");
313
    }
314

    
315
    public boolean isEnabled() {
316
        return true;
317
    }
318

    
319
    public boolean isVisible() {
320
        return true;
321
    }
322
}