gvsig-webmap / org.gvsig.googlemaps / trunk / org.gvsig.googlemaps / org.gvsig.googlemaps.swing / org.gvsig.googlemaps.swing.impl / src / main / java / org / gvsig / googlemaps / swing / impl / DefaultGoogleMapsParametersPanelController.java @ 124
History | View | Annotate | Download (9.98 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2016 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.googlemaps.swing.impl; |
24 |
|
25 |
import java.awt.event.ActionEvent; |
26 |
import java.awt.event.ActionListener; |
27 |
import java.awt.event.MouseEvent; |
28 |
import java.awt.event.MouseListener; |
29 |
import java.net.URISyntaxException; |
30 |
import java.net.URL; |
31 |
|
32 |
import javax.swing.ComboBoxModel; |
33 |
import javax.swing.DefaultComboBoxModel; |
34 |
import javax.swing.JComponent; |
35 |
|
36 |
import org.gvsig.fmap.dal.DALLocator; |
37 |
import org.gvsig.fmap.dal.DataParameters; |
38 |
import org.gvsig.fmap.dal.DataStoreParameters; |
39 |
import org.gvsig.fmap.dal.coverage.store.parameter.TileDataParameters; |
40 |
import org.gvsig.fmap.dal.exception.InitializeException; |
41 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
42 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
43 |
import org.gvsig.googlemaps.lib.api.GoogleMapsLocator; |
44 |
import org.gvsig.googlemaps.lib.api.GoogleMapsManager; |
45 |
import org.gvsig.googlemaps.lib.api.MapTypes; |
46 |
import org.gvsig.googlemaps.prov.dalraster.GoogleMapsParams; |
47 |
import org.gvsig.googlemaps.swing.api.GoogleMapsParametersPanel; |
48 |
import org.gvsig.tools.ToolsLocator; |
49 |
import org.gvsig.tools.i18n.I18nManager; |
50 |
import org.gvsig.tools.swing.api.ActionListenerSupport; |
51 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
52 |
import org.slf4j.Logger; |
53 |
import org.slf4j.LoggerFactory; |
54 |
|
55 |
|
56 |
/**
|
57 |
* Implements a parameters panel to generate a Google Map Web layer
|
58 |
* @author daniel
|
59 |
*
|
60 |
*/
|
61 |
public class DefaultGoogleMapsParametersPanelController extends DefaultGoogleMapsParametersPanelView implements GoogleMapsParametersPanel { |
62 |
/**
|
63 |
*
|
64 |
*/
|
65 |
private static final long serialVersionUID = -3240423223084879324L; |
66 |
private static final String NAME="DefaultGoogleMapsParametersPanelController"; |
67 |
private static final Logger logger = LoggerFactory.getLogger(DefaultGoogleMapsParametersPanelController.class); |
68 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
69 |
GoogleMapsManager googleMapsManager=GoogleMapsLocator.getManager(); |
70 |
DefaultGoogleMapsConfigDataParameters params; |
71 |
private boolean showCrsWarning = false; |
72 |
private ActionListenerSupport listeners = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
|
73 |
private PanelAction selectedAction=null; |
74 |
|
75 |
/**
|
76 |
* creates a DefaultGoogleMapsParametersPanelController
|
77 |
* @param params
|
78 |
*/
|
79 |
public DefaultGoogleMapsParametersPanelController(DefaultGoogleMapsConfigDataParameters params){
|
80 |
super();
|
81 |
this.params=params;
|
82 |
initComponents(); |
83 |
translate(); |
84 |
} |
85 |
|
86 |
public void addActionListener(ActionListener listener) { |
87 |
listeners.addActionListener(listener); |
88 |
} |
89 |
|
90 |
public void removeActionListener(ActionListener listener) { |
91 |
listeners.removeActionListener(listener); |
92 |
} |
93 |
|
94 |
private void translate() { |
95 |
|
96 |
lblSelectMapType.setText(i18nManager.getTranslation(lblSelectMapType.getText())); |
97 |
lblSelectMapType.setToolTipText(i18nManager.getTranslation(lblSelectMapType.getToolTipText())); |
98 |
chkboxUseAPIKey.setText(i18nManager.getTranslation(chkboxUseAPIKey.getText())); |
99 |
chkboxUseAPIKey.setToolTipText(i18nManager.getTranslation(chkboxUseAPIKey.getToolTipText())); |
100 |
cboMapTypes.setToolTipText(i18nManager.getTranslation(cboMapTypes.getToolTipText())); |
101 |
txtAPIKey.setToolTipText(i18nManager.getTranslation(txtAPIKey.getToolTipText())); |
102 |
lblZoomLvl.setText(i18nManager.getTranslation(lblZoomLvl.getText())); |
103 |
lblZoomLvl.setToolTipText(i18nManager.getTranslation(lblZoomLvl.getToolTipText())); |
104 |
//txtDisclaimer.setText(i18nManager.getTranslation(txtDisclaimer.getText()));
|
105 |
txtDisclaimer.setText(i18nManager.getTranslation("_google_maps_disclaimer"));
|
106 |
txtDisclaimer.addMouseListener(new MouseListener() { |
107 |
|
108 |
|
109 |
@Override
|
110 |
public void mouseReleased(MouseEvent e) { |
111 |
// TODO Auto-generated method stub
|
112 |
|
113 |
} |
114 |
|
115 |
@Override
|
116 |
public void mousePressed(MouseEvent e) { |
117 |
// TODO Auto-generated method stub
|
118 |
|
119 |
} |
120 |
|
121 |
@Override
|
122 |
public void mouseExited(MouseEvent e) { |
123 |
// TODO Auto-generated method stub
|
124 |
|
125 |
} |
126 |
|
127 |
@Override
|
128 |
public void mouseEntered(MouseEvent e) { |
129 |
// TODO Auto-generated method stub
|
130 |
|
131 |
} |
132 |
|
133 |
@Override
|
134 |
public void mouseClicked(MouseEvent e) { |
135 |
selectedAction=GoogleMapsParametersPanel.PanelAction.SHOW_DISCLAIMER; |
136 |
listeners.fireActionEvent(new ActionEvent(DefaultGoogleMapsParametersPanelController.this, 0, "disclaimer")); |
137 |
} |
138 |
}); |
139 |
|
140 |
if(showCrsWarning){
|
141 |
StringBuilder builder = new StringBuilder(); |
142 |
builder.append("<html><font color=\"red\">");
|
143 |
builder.append(i18nManager.getTranslation("_crs_warning"));
|
144 |
builder.append("</font></body></html>");
|
145 |
txtCrsWarning.setText(builder.toString()); |
146 |
} else {
|
147 |
txtCrsWarning.setText("");
|
148 |
} |
149 |
} |
150 |
|
151 |
private void initComponents() { |
152 |
ComboBoxModel<MapTypes> MapTypeModel =
|
153 |
new DefaultComboBoxModel<MapTypes>(MapTypes.values()); |
154 |
String lastUsedApiKey= params.getLastUsedApiKey();
|
155 |
Integer lastUsedZoomLvl=params.getLastUsedZoomLvl();
|
156 |
|
157 |
cboMapTypes.setModel(MapTypeModel); |
158 |
cboMapTypes.setRenderer(new MapTypesCellRenderer());
|
159 |
|
160 |
if (lastUsedApiKey!=null && !lastUsedApiKey.isEmpty()){ |
161 |
chkboxUseAPIKey.setSelected(true);
|
162 |
txtAPIKey.setText(lastUsedApiKey); |
163 |
} |
164 |
|
165 |
if (lastUsedZoomLvl!=null ){ |
166 |
txtZoomLvl.setText(lastUsedZoomLvl.toString()); |
167 |
} |
168 |
txtAPIKey.setEnabled(chkboxUseAPIKey.isSelected()); |
169 |
chkboxUseAPIKey.addActionListener(new ActionListener() { |
170 |
|
171 |
@Override
|
172 |
public void actionPerformed(ActionEvent e) { |
173 |
txtAPIKey.setEnabled(chkboxUseAPIKey.isSelected()); |
174 |
} |
175 |
}); |
176 |
} |
177 |
|
178 |
|
179 |
|
180 |
|
181 |
@Override
|
182 |
public JComponent asJComponent() { |
183 |
return this; |
184 |
} |
185 |
|
186 |
@Override
|
187 |
public DataStoreParameters getParameters() throws InitializeException, ProviderNotRegisteredException { |
188 |
DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager(); |
189 |
GoogleMapsParams parameters = (GoogleMapsParams)dataman.createStoreParameters("Google Maps Provider");
|
190 |
|
191 |
String apiKey=txtAPIKey.getText();
|
192 |
boolean hasApiKey=chkboxUseAPIKey.isSelected();
|
193 |
MapTypes mapType=(MapTypes)cboMapTypes.getSelectedItem(); |
194 |
String idMapType=mapType.getId();
|
195 |
String descMapType=mapType.toString();
|
196 |
Integer zoomLvl=null; |
197 |
if (txtZoomLvl.getText().isEmpty()){
|
198 |
logger.error("Invalid zoom level");
|
199 |
throw new InitializeException(i18nManager.getTranslation("_zoom_lvl_not_valid"), null); |
200 |
} |
201 |
try{
|
202 |
zoomLvl=Integer.valueOf(txtZoomLvl.getText());
|
203 |
parameters.setNumberOfLevels(zoomLvl); |
204 |
}catch(Exception e){ |
205 |
logger.error("Invalid zoom level");
|
206 |
throw new InitializeException(i18nManager.getTranslation("_zoom_lvl_not_valid"), null); |
207 |
} |
208 |
|
209 |
parameters.setAPIKey(apiKey); |
210 |
parameters.setHasAPIKey(hasApiKey); |
211 |
parameters.setMapType(idMapType); |
212 |
URL url=params.getGoogleMapsURL();
|
213 |
try {
|
214 |
parameters.setURI(url.toURI()); |
215 |
} catch (URISyntaxException e) { |
216 |
logger.error("URI to data store couldn't be initialized "+url);
|
217 |
parameters.setURI(null);
|
218 |
} |
219 |
|
220 |
//Parameters used are saved for next time
|
221 |
if (apiKey!=null&&!apiKey.isEmpty()){ |
222 |
params.setLastUsedApiKey(apiKey); |
223 |
} |
224 |
if (zoomLvl!=null){ |
225 |
params.setLastUsedZoomLvl(zoomLvl); |
226 |
} |
227 |
|
228 |
//Locale parameters (Optional)
|
229 |
if (this.getLocale()!=null){ |
230 |
parameters.setLanguage(this.getLocale().getLanguage());
|
231 |
parameters.setRegion(this.getLocale().getCountry());
|
232 |
} |
233 |
|
234 |
parameters.setLayerName(i18nManager.getTranslation("_google_maps")+" "+descMapType); |
235 |
|
236 |
//return parameters;
|
237 |
TileDataParameters tileParams = (TileDataParameters) dataman.createStoreParameters("Tile Store");
|
238 |
tileParams.setDataParameters((DataParameters)parameters); |
239 |
return tileParams;
|
240 |
} |
241 |
|
242 |
@Override
|
243 |
public boolean areSettingsValid() { |
244 |
if (chkboxUseAPIKey.isSelected()){
|
245 |
if (txtAPIKey.getText().isEmpty()){
|
246 |
return false; |
247 |
} |
248 |
} |
249 |
if (txtZoomLvl.getText().isEmpty()){
|
250 |
return false; |
251 |
} |
252 |
try{
|
253 |
Integer.valueOf(txtZoomLvl.getText());
|
254 |
}catch(Exception e){ |
255 |
return false; |
256 |
} |
257 |
return true; |
258 |
} |
259 |
|
260 |
@Override
|
261 |
public String getName() { |
262 |
return NAME;
|
263 |
} |
264 |
|
265 |
/* (non-Javadoc)
|
266 |
* @see org.gvsig.googlemaps.swing.api.GoogleMapsParametersPanel#showCrsWarning(boolean)
|
267 |
*/
|
268 |
@Override
|
269 |
public void showCrsWarning(boolean showCrsWarning) { |
270 |
this.showCrsWarning = showCrsWarning;
|
271 |
translate(); |
272 |
} |
273 |
|
274 |
@Override
|
275 |
public boolean isSelectedAction(PanelAction action) { |
276 |
return action.equals(this.selectedAction); |
277 |
} |
278 |
|
279 |
} |