svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / gui / preferencespage / ViewPage.java @ 45661
History | View | Annotate | Download (18.8 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.app.gui.preferencespage; |
24 |
|
25 |
import java.awt.Color; |
26 |
import java.awt.event.ActionEvent; |
27 |
import java.awt.event.ActionListener; |
28 |
import java.awt.event.ItemEvent; |
29 |
import java.awt.event.ItemListener; |
30 |
|
31 |
import javax.swing.DefaultComboBoxModel; |
32 |
import javax.swing.ImageIcon; |
33 |
import javax.swing.JButton; |
34 |
import javax.swing.JColorChooser; |
35 |
import javax.swing.JPanel; |
36 |
import javax.swing.JSlider; |
37 |
import javax.swing.JTextField; |
38 |
import javax.swing.SpinnerNumberModel; |
39 |
import javax.swing.SwingUtilities; |
40 |
import javax.swing.UIManager; |
41 |
import javax.swing.event.ChangeEvent; |
42 |
import javax.swing.event.ChangeListener; |
43 |
import org.apache.commons.lang3.BooleanUtils; |
44 |
|
45 |
import org.cresques.cts.IProjection; |
46 |
import org.gvsig.andami.IconThemeHelper; |
47 |
import org.gvsig.andami.PluginServices; |
48 |
import org.gvsig.andami.PluginsLocator; |
49 |
import org.gvsig.andami.preferences.IPreference; |
50 |
import org.gvsig.andami.preferences.StoreException; |
51 |
import org.gvsig.app.ApplicationLocator; |
52 |
import org.gvsig.app.gui.panels.CRSSelectPanel; |
53 |
import org.gvsig.app.gui.panels.crs.ISelectCrsPanel; |
54 |
import org.gvsig.app.project.ProjectPreferences; |
55 |
import org.gvsig.fmap.crs.CRSFactory; |
56 |
import org.gvsig.fmap.mapcontext.MapContext; |
57 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
58 |
import org.gvsig.i18n.Messages; |
59 |
|
60 |
import org.gvsig.tools.ToolsLocator; |
61 |
import org.gvsig.tools.dynobject.DynObject; |
62 |
import org.gvsig.tools.i18n.I18nManager; |
63 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
64 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
65 |
import org.gvsig.utils.XMLEntity; |
66 |
|
67 |
/**
|
68 |
* View document configuration page.
|
69 |
*
|
70 |
* Here the user can establish what settings wants to use by default regarding
|
71 |
* to the document View.
|
72 |
*
|
73 |
*/
|
74 |
public class ViewPage extends ViewPageView implements ChangeListener, ItemListener, IPreference { |
75 |
//
|
76 |
// The class ViewPageView can extend from AbstractPreferencePage, not JPanel,
|
77 |
// if rereate it from the GUI builder change te class declaration to:
|
78 |
// public abstract class ViewPageView extends AbstractPreferencePage
|
79 |
|
80 |
private static final long serialVersionUID = 1204688910771374514L; |
81 |
|
82 |
public static String id = ViewPage.class.getName(); |
83 |
|
84 |
private static final IProjection FACTORY_DEFAULT_PROJECTION |
85 |
= MapContextLocator.getMapContextManager().getDefaultCRS(); |
86 |
|
87 |
private static String[] unitsNames = null; |
88 |
private static String[] areaUnitsNames = null; |
89 |
|
90 |
private static final double DEFAULT_ZOOM_IN_FACTOR = 2.0; |
91 |
private static final double DEFAULT_ZOOM_OUT_FACTOR = 1.5; |
92 |
private static final Color FACTORY_DEFAULT_SELECTION_COLOR = Color.YELLOW; |
93 |
private static final Color FACTORY_DEFAULT_VIEW_BACK_COLOR = Color.WHITE; |
94 |
private static int FACTORY_DEFAULT_MAP_UNITS; |
95 |
private static int FACTORY_DEFAULT_DISTANCE_UNITS; |
96 |
private static int FACTORY_DEFAULT_DISTANCE_AREA; |
97 |
|
98 |
private final ImageIcon icon; |
99 |
|
100 |
private ProjectionController defaultProjection = null; |
101 |
private ColorChooserController defaultViewBackColor = null; |
102 |
private ColorChooserController defaultSelectionColor = null; |
103 |
|
104 |
private boolean hasChanges = false; |
105 |
|
106 |
private static class ProjectionController implements ActionListener { |
107 |
|
108 |
private static final long serialVersionUID = -1442364274073324390L; |
109 |
|
110 |
private final JTextField txtLabel; |
111 |
private final JButton btnShowDialog; |
112 |
private IProjection proj = null; |
113 |
private ChangeListener changeListener; |
114 |
|
115 |
public ProjectionController(JTextField txtLabel, JButton btnShowDialog) { |
116 |
this.txtLabel = txtLabel;
|
117 |
this.btnShowDialog = btnShowDialog;
|
118 |
|
119 |
this.btnShowDialog.addActionListener(this); |
120 |
this.set(null); |
121 |
} |
122 |
|
123 |
public void addChangeListener(ChangeListener changeListener) { |
124 |
this.changeListener = changeListener;
|
125 |
} |
126 |
|
127 |
public void fireChanged() { |
128 |
if (this.changeListener != null) { |
129 |
this.changeListener.stateChanged(null); |
130 |
} |
131 |
} |
132 |
|
133 |
public void set(IProjection proj) { |
134 |
this.proj = proj;
|
135 |
if (this.proj == null) { |
136 |
this.txtLabel.setText(""); |
137 |
} else {
|
138 |
this.txtLabel.setText(proj.getAbrev());
|
139 |
} |
140 |
} |
141 |
|
142 |
public IProjection get() {
|
143 |
return this.proj; |
144 |
} |
145 |
|
146 |
@Override
|
147 |
public void actionPerformed(ActionEvent e) { |
148 |
ISelectCrsPanel panel = CRSSelectPanel.getUIFactory().getSelectCrsPanel( |
149 |
CRSFactory.getCRS(this.txtLabel.getText()), false); |
150 |
ToolsSwingLocator.getWindowManager().showWindow( |
151 |
panel.asJComponent(), |
152 |
ToolsLocator.getI18nManager().getTranslation("selecciona_sistema_de_referencia"),
|
153 |
WindowManager.MODE.DIALOG |
154 |
); |
155 |
if (panel.isOkPressed()) {
|
156 |
this.set(panel.getProjection());
|
157 |
this.fireChanged();
|
158 |
} |
159 |
} |
160 |
|
161 |
} |
162 |
|
163 |
private static class ColorChooserController implements ActionListener, ChangeListener { |
164 |
|
165 |
private final JTextField txtLabel; |
166 |
private final JButton btnShowDialog; |
167 |
private final JSlider sldAlpha; |
168 |
private Color color; |
169 |
private boolean allowNull; |
170 |
private ChangeListener changeListener; |
171 |
|
172 |
public ColorChooserController(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha, boolean allowNull) { |
173 |
this.txtLabel = txtLabel;
|
174 |
this.btnShowDialog = btnShowDialog;
|
175 |
this.sldAlpha = sldAlpha;
|
176 |
this.btnShowDialog.addActionListener(this); |
177 |
if (this.sldAlpha != null) { |
178 |
this.sldAlpha.addChangeListener(this); |
179 |
} |
180 |
this.allowNull = allowNull;
|
181 |
if (!this.allowNull) { |
182 |
this.color = Color.BLACK; |
183 |
} |
184 |
} |
185 |
|
186 |
public ColorChooserController(JTextField txtLabel, JButton btnShowDialog) { |
187 |
this(txtLabel, btnShowDialog, null, false); |
188 |
} |
189 |
|
190 |
public ColorChooserController(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha) { |
191 |
this(txtLabel, btnShowDialog, sldAlpha, false); |
192 |
} |
193 |
|
194 |
@Override
|
195 |
public void stateChanged(ChangeEvent e) { |
196 |
if (this.color == null) { |
197 |
set(Color.BLACK);
|
198 |
} |
199 |
if (this.sldAlpha == null) { |
200 |
this.color = new Color( |
201 |
color.getRed(), |
202 |
color.getGreen(), |
203 |
color.getBlue() |
204 |
); |
205 |
} else {
|
206 |
this.color = new Color( |
207 |
color.getRed(), |
208 |
color.getGreen(), |
209 |
color.getBlue(), |
210 |
this.sldAlpha.getValue()
|
211 |
); |
212 |
} |
213 |
this.txtLabel.setBackground(this.color); |
214 |
} |
215 |
|
216 |
public void addChangeListener(ChangeListener changeListener) { |
217 |
this.changeListener = changeListener;
|
218 |
} |
219 |
|
220 |
public void fireChanged() { |
221 |
if (this.changeListener != null) { |
222 |
this.changeListener.stateChanged(null); |
223 |
} |
224 |
} |
225 |
|
226 |
@Override
|
227 |
public void actionPerformed(ActionEvent e) { |
228 |
Color c = JColorChooser.showDialog( |
229 |
SwingUtilities.windowForComponent(this.btnShowDialog), |
230 |
Messages.getText("choose_color"),
|
231 |
this.get()
|
232 |
); |
233 |
if (c == null) { |
234 |
return;
|
235 |
} |
236 |
set(c); |
237 |
} |
238 |
|
239 |
private void set(Color color) { |
240 |
if (color == null) { |
241 |
if (allowNull) {
|
242 |
this.color = null; |
243 |
return;
|
244 |
} |
245 |
color = Color.BLACK;
|
246 |
} |
247 |
this.color = color;
|
248 |
this.txtLabel.setBackground(this.color); |
249 |
if (this.sldAlpha != null) { |
250 |
this.sldAlpha.setValue(this.color.getAlpha()); |
251 |
} |
252 |
} |
253 |
|
254 |
public Color get() { |
255 |
return this.color; |
256 |
} |
257 |
|
258 |
} |
259 |
|
260 |
/**
|
261 |
* Creates a new panel containing View preferences settings.
|
262 |
*
|
263 |
*/
|
264 |
public ViewPage() {
|
265 |
super();
|
266 |
|
267 |
initUnitsNames(); |
268 |
|
269 |
icon = IconThemeHelper.getImageIcon("document-view-icon");
|
270 |
|
271 |
this.chkKeepScaleOnResizing.setEnabled(false); |
272 |
|
273 |
this.defaultProjection = new ProjectionController( |
274 |
this.txtDefaultProjection,
|
275 |
this.btnChangeProjection
|
276 |
); |
277 |
this.defaultProjection.addChangeListener(this); |
278 |
|
279 |
this.defaultSelectionColor = new ColorChooserController( |
280 |
this.txtDefaultSelectionColor,
|
281 |
this.btnDefaultSelectionColor,
|
282 |
this.sldDefaultSelectionColor
|
283 |
); |
284 |
this.defaultSelectionColor.addChangeListener(this); |
285 |
|
286 |
this.defaultViewBackColor = new ColorChooserController( |
287 |
this.txtDefaultViewBackColor,
|
288 |
this.btnDefaultViewBackColor
|
289 |
); |
290 |
this.defaultViewBackColor.addChangeListener(this); |
291 |
|
292 |
this.chkInvisibleNewLayers.addChangeListener(this); |
293 |
this.chkKeepScaleOnResizing.addChangeListener(this); |
294 |
|
295 |
this.jCmbDistanceArea.addItemListener(this); |
296 |
this.jCmbDistanceUnits.addItemListener(this); |
297 |
this.jCmbMapUnits.addItemListener(this); |
298 |
|
299 |
this.translateAll();
|
300 |
this.initializeValues();
|
301 |
} |
302 |
|
303 |
private void translateAll() { |
304 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
305 |
|
306 |
this.lblDefaultProjection.setText(i18n.getTranslation("default_projection")); |
307 |
this.chkInvisibleNewLayers.setText(i18n.getTranslation("options.view.invisible_new_layers")); |
308 |
this.chkKeepScaleOnResizing.setText(i18n.getTranslation("options.view.keep_scale_on_resizing")); |
309 |
this.lblZoomInFactor.setText(i18n.getTranslation("zoom_in_factor")); |
310 |
this.lblZoomOutFactor.setText(i18n.getTranslation("zoom_out_factor")); |
311 |
this.lblDefaultViewBackColor.setText(i18n.getTranslation("options.view.default_view_back_color")); |
312 |
this.lblDefaultSelectionColor.setText(i18n.getTranslation("options.view.default_selection_color")); |
313 |
this.lblMapUnits.setText(i18n.getTranslation("map_units")); |
314 |
this.lblDistanceUnits.setText(i18n.getTranslation("distance_units")); |
315 |
this.lblDistanceArea.setText(i18n.getTranslation("distance_area")); |
316 |
this.chkHideLegendOfNonVisibleLayers.setText(i18n.getTranslation("_Hide_legend_of_non_visible_layers")); |
317 |
this.chkOpenWithANewMaximizedView.setText(i18n.getTranslation("_Open_with_a_new_maximized_view")); |
318 |
this.btnChangeProjection.setText(i18n.getTranslation("_Change")); |
319 |
this.lblAlpha.setText(i18n.getTranslation("Alpha")); |
320 |
} |
321 |
|
322 |
@Override
|
323 |
public boolean isResizeable() { |
324 |
return true; |
325 |
} |
326 |
|
327 |
@Override
|
328 |
public void initializeValues() { |
329 |
Double zif;
|
330 |
Double zof;
|
331 |
|
332 |
ProjectPreferences projectPreferences = ApplicationLocator.getProjectManager().getProjectPreferences(); |
333 |
PluginServices plugin = PluginsLocator.getManager().getPlugin(this);
|
334 |
DynObject props = plugin.getPluginProperties(); |
335 |
XMLEntity xml = plugin.getPersistentXML(); |
336 |
|
337 |
this.defaultProjection.set(projectPreferences.getDefaultProjection());
|
338 |
|
339 |
this.chkInvisibleNewLayers.setSelected(projectPreferences.getAddNewLayersInInvisibleMode());
|
340 |
this.chkKeepScaleOnResizing.setSelected(projectPreferences.getKeepScaleOnResizing());
|
341 |
this.chkOpenWithANewMaximizedView.setSelected(projectPreferences.getOpenWithANewMaximizedView());
|
342 |
this.chkHideLegendOfNonVisibleLayers.setSelected(projectPreferences.getHideLegendInToCOfNonVisibleLayers());
|
343 |
this.txtZoomInFactor.setModel(
|
344 |
new SpinnerNumberModel( |
345 |
projectPreferences.getZoomInFactor(), //initial value
|
346 |
0.01, //min |
347 |
1000, //max |
348 |
0.25 //step |
349 |
) |
350 |
); |
351 |
this.txtZoomOutFactor.setModel(
|
352 |
new SpinnerNumberModel( |
353 |
projectPreferences.getZoomOutFactor(), //initial value
|
354 |
0.01, //min |
355 |
1000, //max |
356 |
0.25 //step |
357 |
) |
358 |
); |
359 |
MapContext.ZOOMINFACTOR = projectPreferences.getZoomInFactor(); |
360 |
MapContext.ZOOMOUTFACTOR = projectPreferences.getZoomOutFactor(); |
361 |
|
362 |
this.defaultViewBackColor.set(projectPreferences.getDefaultViewBackColor());
|
363 |
this.defaultSelectionColor.set(projectPreferences.getDefaultSelectionColor());
|
364 |
|
365 |
this.jCmbMapUnits.setModel(new DefaultComboBoxModel(unitsNames)); |
366 |
this.jCmbMapUnits.setSelectedIndex(projectPreferences.getDefaultMapUnits());
|
367 |
|
368 |
this.jCmbDistanceUnits.setModel(new DefaultComboBoxModel(unitsNames)); |
369 |
this.jCmbDistanceUnits.setSelectedIndex(projectPreferences.getDefaultDistanceUnits());
|
370 |
|
371 |
this.jCmbDistanceArea.setModel(new DefaultComboBoxModel(areaUnitsNames)); |
372 |
this.jCmbDistanceArea.setSelectedIndex(projectPreferences.getDefaultDistanceArea());
|
373 |
} |
374 |
|
375 |
@Override
|
376 |
public void stateChanged(ChangeEvent e) { |
377 |
this.hasChanges = true; |
378 |
} |
379 |
|
380 |
@Override
|
381 |
public void itemStateChanged(ItemEvent e) { |
382 |
this.hasChanges = true; |
383 |
} |
384 |
|
385 |
@Override
|
386 |
public String getID() { |
387 |
return id;
|
388 |
} |
389 |
|
390 |
@Override
|
391 |
public String getTitle() { |
392 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
393 |
return i18nManager.getTranslation("Vista"); |
394 |
} |
395 |
|
396 |
@Override
|
397 |
public JPanel getPanel() { |
398 |
return this; |
399 |
} |
400 |
|
401 |
@Override
|
402 |
public void storeValues() throws StoreException { |
403 |
|
404 |
ProjectPreferences projectPreferences = ApplicationLocator.getProjectManager().getProjectPreferences(); |
405 |
|
406 |
try {
|
407 |
double zif = (double) txtZoomInFactor.getValue(); |
408 |
double zof = (double) txtZoomOutFactor.getValue(); |
409 |
if (zif == 0 || zof == 0) { |
410 |
throw new NumberFormatException(); |
411 |
} |
412 |
|
413 |
projectPreferences.setZoomInFactor(zif); |
414 |
projectPreferences.setZoomOutFactor(zof); |
415 |
MapContext.ZOOMINFACTOR = zif; |
416 |
MapContext.ZOOMOUTFACTOR = zof; |
417 |
|
418 |
projectPreferences.setDefaultProjection(this.defaultProjection.get());
|
419 |
projectPreferences.setDefaultSelectionColor(defaultSelectionColor.get()); |
420 |
projectPreferences.setDefaultViewBackColor(defaultViewBackColor.get()); |
421 |
projectPreferences.setDefaultOverviewBackColor(defaultViewBackColor.get()); |
422 |
|
423 |
projectPreferences.setDefaultMapUnits(jCmbMapUnits.getSelectedIndex()); |
424 |
projectPreferences.setDefaultDistanceUnits(jCmbDistanceUnits.getSelectedIndex()); |
425 |
projectPreferences.setDefaultDistanceArea(jCmbDistanceArea.getSelectedIndex()); |
426 |
|
427 |
projectPreferences.setAddNewLayersInInvisibleMode(chkInvisibleNewLayers.isSelected()); |
428 |
projectPreferences.setKeepScaleOnResizing(chkKeepScaleOnResizing.isSelected()); |
429 |
projectPreferences.setOpenWithANewMaximizedView(chkOpenWithANewMaximizedView.isSelected()); |
430 |
projectPreferences.setHideLegendInToCOfNonVisibleLayers(this.chkHideLegendOfNonVisibleLayers.isSelected());
|
431 |
|
432 |
} catch (Exception e) { |
433 |
throw new StoreException(PluginServices.getText(this, "factor_zoom_incorrecto")); |
434 |
} |
435 |
} |
436 |
|
437 |
@Override
|
438 |
public void initializeDefaults() { |
439 |
defaultProjection.set(FACTORY_DEFAULT_PROJECTION); |
440 |
txtZoomInFactor.setModel( |
441 |
new SpinnerNumberModel( |
442 |
DEFAULT_ZOOM_IN_FACTOR, //initial value
|
443 |
1, //min |
444 |
1000, //max |
445 |
0.25 //step |
446 |
) |
447 |
); |
448 |
txtZoomOutFactor.setModel( |
449 |
new SpinnerNumberModel( |
450 |
DEFAULT_ZOOM_OUT_FACTOR, //initial value
|
451 |
1, //min |
452 |
1000, //max |
453 |
0.25 //step |
454 |
) |
455 |
); |
456 |
// txtZoomInFactor.setText(String.valueOf(DEFAULT_ZOOM_IN_FACTOR));
|
457 |
// txtZoomOutFactor.setText(String.valueOf(DEFAULT_ZOOM_OUT_FACTOR));
|
458 |
this.chkInvisibleNewLayers.setSelected(false); |
459 |
this.chkKeepScaleOnResizing.setSelected(false); |
460 |
this.chkHideLegendOfNonVisibleLayers.setSelected(true); |
461 |
try {
|
462 |
this.defaultViewBackColor.set(
|
463 |
(Color) UIManager.getLookAndFeel().getDefaults().get("TextField.background") |
464 |
); |
465 |
} catch(Exception es ) { |
466 |
this.defaultViewBackColor.set(FACTORY_DEFAULT_VIEW_BACK_COLOR);
|
467 |
} |
468 |
this.defaultSelectionColor.set(FACTORY_DEFAULT_SELECTION_COLOR);
|
469 |
this.jCmbMapUnits.setSelectedIndex(FACTORY_DEFAULT_MAP_UNITS);
|
470 |
this.jCmbDistanceUnits.setSelectedIndex(FACTORY_DEFAULT_DISTANCE_UNITS);
|
471 |
this.jCmbDistanceArea.setSelectedIndex(FACTORY_DEFAULT_DISTANCE_AREA);
|
472 |
|
473 |
} |
474 |
|
475 |
@Override
|
476 |
public ImageIcon getIcon() { |
477 |
return icon;
|
478 |
} |
479 |
|
480 |
@Override
|
481 |
public boolean isValueChanged() { |
482 |
return this.hasChanges; |
483 |
} |
484 |
|
485 |
@Override
|
486 |
public void setChangesApplied() { |
487 |
this.hasChanges = false; |
488 |
} |
489 |
|
490 |
private void initUnitsNames() { |
491 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
492 |
if (unitsNames == null) { |
493 |
String[] names = MapContext.getDistanceNames(); |
494 |
unitsNames = new String[names.length]; |
495 |
for (int i = 0; i < names.length; i++) { |
496 |
unitsNames[i] = i18n.getTranslation(names[i]); |
497 |
if (names[i].equals("Metros")) { |
498 |
FACTORY_DEFAULT_DISTANCE_UNITS = i; |
499 |
FACTORY_DEFAULT_DISTANCE_AREA = i; |
500 |
FACTORY_DEFAULT_MAP_UNITS = i; |
501 |
} |
502 |
} |
503 |
} |
504 |
if( areaUnitsNames == null ) { |
505 |
String[] names = MapContext.getAreaNames(); |
506 |
areaUnitsNames = new String[names.length]; |
507 |
for (int i = 0; i < names.length; i++) { |
508 |
areaUnitsNames[i] = i18n.getTranslation(names[i])+MapContext.getOfLinear(i); |
509 |
} |
510 |
} |
511 |
} |
512 |
} |