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 / project / documents / layout / DefaultLayoutManager.java @ 345

History | View | Annotate | Download (16.4 KB)

1 5 jldominguez
/* 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.project.documents.layout;
23
24
import java.awt.geom.AffineTransform;
25
import java.lang.reflect.Array;
26
import java.text.NumberFormat;
27
import java.util.ArrayList;
28
import java.util.Arrays;
29
import java.util.Comparator;
30
import java.util.Iterator;
31
import java.util.List;
32
33
import javax.swing.ImageIcon;
34
35
import org.gvsig.andami.PluginServices;
36 216 cmartinez
import org.gvsig.andami.PluginsLocator;
37 5 jldominguez
import org.gvsig.andami.messages.NotificationManager;
38
import org.gvsig.andami.ui.mdiManager.IWindow;
39 216 cmartinez
import org.gvsig.app.ApplicationLocator;
40
import org.gvsig.app.gui.preferencespage.PreferenceKeys;
41 5 jldominguez
import org.gvsig.app.project.ProjectManager;
42
import org.gvsig.app.project.documents.AbstractDocument;
43
import org.gvsig.app.project.documents.AbstractDocumentManager;
44
import org.gvsig.app.project.documents.Document;
45 345 cmartinez
import org.gvsig.app.project.documents.actions.CopyDocumentAction;
46
import org.gvsig.app.project.documents.actions.CutDocumentAction;
47
import org.gvsig.app.project.documents.actions.PasteDocumentAction;
48 5 jldominguez
import org.gvsig.app.project.documents.gui.WindowLayout;
49
import org.gvsig.app.project.documents.layout.contextmenu.gui.AbstractLayoutContextMenuAction;
50
import org.gvsig.app.project.documents.layout.fframes.FrameFactory;
51
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
52
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.IFFrameDialog;
53
import org.gvsig.app.project.documents.layout.gui.DefaultLayoutPanel;
54
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
55
import org.gvsig.app.project.documents.layout.gui.MapProperties;
56 216 cmartinez
import org.gvsig.app.project.documents.layout.gui.dialogs.FConfigLayoutDialog;
57 5 jldominguez
import org.gvsig.app.project.documents.view.IContextMenuAction;
58
import org.gvsig.tools.ToolsLocator;
59
import org.gvsig.tools.dynobject.DynStruct;
60
import org.gvsig.tools.extensionpoint.ExtensionPoint;
61
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
62
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
63
import org.gvsig.tools.persistence.PersistenceManager;
64 216 cmartinez
import org.gvsig.tools.swing.api.ToolsSwingLocator;
65 5 jldominguez
import org.gvsig.utils.XMLEntity;
66 216 cmartinez
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
68 5 jldominguez
69
/**
70
 * Factory of maps.
71
 *
72
 * @author Vicente Caballero Navarro
73
 */
