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