ingo@534: package de.intevation.flys.client.client.ui.chart; ingo@534: raimund@2477: import java.util.ArrayList; raimund@2477: ingo@534: import com.smartgwt.client.widgets.Canvas; ingo@534: import com.smartgwt.client.widgets.Label; ingo@534: import com.smartgwt.client.widgets.layout.HLayout; ingo@534: import com.smartgwt.client.widgets.events.MouseMoveEvent; ingo@534: import com.smartgwt.client.widgets.events.MouseMoveHandler; ingo@538: ingo@538: import de.intevation.flys.client.shared.Transform2D; ingo@534: ingo@534: ingo@534: /** ingo@3549: * Panel showing the mouse position in data space. ingo@534: * @author Ingo Weinzierl ingo@534: */ ingo@538: public class MousePositionPanel extends HLayout implements MouseMoveHandler { ingo@538: felix@4038: /** Is associated to a ChartTab. */ ingo@538: protected ChartOutputTab chartTab; ingo@538: raimund@2477: protected HLayout xLayout; raimund@2477: protected ArrayList yLayouts; ingo@534: felix@4038: ingo@538: public MousePositionPanel(ChartOutputTab chartTab) { ingo@534: super(); ingo@534: ingo@538: this.chartTab = chartTab; ingo@534: ingo@538: chartTab.getChartPanel().addMouseMoveHandler(this); ingo@538: ingo@534: initLayout(); ingo@534: } ingo@534: ingo@534: ingo@534: /** ingo@539: * Initializes the layout of this component. Note: This layout has a ingo@539: * fixed width of 195px plus a margin of 5px. ingo@534: */ ingo@534: protected void initLayout() { ingo@534: setMembersMargin(5); ingo@534: raimund@2477: xLayout = null; raimund@2477: yLayouts = new ArrayList(); ingo@534: } ingo@534: ingo@534: ingo@534: /** ingo@538: * Listens to mouse move events to refresh the xy position. ingo@534: * ingo@538: * @param event The move event. ingo@534: */ ingo@538: public void onMouseMove(MouseMoveEvent event) { ingo@538: updateMousePosition(event.getX(), event.getY()); ingo@534: } ingo@534: ingo@534: ingo@534: /** ingo@538: * This method takes pixel coordinates, transforms those values into chart ingo@538: * coordinates using the Transform2D class and updates the mouse position. ingo@534: * ingo@538: * @param x The x part of the pixel. ingo@538: * @param y The y part of the pixel. ingo@534: */ ingo@538: public void updateMousePosition(double x, double y) { raimund@2477: int transformerCount = chartTab.getTransformerCount(); ingo@534: ingo@538: Canvas chart = chartTab.getChartPanel(); ingo@538: int xOffset = chart.getPageLeft(); ingo@538: int yOffset = chart.getPageTop(); ingo@534: ingo@538: x = x - xOffset; ingo@538: y = y - yOffset; ingo@538: raimund@2477: // Create Layout for x coordinates. raimund@2477: if (xLayout == null){ raimund@2477: Label xDesc = new Label("Position: X = "); raimund@2477: Label xLabel = new Label(); raimund@2477: xLayout = new HLayout(); raimund@2477: xLayout.setWidth(125); raimund@2477: xLayout.addMember(xDesc); raimund@2477: xLayout.addMember(xLabel); raimund@2477: xDesc.setWidth(70); raimund@2477: xLabel.setWidth(55); raimund@2477: addMember(xLayout); raimund@2477: } ingo@538: raimund@2477: for (int i = 0; i < transformerCount; i++) { raimund@2477: HLayout yLayout = null; raimund@2477: // If no layout exists for this y axis, create one. raimund@2477: // else use the existing one. raimund@2477: if (yLayouts.size() <= i) { raimund@2477: Label yDesc = new Label("Y" + (i+1) + " = "); raimund@2477: Label yLabel = new Label(); raimund@2477: yLayout = new HLayout(); raimund@2477: yLayout.setWidth(80); raimund@2477: yLayout.addMember(yDesc); raimund@2477: yLayout.addMember(yLabel); raimund@2477: yDesc.setWidth(30); raimund@2477: yLabel.setWidth(50); raimund@2477: addMember(yLayout); raimund@2477: yLayouts.add(i, yLayout); raimund@2477: } raimund@2477: else { raimund@2477: yLayout = yLayouts.get(i); raimund@2477: } raimund@2477: raimund@2477: Transform2D transformer = chartTab.getTransformer(i); raimund@2477: raimund@2477: if (transformer == null) { raimund@2477: return; raimund@2477: } raimund@2477: raimund@2477: // Get the label for the coordinates. raimund@2477: Canvas xLabel = xLayout.getMember(1); raimund@2477: Canvas yLabel = yLayout.getMember(1); raimund@2477: felix@4038: double[] xy = transformer.transform(x, y); raimund@2477: String[] xyStr = transformer.format(new Number[] { raimund@2477: new Double(xy[0]), new Double(xy[1]) }); raimund@2477: // Set the coordinates. raimund@2477: xLabel.setContents(xyStr[0]); raimund@2477: yLabel.setContents(xyStr[1]); raimund@2477: } raimund@2477: raimund@2477: // Remove y coordinates. raimund@2477: if (yLayouts.size() > transformerCount) { raimund@2477: for (int i = yLayouts.size() - 1; i >= transformerCount; i--) { raimund@2477: removeMember(yLayouts.get(i)); raimund@2477: yLayouts.remove(i); raimund@2477: } raimund@2477: } ingo@534: } ingo@534: } ingo@534: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :