001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.gui.statusbar; 025 026 import java.text.NumberFormat; 027 028 import javax.swing.JLabel; 029 import javax.swing.event.ListSelectionEvent; 030 import javax.swing.event.ListSelectionListener; 031 import javax.swing.event.TableModelEvent; 032 import javax.swing.event.TableModelListener; 033 034 import org.ujmp.core.Matrix; 035 import org.ujmp.core.calculation.Calculation.Ret; 036 import org.ujmp.gui.MatrixGUIObject; 037 038 public class MatrixStatisticsBar extends JLabel implements TableModelListener, 039 ListSelectionListener { 040 private static final long serialVersionUID = 3434122072016632576L; 041 042 private MatrixGUIObject matrixGUIObject = null; 043 044 private UpdateThread updateThread = null; 045 046 public MatrixStatisticsBar(MatrixGUIObject m) { 047 this.matrixGUIObject = m; 048 m.addTableModelListener(this); 049 m.getRowSelectionModel().addListSelectionListener(this); 050 m.getColumnSelectionModel().addListSelectionListener(this); 051 update(); 052 } 053 054 public void update() { 055 if (updateThread != null) { 056 updateThread.interrupt(); 057 } 058 updateThread = new UpdateThread(matrixGUIObject, this); 059 updateThread.start(); 060 } 061 062 public void tableChanged(TableModelEvent e) { 063 update(); 064 } 065 066 public void valueChanged(ListSelectionEvent e) { 067 update(); 068 } 069 070 } 071 072 class UpdateThread extends Thread { 073 074 private MatrixGUIObject matrixGUIObject = null; 075 076 private JLabel jLabel = null; 077 078 private static final NumberFormat nf = NumberFormat.getNumberInstance(); 079 080 static { 081 nf.setMinimumFractionDigits(2); 082 nf.setMaximumFractionDigits(2); 083 } 084 085 public UpdateThread(MatrixGUIObject matrixGUIObject, JLabel jLabel) { 086 this.matrixGUIObject = matrixGUIObject; 087 this.jLabel = jLabel; 088 this.setPriority(Thread.MIN_PRIORITY); 089 } 090 091 public void run() { 092 jLabel.setText("calculating statistics..."); 093 094 long colMin = matrixGUIObject.getColumnSelectionModel() 095 .getMinSelectionIndex(); 096 long colMax = matrixGUIObject.getColumnSelectionModel() 097 .getMaxSelectionIndex(); 098 long rowMin = matrixGUIObject.getRowSelectionModel() 099 .getMinSelectionIndex(); 100 long rowMax = matrixGUIObject.getRowSelectionModel() 101 .getMaxSelectionIndex(); 102 103 Matrix m = null; 104 105 if (colMin < 0 || colMax < 0 || rowMin < 0 || rowMax < 0) { 106 m = matrixGUIObject.getMatrix(); 107 } else { 108 m = matrixGUIObject.getMatrix().subMatrix(Ret.LINK, rowMin, colMin, 109 rowMax, colMax); 110 } 111 112 long count = m.getValueCount(); 113 double min = m.getMinValue(); 114 double max = m.getMaxValue(); 115 double mean = m.getMeanValue(); 116 double std = m.getStdValue(); 117 double sum = m.getValueSum(); 118 119 StringBuffer s = new StringBuffer(); 120 s.append(count + " cells selected:"); 121 s.append(" min=" + nf.format(min)); 122 s.append(" max=" + nf.format(max)); 123 s.append(" mean=" + nf.format(mean)); 124 s.append(" std=" + nf.format(std)); 125 s.append(" sum=" + nf.format(sum)); 126 jLabel.setText(s.toString()); 127 } 128 }