/**
* @(#)GisApp.java
* hcmfys@163.com
* GisApp application
* @author
* @version 1.00 2010/7/25
*/
import com.mapinfo.beans.tools.*;
import com.mapinfo.beans.vmapj.VisualMapJ;
import com.mapinfo.mapj.LabelProperties;
import com.mapinfo.mapj.Layer;
import com.mapinfo.mapj.Layers;
import com.mapinfo.mapj.MapJ;
import com.mapinfo.util.DoublePoint;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.concurrent.atomic.AtomicReference;
public class GisApp extends JFrame {
private VisualMapJ mapCtrl = null;
private JStatusBar myBar = null;
private MapToolBar toolBar;
private JPopupMenu popMenu = null;
private final int MOVE2LEFT = 1;
private final int MOVE2RIGHT = 2;
private final int MOVE2UP = 3;
private final int MOVE2DOWN = 4;
public GisApp() {
this.setSize(1000, 468);
this.setTitle("java gis ");
initToolBar();
initContextMenu();
makeCenter(this);
mapCtrl = new VisualMapJ();
mapCtrl.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent e) {
int type = e.getWheelRotation();
int x = e.getX();
int y = e.getY();
try {
double zoom = mapCtrl.getMapJ().getZoom();
myBar.getStatus(1).setText(String.format("zoom=%f", zoom));
DoublePoint dp = mapCtrl.getMapJ().transformScreenToNumeric(new DoublePoint(x, y));
myBar.getStatus(1).setText(String.format("zoom=%f", zoom));
if (type == -1) {
zoom = zoom / 2;
mapCtrl.getMapJ().setZoomAndCenter(zoom, dp);
mapCtrl.repaint();
} else {
zoom = zoom * 2;
mapCtrl.getMapJ().setZoomAndCenter(zoom, dp);
mapCtrl.repaint();
}
}
catch (Exception ex) {
}
}
}
);
mapCtrl.addMouseMotionListener(new
MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
try {
DoublePoint dp = mapCtrl.getMapJ().transformScreenToNumeric(new DoublePoint(x, y));
myBar.getStatus(0).setText(String.format("x=%f y=%f", dp.x, dp.y));
double zoom = mapCtrl.getMapJ().getZoom();
myBar.getStatus(1).setText(String.format("zoom=%f", zoom));
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
);
mapCtrl.addMouseListener(new
MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == e.BUTTON3) {
popMenu.show(GisApp.this, e.getX(), e.getY());
}
}
}
);
setLayerAttribute(mapCtrl.getMapJ());
this.setLayout(new BorderLayout());
this.getContentPane().add(toolBar, BorderLayout.NORTH);
this.getContentPane().add(mapCtrl, BorderLayout.CENTER);
initStatusBar();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initStatusBar() {
JPanel sPanel = new JPanel();
sPanel.setSize(600, 30);
int widths[] = {200, 150, 150, 160};
myBar = new JStatusBar(widths, sPanel);
this.getContentPane().add(sPanel, BorderLayout.SOUTH);
}
private void initToolBar() {
toolBar = new MapToolBar();
JButton btJb;
toolBar.removeAllMapTools();
toolBar.add(btJb = new JButton("open"));
toolBar.add(new ZoomInMapTool());
toolBar.add(new ZoomOutMapTool());
toolBar.add(new PanMapTool());
toolBar.add(new RecenterMapTool());
toolBar.add(new RulerMapTool());
toolBar.add(new InfoMapTool());
toolBar.add(new RectangleSelectionMapTool());
toolBar.add(new RadiusSelectionMapTool());
btJb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("GST files", "gst");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(GisApp.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String fileName = chooser.getSelectedFile().getAbsolutePath();
String dir = chooser.getCurrentDirectory().getAbsolutePath();
try {
mapCtrl.getMapJ().loadGeoset(fileName, dir, null);
mapCtrl.repaint();
Enumeration toolList = toolBar.getMapTools();
while (toolList.hasMoreElements()) {
MapTool tool = (MapTool) toolList.nextElement();
try {
if (tool instanceof PanMapTool) {
tool.setSelected(true);
break;
}
} catch (PropertyVetoException e1) {
e1.printStackTrace();
}
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
}
private void makeCenter(Component c) {
int x;
int y;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(screenSize.width, screenSize.height - toolBar.getHeight() - 1);
x = screenSize.width / 2 - c.getWidth() / 2;
y = screenSize.height / 2 - c.getHeight() / 2;
c.setLocation(x, y);
}
private void zoomIn() {
double zoom = 0;
try {
zoom = mapCtrl.getMapJ().getZoom();
myBar.getStatus(1).setText(String.format("zoom=%f", zoom));
zoom = zoom / 2;
mapCtrl.getMapJ().setZoom(zoom);
mapCtrl.repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
private void zoomOut() {
double zoom = 0;
try {
zoom = mapCtrl.getMapJ().getZoom();
myBar.getStatus(1).setText(String.format("zoom=%f", zoom));
zoom = zoom * 2;
mapCtrl.getMapJ().setZoom(zoom);
mapCtrl.repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param curTool
*/
private void selectTool(MapTool curTool) {
Enumeration toolList = toolBar.getMapTools();
while (toolList.hasMoreElements()) {
MapTool tool = (MapTool) toolList.nextElement();
try {
if (tool.toString().equals(curTool.toString())) {
tool.setSelected(true);
break;
}
} catch (PropertyVetoException e1) {
e1.printStackTrace();
}
}
}
/**
* @param actionType
*/
private void moveAction(int actionType) {
try {
double x = mapCtrl.getMapJ().getCenter().x;
double y = mapCtrl.getMapJ().getCenter().y;
double dx = mapCtrl.getMapJ().getBounds().width() / 2;
double dy = mapCtrl.getMapJ().getBounds().height() / 2;
switch (actionType) {
case MOVE2LEFT:
x += dx;
break;
case MOVE2RIGHT:
x -= dx;
break;
case MOVE2UP:
y -= dy;
break;
case MOVE2DOWN:
y += dy;
break;
default:
break;
}
mapCtrl.getMapJ().setCenter(new DoublePoint(x, y));
mapCtrl.repaint();
} catch (Exception ex) {
}
}
private void setLayerAttribute(MapJ mapj) {
Layers lys = mapj.getLayers();
int len = lys.size();
for (int i = 0; i < len; i++) {
Layer la = lys.elementAt(i);
LabelProperties lp = new LabelProperties(la.getLabelProperties());
com.mapinfo.graphics.Rendition re = lp.getRendition();
re.setValue(com.mapinfo.graphics.Rendition.FONT_FAMILY, "宋体");
lp.setRendition(re);
la.setVisible(true);
}
}
private void initContextMenu() {
popMenu = new JPopupMenu();
JMenuItem zoomIn = new JMenuItem("放大");
zoomIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
zoomIn();
}
});
popMenu.add(zoomIn);
JMenuItem zoomOut = new JMenuItem("缩小");
zoomOut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
zoomOut();
}
});
popMenu.add(zoomOut);
popMenu.addSeparator();
JMenuItem move2Left = new JMenuItem("向左移动");
move2Left.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveAction(MOVE2LEFT);
}
});
popMenu.add(move2Left);
JMenuItem move2Right = new JMenuItem("向右移动");
move2Right.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveAction(MOVE2RIGHT);
}
});
popMenu.add(move2Right);
JMenuItem move2Up = new JMenuItem("向上移动");
move2Up.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveAction(MOVE2UP);
}
});
popMenu.add(move2Up);
JMenuItem move2Down = new JMenuItem("向下移动");
move2Down.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveAction(MOVE2DOWN);
}
});
popMenu.add(move2Down);
}
public static void main(String[] args) {
GisApp app = new GisApp();
/*
ImageRequestComposer imageRC = ImageRequestComposer.create(mapj, 256, Color.blue, "image/gif");
MapXtremeImageRenderer renderer =new MapXtremeImageRenderer("http://localhost:8080/mapxtreme45/servlet/mapxtreme");
renderer.render(imageRC);
renderer.toFile("e:/12.gif");
*/
}
}