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 @ 1756
History | View | Annotate | Download (16.5 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 |
public static class AccesoryPanelForTemplates extends JPanel { |
120 |
|
121 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
122 |
public AccesoryPanelForTemplates(File projectfile, FileDialogChooser fc) { |
123 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
124 |
ApplicationManager application = ApplicationLocator.getApplicationManager(); |
125 |
PreferencesNode prefs = application.getPreferences("gvsig.foldering");
|
126 |
String prefsPath = prefs.get(TEMPLATES_FOLDER_PROPERTY_NAME, null); |
127 |
|
128 |
BoxLayout l = new BoxLayout(this, BoxLayout.PAGE_AXIS); |
129 |
this.setLayout(l);
|
130 |
ImageIcon projectIcon = ToolsSwingLocator.getIconThemeManager().getActive().get("application-project-gotofolder"); |
131 |
JButton goToProjectButton = new JButton(projectIcon); |
132 |
|
133 |
goToProjectButton.setToolTipText(i18n.getTranslation("_Go_to_folder_project"));
|
134 |
this.add(goToProjectButton);
|
135 |
if(projectfile != null) { |
136 |
goToProjectButton.addActionListener((ActionEvent e) -> {
|
137 |
fc.setCurrentDirectory(projectfile.getParentFile()); |
138 |
}); |
139 |
goToProjectButton.setEnabled(true);
|
140 |
} else {
|
141 |
goToProjectButton.setEnabled(false);
|
142 |
} |
143 |
ImageIcon templatesIcon = ToolsSwingLocator.getIconThemeManager().getActive().get("application-layout-gotodefaulttemplatesfolder"); |
144 |
JButton goToTemplatesButton = new JButton(templatesIcon); |
145 |
|
146 |
goToTemplatesButton.setToolTipText(i18n.getTranslation("_Go_to_default_templates_folder"));
|
147 |
this.add(goToTemplatesButton);
|
148 |
goToTemplatesButton.addActionListener((ActionEvent e) -> {
|
149 |
File prefsFile = new File(prefsPath); |
150 |
if (prefsFile.exists()) {
|
151 |
fc.setCurrentDirectory(prefsFile); |
152 |
} |
153 |
}); |
154 |
goToTemplatesButton.setEnabled(StringUtils.isNotBlank(prefsPath)); |
155 |
|
156 |
} |
157 |
|
158 |
} |
159 |
|
160 |
private void saveLayout() { |
161 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
162 |
layout = (LayoutPanel) PluginServices.getMDIManager().getActiveWindow(); |
163 |
FileDialogChooserManager manager = ToolsUtilLocator.getFileDialogChooserManager(); |
164 |
FileDialogChooser fc = manager.create("_Last_layout_template_folder");
|
165 |
GenericFileFilter layoutMainFilter = new GenericFileFilter(
|
166 |
LayoutManager.TEMPLATE_FILE_POINTEXT,
|
167 |
i18n.getTranslation("_Layout_template") + " (*" |
168 |
+ LayoutManager.TEMPLATE_FILE_POINTEXT + ")"); |
169 |
|
170 |
fc.addChoosableFileFilter(layoutMainFilter); |
171 |
fc.setFileFilter(layoutMainFilter); |
172 |
ApplicationManager application = ApplicationLocator.getApplicationManager(); |
173 |
PreferencesNode prefs = application.getPreferences("gvsig.foldering");
|
174 |
|
175 |
File projectfile = application.getCurrentProject().getFile();
|
176 |
|
177 |
JPanel accesory = new AccesoryPanelForTemplates(projectfile, fc); |
178 |
fc.setAccessory(accesory); |
179 |
|
180 |
File folder = manager.getLastPath("_Last_layout_template_folder", new File("")); |
181 |
if(folder == null || StringUtils.isBlank(folder.getPath())) { |
182 |
if(projectfile != null){ |
183 |
folder = projectfile.getParentFile(); |
184 |
} |
185 |
} |
186 |
|
187 |
if (folder == null || StringUtils.isBlank(folder.getPath())) { |
188 |
String path = prefs.get(TEMPLATES_FOLDER_PROPERTY_NAME, null); |
189 |
if (StringUtils.isNotBlank(path)) {
|
190 |
folder = new File(path); |
191 |
} |
192 |
} |
193 |
|
194 |
if (folder == null || StringUtils.isBlank(folder.getPath())) { |
195 |
folder = ToolsLocator.getFoldersManager().getHome(); |
196 |
} |
197 |
|
198 |
|
199 |
if (fc.showSaveDialog((Component) PluginServices.getMainFrame(), folder) != JFileChooser.APPROVE_OPTION) { |
200 |
return;
|
201 |
} |
202 |
File file = fc.getSelectedFile();
|
203 |
if (!(file.getPath().endsWith(
|
204 |
LayoutManager.TEMPLATE_FILE_POINTEXT.toLowerCase()) || file
|
205 |
.getPath().endsWith(LayoutManager.TEMPLATE_FILE_POINTEXT))) {
|
206 |
file = new File(file.getPath() |
207 |
+ LayoutManager.TEMPLATE_FILE_POINTEXT);
|
208 |
} |
209 |
if (file.exists()) {
|
210 |
int resp = JOptionPane.showConfirmDialog( |
211 |
(Component) PluginServices.getMainFrame(),
|
212 |
PluginServices.getText(this,
|
213 |
"fichero_ya_existe_seguro_desea_guardarlo"),
|
214 |
PluginServices.getText(this, "guardar"), |
215 |
JOptionPane.YES_NO_OPTION);
|
216 |
if (resp != JOptionPane.YES_OPTION) { |
217 |
return;
|
218 |
} |
219 |
} |
220 |
|
221 |
try {
|
222 |
FileOutputStream fos = new FileOutputStream( |
223 |
file.getAbsolutePath()); |
224 |
PersistenceManager persistenceManager = ToolsLocator |
225 |
.getPersistenceManager(); |
226 |
PersistentState persistentState = persistenceManager |
227 |
.getState(layout); |
228 |
persistenceManager.saveState(persistentState, fos); |
229 |
} catch (Exception e) { |
230 |
NotificationManager.addError(PluginServices.getText(this,
|
231 |
"Error_guardando_la_plantilla"), e);
|
232 |
} |
233 |
|
234 |
// manager.setLastPath("_Last_layout_template_folder", file.getParentFile());
|
235 |
|
236 |
} |
237 |
|
238 |
/**
|
239 |
* @see com.iver.mdiApp.plugins.IExtension#isVisible()
|
240 |
*/
|
241 |
@Override
|
242 |
public boolean isVisible() { |
243 |
IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
244 |
|
245 |
if (f == null) { |
246 |
return false; |
247 |
} |
248 |
|
249 |
if (f instanceof LayoutPanel) { |
250 |
return true; |
251 |
} |
252 |
return false; |
253 |
} |
254 |
|
255 |
/**
|
256 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
257 |
*/
|
258 |
@Override
|
259 |
public void initialize() { |
260 |
registerPersistence(); |
261 |
registerIcons(); |
262 |
} |
263 |
|
264 |
@Override
|
265 |
public void postInitialize() { |
266 |
LayoutReportActionFactory.selfRegister(); |
267 |
} |
268 |
|
269 |
private void registerPersistence() { |
270 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
271 |
JavaTextFactory factory = new JavaTextFactory();
|
272 |
manager.registerFactory(factory); |
273 |
} |
274 |
|
275 |
private void registerIcons() { |
276 |
|
277 |
IconThemeHelper.registerIcon("action",
|
278 |
"application-layout-template-save", this); |
279 |
|
280 |
IconThemeHelper.registerIcon("action",
|
281 |
"application-project-gotofolder", this); |
282 |
IconThemeHelper.registerIcon("action",
|
283 |
"application-layout-gotodefaulttemplatesfolder", this); |
284 |
|
285 |
IconThemeHelper.registerIcon("action",
|
286 |
"layout-navigation-zoom-in-center", this); |
287 |
IconThemeHelper.registerIcon("action",
|
288 |
"layout-navigation-zoom-out-center", this); |
289 |
IconThemeHelper.registerIcon("action",
|
290 |
"layout-navigation-zoom-in-topoint", this); |
291 |
IconThemeHelper.registerIcon("action",
|
292 |
"layout-navigation-zoom-out-topoint", this); |
293 |
IconThemeHelper.registerIcon("action", "layout-navigation-zoom-all", |
294 |
this);
|
295 |
IconThemeHelper.registerIcon("action", "layout-navigation-zoom-real", |
296 |
this);
|
297 |
IconThemeHelper.registerIcon("action",
|
298 |
"layout-navigation-zoom-selected", this); |
299 |
IconThemeHelper.registerIcon("action", "layout-navigation-pan", this); |
300 |
|
301 |
IconThemeHelper.registerIcon("action",
|
302 |
"layout-view-navigation-zoom-in-topoint", this); |
303 |
IconThemeHelper.registerIcon("action",
|
304 |
"layout-view-navigation-zoom-out-topoint", this); |
305 |
IconThemeHelper.registerIcon("action",
|
306 |
"layout-view-navigation-zoom-all", this); |
307 |
IconThemeHelper.registerIcon("action", "layout-view-navigation-pan", |
308 |
this);
|
309 |
// ================================================
|
310 |
// Cursors
|
311 |
IconThemeHelper.registerIcon("cursor",
|
312 |
"cursor-layout-navigation-zoom-in-topoint", this); |
313 |
IconThemeHelper.registerIcon("cursor",
|
314 |
"cursor-layout-navigation-zoom-out-topoint", this); |
315 |
IconThemeHelper.registerIcon("cursor", "cursor-selection-by-rectangle", |
316 |
this);
|
317 |
IconThemeHelper.registerIcon("cursor", "cursor-selection-simple", this); |
318 |
IconThemeHelper |
319 |
.registerIcon("cursor", "cursor-selection-complex", this); |
320 |
IconThemeHelper.registerIcon("cursor",
|
321 |
"cursor-layout-graphic-edit-vertex", this); |
322 |
IconThemeHelper.registerIcon("cursor", "cursor-layout-insert-circle", |
323 |
this);
|
324 |
IconThemeHelper.registerIcon("cursor", "cursor-layout-insert-polyline", |
325 |
this);
|
326 |
IconThemeHelper.registerIcon("cursor", "cursor-layout-insert-point", |
327 |
this);
|
328 |
IconThemeHelper.registerIcon("cursor", "cursor-layout-insert-polygon", |
329 |
this);
|
330 |
IconThemeHelper.registerIcon("cursor",
|
331 |
"cursor-layout-insert-rectangle", this); |
332 |
IconThemeHelper.registerIcon("cursor",
|
333 |
"cursor-layout-view-navigation-zoom-in-topoint", this); |
334 |
IconThemeHelper.registerIcon("cursor",
|
335 |
"cursor-layout-view-navigation-zoom-out-topoint", this); |
336 |
// End cursors
|
337 |
// ================================================
|
338 |
|
339 |
IconThemeHelper.registerIcon("document", "document-map-icon", this); |
340 |
IconThemeHelper.registerIcon("document", "document-map-icon-sel", this); |
341 |
|
342 |
IconThemeHelper.registerIcon("layout", "neresize-icon", this); |
343 |
IconThemeHelper.registerIcon("layout", "eresize-icon", this); |
344 |
IconThemeHelper.registerIcon("layout", "nresize-icon", this); |
345 |
IconThemeHelper.registerIcon("layout", "graphic-move-icon", this); |
346 |
|
347 |
IconThemeHelper.registerIcon("layout", "sereresize-icon", this); |
348 |
IconThemeHelper.registerIcon("layout", "symboltag-icon", this); |
349 |
IconThemeHelper.registerIcon("layout", "numero-icon", this); |
350 |
IconThemeHelper.registerIcon("layout", "barra1-icon", this); |
351 |
IconThemeHelper.registerIcon("layout", "barra2-icon", this); |
352 |
IconThemeHelper.registerIcon("layout", "barra3-icon", this); |
353 |
|
354 |
IconThemeHelper.registerIcon("layout", "text-left-icon", this); |
355 |
IconThemeHelper.registerIcon("layout", "text-center-v-icon", this); |
356 |
IconThemeHelper.registerIcon("layout", "text-right-icon", this); |
357 |
IconThemeHelper.registerIcon("layout", "left-rotation-icon", this); |
358 |
IconThemeHelper.registerIcon("layout", "text-up-icon", this); |
359 |
IconThemeHelper.registerIcon("layout", "text-center-h-icon", this); |
360 |
IconThemeHelper.registerIcon("layout", "text-down-icon", this); |
361 |
IconThemeHelper.registerIcon("layout", "text-distup-icon", this); |
362 |
IconThemeHelper.registerIcon("layout", "text-distcenterh-icon", this); |
363 |
IconThemeHelper.registerIcon("layout", "text-distdown-icon", this); |
364 |
IconThemeHelper.registerIcon("layout", "text-distleft-icon", this); |
365 |
|
366 |
IconThemeHelper.registerIcon("layout", "text-distcenterv-icon", this); |
367 |
IconThemeHelper.registerIcon("layout", "text-distright-icon", this); |
368 |
IconThemeHelper.registerIcon("layout", "text-size-width-icon", this); |
369 |
IconThemeHelper.registerIcon("layout", "text-size-height-icon", this); |
370 |
IconThemeHelper.registerIcon("layout", "text-size-other-icon", this); |
371 |
IconThemeHelper.registerIcon("layout", "text-space-right-icon", this); |
372 |
IconThemeHelper.registerIcon("layout", "text-space-down-icon", this); |
373 |
IconThemeHelper.registerIcon("layout", "text-inlayout-icon", this); |
374 |
IconThemeHelper.registerIcon("layout", "layout-pan-icon", this); |
375 |
IconThemeHelper.registerIcon("layout", "view-pan-icon", this); |
376 |
IconThemeHelper.registerIcon("layout", "right-rotation-icon", this); |
377 |
} |
378 |
|
379 |
/**
|
380 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
381 |
*/
|
382 |
@Override
|
383 |
public boolean isEnabled() { |
384 |
return true; |
385 |
} |
386 |
|
387 |
/**
|
388 |
* Devuelve el Layout sobre el que se opera.
|
389 |
*
|
390 |
* @return Layout.
|
391 |
*/
|
392 |
public LayoutPanel getLayout() {
|
393 |
return layout;
|
394 |
} |
395 |
|
396 |
@Override
|
397 |
public IPreference[] getPreferencesPages() { |
398 |
return new IPreference[] { new LayoutPage() }; |
399 |
} |
400 |
} |