Select Git revision
MapDialog.java
Forked from an inaccessible project.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
MapDialog.java 9.99 KiB
package fi.utu.dtek1066;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
// Kartankatseluohjelman graafinen käyttöliittymä
public class MapDialog extends JFrame {
// Kontrollinappien kuuntelija
// KAIKKIEN NAPPIEN YHTEYDESSÄ VOINEE HYÖDYNTÄÄ updateImage()-METODIA
private class ButtonListener implements ActionListener {
@Override
public synchronized void actionPerformed(ActionEvent e) {
if (e.getSource() == MapDialog.this.refreshB) {
MapDialog.this.updateImage();
}
if (e.getSource() == MapDialog.this.leftB) {
if (MapDialog.this.westernLongitude == -180) {
;
} else {
MapDialog.this.updateCoordinates(-10, -10, 0, 0);
MapDialog.this.updateImage();
}
}
if (e.getSource() == MapDialog.this.rightB) {
if (MapDialog.this.easternLongitude == 180) {
;
} else {
MapDialog.this.updateCoordinates(10, 10, 0, 0);
MapDialog.this.updateImage();
}
}
if (e.getSource() == MapDialog.this.upB) {
if (MapDialog.this.northernLatitude == 90) {
;
} else {
MapDialog.this.updateCoordinates(0, 0, 10, 10);
MapDialog.this.updateImage();
}
}
if (e.getSource() == MapDialog.this.downB) {
if (MapDialog.this.southernLatitude == -90) {
;
} else {
MapDialog.this.updateCoordinates(0, 0, -10, -10);
MapDialog.this.updateImage();
}
}
if (e.getSource() == MapDialog.this.zoomInB) {
if (Math.abs(MapDialog.this.westernLongitude - MapDialog.this.easternLongitude) <= 40
|| Math.abs(MapDialog.this.southernLatitude - MapDialog.this.northernLatitude) <= 20) {
;
} else {
MapDialog.this.updateCoordinates(20, -20, 10, -10);
MapDialog.this.updateImage();
}
}
if (e.getSource() == MapDialog.this.zoomOutB) {
if (MapDialog.this.westernLongitude == -180 || MapDialog.this.easternLongitude == 180
|| MapDialog.this.southernLatitude == -90 || MapDialog.this.northernLatitude == 90) {
;
} else {
MapDialog.this.updateCoordinates(-20, 20, -10, 10);
MapDialog.this.updateImage();
}
}
} // actionPerformed(ActionEvent)
} // class ButtonListener
// Valintalaatikko, joka muistaa karttakerroksen nimen
private class LayerCheckBox extends JCheckBox {
private static final long serialVersionUID = 1L;
private String name = "";
public LayerCheckBox(String name, String title, boolean selected) {
super(title, null, selected);
this.name = name;
} // LayerCheckBox(String, String, boolean)
@Override
public String getName() {
return this.name;
} // getName()
} // class LayerCheckBox
private static final long serialVersionUID = 1L;
public static void main(String[] args) throws Exception {
new MapDialog();
} // main(String[])
private final JLabel imageLabel = new JLabel();
private JPanel leftPanel = new JPanel();
private JButton refreshB = new JButton("Päivitä");
private JButton leftB = new JButton("<");
private JButton rightB = new JButton(">");
private JButton upB = new JButton("^");
private JButton downB = new JButton("v");
private JButton zoomInB = new JButton("+");
private JButton zoomOutB = new JButton("-");
private JButton resetB = new JButton("reset");
private Timer resizeTimer;
private int westernLongitude = -180;
private int easternLongitude = 180;
private int southernLatitude = -90;
private int northernLatitude = 90;
public MapDialog() throws Exception {
// Valmistele ikkuna ja lisää siihen komponentit
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());
// ALLA OLEVAN TESTIRIVIN VOI KORVATA JOLLAKIN MUULLA ERI ALOITUSNÄKYMÄN
// LATAAVALLA RIVILLÄ
this.imageLabel.setIcon(new ImageIcon(new URL("http://demo.mapserver.org/cgi-bin/wms?SERVICE="
+ "WMS&VERSION=1.1.1&REQUEST=GetMap&BBOX=-180,-90,180,90&SRS="
+ "EPSG:4326&WIDTH=1280&HEIGHT=720&LAYERS=WMS_server&STYLES="
+ "&FORMAT=image/png&TRANSPARENT=true")));
this.add(this.imageLabel, BorderLayout.EAST);
ButtonListener bl = new ButtonListener();
this.refreshB.addActionListener(bl);
this.leftB.addActionListener(bl);
this.rightB.addActionListener(bl);
this.upB.addActionListener(bl);
this.downB.addActionListener(bl);
this.zoomInB.addActionListener(bl);
this.zoomOutB.addActionListener(bl);
this.resetB.addActionListener(bl);
// Timer for delaying image updates when resizing the window
this.resizeTimer = new Timer(250, new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
MapDialog.this.updateImage();
}
});
this.resizeTimer.setRepeats(false);
this.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent evt) {
MapDialog.this.resizeTimer.restart();
}
});
this.leftPanel.setLayout(new BoxLayout(this.leftPanel, BoxLayout.Y_AXIS));
this.leftPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
this.leftPanel.setMaximumSize(new Dimension(100, 600));
this.populateLayerList();
this.leftPanel.add(this.refreshB);
this.leftPanel.add(Box.createVerticalStrut(20));
this.leftPanel.add(this.leftB);
this.leftPanel.add(this.rightB);
this.leftPanel.add(this.upB);
this.leftPanel.add(this.downB);
this.leftPanel.add(this.zoomInB);
this.leftPanel.add(this.zoomOutB);
this.leftPanel.add(this.resetB);
this.add(this.leftPanel, BorderLayout.WEST);
this.pack();
this.setVisible(true);
} // MapDialog()
// Tarkastetaan mitkä karttakerrokset on valittu,
// tehdään uudesta karttakuvasta pyyntö palvelimelle ja päivitetään kuva
public synchronized void updateImage() {
String s = "";
// Tutkitaan, mitkä valintalaatikot on valittu, ja
// kerätään s:ään pilkulla erotettu lista valittujen kerrosten
// nimistä (käytetään haettaessa uutta kuvaa)
Component[] components = this.leftPanel.getComponents();
for (Component com : components) {
if (com instanceof LayerCheckBox) {
if (((LayerCheckBox) com).isSelected()) {
s = s + com.getName() + ",";
}
}
}
if (s.endsWith(",")) {
s = s.substring(0, s.length() - 1);
}
final String layerSelection = s.toString();
new Thread() {
@Override
public void run() {
URL url = null;
try {
url = new URL("http://demo.mapserver.org/cgi-bin/wms?SERVICE="
+ "WMS&VERSION=1.1.1&REQUEST=GetMap&BBOX=" + MapDialog.this.westernLongitude + ","
+ MapDialog.this.southernLatitude + "," + MapDialog.this.easternLongitude + ","
+ MapDialog.this.northernLatitude + "&SRS=" + "EPSG:4326&WIDTH="
+ (MapDialog.this.getWidth() - MapDialog.this.leftPanel.getWidth()) + "&HEIGHT="
+ MapDialog.this.getHeight() + "&LAYERS=" + layerSelection + "&STYLES="
+ "&FORMAT=image/png&TRANSPARENT=true");
} catch (MalformedURLException e) {
e.printStackTrace();
}
MapDialog.this.imageLabel.setIcon(new ImageIcon(url));
} // run()
}.start();
} // updateImage()
/**
* Fetch the mapserver's capabilities and add their checkboxes to the left panel.
*
* @throws ParserConfigurationException If the capability parser creation fails
* @throws SAXException If parsing the capabilities fails
* @throws IOException If any IO errors occur
*/
private void populateLayerList() throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
Document document = parser.parse(new URL("http://demo.mapserver.org/cgi-bin/"
+ "wms?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetCapabilities").openStream());
Element mapservice = document.getDocumentElement();
NodeList capabilities = mapservice.getElementsByTagName("Layer");
for (int i = 0; i < capabilities.getLength(); i++) {
Element layer = (Element) capabilities.item(i);
String name = layer.getElementsByTagName("Name").item(0).getTextContent();
String title = layer.getElementsByTagName("Title").item(0).getTextContent();
this.leftPanel.add(new LayerCheckBox(name, title, (i == 0) ? true : false));
}
} // populateLayerList()
/**
* Updates the map focus coordinates according to the deltas provided.
*
* @param westDelta Difference in western longitude.
* @param eastDelta Difference in eastern longitude.
* @param southDelta Difference in southern latitude.
* @param northDelta Difference in northern latitude.
*/
private synchronized void updateCoordinates(int westDelta, int eastDelta, int southDelta,
int northDelta) {
this.westernLongitude += westDelta;
this.easternLongitude += eastDelta;
this.southernLatitude += southDelta;
this.northernLatitude += northDelta;
} // updateCoordinates(int, int, int, int)
// Käyttöliittymän komponentit
} // MapDialog