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