/* * Copyright (c) 2010, Oracle Corporation and/or its affiliates * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Sun Microsystems nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package map; /** * * @author sk219469 */ import javax.swing.*; import java.awt.GridLayout; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import org.jdesktop.swingx.JXMapKit; import org.jdesktop.swingx.mapviewer.GeoPosition; import org.jdesktop.swingx.mapviewer.util.GeoUtil; public class MapViewer extends JPanel implements ActionListener { JTextField streetField = null; JTextField cityField = null; JTextField stateField = null; JButton showBtn = null; JXMapKit mapkit = null; MapApplet mapApplet = null; public MapViewer(MapApplet app) { mapApplet = app; createGUI(); } private void createGUI() { JPanel addrFields = new JPanel(); addrFields.setLayout(new GridLayout(4, 2)); addrFields.setBackground(Color.WHITE); JLabel lbl = new JLabel("Hello " + mapApplet.userName + "!"); lbl.setFont(new Font("Serif", Font.BOLD, 16)); lbl.setForeground(Color.MAGENTA); addrFields.add(lbl); lbl = new JLabel(""); addrFields.add(lbl); lbl = new JLabel("Street: "); addrFields.add(lbl); streetField = new JTextField(25); addrFields.add(streetField); lbl = new JLabel("City: "); addrFields.add(lbl); cityField = new JTextField(25); addrFields.add(cityField); lbl = new JLabel("State (United States state or leave blank): "); addrFields.add(lbl); stateField = new JTextField(25); addrFields.add(stateField); setBackground(Color.WHITE); setLayout(new BorderLayout()); add(addrFields, BorderLayout.WEST); showBtn = new JButton("Show on Map"); add(showBtn, BorderLayout.EAST); showBtn.addActionListener(this); mapkit = createMapKit(); add(mapkit, BorderLayout.SOUTH); } private JXMapKit createMapKit() { JXMapKit mk = new JXMapKit(); mk.setDefaultProvider(JXMapKit.DefaultProviders.OpenStreetMaps); return mk; } public void actionPerformed(ActionEvent e) { if (e.getSource() == showBtn) { showAddressOnMap(); } } private void showAddressOnMap() { GeoPosition gpos = null; String street = null; String city = null; String state = null; try { street = streetField.getText().trim(); city = cityField.getText().trim(); state = stateField.getText().trim(); gpos = GeoUtil.getPositionForAddress(street, city, state); mapApplet.updateWebPage(street, city, state); } catch (Exception e) { e.printStackTrace(); gpos = new GeoPosition(51.5,0); } mapkit.setAddressLocation(gpos); } }