74
public class DefaultLayoutManager extends AbstractDocumentManager implements
75
    LayoutManager {
76
    static final Logger LOG = LoggerFactory
77
        .getLogger(DefaultLayoutManager.class);
78
79
    static final String KEY_LAYOUT_FFRAMEDIALOG =
80
        "app.project.documents.layout.fframes.gui";
81
    static final String KEY_LAYOUT_FFRAME =
82
        "app.project.documents.layout.fframes";
83
84 147 cmartinez
    public static final String PERSISTENCE_LAYOUT_DOCUMENT_DEFINITION_NAME =
85 5 jldominguez
        "LayoutDocument";
86
87
    private static final String LAYOUT_CONTEXT_MENUS = "Layout_ContextMenus";
88
89
    ExtensionPointManager extensionPoints = ToolsLocator
90
        .getExtensionPointManager();
91
92
    private Boolean defaultShowGrid = null;
93
    private Boolean defaultAdjustToGrid = null;
94
    private Boolean defaultShowRulers = null;
95
96
    private DynStruct persistenceDefinition;
97
98
    /**
99
     * Returns image of button.
100
     *
101
     * @return Image button.
102
     */
103
    public ImageIcon getIcon() {
104 216 cmartinez
            return ToolsSwingLocator.getIconThemeManager().getCurrent().get("document-map-icon");
105 5 jldominguez
    }
106
107
    /**
108
     * Returns image of selected button.
109
     *
110
     * @return Image button.
111
     */
112
    public ImageIcon getIconSelected() {
113 216 cmartinez
        return ToolsSwingLocator.getIconThemeManager().getCurrent().get("document-map-icon-sel");
114 5 jldominguez
    }
115
116
    /**
117
     * Returns the name of registration in the point of extension.
118
     *
119
     * @return Name of registration
120
     */
121
    public String getTypeName() {
122
        return TYPENAME;
123
    }
124
125
    /**
126
     * Returns the name of ProjectDocument.
127
     *
128
     * @return Name of ProjectDocument.
129
     */
130
    public String getTitle() {
131 262 cmartinez
        return PluginServices.getText(this, "Mapa");
132 5 jldominguez
    }
133
134
    public AbstractDocument createDocument() {
135
        return new DefaultLayoutDocument(this);
136
    }
137
138
    public Class getMainWindowClass() {
139
        return DefaultLayoutPanel.class;
140
    }
141
142
    public IWindow getMainWindow(Document doc, WindowLayout layout) {
143
        LayoutPanel layoutPanel;
144
        layoutPanel =
145
            (LayoutPanel) PluginServices.getMDIManager().getSingletonWindow(
146
                getMainWindowClass(), doc);
147
        if (layoutPanel != null) {
148 216 cmartinez
            // The layout window document is already created, return it.
149 5 jldominguez
            return layoutPanel;
150
        }
151
152
        layoutPanel = (LayoutPanel) this.createDocumentWindow(doc);
153
        if (layout != null) {
154
            layoutPanel.setWindowLayout(layout);
155
        }
156
        layoutPanel.setLayoutManager(this);
157
        layoutPanel.getLayoutControl().fullRect();
158
        layoutPanel.getWindowInfo().setTitle(
159
            PluginServices.getText(this, "Mapa") + " : "
160
                + layoutPanel.getName());
161
        ((AbstractDocument) doc).raiseEventCreateWindow(layoutPanel);
162
        return layoutPanel;
163
    }
164
165
    public IFFrameDialog createFFrameDialog(IFFrame fframe,
166
        LayoutPanel layoutPanel, AffineTransform affineTransform) {
167
        ExtensionPoint ep = extensionPoints.add(KEY_LAYOUT_FFRAMEDIALOG);
168
169
        try {
170
            Object[] args = new Object[2];
171
            args[0] = layoutPanel;
172
            args[1] = fframe;
173
            Object obj = ep.create(fframe.getName(), args);
174
            if (obj != null) {
175
                IFFrameDialog fframedialog = (IFFrameDialog) obj;
176
                fframedialog.setRectangle(fframe
177
                    .getBoundingBox(affineTransform));
178
                return fframedialog;
179
            }
180
        } catch (Exception e) {
181 273 cmartinez
            LOG.error("Error creating a FFrameDialog", e);
182 5 jldominguez
        }
183
        return null;
184
    }
185
186
    public IFFrameDialog createFFrameDialog(IFFrame fframe,
187
        LayoutPanel layoutPanel) {
188
        return createFFrameDialog(fframe, layoutPanel, layoutPanel
189
            .getLayoutControl().getAT());
190
    }
191
192
    public void registerFrameFactory(FrameFactory frameFactory, String alias) {
193
        ExtensionPoint ep =
194
            ToolsLocator.getExtensionPointManager().add(KEY_LAYOUT_FFRAME);
195
        ep.append(frameFactory.getRegisterName(), "", frameFactory);
196
        if (alias != null) {
197
            ep.addAlias(frameFactory.getRegisterName(), alias);
198
        }
199
    }
200
201
    public void registerFrameFactory(FrameFactory frameFactory) {
202
        registerFrameFactory(frameFactory, null);
203
    }
204
205
    @SuppressWarnings("unchecked")
206
    public IFFrame createFrame(String frameName) {
207
208
        Iterator<Extension> iterator =
209
            ToolsLocator.getExtensionPointManager().get(KEY_LAYOUT_FFRAME)
210
                .iterator();
211
        while (iterator.hasNext()) {
212
            try {
213
                FrameFactory frameFactory =
214
                    (FrameFactory) iterator.next().create();
215
                if (frameFactory.getRegisterName().equals(frameName)) {
216
                    IFFrame frame = frameFactory.createFrame();
217
                    if (frame == null) {
218
                        return null;
219
                    }
220
                    frame.setFrameFactory(frameFactory);
221
                    return frame;
222
                }
223
            } catch (Exception e) {
224
                NotificationManager.addError(e);
225
            }
226
        }
227
        return null;
228
    }
229
230
    @SuppressWarnings("unchecked")
231
    public void registerFFrameDialog(String name, Class clazz) {
232
        if (!IFFrameDialog.class.isAssignableFrom(clazz)) {
233
            throw new IllegalArgumentException(clazz.getName()
234
                + " must implement the IFFrameDialog interface");
235
        }
236
        ExtensionPoint extensionPoint =
237
            extensionPoints.add(KEY_LAYOUT_FFRAMEDIALOG, "");
238
        extensionPoint.append(name, name, clazz);
239
    }
240
241
    public IWindow getPropertiesWindow(Document doc) {
242
        return new MapProperties((LayoutDocument) doc);
243
    }
244
245
    /**
246
     * Registers in the points of extension the Factory with alias.
247
     *
248
     */
249
    public static void register() {
250
        DefaultLayoutManager factory = new DefaultLayoutManager();
251
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
252
        manager.registerFactory(factory);
253
254
        ProjectManager.getInstance().registerDocumentFactory(factory);
255 345 cmartinez
256
        ProjectManager.getInstance().registerDocumentAction(TYPENAME,new CopyDocumentAction());
257
        ProjectManager.getInstance().registerDocumentAction(TYPENAME,new CutDocumentAction());
258
        ProjectManager.getInstance().registerDocumentAction(TYPENAME,new PasteDocumentAction());
259 5 jldominguez
260 345 cmartinez
261 5 jldominguez
        if (factory.persistenceDefinition == null) {
262
            factory.persistenceDefinition =
263
                manager.addDefinition(LayoutDocument.class,
264
                    PERSISTENCE_LAYOUT_DOCUMENT_DEFINITION_NAME,
265 62 jldominguez
                    "Layout document Persistence definition", null, null);
266 5 jldominguez
            factory.persistenceDefinition.extend(manager
267
                .getDefinition(AbstractDocument.PERSISTENCE_DEFINITION_NAME));
268
269
            factory.persistenceDefinition
270 147 cmartinez
                .addDynFieldObject(DefaultLayoutDocument.LAYOUT_CONTEXT_OBJECT)
271 5 jldominguez
                .setClassOfValue(LayoutContext.class).setMandatory(false);
272
273
            DefaultLayoutPanel.registerPersistent();
274
            DefaultLayoutContext.registerPersistent();
275
        }
276
    }
277
278
    public int getPriority() {
279
        return 2;
280
    }
281
282
    /**
283 216 cmartinez
     * Sets whether the grid should be shown.
284 5 jldominguez
     *
285
     * @param showGrid
286
     */
287
    public void setDefaultShowGrid(boolean showGrid) {
288
        defaultShowGrid = new Boolean(showGrid);
289
    }
290
291
    /**
292 216 cmartinez
     * Sets whether the snapping to grid should be enabled or not
293 5 jldominguez
     *
294
     * @param gridEnable
295
     */
296
    public void setDefaultAdjustToGrid(boolean gridEnabled) {
297
        defaultAdjustToGrid = new Boolean(gridEnabled);
298
    }
299
300
    /**
301 216 cmartinez
     * Sets whether the ruler should be shown or not
302 5 jldominguez
     *
303
     * @param showRuler
304
     */
305
    public void setDefaultShowRulers(boolean showRules) {
306
        defaultShowRulers = new Boolean(showRules);
307
    }
308
309
    /**
310 216 cmartinez
     * Returns if the grid should be shown.
311 5 jldominguez
     *
312 216 cmartinez
     * @return True if the grid should be shown.
313 5 jldominguez
     */
314
    public boolean getDefaultShowGrid() {
315
        if (defaultShowGrid == null) {
316 216 cmartinez
                XMLEntity xml = PluginsLocator.getManager().getPlugin(this).getPersistentXML();
317
            if (xml.contains(PreferenceKeys.DEFAULT_SHOW_LAYOUT_GRID_KEY_NAME)) {
318 5 jldominguez
                defaultShowGrid =
319 216 cmartinez
                    new Boolean(xml.getBooleanProperty(PreferenceKeys.DEFAULT_SHOW_LAYOUT_GRID_KEY_NAME));
320 5 jldominguez
            } else {
321 249 cmartinez
                defaultShowGrid = new Boolean(PreferenceKeys.FACTORY_DEFAULT_LAYOUT_GRID_SHOW);
322 5 jldominguez
            }
323
        }
324
        return defaultShowGrid.booleanValue();
325
    }
326
327
    /**
328 216 cmartinez
     * Returns if the adjust to grid should be active.
329 5 jldominguez
     *
330 216 cmartinez
     * @return True if the adjust to grid should be active.
331 5 jldominguez
     */
332
    public boolean getDefaultAdjustToGrid() {
333
        if (defaultAdjustToGrid == null) {
334 216 cmartinez
                XMLEntity xml = PluginsLocator.getManager().getPlugin(this).getPersistentXML();
335
            if (xml.contains(PreferenceKeys.DEFAULT_ENABLE_LAYOUT_GRID_KEY_NAME)) {
336 5 jldominguez
                defaultAdjustToGrid =
337
                    new Boolean(
338 216 cmartinez
                        xml.getBooleanProperty(PreferenceKeys.DEFAULT_ENABLE_LAYOUT_GRID_KEY_NAME));
339 5 jldominguez
            } else {
340 216 cmartinez
                defaultAdjustToGrid = new Boolean(PreferenceKeys.FACTORY_DEFAULT_LAYOUT_GRID_ENABLE);
341 5 jldominguez
            }
342
        }
343
        return defaultAdjustToGrid.booleanValue();
344
    }
345
346
    /**
347 216 cmartinez
     * Returns if the ruler should be shown.
348 5 jldominguez
     *
349 216 cmartinez
     * @return True if the ruler should be shown.
350 5 jldominguez
     */
351
    public boolean getDefaultShowRulers() {
352
        if (defaultShowRulers == null) {
353 216 cmartinez
                XMLEntity xml = PluginsLocator.getManager().getPlugin(this).getPersistentXML();
354
            if (xml.contains(PreferenceKeys.DEFAULT_SHOW_LAYOUT_RULERS_KEY_NAME)) {
355 5 jldominguez
                defaultShowRulers =
356
                    new Boolean(
357 216 cmartinez
                        xml.getBooleanProperty(PreferenceKeys.DEFAULT_SHOW_LAYOUT_RULERS_KEY_NAME));
358 5 jldominguez
            } else {
359 216 cmartinez
                defaultShowRulers = new Boolean(PreferenceKeys.FACTORY_DEFAULT_LAYOUT_ENABLE_RULERS);
360 5 jldominguez
            }
361
        }
362
        return defaultShowRulers.booleanValue();
363
    }
364
365 216 cmartinez
    public boolean getDefaultShowInitialPageConfigDialog() {
366
            XMLEntity xml = PluginsLocator.getManager().getPlugin(this).getPersistentXML();
367
            boolean value;
368
        if (xml.contains(PreferenceKeys.DEFAULT_SHOW_INITIAL_CONFIG_DIALOG_KEY_NAME)) {
369
            value = xml.getBooleanProperty(PreferenceKeys.DEFAULT_SHOW_INITIAL_CONFIG_DIALOG_KEY_NAME);
370
        } else {
371
                value = PreferenceKeys.FACTORY_DEFAULT_SHOW_INITIAL_CONFIG_DIALOG_FOR_LAYOUT;
372
        }
373
        return value;
374
    }
375
376 5 jldominguez
    public DynStruct getDefinition(String className) {
377
378
        if (this.persistenceDefinition.getName().equalsIgnoreCase(className)) {
379
            return this.persistenceDefinition;
380
        }
381
        if (this.persistenceDefinition.getFullName()
382
            .equalsIgnoreCase(className)) {
383
            return this.persistenceDefinition;
384
        }
385
        if (this.getDocumentClass().getName().equals(className)) {
386
            return this.persistenceDefinition;
387
        }
388
        return null;
389
    }
390
391
    @SuppressWarnings("unchecked")
392
    protected Class getDocumentClass() {
393
        return DefaultLayoutDocument.class;
394
    }
395
396
    public boolean manages(Object object) {
397
        return object instanceof LayoutDocument;
398
    }
399
400
    public void registerLayoutMenuAction(String name,
401
        Class<? extends IContextMenuAction> clazz) {
402
        ExtensionPoint extensionPoint =
403
            ToolsLocator.getExtensionPointManager().add(LAYOUT_CONTEXT_MENUS);
404
        extensionPoint.append(name, "", clazz);
405
    }
406
407
    public IContextMenuAction[] createLayoutMenuActions(LayoutPanel layoutPanel) {
408
        List<IContextMenuAction> actionArrayList =
409
            new ArrayList<IContextMenuAction>();
410
        @SuppressWarnings("unchecked")
411
        Iterator<ExtensionPoint.Extension> iter =
412
            ToolsLocator.getExtensionPointManager().get(LAYOUT_CONTEXT_MENUS).iterator();
413
        AbstractLayoutContextMenuAction action;
414
        while (iter.hasNext()) {
415
            action = null;
416
            try {
417
                action = (AbstractLayoutContextMenuAction) iter.next().create();
418
            } catch (InstantiationException e) {
419
                LOG.error("Error creating the context menu", e);
420
            } catch (IllegalAccessException e) {
421
                LOG.error("Error creating the context menu", e);
422
            }
423
            if (action != null) {
424
                action.setLayout(layoutPanel);
425 140 cmartinez
                if (action.isVisible(null, layoutPanel.getLayoutContext().getSelectedFFrames())) {
426 5 jldominguez
                    actionArrayList.add(action);
427
                }
428
            }
429
        }
430
        IContextMenuAction[] result =
431
            (IContextMenuAction[]) Array.newInstance(IContextMenuAction.class,
432
                actionArrayList.size());
433
        System.arraycopy(actionArrayList.toArray(), 0, result, 0,
434
            actionArrayList.size());
435
        Arrays.sort(result, new CompareAction());
436
        return result;
437
    }
438
439
    private class CompareAction implements Comparator<IContextMenuAction> {
440
441
        public int compare(IContextMenuAction o1, IContextMenuAction o2) {
442
            NumberFormat formater = NumberFormat.getInstance();
443
            formater.setMinimumIntegerDigits(3);
444
            String key1 =
445
                "" + formater.format(o1.getGroupOrder()) + o1.getGroup()
446
                    + formater.format(o1.getOrder());
447
            String key2 =
448
                "" + formater.format(o2.getGroupOrder()) + o2.getGroup()
449
                    + formater.format(o2.getOrder());
450
            return key1.compareTo(key2);
451
        }
452
    }
453
}