]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/TerrainDefinitionPanel.java
Version 17, September 2014
[GpsPrune.git] / tim / prune / gui / TerrainDefinitionPanel.java
1 package tim.prune.gui;
2
3 import java.awt.Dimension;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6
7 import javax.swing.Box;
8 import javax.swing.BoxLayout;
9 import javax.swing.JCheckBox;
10 import javax.swing.JLabel;
11 import javax.swing.JPanel;
12
13 import tim.prune.I18nManager;
14 import tim.prune.config.Config;
15 import tim.prune.threedee.TerrainDefinition;
16
17 /**
18  * Gui component for defining the 3d terrain,
19  * including whether to use one or not, and if so
20  * what resolution to use for the grid
21  */
22 public class TerrainDefinitionPanel extends JPanel
23 {
24         /** Checkbox to use a terrain or not */
25         private JCheckBox _useCheckbox = null;
26         /** Field for entering the grid size */
27         private WholeNumberField _gridSizeField = null;
28
29
30         /**
31          * Constructor
32          */
33         public TerrainDefinitionPanel()
34         {
35                 setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
36                 // Components
37                 _useCheckbox = new JCheckBox(I18nManager.getText("dialog.3d.useterrain"));
38                 _useCheckbox.addActionListener(new ActionListener() {
39                         public void actionPerformed(ActionEvent arg0) {
40                                 activateGridField();
41                         }
42                 });
43                 add(_useCheckbox);
44                 add(Box.createHorizontalGlue());
45                 JLabel label = new JLabel(I18nManager.getText("dialog.3d.terraingridsize") + ": ");
46                 add(label);
47                 _gridSizeField = new WholeNumberField(4);
48                 _gridSizeField.setValue(Config.getConfigInt(Config.KEY_TERRAIN_GRID_SIZE)); // default grid size
49                 _gridSizeField.setMaximumSize(new Dimension(100, 50));
50                 _gridSizeField.setEnabled(false);
51                 add(_gridSizeField);
52         }
53
54         /**
55          * @param inDefinition terrain parameters to set
56          */
57         public void initTerrainParameters(TerrainDefinition inDefinition)
58         {
59                 _useCheckbox.setSelected(inDefinition != null && inDefinition.getUseTerrain());
60                 if (inDefinition != null && inDefinition.getGridSize() > 0) {
61                         _gridSizeField.setValue(inDefinition.getGridSize());
62                 }
63                 activateGridField();
64         }
65
66         /**
67          * @return true if the terrain is selected
68          */
69         public boolean getUseTerrain() {
70                 return _useCheckbox.isSelected() && getGridSize() > 2;
71         }
72
73         /**
74          * @return number of nodes along each side of the grid
75          */
76         public int getGridSize() {
77                 return _gridSizeField.getValue();
78         }
79
80         /**
81          * Set the grid field to be enabled or not based on the checkbox
82          */
83         private void activateGridField() {
84                 _gridSizeField.setEnabled(_useCheckbox.isSelected());
85         }
86 }