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

History | View | Annotate | Download (15.2 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.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.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

    
38
import org.gvsig.andami.PluginServices;
39
import org.gvsig.andami.messages.NotificationManager;
40
import org.gvsig.andami.ui.mdiManager.IWindow;
41
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
import org.gvsig.app.project.documents.gui.WindowLayout;
46
import org.gvsig.app.project.documents.layout.contextmenu.gui.AbstractLayoutContextMenuAction;
47
import org.gvsig.app.project.documents.layout.fframes.FrameFactory;
48
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
49
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.IFFrameDialog;
50
import org.gvsig.app.project.documents.layout.gui.DefaultLayoutPanel;
51
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
52
import org.gvsig.app.project.documents.layout.gui.MapProperties;
53
import org.gvsig.app.project.documents.view.IContextMenuAction;
54
import org.gvsig.tools.ToolsLocator;
55
import org.gvsig.tools.dynobject.DynStruct;
56
import org.gvsig.tools.extensionpoint.ExtensionPoint;
57
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
58
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
59
import org.gvsig.tools.persistence.PersistenceManager;
60
import org.gvsig.utils.XMLEntity;
61

    
62
/**
63
 * Factory of maps.
64
 * 
65
 * @author Vicente Caballero Navarro
66
 */
67
public class DefaultLayoutManager extends AbstractDocumentManager implements
68
    LayoutManager {
69

    
70
    public static String TYPENAME = "project.document.layout";
71

    
72
    static final Logger LOG = LoggerFactory
73
        .getLogger(DefaultLayoutManager.class);
74

    
75
    static final String KEY_LAYOUT_FFRAMEDIALOG =
76
        "app.project.documents.layout.fframes.gui";
77
    static final String KEY_LAYOUT_FFRAME =
78
        "app.project.documents.layout.fframes";
79

    
80
    public static final String PERSISTENCE_LAYOUT_DOCUMENT_DEFINITION_NAME =
81
        "LayoutDocument";
82
    
83
    private static final String LAYOUT_CONTEXT_MENUS = "Layout_ContextMenus";
84

    
85
    ExtensionPointManager extensionPoints = ToolsLocator
86
        .getExtensionPointManager();
87

    
88
    private Boolean defaultShowGrid = null;
89
    private Boolean defaultAdjustToGrid = null;
90
    private Boolean defaultShowRulers = null;
91

    
92
    private DynStruct persistenceDefinition;
93

    
94
    /**
95
     * Returns image of button.
96
     * 
97
     * @return Image button.
98
     */
99
    public ImageIcon getIcon() {
100
        return PluginServices.getIconTheme().get("document-map-icon");
101
    }
102

    
103
    /**
104
     * Returns image of selected button.
105
     * 
106
     * @return Image button.
107
     */
108
    public ImageIcon getIconSelected() {
109
        return PluginServices.getIconTheme().get("document-map-icon-sel");
110
    }
111

    
112
    /**
113
     * Returns the name of registration in the point of extension.
114
     * 
115
     * @return Name of registration
116
     */
117
    public String getTypeName() {
118
        return TYPENAME;
119
    }
120

    
121
    /**
122
     * Returns the name of ProjectDocument.
123
     * 
124
     * @return Name of ProjectDocument.
125
     */
126
    public String getTitle() {
127
        return PluginServices.getText(this, "Mapa2");
128
    }
129

    
130
    public AbstractDocument createDocument() {
131
        return new DefaultLayoutDocument(this);
132
    }
133

    
134
    public Class getMainWindowClass() {
135
        return DefaultLayoutPanel.class;
136
    }
137

    
138
    public IWindow getMainWindow(Document doc, WindowLayout layout) {
139
        LayoutPanel layoutPanel;
140
        layoutPanel =
141
            (LayoutPanel) PluginServices.getMDIManager().getSingletonWindow(
142
                getMainWindowClass(), doc);
143
        if (layoutPanel != null) {
144
            // The table window document is already created, return it.
145
            return layoutPanel;
146
        }
147

    
148
        layoutPanel = (LayoutPanel) this.createDocumentWindow(doc);
149
        if (layout != null) {
150
            layoutPanel.setWindowLayout(layout);
151
        }
152
        layoutPanel.setLayoutManager(this);
153
        layoutPanel.getLayoutControl().fullRect();
154
        layoutPanel.getWindowInfo().setTitle(
155
            PluginServices.getText(this, "Mapa") + " : "
156
                + layoutPanel.getName());
157
        ((AbstractDocument) doc).raiseEventCreateWindow(layoutPanel);
158
        return layoutPanel;
159
    }
160

    
161
    public IFFrameDialog createFFrameDialog(IFFrame fframe,
162
        LayoutPanel layoutPanel, AffineTransform affineTransform) {
163
        ExtensionPoint ep = extensionPoints.add(KEY_LAYOUT_FFRAMEDIALOG);
164

    
165
        try {
166
            Object[] args = new Object[2];
167
            args[0] = layoutPanel;
168
            args[1] = fframe;
169
            Object obj = ep.create(fframe.getName(), args);
170
            if (obj != null) {
171
                IFFrameDialog fframedialog = (IFFrameDialog) obj;
172
                fframedialog.setRectangle(fframe
173
                    .getBoundingBox(affineTransform));
174
                return fframedialog;
175
            }
176
        } catch (Exception e) {
177
            LOG.error("Error creating a FFrameDialog");
178
        }
179
        return null;
180
    }
181

    
182
    public IFFrameDialog createFFrameDialog(IFFrame fframe,
183
        LayoutPanel layoutPanel) {
184
        return createFFrameDialog(fframe, layoutPanel, layoutPanel
185
            .getLayoutControl().getAT());
186
    }
187

    
188
    public void registerFrameFactory(FrameFactory frameFactory, String alias) {
189
        ExtensionPoint ep =
190
            ToolsLocator.getExtensionPointManager().add(KEY_LAYOUT_FFRAME);
191
        ep.append(frameFactory.getRegisterName(), "", frameFactory);
192
        if (alias != null) {
193
            ep.addAlias(frameFactory.getRegisterName(), alias);
194
        }
195
    }
196

    
197
    public void registerFrameFactory(FrameFactory frameFactory) {
198
        registerFrameFactory(frameFactory, null);
199
    }
200

    
201
    @SuppressWarnings("unchecked")
202
    public IFFrame createFrame(String frameName) {
203

    
204
        Iterator<Extension> iterator =
205
            ToolsLocator.getExtensionPointManager().get(KEY_LAYOUT_FFRAME)
206
                .iterator();
207
        while (iterator.hasNext()) {
208
            try {
209
                FrameFactory frameFactory =
210
                    (FrameFactory) iterator.next().create();
211
                if (frameFactory.getRegisterName().equals(frameName)) {
212
                    IFFrame frame = frameFactory.createFrame();
213
                    if (frame == null) {
214
                        return null;
215
                    }
216
                    frame.setFrameFactory(frameFactory);
217
                    return frame;
218
                }
219
            } catch (Exception e) {
220
                NotificationManager.addError(e);
221
            }
222
        }
223
        return null;
224
    }
225

    
226
    @SuppressWarnings("unchecked")
227
    public void registerFFrameDialog(String name, Class clazz) {
228
        if (!IFFrameDialog.class.isAssignableFrom(clazz)) {
229
            throw new IllegalArgumentException(clazz.getName()
230
                + " must implement the IFFrameDialog interface");
231
        }
232
        ExtensionPoint extensionPoint =
233
            extensionPoints.add(KEY_LAYOUT_FFRAMEDIALOG, "");
234
        extensionPoint.append(name, name, clazz);
235
    }
236

    
237
    public IWindow getPropertiesWindow(Document doc) {
238
        return new MapProperties((LayoutDocument) doc);
239
    }
240

    
241
    /**
242
     * Registers in the points of extension the Factory with alias.
243
     * 
244
     */
245
    public static void register() {
246
        DefaultLayoutManager factory = new DefaultLayoutManager();
247
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
248
        manager.registerFactory(factory);
249

    
250
        ProjectManager.getInstance().registerDocumentFactory(factory);
251

    
252
        if (factory.persistenceDefinition == null) {
253
            factory.persistenceDefinition =
254
                manager.addDefinition(LayoutDocument.class,
255
                    PERSISTENCE_LAYOUT_DOCUMENT_DEFINITION_NAME,
256
                    "Layout document Persistence definition", null, null);
257
            factory.persistenceDefinition.extend(manager
258
                .getDefinition(AbstractDocument.PERSISTENCE_DEFINITION_NAME));
259

    
260
            factory.persistenceDefinition
261
                .addDynFieldObject(DefaultLayoutDocument.LAYOUT_CONTEXT_OBJECT)
262
                .setClassOfValue(LayoutContext.class).setMandatory(false);
263

    
264
            DefaultLayoutPanel.registerPersistent();
265
            DefaultLayoutContext.registerPersistent();
266
        }
267
    }
268

    
269
    /**
270
     * Returns the priority of de ProjectDocument.
271
     * 
272
     * @return Priority.
273
     */
274
    public int getPriority() {
275
        return 2;
276
    }
277

    
278
    /**
279
     * Inserts if the grid sould be show.
280
     * 
281
     * @param showGrid
282
     */
283
    public void setDefaultShowGrid(boolean showGrid) {
284
        defaultShowGrid = new Boolean(showGrid);
285
    }
286

    
287
    /**
288
     * Inserts if the adjust togrid sould be actived.
289
     * 
290
     * @param gridEnable
291
     */
292
    public void setDefaultAdjustToGrid(boolean gridEnabled) {
293
        defaultAdjustToGrid = new Boolean(gridEnabled);
294
    }
295

    
296
    /**
297
     * Inserts if the ruler sould be show.
298
     * 
299
     * @param showRuler
300
     */
301
    public void setDefaultShowRulers(boolean showRules) {
302
        defaultShowRulers = new Boolean(showRules);
303
    }
304

    
305
    /**
306
     * Returns if the grid sould be show.
307
     * 
308
     * @return True if the grid sould be show.
309
     */
310
    public boolean getDefaultShowGrid() {
311
        if (defaultShowGrid == null) {
312
            XMLEntity xml =
313
                PluginServices.getPluginServices("org.gvsig.app")
314
                    .getPersistentXML();
315
            if (xml.contains("DefaultShowLayoutGrid")) {
316
                defaultShowGrid =
317
                    new Boolean(xml.getBooleanProperty("DefaultShowLayoutGrid"));
318
            } else {
319
                // factory default is true
320
                defaultShowGrid = new Boolean(true);
321
            }
322
        }
323
        return defaultShowGrid.booleanValue();
324
    }
325

    
326
    /**
327
     * Returns if the adjust to grid sould be actived.
328
     * 
329
     * @return True if the adjust to grid sould be actived.
330
     */
331
    public boolean getDefaultAdjustToGrid() {
332
        if (defaultAdjustToGrid == null) {
333
            XMLEntity xml =
334
                PluginServices.getPluginServices("org.gvsig.app")
335
                    .getPersistentXML();
336
            if (xml.contains("DefaultEnableLayoutGrid")) {
337
                defaultAdjustToGrid =
338
                    new Boolean(
339
                        xml.getBooleanProperty("DefaultEnableLayoutGrid"));
340
            } else {
341
                // factory default is false
342
                defaultAdjustToGrid = new Boolean(false);
343
            }
344
        }
345
        return defaultAdjustToGrid.booleanValue();
346
    }
347

    
348
    /**
349
     * Returns if the ruler sould be show.
350
     * 
351
     * @return True if the ruler sould be show.
352
     */
353
    public boolean getDefaultShowRulers() {
354
        if (defaultShowRulers == null) {
355
            XMLEntity xml =
356
                PluginServices.getPluginServices("org.gvsig.app")
357
                    .getPersistentXML();
358
            if (xml.contains("DefaultShowLayoutRules")) {
359
                defaultShowRulers =
360
                    new Boolean(
361
                        xml.getBooleanProperty("DefaultShowLayoutRules"));
362
            } else {
363
                // factory default is true
364
                defaultShowRulers = new Boolean(true);
365
            }
366
        }
367
        return defaultShowRulers.booleanValue();
368
    }
369

    
370
    public DynStruct getDefinition(String className) {
371

    
372
        if (this.persistenceDefinition.getName().equalsIgnoreCase(className)) {
373
            return this.persistenceDefinition;
374
        }
375
        if (this.persistenceDefinition.getFullName()
376
            .equalsIgnoreCase(className)) {
377
            return this.persistenceDefinition;
378
        }
379
        if (this.getDocumentClass().getName().equals(className)) {
380
            return this.persistenceDefinition;
381
        }
382
        return null;
383
    }
384

    
385
    @SuppressWarnings("unchecked")
386
    protected Class getDocumentClass() {
387
        return DefaultLayoutDocument.class;
388
    }
389

    
390
    public boolean manages(Object object) {
391
        return object instanceof LayoutDocument;
392
    }
393

    
394
    public void registerLayoutMenuAction(String name,
395
        Class<? extends IContextMenuAction> clazz) {
396
        ExtensionPoint extensionPoint =
397
            ToolsLocator.getExtensionPointManager().add(LAYOUT_CONTEXT_MENUS);
398
        extensionPoint.append(name, "", clazz);
399
    }
400

    
401
    public IContextMenuAction[] createLayoutMenuActions(LayoutPanel layoutPanel) {
402
        List<IContextMenuAction> actionArrayList =
403
            new ArrayList<IContextMenuAction>();
404
        @SuppressWarnings("unchecked")
405
        Iterator<ExtensionPoint.Extension> iter =
406
            ToolsLocator.getExtensionPointManager().get(LAYOUT_CONTEXT_MENUS).iterator();
407
        AbstractLayoutContextMenuAction action;
408
        while (iter.hasNext()) {
409
            action = null;
410
            try {
411
                action = (AbstractLayoutContextMenuAction) iter.next().create();
412
            } catch (InstantiationException e) {
413
                LOG.error("Error creating the context menu", e);
414
            } catch (IllegalAccessException e) {
415
                LOG.error("Error creating the context menu", e);
416
            }
417
            if (action != null) {
418
                action.setLayout(layoutPanel);
419
                if (action.isVisible(null, layoutPanel.getLayoutContext().getSelectedFFrames())) {
420
                    actionArrayList.add(action);
421
                }
422
            }
423
        }
424
        IContextMenuAction[] result =
425
            (IContextMenuAction[]) Array.newInstance(IContextMenuAction.class,
426
                actionArrayList.size());
427
        System.arraycopy(actionArrayList.toArray(), 0, result, 0,
428
            actionArrayList.size());
429
        Arrays.sort(result, new CompareAction());
430
        return result;
431
    }
432
    
433
    private class CompareAction implements Comparator<IContextMenuAction> {
434

    
435
        public int compare(IContextMenuAction o1, IContextMenuAction o2) {
436
            NumberFormat formater = NumberFormat.getInstance();
437
            formater.setMinimumIntegerDigits(3);
438
            String key1 =
439
                "" + formater.format(o1.getGroupOrder()) + o1.getGroup()
440
                    + formater.format(o1.getOrder());
441
            String key2 =
442
                "" + formater.format(o2.getGroupOrder()) + o2.getGroup()
443
                    + formater.format(o2.getOrder());
444
            return key1.compareTo(key2);
445
        }
446
    }    
447
}