[Schmitzm-commits] r2238 - trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/selection
scm-commit at wald.intevation.org
scm-commit at wald.intevation.org
Thu Feb 21 16:57:25 CET 2013
Author: mojays
Date: 2013-02-21 16:57:25 +0100 (Thu, 21 Feb 2013)
New Revision: 2238
Added:
trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/selection/ChartTableSynchronizer.java
Log:
New generic ChartTableSynchronizer
Added: trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/selection/ChartTableSynchronizer.java
===================================================================
--- trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/selection/ChartTableSynchronizer.java (rev 0)
+++ trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/selection/ChartTableSynchronizer.java 2013-02-21 15:57:25 UTC (rev 2238)
@@ -0,0 +1,184 @@
+/**
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ *
+ * This file is part of the SCHMITZM library - a collection of utility
+ * classes based on Java 1.6, focusing (not only) on Java Swing
+ * and the Geotools library.
+ *
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ *
+ * Contributors:
+ * Martin O. J. Schmitz - initial API and implementation
+ * Stefan A. Tzeggai - additional utility classes
+ */
+package de.schmitzm.jfree.chart.selection;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.swing.ListSelectionModel;
+import javax.swing.event.ListSelectionEvent;
+import javax.swing.event.ListSelectionListener;
+
+import org.jfree.chart.ChartPanel;
+import org.jfree.chart.JFreeChart;
+import org.jfree.data.xy.TableXYDataset;
+
+import de.schmitzm.jfree.JFreeChartUtil;
+import de.schmitzm.jfree.chart.SelectableChartPanel;
+import de.schmitzm.swing.SelectableJTable;
+
+/**
+ * Generic synchronizer to keep a {@link SelectableChartPanel} selection
+ * synchronized with a {@link SelectableJTable} which shows the chart data.
+ * This is done by implementing:
+ * <ul>
+ * <li>a {@link ListSelectionListener} which listens to the {@link SelectableJTable}
+ * and accordingly changes the {@link DatasetSelectionModel} selection of the
+ * chart/plot</li>
+ * <li>a {@link DatasetSelectionModel} which listens to the {@link DatasetSelectionModel}
+ * and accordingly changes the {@link ListSelectionModel} selection of the table</li>
+ * </ul>
+ * After creating, the instance of this synchronizer must be added as listener
+ * to both, the table's {@link ListSelectionModel} and the chart's
+ * {@link DatasetSelectionModel} (e.g. the renderer).
+ * @see DatasetSelectionModelProvider
+ * @author Martin O.J. Schmitz
+ */
+public class ChartTableSynchronizer implements DatasetSelectionListener, ListSelectionListener {
+ private boolean selectionChangeCausedByMe = false;
+ private boolean enabled = true;
+ private JFreeChart chart = null;
+ private TableXYDataset dataset = null;
+ private SelectableChartPanel chartPanel = null;
+ private SelectableJTable chartTable = null;
+
+ /**
+ * Creates a new synchronizer.
+ * @param table table too keep synchronized
+ * @param dataset the dataset (in {@code chart}) which holds the data to synchronized
+ * @param chart chart where the selection is performed on (also holds the plot with the
+ * {@link DatasetSelectionModel DatasetSelectionModels}); if {@code null},
+ * {@link ChartPanel#getChart()} is used; {@code null} should also work
+ * in case of a combined charts, but specifying the chart directly may
+ * result in a bit more performance, because of less calls/recursions in
+ * {@link JFreeChartUtil#getSelectionModelForDataset(org.jfree.chart.plot.Plot, org.jfree.data.general.Dataset)}
+ * @param chartPanel chart panel (only used for refresh after synchronize from table)
+ */
+ public ChartTableSynchronizer(SelectableJTable table, TableXYDataset dataset, JFreeChart chart, SelectableChartPanel chartPanel) {
+ this.chartTable = table;
+ this.dataset = dataset;
+ this.chartPanel = chartPanel;
+ this.chart = chart != null ? chart : chartPanel.getChart();
+ }
+
+ /**
+ * Enables/disables the synchronization completely.
+ */
+ public void setEnabled(boolean enabled) {
+ this.enabled = enabled;
+ }
+
+ /**
+ * Checks whether the synchronization is enabled.
+ */
+ public boolean isEnabled() {
+ return this.enabled;
+ }
+
+ /**
+ * Reacts on table selection change with updating the chart selection.
+ */
+ @Override
+ public void valueChanged(ListSelectionEvent e) {
+ if ( !isEnabled() )
+ return;
+ if ( selectionChangeCausedByMe || e.getValueIsAdjusting() )
+ return;
+
+ selectionChangeCausedByMe = true;
+
+ List<Comparable<?>> seriesKeys = getAllSeriesKeys();
+ DatasetSelectionModel<TableXYDataset, Comparable<?>, Integer> chartSelModel = (DatasetSelectionModel<TableXYDataset, Comparable<?>, Integer>)JFreeChartUtil.getSelectionModelForDataset(chart.getPlot(), dataset);
+ chartSelModel.setValueIsAdjusting(true);
+ chartSelModel.clearSelection();
+ // For all selected rows in table, mark the corresponding
+ // items in chart
+ int[] selRows = chartTable.getSelectedRows();
+ for (int row : selRows) {
+ // Mark the items of all series!
+ for (Comparable<?> seriesKey : seriesKeys) {
+ chartSelModel.setItemSelected(seriesKey, row, true);
+ }
+ }
+ chartSelModel.setValueIsAdjusting(false);
+
+ selectionChangeCausedByMe = false;
+ chartPanel.refresh();
+ }
+
+ /**
+ * Reacts on chart selection change with updating the table selection.
+ */
+ @Override
+ public void selectionChanged(DatasetSelectionChangeEvent e) {
+ if ( !isEnabled() )
+ return;
+ if ( selectionChangeCausedByMe || e.getSource().getValueIsAdjusting() )
+ return;
+ selectionChangeCausedByMe = true;
+
+ List<Comparable<?>> seriesKeys = getAllSeriesKeys();
+ DatasetSelectionModel<TableXYDataset, Comparable<?>, Integer> chartSelModel = (DatasetSelectionModel<TableXYDataset, Comparable<?>, Integer>)JFreeChartUtil.getSelectionModelForDataset(chart.getPlot(), dataset);
+ ListSelectionModel listSelModel = chartTable.getSelectionModel();
+ listSelModel.setValueIsAdjusting(true);
+ listSelModel.clearSelection();
+ // Check all rows of the table if it must be selected
+ int firstSelectedRow = -1;
+ for (int i=0; i<chartTable.getRowCount(); i++) {
+ // Check all series if at least one is selected.
+ // In this case, select the whole row in table!
+ for (Comparable<?> seriesKey : seriesKeys) {
+ if ( chartSelModel.isItemSelected(seriesKey, i) ) {
+ if ( firstSelectedRow < 0 )
+ firstSelectedRow = i;
+ listSelModel.addSelectionInterval(i, i);
+ break;
+ }
+ }
+ }
+ selectionChangeCausedByMe = false; // IMPORTANT!! Set FALSE before setValueIsAdjusting(false)!!
+ listSelModel.setValueIsAdjusting(false);
+
+ if ( firstSelectedRow >= 0 )
+ chartTable.scrollRectToVisible( chartTable.getCellRect(firstSelectedRow, 0, true) );
+ }
+
+ /**
+ * Returns all series keys from dataset.
+ */
+ private List<Comparable<?>> getAllSeriesKeys() {
+ List<Comparable<?>> seriesKeys = new ArrayList<Comparable<?>>();
+ for (int series=0; series<dataset.getSeriesCount(); series++)
+ seriesKeys.add(dataset.getSeriesKey(series));
+ return seriesKeys;
+
+ }
+
+}
More information about the Schmitzm-commits
mailing list