1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
public class CreateSensorSiteHandler implements EditionHandler{
/** The cursor to display on the map while the handler is installed. */
private Cursor CUR_NEW_SENSOR_SITE = null;
/** Is the handler currently installed ? */
private boolean installed = false;
/** The map associated with the handler. */
private MSS_Map map;
/**
* The {@link javax.swing.event.MouseInputListener}
* used to create a new site.
*/
MouseInputListener listener = new MouseListen();
/** The decoration used to diplay the coordinates of mouse. */
PointerDecoration decoration = new PointerDecoration();
/**
* Constructor.
* Creates a handler with a specific cursor.
*/
public CreateSensorSiteHandler(){
buildcursor();
}
/**
* Builds a specific cursor, for the action of
* creating new sensor sites.
*/
private void buildcursor(){
Toolkit tk = Toolkit.getDefaultToolkit();
ImageIcon ico_newSite = new ImageIcon(
this.getClass().getResource("/resources/cursors/createSite.gif"));
BufferedImage img =
new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
img.getGraphics().drawImage(ico_newSite.getImage(), 0, 0, null);
CUR_NEW_SENSOR_SITE = tk.createCustomCursor(img,
new Point(1, 1), "new_site");
}
public void cancelEdition() {}
public ImageIcon getIcon() {return null;}
public String getTitle() {
return "Create Sensor Site Handler";
}
public boolean isInstalled() {
return installed;
}
/**
* Called when {@code map.setEditionHandler(new CreateSensorSiteHandler());}
*
* @param curMap The map used.
*/
public void install(EditableMap2D curMap) {
installed = true;
map = (MSS_Map)curMap;
map.addDecoration(decoration);
}
public void uninstall() {
installed = false;
map.removeDecoration(decoration);
map = null;
}
/**
* Call when {@code map.setActionState(ACTION_STATE.EDIT);}
*
* @param curMap The map used.
*/
public void installListeners(EditableMap2D curMap) {
map = (MSS_Map)curMap;
map.getComponent().addMouseListener(listener);
map.getComponent().addMouseMotionListener(listener);
}
public void uninstallListeners() {
map.getComponent().removeMouseListener(listener);
map.getComponent().removeMouseMotionListener(listener);
}
/* -------------------------- INNER CLASS ------------------------- */
/**
* This inner class specify the action of the handler.
* In our case, when the mouse is clicked, we display a {@code JOptionPane}
* to get the name of the site, and then , we create the new sensor
* site.
* <br><br>
* We also specify that when the mouse is on the map, the cursos has a
* specific form.
*
* @author Thomas Bonavia
*
* @see java.awt.event.MouseListener
*/
private class MouseListen implements MouseInputListener{
public void mouseMoved(MouseEvent e) {
decoration.setInfosToDraw(e.getX(), e.getY(), map, true);
}
/**
* Allows to create a new site.
*
* @param e The mouse event generated.
*/
public void mouseClicked(MouseEvent e) {
decoration.setInfosToDraw(e.getX(), e.getY(), map, false);
/** PARTIE A MODIFIER - PLUS DE JOPTIONPANE */
String reponse = JOptionPane.showInputDialog(null,
"what is the name for the site?");
// If nothing was entered in the input area
if(reponse == null) {
return;
} else if(reponse.equals("")) {
JOptionPane.showMessageDialog(null,
"A name must be given to the state",
"Warning",JOptionPane.WARNING_MESSAGE);
} else {
// We get the map coordinates
Coordinate coord =
MSS_Window.getInstance().getMap().getRenderingStrategy().
toMapCoord(e.getX(), e.getY());
double tElev = MSS_ControlProject.getRaster().getElevation(
coord.x, coord.y);
// We create the new sensor site -> Replace 0 by the the height
MSS_ControlProject.addSensorSite(
new SensorSiteStation(reponse,coord.x, coord.y,0,tElev));
}
/** FIN PARTIE A MODIFIER */
decoration.setInfosToDraw(e.getX(), e.getY(), map, true);
}
/**
* Allows to change the cursor.
*
* @param e The mouse event generated.
*/
public void mouseEntered(MouseEvent e) {
map.getComponent().setCursor(CUR_NEW_SENSOR_SITE);
}
public void mouseExited(MouseEvent e) {
decoration.setInfosToDraw(e.getX(), e.getY(), map, false);
}
public void mouseDragged(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
} |
Partager