📄 gazetteerpanel.java
字号:
/*Copyright (C) 2001, 2006 United States Governmentas represented by the Administrator of theNational Aeronautics and Space Administration.All Rights Reserved.*/package gov.nasa.worldwind.examples;import gov.nasa.worldwind.WorldWindow;import gov.nasa.worldwind.exception.NoItemException;import gov.nasa.worldwind.geom.*;import gov.nasa.worldwind.geom.Position;import gov.nasa.worldwind.globes.Globe;import gov.nasa.worldwind.poi.*;import gov.nasa.worldwind.view.*;import org.xml.sax.SAXException;import javax.swing.*;import javax.swing.text.*;import javax.xml.parsers.ParserConfigurationException;import javax.xml.xpath.XPathExpressionException;import java.awt.*;import java.awt.event.*;import java.io.IOException;import java.net.URL;import java.util.ArrayList;import java.util.regex.*;/** * @author tag * @version $Id: GazetteerPanel.java 9192 2009-03-05 20:17:36Z tgaskins $ */public class GazetteerPanel extends JPanel{ private final WorldWindow wwd; private Gazetteer gazeteer; private JPanel resultsPanel; private JComboBox resultsBox; public GazetteerPanel(final WorldWindow wwd, String gazetteerClassName) throws IllegalAccessException, InstantiationException, ClassNotFoundException { super(new BorderLayout()); if (gazetteerClassName != null) this.gazeteer = this.constructGazetteer(gazetteerClassName); else this.gazeteer = new YahooGazetteer(); this.wwd = wwd; // The label URL imageURL = this.getClass().getResource("/images/32x32-icon-earth.png"); ImageIcon icon = new ImageIcon(imageURL); JLabel label = new JLabel(icon); label.setBorder(BorderFactory.createEmptyBorder(0, 15, 0, 15)); // The text field final JTextField field = new JTextField("Name or Lat,Lon?"); field.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent actionEvent) { EventQueue.invokeLater(new Runnable() { public void run() { try { handleEntryAction(actionEvent); } catch (NoItemException e) { JOptionPane.showMessageDialog(GazetteerPanel.this, "Location not available \"" + (field.getText() != null ? field.getText() : "") + "\"\n" + "(" + e.getMessage() + ")", "Location Not Available", JOptionPane.ERROR_MESSAGE); } catch (IllegalArgumentException e) { JOptionPane.showMessageDialog(GazetteerPanel.this, "Error parsing input \"" + (field.getText() != null ? field.getText() : "") + "\"\n" + e.getMessage(), "Lookup Failure", JOptionPane.ERROR_MESSAGE); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(GazetteerPanel.this, "Error looking up \"" + (field.getText() != null ? field.getText() : "") + "\"\n" + e.getMessage(), "Lookup Failure", JOptionPane.ERROR_MESSAGE); } } }); } }); // Enclose entry field in an inner panel in order to control spacing/padding JPanel fieldPanel = new JPanel(new BorderLayout()); fieldPanel.add(field, BorderLayout.CENTER); fieldPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 15)); // Put everything together this.add(label, BorderLayout.WEST); this.add(fieldPanel, BorderLayout.CENTER); resultsPanel = new JPanel(new GridLayout(1,2)); resultsPanel.add(new JLabel("Results: ")); resultsBox = new JComboBox(); resultsBox.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent actionEvent) { EventQueue.invokeLater(new Runnable() { public void run() { JComboBox cb = (JComboBox)actionEvent.getSource(); PointOfInterest selectedPoi = (PointOfInterest)cb.getSelectedItem(); moveToLocation(selectedPoi); } }); } }); resultsPanel.add(resultsBox); resultsPanel.setVisible(false); this.add(resultsPanel, BorderLayout.EAST); } private Gazetteer constructGazetteer(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException { if (className == null || className.length() == 0) { throw new IllegalArgumentException("Gazetteer class name is null"); } Class c = Class.forName(className.trim()); Object o = c.newInstance(); if (!(o instanceof Gazetteer)) throw new IllegalArgumentException("Gazetteer class name is null"); return (Gazetteer) o; } private void handleEntryAction(ActionEvent actionEvent) throws IOException, ParserConfigurationException, XPathExpressionException, SAXException, NoItemException, IllegalArgumentException { String lookupString = null; //hide any previous results resultsPanel.setVisible(false); if (actionEvent.getSource() instanceof JTextComponent) lookupString = ((JTextComponent) actionEvent.getSource()).getText(); if (lookupString == null || lookupString.length() < 1) return; java.util.List<PointOfInterest> poi = parseSearchValues(lookupString); if (poi != null) { if (poi.size() == 1) { this.moveToLocation(poi.get(0)); } else { resultsBox.removeAllItems(); for ( PointOfInterest p:poi) { resultsBox.addItem(p); } resultsPanel.setVisible(true); } } } /* Sample imputs Coordinate formats: 39.53, -119.816 (Reno, NV) 21 10 14 N, 86 51 0 W (Cancun) -31
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -