Statistics
| Revision:

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

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

    
103
/**
104
 * Extension que proporciona controles para crear proyectos nuevos, abrirlos y
105
 * guardarlos. Adem?s los tipos de tabla que soporta el proyecto son a?adidos
106
 * en esta clase.
107
 * 
108
 * @author Fernando Gonz?lez Cort?s
109
 */
110
public class LayoutMainExtension extends Extension {
111

    
112
    private String templatesPath;
113
    public static final String LAYOUT_TEMPLATE_FILECHOOSER_ID =
114
        "LAYOUT_TEMPLATE_FILECHOOSER_ID";
115

    
116
    private void registerIcons() {
117
        
118
        IconThemeHelper.registerIcon("action",
119
            "application-layout-template-open", this);
120
    }
121

    
122
    public void initialize() {
123
        registerIcons();
124
    }
125

    
126
    public void postInitialize() {
127
        super.postInitialize();
128
        initializeDocumentActionsExtensionPoint();
129
        registerDocuments();
130
        registerFFrames();
131
        registerContextMenuOptions();
132
    }
133

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

    
143
    private void openLayout() {
144
        // Project project = ((ProjectExtension)
145
        // PluginServices.getExtension(ProjectExtension.class)).getProject();
146
        LayoutPanel layout = null;
147

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

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

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

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

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

    
190
            } catch (Exception e) {
191
                
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
                JOptionPane.showMessageDialog(
204
                    ApplicationLocator.getManager().getRootComponent(),
205
                    msg, tit, JOptionPane.ERROR_MESSAGE);
206
            }
207
            
208
            if (layout == null) {
209
                return;
210
            }
211

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

    
229
        }
230
    }
231

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

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

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

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

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