]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/ShowThreeDFunction.java
Version 17, September 2014
[GpsPrune.git] / tim / prune / function / ShowThreeDFunction.java
1 package tim.prune.function;
2
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7
8 import javax.swing.Box;
9 import javax.swing.BoxLayout;
10 import javax.swing.JButton;
11 import javax.swing.JDialog;
12 import javax.swing.JLabel;
13 import javax.swing.JOptionPane;
14 import javax.swing.JPanel;
15
16 import tim.prune.App;
17 import tim.prune.GenericFunction;
18 import tim.prune.I18nManager;
19 import tim.prune.config.Config;
20 import tim.prune.gui.BaseImageDefinitionPanel;
21 import tim.prune.gui.DecimalNumberField;
22 import tim.prune.gui.TerrainDefinitionPanel;
23 import tim.prune.threedee.TerrainDefinition;
24 import tim.prune.threedee.ThreeDException;
25 import tim.prune.threedee.ThreeDWindow;
26 import tim.prune.threedee.WindowFactory;
27 import tim.prune.tips.TipManager;
28
29 /**
30  * Class to show the 3d window
31  */
32 public class ShowThreeDFunction extends GenericFunction
33 {
34         /** Dialog for input parameters */
35         private JDialog _dialog = null;
36         /** Field for altitude exaggeration value */
37         private DecimalNumberField _exaggField = null;
38         /** Component for defining the base image */
39         private BaseImageDefinitionPanel _baseImagePanel = null;
40         /** Component for defining the terrain */
41         private TerrainDefinitionPanel _terrainPanel = null;
42
43         /**
44          * Constructor
45          * @param inApp app object
46          */
47         public ShowThreeDFunction(App inApp)
48         {
49                 super(inApp);
50         }
51
52         /**
53          * Get the name key
54          */
55         public String getNameKey() {
56                 return "function.show3d";
57         }
58
59         /**
60          * Begin the function
61          */
62         public void begin()
63         {
64                 ThreeDWindow window = WindowFactory.getWindow(_parentFrame);
65                 if (window == null)
66                 {
67                         JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.function.nojava3d"),
68                                 I18nManager.getText("error.function.notavailable.title"), JOptionPane.WARNING_MESSAGE);
69                 }
70                 else
71                 {
72                         // See if the track has any altitudes at all - if not, show a tip to use SRTM
73                         if (!_app.getTrackInfo().getTrack().hasAltitudeData()) {
74                                 _app.showTip(TipManager.Tip_UseSrtmFor3d);
75                         }
76                         // Show a dialog to get the parameters
77                         if (_dialog == null)
78                         {
79                                 _dialog = new JDialog(_app.getFrame(), I18nManager.getText(getNameKey()), true);
80                                 _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
81                                 _dialog.getContentPane().add(makeDialogComponents());
82                                 _dialog.pack();
83                         }
84                         final int exaggFactor = Config.getConfigInt(Config.KEY_HEIGHT_EXAGGERATION);
85                         if (exaggFactor > 0) {
86                                 _exaggField.setValue(exaggFactor / 100.0);
87                         }
88                         _baseImagePanel.updateBaseImageDetails();
89                         _dialog.setLocationRelativeTo(_app.getFrame());
90                         _dialog.setVisible(true);
91                 }
92         }
93
94         /**
95          * Make the dialog components to select the options
96          * @return JPanel holding the gui elements
97          */
98         private JPanel makeDialogComponents()
99         {
100                 JPanel mainPanel = new JPanel();
101                 mainPanel.setLayout(new BorderLayout(4, 4));
102
103                 JPanel innerPanel = new JPanel();
104                 innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.Y_AXIS));
105                 // Panel for altitude exaggeration
106                 JPanel exaggPanel = new JPanel();
107                 exaggPanel.setLayout(new FlowLayout());
108                 exaggPanel.add(new JLabel(I18nManager.getText("dialog.3d.altitudefactor") + ": "));
109                 _exaggField = new DecimalNumberField(); // don't allow negative numbers
110                 _exaggField.setText("5.0");
111                 exaggPanel.add(_exaggField);
112                 innerPanel.add(exaggPanel);
113                 innerPanel.add(Box.createVerticalStrut(4));
114
115                 // Panel for terrain
116                 _terrainPanel = new TerrainDefinitionPanel();
117                 innerPanel.add(_terrainPanel);
118                 mainPanel.add(innerPanel, BorderLayout.NORTH);
119                 innerPanel.add(Box.createVerticalStrut(4));
120
121                 // Panel for base image (null because we don't need callback)
122                 _baseImagePanel = new BaseImageDefinitionPanel(null, _dialog, _app.getTrackInfo().getTrack());
123                 innerPanel.add(_baseImagePanel);
124
125                 // OK, Cancel buttons
126                 JPanel buttonPanel = new JPanel();
127                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
128                 JButton okButton = new JButton(I18nManager.getText("button.ok"));
129                 okButton.addActionListener(new ActionListener() {
130                         public void actionPerformed(ActionEvent e)
131                         {
132                                 _dialog.dispose();
133                                 new Thread(new Runnable() {
134                                         public void run() {
135                                                 finish();  // needs to be in separate thread
136                                         }
137                                 }).start();
138                         }
139                 });
140                 buttonPanel.add(okButton);
141                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
142                 cancelButton.addActionListener(new ActionListener() {
143                         public void actionPerformed(ActionEvent e)
144                         {
145                                 _dialog.dispose();
146                         }
147                 });
148                 buttonPanel.add(cancelButton);
149                 mainPanel.add(buttonPanel, BorderLayout.SOUTH);
150
151                 return mainPanel;
152         }
153
154         /**
155          * All parameters have been selected in the input dialog, now we can go to the 3d window
156          */
157         private void finish()
158         {
159                 // Store exaggeration factor and grid size in config
160                 Config.setConfigInt(Config.KEY_HEIGHT_EXAGGERATION, (int) (_exaggField.getValue() * 100));
161                 int terrainGridSize = _terrainPanel.getGridSize();
162                 if (terrainGridSize < 20) {terrainGridSize = 20;}
163                 Config.setConfigInt(Config.KEY_TERRAIN_GRID_SIZE, terrainGridSize);
164
165                 ThreeDWindow window = WindowFactory.getWindow(_parentFrame);
166                 if (window != null)
167                 {
168                         try
169                         {
170                                 // Pass the parameters to use and show the window
171                                 window.setTrack(_app.getTrackInfo().getTrack());
172                                 window.setAltitudeFactor(_exaggField.getValue());
173                                 // Also pass the base image parameters from input dialog
174                                 window.setBaseImageParameters(_baseImagePanel.getImageDefinition());
175                                 window.setTerrainParameters(new TerrainDefinition(_terrainPanel.getUseTerrain(), _terrainPanel.getGridSize()));
176                                 window.setDataStatus(_app.getCurrentDataStatus());
177                                 window.show();
178                         }
179                         catch (ThreeDException e)
180                         {
181                                 _app.showErrorMessageNoLookup(getNameKey(), I18nManager.getText("error.3d") + ": " + e.getMessage());
182                         }
183                 }
184         }
185 }