]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/BaseImageDefinitionPanel.java
Version 16, February 2014
[GpsPrune.git] / tim / prune / gui / BaseImageDefinitionPanel.java
1 package tim.prune.gui;
2
3 import java.awt.BorderLayout;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6
7 import javax.swing.BorderFactory;
8 import javax.swing.JButton;
9 import javax.swing.JDialog;
10 import javax.swing.JLabel;
11 import javax.swing.JPanel;
12 import javax.swing.border.EtchedBorder;
13
14 import tim.prune.I18nManager;
15 import tim.prune.data.Track;
16 import tim.prune.gui.map.MapSource;
17 import tim.prune.gui.map.MapSourceLibrary;
18 import tim.prune.save.BaseImageConfigDialog;
19 import tim.prune.save.BaseImageConsumer;
20 import tim.prune.save.MapGrouter;
21 import tim.prune.threedee.ImageDefinition;
22
23 /**
24  * Panel used to show the current base image details
25  * and an edit button to change the definition
26  */
27 public class BaseImageDefinitionPanel extends JPanel implements BaseImageConsumer
28 {
29         /** Parent object (if any) */
30         private BaseImageConsumer _parent = null;
31         /** Label to describe the current settings */
32         private JLabel _baseImageLabel = null;
33         /** Button for changing the definition */
34         private JButton _editButton = null;
35         /** Dialog called by the "Edit" button to change the settings */
36         private BaseImageConfigDialog _baseImageConfig = null;
37
38
39         /**
40          * Constructor
41          * @param inParent parent object to inform about changes
42          * @param inParentDialog parent dialog
43          * @param inTrack track object
44          */
45         public BaseImageDefinitionPanel(BaseImageConsumer inParent, JDialog inParentDialog,
46                 Track inTrack)
47         {
48                 _parent = inParent;
49                 _baseImageConfig = new BaseImageConfigDialog(this, inParentDialog, inTrack);
50
51                 // Etched border
52                 setBorder(BorderFactory.createCompoundBorder(
53                         BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(4, 4, 4, 4))
54                 );
55                 setLayout(new BorderLayout());
56
57                 // GUI components
58                 JPanel subPanel = new JPanel();
59                 subPanel.setLayout(new BorderLayout(10, 4));
60                 subPanel.add(new JLabel(I18nManager.getText("dialog.exportpov.baseimage") + ": "), BorderLayout.WEST);
61                 _baseImageLabel = new JLabel("Typical sourcename");
62                 subPanel.add(_baseImageLabel, BorderLayout.CENTER);
63                 _editButton = new JButton(I18nManager.getText("button.edit"));
64                 _editButton.addActionListener(new ActionListener() {
65                         public void actionPerformed(ActionEvent event) {
66                                 changeBaseImage();
67                         }
68                 });
69                 subPanel.add(_editButton, BorderLayout.EAST);
70                 add(subPanel, BorderLayout.NORTH);
71         }
72
73         /**
74          * @param inDefinition image definition from interactive step
75          */
76         public void initImageParameters(ImageDefinition inDefinition)
77         {
78                 _baseImageConfig.setImageDefinition(inDefinition);
79         }
80
81         /**
82          * Change the base image by calling the BaseImageConfigDialog
83          */
84         private void changeBaseImage()
85         {
86                 // Check if there is a cache to use
87                 if (BaseImageConfigDialog.isImagePossible())
88                 {
89                         // Show new dialog to choose image details
90                         _baseImageConfig.begin();
91                 }
92                 // TODO: What if it isn't possible?  Should the caller show the error message?
93                 //else {
94                 //      _app.showErrorMessage(getNameKey(), "dialog.exportimage.noimagepossible");
95                 //}
96         }
97
98         /**
99          * Callback from base image config dialog
100          */
101         public void baseImageChanged()
102         {
103                 updateBaseImageDetails();
104                 if (_parent != null) {
105                         _parent.baseImageChanged();
106                 }
107         }
108
109         /**
110          * Update the description label according to the selected base image details
111          */
112         public void updateBaseImageDetails()
113         {
114                 String desc = null;
115                 ImageDefinition imageDef = _baseImageConfig.getImageDefinition();
116                 // Check if selected zoom level is suitable or not, if not then set image to no
117                 if (imageDef.getUseImage() && !_baseImageConfig.isSelectedZoomValid()) {
118                         imageDef.setUseImage(false, imageDef.getSourceIndex(), 5);
119                 }
120                 // Make a description for the label
121                 if (imageDef.getUseImage())
122                 {
123                         MapSource source = MapSourceLibrary.getSource(imageDef.getSourceIndex());
124                         if (source != null)
125                         {
126                                 desc = source.getName() + " (" + imageDef.getZoom() + ")";
127                         }
128                 }
129                 if (desc == null) {
130                         desc = I18nManager.getText("dialog.about.no");
131                 }
132                 _baseImageLabel.setText(desc);
133                 _editButton.setEnabled(BaseImageConfigDialog.isImagePossible());
134         }
135
136         /**
137          * @return the grouter object for reuse of the prepared images
138          */
139         public MapGrouter getGrouter()
140         {
141                 return _baseImageConfig.getGrouter();
142         }
143
144         /**
145          * @return image definition
146          */
147         public ImageDefinition getImageDefinition() {
148                 return _baseImageConfig.getImageDefinition();
149         }
150
151         /**
152          * @return true if any tiles were found
153          */
154         public boolean getFoundData() {
155                 return _baseImageConfig.getFoundData();
156         }
157 }