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