MOA 12.03
Real Time Analytics for Data Streams
ClusteringVisualEvalPanel.java
Go to the documentation of this file.
00001 /*
00002  *    ClusteringVisualEvalPanel.java
00003  *    Copyright (C) 2010 RWTH Aachen University, Germany
00004  *    @author Jansen (moa@cs.rwth-aachen.de)
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 
00021 package moa.gui.clustertab;
00022 
00023 import java.awt.Color;
00024 import java.awt.Dimension;
00025 import java.awt.Graphics;
00026 import java.awt.GridBagConstraints;
00027 import java.awt.event.ActionListener;
00028 import java.awt.event.ComponentEvent;
00029 import java.awt.event.ComponentListener;
00030 import java.text.DecimalFormat;
00031 import java.util.ArrayList;
00032 import javax.swing.ButtonGroup;
00033 import javax.swing.JLabel;
00034 import javax.swing.JRadioButton;
00035 import moa.evaluation.MeasureCollection;
00036 
00037 public class ClusteringVisualEvalPanel extends javax.swing.JPanel{
00038     private ArrayList<JLabel> names;
00039     private ArrayList<JLabel> values;
00040     private ArrayList<JRadioButton> radiobuttons;
00041     private MeasureCollection[] measures0;
00042     private MeasureCollection[] measures1;
00043     private ButtonGroup radioGroup;
00044 
00045     private JLabel labelDummy;
00046     private JLabel labelMeasure;
00047     private JLabel labelCurrent0;
00048     private JLabel labelMean0;
00049 
00051     public ClusteringVisualEvalPanel() {
00052         initComponents();
00053         radioGroup = new ButtonGroup();
00054     }
00055 
00056 
00057     public void setMeasures(MeasureCollection[] measures0, MeasureCollection[] measures1, ActionListener listener){
00058         this.measures0 = measures0;
00059         this.measures1 = measures1;
00060 
00061         names = new ArrayList<JLabel>();
00062         values = new ArrayList<JLabel>();
00063         radiobuttons = new ArrayList<JRadioButton>();
00064 
00065         for (int i = 0; i < measures0.length; i++) {
00066             for (int j = 0; j < measures0[i].getNumMeasures(); j++) {
00067                 if(measures0[i].isEnabled(j)){
00068                     names.add(new JLabel(measures0[i].getName(j)));
00069 
00070                 }
00071             }
00072         }
00073         
00074         setLayout(new java.awt.GridBagLayout());
00075         GridBagConstraints gb;
00076 
00077         //Radiobuttons
00078         radioGroup = new ButtonGroup();
00079         gb = new GridBagConstraints();
00080         gb.gridx=0;
00081         for (int i = 0; i < names.size(); i++) {
00082             JRadioButton rb = new JRadioButton();
00083             rb.setActionCommand(Integer.toString(i));
00084             rb.addActionListener(listener);
00085             radiobuttons.add(rb);
00086 
00087             gb.gridy = i+1;
00088             contentPanel.add(rb, gb);
00089             radioGroup.add(rb);
00090         }
00091         radiobuttons.get(0).setSelected(true);
00092 
00093         //name labels
00094         gb = new GridBagConstraints();
00095         gb.gridx=1;
00096         for (int i = 0; i < names.size(); i++) {
00097             names.get(i).setPreferredSize(new Dimension(40, 14));
00098             gb.anchor = GridBagConstraints.WEST;
00099             gb.insets = new java.awt.Insets(4, 7, 4, 7);
00100             gb.gridy = i+1;
00101             contentPanel.add(names.get(i), gb);
00102         }
00103 
00104         
00105 
00106         int counter = 0;
00107         for (int i = 0; i < measures0.length; i++) {
00108             for (int j = 0; j < measures0[i].getNumMeasures(); j++) {
00109                 if(!measures0[i].isEnabled(j)) continue;
00110                 for (int k = 0; k < 4; k++) {
00111                     String tooltip ="";
00112                     Color color = Color.black;
00113                     switch(k){
00114                         case 0:
00115                             tooltip = "current value";
00116                             color = Color.red;
00117                         break;
00118                         case 1:
00119                             tooltip = "current value";
00120                             color = Color.blue;
00121                         break;
00122                         case 2:
00123                             tooltip = "mean";
00124                             color = Color.black;
00125                         break;
00126                         case 3:
00127                             tooltip = "mean";
00128                             color = Color.black;
00129                         break;
00130                     }
00131                     JLabel l = new JLabel("-");
00132                     l.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
00133                     l.setPreferredSize(new java.awt.Dimension(50, 14));
00134                     l.setToolTipText(measures0[i].getName(j)+" "+tooltip);
00135                     l.setForeground(color);
00136                     values.add(l);
00137                     gb = new GridBagConstraints();
00138                     gb.gridy = 1 + counter;
00139                     gb.gridx = 2 + k;
00140                     gb.weightx = 1.0;
00141                     contentPanel.add(l, gb);
00142                 }
00143                 counter++;
00144             }
00145         }
00146 
00147         //dummy label to align the labels at the top
00148         gb = new GridBagConstraints();
00149         gb.gridx = 0;
00150         gb.gridy = names.size()+2;
00151         gb.gridwidth = 6;
00152         gb.weighty = 1.0;
00153         JLabel fill = new JLabel();
00154         contentPanel.add(fill, gb);
00155 
00156         addLabels();
00157 
00158         contentPanel.setPreferredSize(new Dimension(250, names.size()*(14+8)+20));
00159     }
00160 
00161     private void addLabels(){
00162         labelMeasure = new javax.swing.JLabel("Measure");
00163         labelCurrent0 = new JLabel("Current");
00164         labelMean0 = new javax.swing.JLabel("Mean");
00165         labelDummy = new javax.swing.JLabel();
00166         GridBagConstraints gb = new GridBagConstraints();
00167         gb.gridy = 0;
00168 
00169         gb.gridx = 0;
00170         contentPanel.add(labelDummy, gb);
00171 
00172         gb.gridx = 1;
00173         contentPanel.add(labelMeasure, gb);
00174 
00175         gb = new GridBagConstraints();
00176         gb.gridy = 0;
00177         gb.gridx = 2;
00178         gb.gridwidth = 2;
00179         contentPanel.add(labelCurrent0, gb);
00180 
00181 
00182         gb = new GridBagConstraints();
00183         gb.gridy = 0;
00184         gb.gridx=4;
00185         gb.gridwidth = 2;
00186         contentPanel.add(labelMean0, gb);
00187 
00188     }
00189 
00190     public void update(){
00191         DecimalFormat d = new DecimalFormat("0.00");
00192 
00193         if(measures0!=null){
00194             int counter = 0;
00195             for (MeasureCollection m : measures0) {
00196                 for (int i = 0; i < m.getNumMeasures(); i++) {
00197                     if(!m.isEnabled(i)) continue;
00198                     if(Double.isNaN(m.getLastValue(i)))
00199                         values.get(counter*4).setText("-");
00200                     else
00201                         values.get(counter*4).setText(d.format(m.getLastValue(i)));
00202 
00203                     if(Double.isNaN(m.getMean(i)))
00204                         values.get(counter*4+2).setText("-");
00205                     else
00206                         values.get(counter*4+2).setText(d.format(m.getMean(i)));
00207                     counter++;
00208                 }
00209             }
00210         }
00211         if(measures1!=null){
00212             int counter = 0;
00213             for (MeasureCollection m : measures1) {
00214                 for (int i = 0; i < m.getNumMeasures(); i++) {
00215                     if(!m.isEnabled(i)) continue;
00216                     if(Double.isNaN(m.getLastValue(i)))
00217                         values.get(counter*4+1).setText("-");
00218                     else
00219                         values.get(counter*4+1).setText(d.format(m.getLastValue(i)));
00220 
00221 
00222                     if(Double.isNaN(m.getMean(i)))
00223                         values.get(counter*4+3).setText("-");
00224                     else
00225                         values.get(counter*4+3).setText(d.format(m.getMean(i)));
00226                     counter++;
00227                 }
00228             }
00229         }
00230     }
00231 
00232     @Override
00233     //this is soooooo bad, but freaking gridbag somehow doesnt kick in...???
00234     protected void paintComponent(Graphics g) {
00235         scrollPane.setPreferredSize(new Dimension(getWidth()-20, getHeight()-30));
00236         super.paintComponent(g);
00237     }
00238 
00244     @SuppressWarnings("unchecked")
00245     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
00246     private void initComponents() {
00247         java.awt.GridBagConstraints gridBagConstraints;
00248 
00249         scrollPane = new javax.swing.JScrollPane();
00250         contentPanel = new javax.swing.JPanel();
00251 
00252         setBorder(javax.swing.BorderFactory.createTitledBorder("Values"));
00253         setPreferredSize(new java.awt.Dimension(250, 115));
00254         setLayout(new java.awt.GridBagLayout());
00255 
00256         scrollPane.setBorder(null);
00257         scrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
00258         scrollPane.setPreferredSize(new java.awt.Dimension(270, 180));
00259 
00260         contentPanel.setPreferredSize(new java.awt.Dimension(100, 105));
00261         contentPanel.setLayout(new java.awt.GridBagLayout());
00262         scrollPane.setViewportView(contentPanel);
00263 
00264         gridBagConstraints = new java.awt.GridBagConstraints();
00265         gridBagConstraints.gridx = 0;
00266         gridBagConstraints.gridy = 0;
00267         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
00268         gridBagConstraints.weightx = 1.0;
00269         gridBagConstraints.weighty = 1.0;
00270         add(scrollPane, gridBagConstraints);
00271     }// </editor-fold>//GEN-END:initComponents
00272 
00273 
00274     // Variables declaration - do not modify//GEN-BEGIN:variables
00275     private javax.swing.JPanel contentPanel;
00276     private javax.swing.JScrollPane scrollPane;
00277     // End of variables declaration//GEN-END:variables
00278 
00279 }
 All Classes Namespaces Files Functions Variables Enumerations