svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / extension / LayoutExtension.java @ 1750
History | View | Annotate | Download (16 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.extension; |
23 |
|
24 |
import java.awt.Component; |
25 |
import java.awt.event.ActionEvent; |
26 |
import java.io.File; |
27 |
import java.io.FileOutputStream; |
28 |
import javax.swing.BoxLayout; |
29 |
import javax.swing.ImageIcon; |
30 |
import javax.swing.JButton; |
31 |
import javax.swing.JFileChooser; |
32 |
import javax.swing.JOptionPane; |
33 |
import javax.swing.JPanel; |
34 |
import org.apache.commons.lang3.StringUtils; |
35 |
import org.gvsig.andami.IconThemeHelper; |
36 |
import org.gvsig.andami.PluginServices; |
37 |
import org.gvsig.andami.messages.NotificationManager; |
38 |
import org.gvsig.andami.plugins.Extension; |
39 |
import org.gvsig.andami.preferences.IPreference; |
40 |
import org.gvsig.andami.preferences.IPreferenceExtension; |
41 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
42 |
import org.gvsig.app.ApplicationLocator; |
43 |
import org.gvsig.app.ApplicationManager; |
44 |
import org.gvsig.app.PreferencesNode; |
45 |
import static org.gvsig.app.extension.InitializeApplicationExtension.TEMPLATES_FOLDER_PROPERTY_NAME; |
46 |
import org.gvsig.app.gui.preferencespage.LayoutPage; |
47 |
import org.gvsig.app.project.documents.layout.FLayoutZooms; |
48 |
import org.gvsig.app.project.documents.layout.LayoutManager; |
49 |
import org.gvsig.app.project.documents.layout.gui.LayoutPanel; |
50 |
import org.gvsig.app.project.documents.layout.report.LayoutReportActionFactory; |
51 |
import org.gvsig.filedialogchooser.FileDialogChooser; |
52 |
import org.gvsig.filedialogchooser.FileDialogChooserManager; |
53 |
import org.gvsig.tools.ToolsLocator; |
54 |
import org.gvsig.tools.i18n.I18nManager; |
55 |
import org.gvsig.tools.persistence.PersistenceManager; |
56 |
import org.gvsig.tools.persistence.PersistentState; |
57 |
import org.gvsig.tools.persistence.text.JavaTextFactory; |
58 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
59 |
import org.gvsig.tools.util.ToolsUtilLocator; |
60 |
import org.gvsig.utils.GenericFileFilter; |
61 |
import org.slf4j.Logger; |
62 |
import org.slf4j.LoggerFactory; |
63 |
|
64 |
/**
|
65 |
* Extensi?n para controlar las operaciones basicas sobre el Layout.
|
66 |
*
|
67 |
* @author Vicente Caballero Navarro
|
68 |
*/
|
69 |
public class LayoutExtension extends Extension implements IPreferenceExtension { |
70 |
|
71 |
private static final Logger logger = LoggerFactory |
72 |
.getLogger(LayoutExtension.class); |
73 |
|
74 |
private LayoutPanel layout = null; |
75 |
|
76 |
/**
|
77 |
* @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
|
78 |
*/
|
79 |
@Override
|
80 |
public void execute(String s) { |
81 |
layout = (LayoutPanel) PluginServices.getMDIManager().getActiveWindow(); |
82 |
|
83 |
FLayoutZooms zooms = new FLayoutZooms(layout);
|
84 |
logger.debug("Comand : " + s);
|
85 |
|
86 |
switch (s) {
|
87 |
case "layout-navigation-pan": |
88 |
layout.getLayoutControl().setTool("layoutpan");
|
89 |
break;
|
90 |
case "layout-navigation-zoom-in-topoint": |
91 |
layout.getLayoutControl().setTool("layoutzoomin");
|
92 |
break;
|
93 |
case "layout-navigation-zoom-out-topoint": |
94 |
layout.getLayoutControl().setTool("layoutzoomout");
|
95 |
break;
|
96 |
case "layout-navigation-zoom-all": |
97 |
layout.getLayoutControl().fullRect(); |
98 |
break;
|
99 |
case "layout-navigation-zoom-real": |
100 |
zooms.realZoom(); |
101 |
break;
|
102 |
case "layout-navigation-zoom-out-center": |
103 |
zooms.zoomOut(); |
104 |
break;
|
105 |
case "layout-navigation-zoom-in-center": |
106 |
zooms.zoomIn(); |
107 |
break;
|
108 |
case "layout-navigation-zoom-selected": |
109 |
zooms.zoomSelect(); |
110 |
break;
|
111 |
case "application-layout-template-save": |
112 |
saveLayout(); |
113 |
break;
|
114 |
default:
|
115 |
break;
|
116 |
} |
117 |
} |
118 |
|
119 |
private void saveLayout() { |
120 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
121 |
layout = (LayoutPanel) PluginServices.getMDIManager().getActiveWindow(); |
122 |
FileDialogChooserManager manager = ToolsUtilLocator.getFileDialogChooserManager(); |
123 |
FileDialogChooser fc = manager.create("_Last_layout_template_folder");
|
124 |
GenericFileFilter layoutMainFilter = new GenericFileFilter(
|
125 |
LayoutManager.TEMPLATE_FILE_POINTEXT,
|
126 |
i18n.getTranslation("_Layout_template") + " (*" |
127 |
+ LayoutManager.TEMPLATE_FILE_POINTEXT + ")"); |
128 |
|
129 |
fc.addChoosableFileFilter(layoutMainFilter); |
130 |
fc.setFileFilter(layoutMainFilter); |
131 |
ApplicationManager application = ApplicationLocator.getApplicationManager(); |
132 |
PreferencesNode prefs = application.getPreferences("gvsig.foldering");
|
133 |
|
134 |
String prefsPath = prefs.get(TEMPLATES_FOLDER_PROPERTY_NAME, null); |
135 |
File projectfile = application.getCurrentProject().getFile();
|
136 |
JPanel accesory = null; |
137 |
if(projectfile != null || prefsPath != null) { |
138 |
accesory = new JPanel(); |
139 |
BoxLayout l = new BoxLayout(accesory, BoxLayout.PAGE_AXIS); |
140 |
accesory.setLayout(l); |
141 |
} |
142 |
if(projectfile != null) { |
143 |
ImageIcon icon = ToolsSwingLocator.getIconThemeManager().getActive().get("application-project-gotofolder"); |
144 |
JButton goToProjectButton = new JButton(icon); |
145 |
|
146 |
goToProjectButton.setToolTipText(i18n.getTranslation("_Go_to_folder_project"));
|
147 |
accesory.add(goToProjectButton); |
148 |
goToProjectButton.addActionListener((ActionEvent e) -> {
|
149 |
fc.setCurrentDirectory(projectfile.getParentFile()); |
150 |
}); |
151 |
} |
152 |
if(prefsPath != null) { |
153 |
ImageIcon icon = ToolsSwingLocator.getIconThemeManager().getActive().get("application-layout-gotodefaulttemplatesfolder"); |
154 |
JButton goToTemplatesButton = new JButton(icon); |
155 |
|
156 |
goToTemplatesButton.setToolTipText(i18n.getTranslation("_Go_to_default_templates_folder"));
|
157 |
accesory.add(goToTemplatesButton); |
158 |
goToTemplatesButton.addActionListener((ActionEvent e) -> {
|
159 |
File prefsFile = new File(prefsPath); |
160 |
if(prefsFile.exists()){
|
161 |
fc.setCurrentDirectory(prefsFile); |
162 |
} |
163 |
}); |
164 |
} |
165 |
if(accesory != null){ |
166 |
fc.setAccessory(accesory); |
167 |
} |
168 |
|
169 |
File folder = null; |
170 |
folder = manager.getLastPath("_Last_layout_template_folder", new File("")); |
171 |
if(folder == null || StringUtils.isBlank(folder.getPath())) { |
172 |
if(projectfile != null){ |
173 |
folder = projectfile.getParentFile(); |
174 |
} |
175 |
} |
176 |
|
177 |
if (folder == null || StringUtils.isBlank(folder.getPath())) { |
178 |
String path = prefs.get(TEMPLATES_FOLDER_PROPERTY_NAME, null); |
179 |
if (StringUtils.isNotBlank(path)) {
|
180 |
folder = new File(path); |
181 |
} |
182 |
} |
183 |
|
184 |
if (folder == null || StringUtils.isBlank(folder.getPath())) { |
185 |
folder = ToolsLocator.getFoldersManager().getHome(); |
186 |
} |
187 |
|
188 |
|
189 |
if (fc.showSaveDialog((Component) PluginServices.getMainFrame(), folder) != JFileChooser.APPROVE_OPTION) { |
190 |
return;
|
191 |
} |
192 |
File file = fc.getSelectedFile();
|
193 |
if (!(file.getPath().endsWith(
|
194 |
LayoutManager.TEMPLATE_FILE_POINTEXT.toLowerCase()) || file
|
195 |
.getPath().endsWith(LayoutManager.TEMPLATE_FILE_POINTEXT))) {
|
196 |
file = new File(file.getPath() |
197 |
+ LayoutManager.TEMPLATE_FILE_POINTEXT);
|
198 |
} |
199 |
if (file.exists()) {
|
200 |
int resp = JOptionPane.showConfirmDialog( |
201 |
(Component) PluginServices.getMainFrame(),
|
202 |
PluginServices.getText(this,
|
203 |
"fichero_ya_existe_seguro_desea_guardarlo"),
|
204 |
PluginServices.getText(this, "guardar"), |
205 |
JOptionPane.YES_NO_OPTION);
|
206 |
if (resp != JOptionPane.YES_OPTION) { |
207 |
return;
|
208 |
} |
209 |
} |
210 |
|
211 |
try {
|
212 |
FileOutputStream fos = new FileOutputStream( |
213 |
file.getAbsolutePath()); |
214 |
PersistenceManager persistenceManager = ToolsLocator |
215 |
.getPersistenceManager(); |
216 |
PersistentState persistentState = persistenceManager |
217 |
.getState(layout); |
218 |
persistenceManager.saveState(persistentState, fos); |
219 |
} catch (Exception e) { |
220 |
NotificationManager.addError(PluginServices.getText(this,
|
221 |
"Error_guardando_la_plantilla"), e);
|
222 |
} |
223 |
|
224 |
// manager.setLastPath("_Last_layout_template_folder", file.getParentFile());
|
225 |
|
226 |
} |
227 |
|
228 |
/**
|
229 |
* @see com.iver.mdiApp.plugins.IExtension#isVisible()
|
230 |
*/
|
231 |
@Override
|
232 |
public boolean isVisible() { |
233 |
IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
234 |
|
235 |
if (f == null) { |
236 |
return false; |
237 |
} |
238 |
|
239 |
if (f instanceof LayoutPanel) { |
240 |
return true; |
241 |
} |
242 |
return false; |
243 |
} |
244 |
|
245 |
/**
|
246 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
247 |
*/
|
248 |
@Override
|
249 |
public void initialize() { |
250 |
registerPersistence(); |
251 |
registerIcons(); |
252 |
} |
253 |
|
254 |
@Override
|
255 |
public void postInitialize() { |
256 |
LayoutReportActionFactory.selfRegister(); |
257 |
} |
258 |
|
259 |
private void registerPersistence() { |
260 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
261 |
JavaTextFactory factory = new JavaTextFactory();
|
262 |
manager.registerFactory(factory); |
263 |
} |
264 |
|
265 |
private void registerIcons() { |
266 |
|
267 |
IconThemeHelper.registerIcon("action",
|
268 |
"application-layout-template-save", this); |
269 |
|
270 |
IconThemeHelper.registerIcon("action",
|
271 |
"application-project-gotofolder", this); |
272 |
IconThemeHelper.registerIcon("action",
|
273 |
"application-layout-gotodefaulttemplatesfolder", this); |
274 |
|
275 |
IconThemeHelper.registerIcon("action",
|
276 |
"layout-navigation-zoom-in-center", this); |
277 |
IconThemeHelper.registerIcon("action",
|
278 |
"layout-navigation-zoom-out-center", this); |
279 |
IconThemeHelper.registerIcon("action",
|
280 |
"layout-navigation-zoom-in-topoint", this); |
281 |
IconThemeHelper.registerIcon("action",
|
282 |
"layout-navigation-zoom-out-topoint", this); |
283 |
IconThemeHelper.registerIcon("action", "layout-navigation-zoom-all", |
284 |
this);
|
285 |
IconThemeHelper.registerIcon("action", "layout-navigation-zoom-real", |
286 |
this);
|
287 |
IconThemeHelper.registerIcon("action",
|
288 |
"layout-navigation-zoom-selected", this); |
289 |
IconThemeHelper.registerIcon("action", "layout-navigation-pan", this); |
290 |
|
291 |
IconThemeHelper.registerIcon("action",
|
292 |
"layout-view-navigation-zoom-in-topoint", this); |
293 |
IconThemeHelper.registerIcon("action",
|
294 |
"layout-view-navigation-zoom-out-topoint", this); |
295 |
IconThemeHelper.registerIcon("action",
|
296 |
"layout-view-navigation-zoom-all", this); |
297 |
IconThemeHelper.registerIcon("action", "layout-view-navigation-pan", |
298 |
this);
|
299 |
// ================================================
|
300 |
// Cursors
|
301 |
IconThemeHelper.registerIcon("cursor",
|
302 |
"cursor-layout-navigation-zoom-in-topoint", this); |
303 |
IconThemeHelper.registerIcon("cursor",
|
304 |
"cursor-layout-navigation-zoom-out-topoint", this); |
305 |
IconThemeHelper.registerIcon("cursor", "cursor-selection-by-rectangle", |
306 |
this);
|
307 |
IconThemeHelper.registerIcon("cursor", "cursor-selection-simple", this); |
308 |
IconThemeHelper |
309 |
.registerIcon("cursor", "cursor-selection-complex", this); |
310 |
IconThemeHelper.registerIcon("cursor",
|
311 |
"cursor-layout-graphic-edit-vertex", this); |
312 |
IconThemeHelper.registerIcon("cursor", "cursor-layout-insert-circle", |
313 |
this);
|
314 |
IconThemeHelper.registerIcon("cursor", "cursor-layout-insert-polyline", |
315 |
this);
|
316 |
IconThemeHelper.registerIcon("cursor", "cursor-layout-insert-point", |
317 |
this);
|
318 |
IconThemeHelper.registerIcon("cursor", "cursor-layout-insert-polygon", |
319 |
this);
|
320 |
IconThemeHelper.registerIcon("cursor",
|
321 |
"cursor-layout-insert-rectangle", this); |
322 |
IconThemeHelper.registerIcon("cursor",
|
323 |
"cursor-layout-view-navigation-zoom-in-topoint", this); |
324 |
IconThemeHelper.registerIcon("cursor",
|
325 |
"cursor-layout-view-navigation-zoom-out-topoint", this); |
326 |
// End cursors
|
327 |
// ================================================
|
328 |
|
329 |
IconThemeHelper.registerIcon("document", "document-map-icon", this); |
330 |
IconThemeHelper.registerIcon("document", "document-map-icon-sel", this); |
331 |
|
332 |
IconThemeHelper.registerIcon("layout", "neresize-icon", this); |
333 |
IconThemeHelper.registerIcon("layout", "eresize-icon", this); |
334 |
IconThemeHelper.registerIcon("layout", "nresize-icon", this); |
335 |
IconThemeHelper.registerIcon("layout", "graphic-move-icon", this); |
336 |
|
337 |
IconThemeHelper.registerIcon("layout", "sereresize-icon", this); |
338 |
IconThemeHelper.registerIcon("layout", "symboltag-icon", this); |
339 |
IconThemeHelper.registerIcon("layout", "numero-icon", this); |
340 |
IconThemeHelper.registerIcon("layout", "barra1-icon", this); |
341 |
IconThemeHelper.registerIcon("layout", "barra2-icon", this); |
342 |
IconThemeHelper.registerIcon("layout", "barra3-icon", this); |
343 |
|
344 |
IconThemeHelper.registerIcon("layout", "text-left-icon", this); |
345 |
IconThemeHelper.registerIcon("layout", "text-center-v-icon", this); |
346 |
IconThemeHelper.registerIcon("layout", "text-right-icon", this); |
347 |
IconThemeHelper.registerIcon("layout", "left-rotation-icon", this); |
348 |
IconThemeHelper.registerIcon("layout", "text-up-icon", this); |
349 |
IconThemeHelper.registerIcon("layout", "text-center-h-icon", this); |
350 |
IconThemeHelper.registerIcon("layout", "text-down-icon", this); |
351 |
IconThemeHelper.registerIcon("layout", "text-distup-icon", this); |
352 |
IconThemeHelper.registerIcon("layout", "text-distcenterh-icon", this); |
353 |
IconThemeHelper.registerIcon("layout", "text-distdown-icon", this); |
354 |
IconThemeHelper.registerIcon("layout", "text-distleft-icon", this); |
355 |
|
356 |
IconThemeHelper.registerIcon("layout", "text-distcenterv-icon", this); |
357 |
IconThemeHelper.registerIcon("layout", "text-distright-icon", this); |
358 |
IconThemeHelper.registerIcon("layout", "text-size-width-icon", this); |
359 |
IconThemeHelper.registerIcon("layout", "text-size-height-icon", this); |
360 |
IconThemeHelper.registerIcon("layout", "text-size-other-icon", this); |
361 |
IconThemeHelper.registerIcon("layout", "text-space-right-icon", this); |
362 |
IconThemeHelper.registerIcon("layout", "text-space-down-icon", this); |
363 |
IconThemeHelper.registerIcon("layout", "text-inlayout-icon", this); |
364 |
IconThemeHelper.registerIcon("layout", "layout-pan-icon", this); |
365 |
IconThemeHelper.registerIcon("layout", "view-pan-icon", this); |
366 |
IconThemeHelper.registerIcon("layout", "right-rotation-icon", this); |
367 |
} |
368 |
|
369 |
/**
|
370 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
371 |
*/
|
372 |
@Override
|
373 |
public boolean isEnabled() { |
374 |
return true; |
375 |
} |
376 |
|
377 |
/**
|
378 |
* Devuelve el Layout sobre el que se opera.
|
379 |
*
|
380 |
* @return Layout.
|
381 |
*/
|
382 |
public LayoutPanel getLayout() {
|
383 |
return layout;
|
384 |
} |
385 |
|
386 |
@Override
|
387 |
public IPreference[] getPreferencesPages() { |
388 |
return new IPreference[] { new LayoutPage() }; |
389 |
} |
390 |
} |