Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

DataDisplayPanel.java

00001 /*
00002  *  Copyright 2006-2007 Columbia University.
00003  *
00004  *  This file is part of MEAPsoft.
00005  *
00006  *  MEAPsoft is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License version 2 as
00008  *  published by the Free Software Foundation.
00009  *
00010  *  MEAPsoft is distributed in the hope that it will be useful, but
00011  *  WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with MEAPsoft; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00018  *  02110-1301 USA
00019  *
00020  *  See the file "COPYING" for the text of the license.
00021  */
00022 
00023 package com.meapsoft.gui;
00024 
00025 import java.awt.BorderLayout;
00026 import java.awt.Dimension;
00027 import java.awt.Point;
00028 import java.awt.event.ActionEvent;
00029 import java.awt.event.ActionListener;
00030 import java.awt.event.MouseEvent;
00031 import java.awt.event.MouseListener;
00032 import java.awt.event.MouseMotionListener;
00033 import java.io.IOException;
00034 
00035 import javax.swing.JButton;
00036 import javax.swing.JFrame;
00037 import javax.swing.JLabel;
00038 import javax.swing.JPanel;
00039 import javax.swing.JScrollPane;
00040 import javax.swing.JToggleButton;
00041 import javax.swing.JToolBar;
00042 
00043 import com.meapsoft.FeatFile;
00044 import com.meapsoft.MEAPUtil;
00045 import com.meapsoft.ParserException;
00046 
00053 public class DataDisplayPanel extends JPanel implements ActionListener, MouseMotionListener, MouseListener, Runnable
00054 {
00055     // constants
00056     private static final float VZOOM_INCR = 2;
00057     private static final float HZOOM_INCR = 2;
00058     private static final float MIN_VZOOM = 0.03125f;
00059     private static final float MIN_HZOOM = 0.03125f;
00060     private static final float MAX_VZOOM = 16;
00061     private static final float MAX_HZOOM = 8;
00062     // Actions performed by various buttons
00063     private static final String ZOOM_RESET = "zoom_reset";
00064     private static final String ZOOM_IN = "zoom_in";
00065     private static final String ZOOM_OUT = "zoom_out";
00066     private static final String ZOOM_V = "zoom_v";
00067     private static final String ZOOM_H = "zoom_h";
00068 
00069     // Matrix of data to be displayed
00070     protected double[][] data;
00071     // title of the JFrame created by run()
00072     protected String windowTitle = "data";
00073 
00074     // GUI components
00075     private SpectrogramPanel dataPanel;
00076     private JScrollPane scroller;
00077     private JLabel statusbar;
00078     private JToolBar toolbar;
00079     private JToggleButton button_zoom_in;
00080     private JToggleButton button_zoom_out;
00081 
00082     // states to keep track of
00083     private float currHZoom = 1.0f;
00084     private float currVZoom = 1.0f;
00085     // are we in the zoomIn state or zoomOut? flag controlled by zoom
00086     // buttons on the menu bar
00087     private boolean zoomIn = true;
00088     private boolean hZoom = true;
00089     private boolean vZoom = false;
00090     // keep track of where zoom started when dragging a zoombox
00091     private Point zoomStart = null;
00092     
00093 
00094     public DataDisplayPanel(double[][] d)
00095     {
00096         super();
00097 
00098         data = d;
00099     }
00100 
00101     public DataDisplayPanel(double[][] d, String title)
00102     {
00103         this(d);
00104 
00105         windowTitle = title;
00106     }
00107 
00108 
00109     public void setup() 
00110     {
00111         buildGUI();
00112     }
00113 
00114     private void buildGUI()
00115     {
00116         dataPanel = new SpectrogramPanel(data);
00117 
00118         setLayout(new BorderLayout());
00119 
00120         //TODO:
00121         // _ still need to set up axes.  and colorbar zooming
00122         // X want status bar on the bottom that lists value in data matrix
00123         //   at point indicated by mouse location 
00124         // X want to be able to do proper vertical scaling (automatically
00125         //   fill the window unless vertical scroll is set manually (then
00126         //   let scrollbar take over)
00127         // X zooming and scrolling don't play nice together yet -  Fixed in SpectrogramPanel.zoom 
00128         // C need to find out how to get the current visible size of the
00129         //   window and set up actionlisteners/whatever to update things
00130 
00131             scroller = new JScrollPane(dataPanel);
00132         add(scroller, BorderLayout.CENTER);
00133         //add(hAxis, BorderLayout.WEST);
00134         //add(dataPanel.getColorBar(), BorderLayout.EAST);
00135 
00136 
00137         // tool bar
00138         toolbar = new JToolBar(JToolBar.HORIZONTAL);
00139         add(toolbar, BorderLayout.PAGE_START);
00140         // keep it anchored at the top
00141         toolbar.setFloatable(false);
00142 
00143         // toolbar buttons
00144         toolbar.add(new JLabel("Zoom:"));
00145         toolbar.addSeparator();
00146 
00147         JToggleButton tbutton = new JToggleButton("h", hZoom);
00148         tbutton.setActionCommand(ZOOM_H);
00149         tbutton.addActionListener(this);
00150         toolbar.add(tbutton);
00151 
00152         tbutton = new JToggleButton("v", vZoom);
00153         tbutton.setActionCommand(ZOOM_V);
00154         tbutton.addActionListener(this);
00155         toolbar.add(tbutton);
00156         toolbar.addSeparator();
00157 
00158         tbutton = new JToggleButton("in", zoomIn);
00159         tbutton.setActionCommand(ZOOM_IN);
00160         tbutton.addActionListener(this);
00161         toolbar.add(tbutton);
00162         button_zoom_in = tbutton;
00163 
00164         tbutton = new JToggleButton("out", !zoomIn);
00165         tbutton.setActionCommand(ZOOM_OUT);
00166         tbutton.addActionListener(this);
00167         toolbar.add(tbutton);
00168         button_zoom_out = tbutton;
00169         toolbar.addSeparator();
00170 
00171         JButton button = new JButton("reset");
00172         button.setActionCommand(ZOOM_RESET);
00173         button.addActionListener(this);
00174         toolbar.add(button);
00175 
00176 
00177         // status bar
00178         statusbar = new JLabel(windowTitle);
00179         add(statusbar, BorderLayout.SOUTH);
00180         dataPanel.addMouseMotionListener(this);
00181 
00182         // Mouse listener to zoom in/out on mouse clicks
00183         dataPanel.addMouseListener(this);
00184 
00185         // set the default size and reset the display
00186         this.setPreferredSize(new Dimension(500, 300));
00187         actionPerformed(new ActionEvent(button, 0, ZOOM_RESET));
00188     }
00189 
00190 
00191     public void actionPerformed(ActionEvent e) 
00192     {
00193         //String cmd = ((AbstractButton)e.getSource()).getActionCommand();
00194         String cmd = e.getActionCommand();
00195             
00196         if(cmd.equals(ZOOM_IN))
00197         {
00198             zoomIn = true;
00199             button_zoom_in.setSelected(true); 
00200             button_zoom_out.setSelected(false); 
00201         }
00202         else if(cmd.equals(ZOOM_OUT))
00203         {
00204             zoomIn = false;
00205             button_zoom_in.setSelected(false);
00206             button_zoom_out.setSelected(true);
00207         }
00208         else if(cmd.equals(ZOOM_RESET))
00209         {
00210             float h = (float)(this.getPreferredSize().getHeight() 
00211                               - scroller.getHorizontalScrollBar()
00212                                         .getPreferredSize().getHeight()
00213                               - statusbar.getPreferredSize().getHeight()
00214                               - toolbar.getPreferredSize().getHeight());
00215             // no, this should not be hardcoded (the 3 especially), but I
00216             // don't know how else to properly set the initial zoom so
00217             // that no vertial scrollbar is required
00218             boolean vz = vZoom, hz = hZoom;
00219             vZoom = true;  hZoom = true;
00220             zoomDataPanel(1, (h-3)/(float)dataPanel.getDataHeight());
00221             vZoom = vz;  hZoom = hz;
00222         }
00223         else if(cmd.equals(ZOOM_V))
00224             vZoom = !vZoom;
00225         else if(cmd.equals(ZOOM_H))
00226             hZoom = !hZoom;
00227     }
00228 
00229     public void mouseMoved(MouseEvent e)
00230     { 
00231         int x = (int)(e.getX()/currHZoom);
00232         // y axis is flipped (axis xy) 
00233         int y = (int)dataPanel.getDataHeight()-(int)(e.getY()/currVZoom)-1;
00234 
00235         if(x < dataPanel.getDataWidth() && x >= 0 
00236            && y < dataPanel.getDataHeight() && y >= 0)   
00237             //statusbar.setText(featFile.filename + ": features(" + x + "," + y 
00238             statusbar.setText("features(" + x + "," + y 
00239                            + ") = " + dataPanel.getData(x,y));
00240     }
00241 
00242     public void mouseDragged(MouseEvent e) { }
00243 
00244     public void mouseClicked(MouseEvent e) 
00245     { 
00246         boolean zoomin = zoomIn && e.getButton() == MouseEvent.BUTTON1 
00247             || !zoomIn && e.getButton() == MouseEvent.BUTTON3;
00248         if(zoomin)
00249             zoomDataPanel(currHZoom*HZOOM_INCR, currVZoom*VZOOM_INCR);
00250         else if(!zoomin)
00251             zoomDataPanel(currHZoom/HZOOM_INCR, currVZoom/VZOOM_INCR);
00252     }
00253 
00254     // take care of dragging a zoom rectangle and whatnot here
00255     public void mousePressed(MouseEvent e) { }
00256     public void mouseReleased(MouseEvent e) { }
00257 
00258     public void mouseEntered(MouseEvent e) { }
00259     public void mouseExited(MouseEvent e) { }
00260 
00261 
00262     private void zoomDataPanel(float h, float v)
00263     {
00264         if(hZoom)
00265         {
00266             if(h > MAX_HZOOM)
00267                 h = MAX_HZOOM;
00268             else if(h < MIN_HZOOM)
00269                 h = MIN_HZOOM;
00270 
00271             currHZoom = h;
00272             dataPanel.hzoomSet(currHZoom);
00273         }
00274         if(vZoom)
00275         {
00276             if(v > MAX_VZOOM)
00277                 v = MAX_VZOOM;
00278             else if(v < MIN_VZOOM)
00279                 v = MIN_VZOOM;
00280 
00281             currVZoom = v;
00282             dataPanel.vzoomSet(currVZoom);
00283         }
00284     }
00285 
00289     public void run()
00290      {
00291          JFrame jframe = new JFrame(windowTitle);
00292          jframe.setContentPane(this);
00293          jframe.pack();
00294          jframe.setVisible(true);
00295     }
00296 
00297     public static void spawnWindow(double[][] d)
00298     {
00299         spawnWindow(d, "data");
00300     }
00301 
00302     public static void spawnWindow(double[][] d, String title)
00303     {
00304         DataDisplayPanel p = new DataDisplayPanel(d, title);
00305 
00306         p.setup();
00307 
00308         p.run();
00309     }
00310 
00311     public static void main(String[] args)
00312     {
00313         // parse arguments
00314         int[] featdim = MEAPUtil.parseFeatDim(args,"i:"); 
00315         String filename = args[args.length-1];
00316 
00317         FeatFile f = new FeatFile(filename);
00318 
00319         try
00320         {
00321             f.readFile();
00322         }
00323         catch(Exception e)
00324         {
00325             e.printStackTrace();
00326             System.exit(1);
00327         }
00328         
00329         DataDisplayPanel p = new DataDisplayPanel(f.getFeatures(featdim), filename);
00330 
00331         JFrame jframe = new JFrame(p.getClass().getName() + ": " + filename);
00332         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00333         jframe.setContentPane(p);
00334         jframe.pack();
00335         jframe.setVisible(true);
00336     }
00337 }
00338 

Generated on Tue Feb 6 19:02:25 2007 for MEAPsoft by doxygen1.2.18