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

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
    static final Logger LOG = LoggerFactory
70
        .getLogger(DefaultLayoutManager.class);
71

    
72
    static final String KEY_LAYOUT_FFRAMEDIALOG =
73
        "app.project.documents.layout.fframes.gui";
74
    static final String KEY_LAYOUT_FFRAME =
75
        "app.project.documents.layout.fframes";
76

    
77
    public static final String PERSISTENCE_LAYOUT_DOCUMENT_DEFINITION_NAME =
78
        "LayoutDocument";
79
    
80
    private static final String LAYOUT_CONTEXT_MENUS = "Layout_ContextMenus";
81

    
82
    ExtensionPointManager extensionPoints = ToolsLocator
83
        .getExtensionPointManager();
84

    
85
    private Boolean defaultShowGrid = null;
86
    private Boolean defaultAdjustToGrid = null;
87
    private Boolean defaultShowRulers = null;
88

    
89
    private DynStruct persistenceDefinition;
90

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

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

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

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

    
127
    public AbstractDocument createDocument() {
128
        return new DefaultLayoutDocument(this);
129
    }
130

    
131
    public Class getMainWindowClass() {
132
        return DefaultLayoutPanel.class;
133
    }
134

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

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

    
158
    public IFFrameDialog createFFrameDialog(IFFrame fframe,
159
        LayoutPanel layoutPanel, AffineTransform affineTransform) {
160
        ExtensionPoint ep = extensionPoints.add(KEY_LAYOUT_FFRAMEDIALOG);
161

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

    
179
    public IFFrameDialog createFFrameDialog(IFFrame fframe,
180
        LayoutPanel layoutPanel) {
181
        return createFFrameDialog(fframe, layoutPanel, layoutPanel
182
            .getLayoutControl().getAT());
183
    }
184

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

    
194
    public void registerFrameFactory(FrameFactory frameFactory) {
195
        registerFrameFactory(frameFactory, null);
196
    }
197

    
198
    @SuppressWarnings("unchecked")
199
    public IFFrame createFrame(String frameName) {
200

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

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

    
234
    public IWindow getPropertiesWindow(Document doc) {
235
        return new MapProperties((LayoutDocument) doc);
236
    }
237

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

    
247
        ProjectManager.getInstance().registerDocumentFactory(factory);
248

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

    
257
            factory.persistenceDefinition
258
                .addDynFieldObject(DefaultLayoutDocument.LAYOUT_CONTEXT_OBJECT)
259
                .setClassOfValue(LayoutContext.class).setMandatory(false);
260

    
261
            DefaultLayoutPanel.registerPersistent();
262
            DefaultLayoutContext.registerPersistent();
263
        }
264
    }
265

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

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

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

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

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

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

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

    
367
    public DynStruct getDefinition(String className) {
368

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

    
382
    @SuppressWarnings("unchecked")
383
    protected Class getDocumentClass() {
384
        return DefaultLayoutDocument.class;
385
    }
386

    
387
    public boolean manages(Object object) {
388
        return object instanceof LayoutDocument;
389
    }
390

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

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

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