Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/artifacts/charts/CrossSectionApp.java @ 1672:0b6dac664bbb
Moved some generic double array code from BackjumpCorrector to DoubleUtil
flys-artifacts/trunk@2885 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 04 Oct 2011 15:29:39 +0000 |
parents | a7def20539fb |
children | 595c404523a6 |
line wrap: on
line source
package de.intevation.flys.artifacts.charts; import java.math.MathContext; import java.awt.Dimension; import java.awt.BorderLayout; import java.awt.FlowLayout; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JTextField; import javax.swing.DefaultComboBoxModel; import java.awt.event.ItemListener; import java.awt.event.ItemEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.geom.Point2D; import java.util.List; import java.io.File; import java.io.IOException; import java.io.FileWriter; import java.io.PrintWriter; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RefineryUtilities; import org.jfree.chart.ChartFactory; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.ChartPanel; import org.jfree.chart.axis.NumberAxis; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.DefaultXYDataset; import de.intevation.flys.model.CrossSection; import de.intevation.flys.model.CrossSectionLine; import de.intevation.flys.model.CrossSectionPoint; import de.intevation.flys.geom.Lines; import de.intevation.flys.backend.SessionFactoryProvider; import org.hibernate.Session; import org.hibernate.Query; public class CrossSectionApp extends ApplicationFrame { public static final String RIVER = System.getProperty("river", "Saar"); public static final double EPSILON = 1e-4; protected Session session; protected JComboBox crossSectionsCB; protected JComboBox crossSectionLinesCB; protected JTextField waterlevelTF; protected ChartPanel chartPanel; protected Double lastWaterLevel; public static class CrossSectionItem { CrossSection crossSection; public CrossSectionItem(CrossSection crossSection) { this.crossSection = crossSection; } public String toString() { return crossSection.getDescription(); } } // CrossSectionItem public static class CrossSectionLineItem { CrossSectionLine line; public CrossSectionLineItem(CrossSectionLine line) { this.line = line; } public String toString() { double v = line.getKm().doubleValue(); return String.valueOf(Math.round(v * 1000.0)/1000d); } } // CrossSectionLineItem public CrossSectionApp(String title) { super(title); session = SessionFactoryProvider .createSessionFactory() .openSession(); JPanel content = createContent(); content.setPreferredSize(new Dimension(640, 480)); setContentPane(content); } public List<CrossSection> crossSections(String river) { Query query = session.createQuery( "from CrossSection where river.name = :river"); query.setParameter("river", river); return query.list(); } public JPanel createContent() { JPanel panel = new JPanel(new BorderLayout()); JPanel nav = new JPanel(new FlowLayout()); Object [] csis = createCrossSectionItems(); crossSectionsCB = new JComboBox(csis); DefaultComboBoxModel dcbm; if (csis.length > 0) { Object [] cslis = createCrossSectionLineItems( ((CrossSectionItem)csis[0]).crossSection); dcbm = new DefaultComboBoxModel(cslis); if (cslis.length > 0) { dcbm.setSelectedItem(cslis[0]); } } else { dcbm = new DefaultComboBoxModel(new Object[0]); } crossSectionLinesCB = new JComboBox(dcbm); nav.add(crossSectionsCB); nav.add(crossSectionLinesCB); crossSectionsCB.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ie) { if (ie.getStateChange() == ItemEvent.SELECTED) { updateCrossSection( ((CrossSectionItem)ie.getItem()).crossSection); } } }); crossSectionLinesCB.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ie) { if (ie.getStateChange() == ItemEvent.SELECTED) { updateChart(); } } }); waterlevelTF = new JTextField(5); waterlevelTF.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { waterLevelChanged(); } }); nav.add(waterlevelTF); JButton dump = new JButton("dump"); dump.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { dumpData(); } }); nav.add(dump); panel.add(nav, BorderLayout.SOUTH); chartPanel = createChartPanel(); panel.add(chartPanel, BorderLayout.CENTER); return panel; } protected void waterLevelChanged() { String value = waterlevelTF.getText(); try { lastWaterLevel = Double.parseDouble(value); } catch (NumberFormatException nfe) { waterlevelTF.setText( lastWaterLevel != null ? lastWaterLevel.toString() : ""); return; } updateChart(); } protected void updateChart() { CrossSectionLineItem csli = (CrossSectionLineItem)crossSectionLinesCB.getSelectedItem(); JFreeChart chart = createChart(csli == null ? new DefaultXYDataset() : generateDataset(csli.line, lastWaterLevel)); chartPanel.setChart(chart); } protected ChartPanel createChartPanel() { CrossSectionLineItem csli = (CrossSectionLineItem)crossSectionLinesCB.getSelectedItem(); JFreeChart chart = createChart(csli == null ? new DefaultXYDataset() : generateDataset(csli.line, lastWaterLevel)); return new ChartPanel(chart); } protected void dumpData() { CrossSectionLineItem csli = (CrossSectionLineItem)crossSectionLinesCB.getSelectedItem(); if (csli == null) { return; } CrossSectionLine line = csli.line; double km = Math.round(line.getKm().doubleValue() * 1000d)/1000d; String kmS = String.valueOf(km).replace(".", "-"); int i = 1; File file = new File("cross-section-" + kmS + ".txt"); while (file.exists()) { file = new File("cross-section-" + kmS + "[" + (i++) + "].txt"); } System.err.println("dump points to file '" + file + "'"); List<CrossSectionPoint> points = line.getPoints(); PrintWriter out = null; MathContext mc = new MathContext(3); try { out = new PrintWriter( new FileWriter(file)); for (CrossSectionPoint point: points) { out.println( point.getX().round(mc) + " " + point.getY().round(mc)); } out.flush(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { if (out != null) { out.close(); } } } public static XYDataset generateDataset( CrossSectionLine line, Double waterlevel ) { DefaultXYDataset dataset = new DefaultXYDataset(); List<Point2D> points = line.fetchCrossSectionLinesPoints(); if (points.isEmpty()) { return dataset; } if (waterlevel != null) { double [][] data = Lines.createWaterLines(points, waterlevel); dataset.addSeries(String.valueOf(waterlevel), data); } CrossSection cs = line.getCrossSection(); String legend = (cs != null ? cs.getDescription() : "???") + " " + Math.round(line.getKm().doubleValue() * 1000d)/1000d; double [][] values = CrossSectionLine.fetchCrossSectionProfile(points); dataset.addSeries(legend, values); return dataset; } protected void updateCrossSection(CrossSection crossSection) { Object [] cslis = createCrossSectionLineItems(crossSection); DefaultComboBoxModel dcbm = new DefaultComboBoxModel(cslis); if (cslis.length > 0) { dcbm.setSelectedItem(cslis[0]); } crossSectionLinesCB.setModel(dcbm); if (cslis.length > 0) { CrossSectionLine line = ((CrossSectionLineItem)cslis[0]).line; } updateChart(); } protected Object [] createCrossSectionLineItems(CrossSection cs) { List<CrossSectionLine> lines = cs.getLines(); Object [] result = new Object[lines.size()]; for (int i = 0; i < result.length; ++i) { result[i] = new CrossSectionLineItem(lines.get(i)); } return result; } protected Object [] createCrossSectionItems() { List<CrossSection> crossSections = crossSections(RIVER); Object [] result = new Object[crossSections.size()]; for (int i = 0; i < result.length; ++i) { result[i] = new CrossSectionItem(crossSections.get(i)); } return result; } public static JFreeChart createChart(XYDataset dataset) { JFreeChart chart = ChartFactory.createXYLineChart( null, "Abstand [m]", "H\u00f6he [m]", dataset, PlotOrientation.VERTICAL, true, true, false); XYPlot plot = chart.getXYPlot(); NumberAxis yAxis = (NumberAxis)plot.getRangeAxis(); yAxis.setAutoRangeIncludesZero(false); ChartUtilities.applyCurrentTheme(chart); return chart; } public static void main(String [] args) { CrossSectionApp csa = new CrossSectionApp("Querprofile"); csa.pack(); RefineryUtilities.centerFrameOnScreen(csa); csa.setVisible(true); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :