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