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