Main Page   Class Hierarchy   Compound List   File List   Compound Members  

DataDisplayPanel.java

00001 /*
00002  *  Copyright 2006 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.5f;
00059     private static final float MIN_HZOOM = 0.5f;
00060     private static final float MAX_VZOOM = 8;
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     FeatFile featFile;
00070     int[] featdim;
00071 
00072     SpectrogramPanel dataPanel;
00073     JScrollPane scroller;
00074     JLabel statusbar;
00075     JToolBar toolbar;
00076     JToggleButton button_zoom_in;
00077     JToggleButton button_zoom_out;
00078 
00079     // states to keep track of
00080     float currHZoom = 1.0f;
00081     float currVZoom = 1.0f;
00082     // are we in the zoomIn state or zoomOut? flag controlled by zoom
00083     // buttons on the menu bar
00084     boolean zoomIn = true;
00085     boolean hZoom = true;
00086     boolean vZoom = false;
00087     // keep track of where zoom started when dragging a zoombox
00088     Point zoomStart = null;
00089     
00090     public DataDisplayPanel(String filename)
00091     {
00092         this(filename, null);
00093     }
00094 
00095     public DataDisplayPanel(FeatFile file)
00096     {
00097         this(file, null);
00098     }
00099 
00100     public DataDisplayPanel(String filename, int[] fd)
00101     {
00102         this(new FeatFile(filename), fd);
00103     }
00104 
00105     public DataDisplayPanel(FeatFile file, int[] fd )
00106     {
00107         super();
00108         featFile = file;
00109         featdim = fd;
00110     }
00111 
00112     public void setup() throws IOException, ParserException
00113     {
00114         if(!featFile.haveReadFile)
00115             featFile.readFile();
00116         
00117         buildGUI();
00118     }
00119 
00120     private void buildGUI()
00121     {
00122         dataPanel = new SpectrogramPanel(featFile.getFeatures(featdim));
00123 
00124         setLayout(new BorderLayout());
00125 
00126         //TODO:
00127         // _ still need to set up axes.  and colorbar zooming
00128         // X want status bar on the bottom that lists value in data matrix
00129         //   at point indicated by mouse location 
00130         // X want to be able to do proper vertical scaling (automatically
00131         //   fill the window unless vertical scroll is set manually (then
00132         //   let scrollbar take over)
00133         // X zooming and scrolling don't play nice together yet -  Fixed in SpectrogramPanel.zoom 
00134         // C need to find out how to get the current visible size of the
00135         //   window and set up actionlisteners/whatever to update things
00136 
00137             scroller = new JScrollPane(dataPanel);
00138         add(scroller, BorderLayout.CENTER);
00139         //add(hAxis, BorderLayout.WEST);
00140         //add(dataPanel.getColorBar(), BorderLayout.EAST);
00141 
00142 
00143         // tool bar
00144         toolbar = new JToolBar(JToolBar.HORIZONTAL);
00145         add(toolbar, BorderLayout.PAGE_START);
00146         // keep it anchored at the top
00147         toolbar.setFloatable(false);
00148 
00149         // toolbar buttons
00150         toolbar.add(new JLabel("Zoom:"));
00151         toolbar.addSeparator();
00152 
00153         JToggleButton tbutton = new JToggleButton("h", hZoom);
00154         tbutton.setActionCommand(ZOOM_H);
00155         tbutton.addActionListener(this);
00156         toolbar.add(tbutton);
00157 
00158         tbutton = new JToggleButton("v", vZoom);
00159         tbutton.setActionCommand(ZOOM_V);
00160         tbutton.addActionListener(this);
00161         toolbar.add(tbutton);
00162         toolbar.addSeparator();
00163 
00164         tbutton = new JToggleButton("in", zoomIn);
00165         tbutton.setActionCommand(ZOOM_IN);
00166         tbutton.addActionListener(this);
00167         toolbar.add(tbutton);
00168         button_zoom_in = tbutton;
00169 
00170         tbutton = new JToggleButton("out", !zoomIn);
00171         tbutton.setActionCommand(ZOOM_OUT);
00172         tbutton.addActionListener(this);
00173         toolbar.add(tbutton);
00174         button_zoom_out = tbutton;
00175         toolbar.addSeparator();
00176 
00177         JButton button = new JButton("reset");
00178         button.setActionCommand(ZOOM_RESET);
00179         button.addActionListener(this);
00180         toolbar.add(button);
00181 
00182 
00183         // status bar
00184         statusbar = new JLabel(featFile.filename);
00185         add(statusbar, BorderLayout.SOUTH);
00186         dataPanel.addMouseMotionListener(this);
00187 
00188         // Mouse listener to zoom in/out on mouse clicks
00189         dataPanel.addMouseListener(this);
00190 
00191         // set the default size and reset the display
00192         this.setPreferredSize(new Dimension(500, 300));
00193         actionPerformed(new ActionEvent(button, 0, ZOOM_RESET));
00194     }
00195 
00196 
00197     public void actionPerformed(ActionEvent e) 
00198     {
00199         //String cmd = ((AbstractButton)e.getSource()).getActionCommand();
00200         String cmd = e.getActionCommand();
00201             
00202         if(cmd.equals(ZOOM_IN))
00203         {
00204             zoomIn = true;
00205             button_zoom_in.setSelected(true); 
00206             button_zoom_out.setSelected(false); 
00207         }
00208         else if(cmd.equals(ZOOM_OUT))
00209         {
00210             zoomIn = false;
00211             button_zoom_in.setSelected(false);
00212             button_zoom_out.setSelected(true);
00213         }
00214         else if(cmd.equals(ZOOM_RESET))
00215         {
00216             float h = (float)(this.getPreferredSize().getHeight() 
00217                               - scroller.getHorizontalScrollBar()
00218                                         .getPreferredSize().getHeight()
00219                               - statusbar.getPreferredSize().getHeight()
00220                               - toolbar.getPreferredSize().getHeight());
00221             // no, this should not be hardcoded (the 3 especially), but I
00222             // don't know how else to properly set the initial zoom so
00223             // that no vertial scrollbar is required
00224             boolean vz = vZoom, hz = hZoom;
00225             vZoom = true;  hZoom = true;
00226             zoomDataPanel(1, (h-3)/(float)dataPanel.getDataHeight());
00227             vZoom = vz;  hZoom = hz;
00228         }
00229         else if(cmd.equals(ZOOM_V))
00230             vZoom = !vZoom;
00231         else if(cmd.equals(ZOOM_H))
00232             hZoom = !hZoom;
00233     }
00234 
00235     public void mouseMoved(MouseEvent e)
00236     { 
00237         int x = (int)(e.getX()/currHZoom);
00238         // y axis is flipped (axis xy) 
00239         int y = (int)dataPanel.getDataHeight()-(int)(e.getY()/currVZoom)-1;
00240 
00241         if(x < dataPanel.getDataWidth() && x >= 0 
00242            && y < dataPanel.getDataHeight() && y >= 0)   
00243             //statusbar.setText(featFile.filename + ": features(" + x + "," + y 
00244             statusbar.setText("features(" + x + "," + y 
00245                            + ") = " + dataPanel.getData(x,y));
00246     }
00247 
00248     public void mouseDragged(MouseEvent e) { }
00249 
00250     public void mouseClicked(MouseEvent e) 
00251     { 
00252         boolean zoomin = zoomIn && e.getButton() == MouseEvent.BUTTON1 
00253             || !zoomIn && e.getButton() == MouseEvent.BUTTON3;
00254         if(zoomin)
00255             zoomDataPanel(currHZoom*HZOOM_INCR, currVZoom*VZOOM_INCR);
00256         else if(!zoomin)
00257             zoomDataPanel(currHZoom/HZOOM_INCR, currVZoom/VZOOM_INCR);
00258     }
00259 
00260     // take care of dragging a zoom rectangle and whatnot here
00261     public void mousePressed(MouseEvent e) { }
00262     public void mouseReleased(MouseEvent e) { }
00263 
00264     public void mouseEntered(MouseEvent e) { }
00265     public void mouseExited(MouseEvent e) { }
00266 
00267 
00268     private void zoomDataPanel(float h, float v)
00269     {
00270         if(hZoom)
00271         {
00272             if(h > MAX_HZOOM)
00273                 h = MAX_HZOOM;
00274             else if(h < MIN_HZOOM)
00275                 h = MIN_HZOOM;
00276 
00277             currHZoom = h;
00278             dataPanel.hzoomSet(currHZoom);
00279         }
00280         if(vZoom)
00281         {
00282             if(v > MAX_VZOOM)
00283                 v = MAX_VZOOM;
00284             else if(v < MIN_VZOOM)
00285                 v = MIN_VZOOM;
00286 
00287             currVZoom = v;
00288             dataPanel.vzoomSet(currVZoom);
00289         }
00290     }
00291 
00295     public void run()
00296      {
00297          JFrame jframe = new JFrame(featFile.filename);
00298          jframe.setContentPane(this);
00299          jframe.pack();
00300          jframe.setVisible(true);
00301     }
00302 
00303 
00304     public static void spawnWindow(FeatFile f)
00305     {
00306         DataDisplayPanel.spawnWindow(f, null);
00307     }
00308 
00309     public static void spawnWindow(String fn)
00310     {
00311         DataDisplayPanel.spawnWindow(new FeatFile(fn), null);
00312     }
00313 
00314     public static void spawnWindow(String fn, int[] fd)
00315     {
00316         DataDisplayPanel.spawnWindow(new FeatFile(fn), fd);
00317     }
00318 
00319     public static void spawnWindow(FeatFile f, int[] fd)
00320     {
00321         DataDisplayPanel p = new DataDisplayPanel(f, fd);
00322 
00323         try
00324         { 
00325             p.setup();
00326         }
00327         catch(Exception e)
00328         {
00329             e.printStackTrace();
00330         }
00331 
00332         p.run();
00333     }
00334 
00335 
00336     public static void main(String[] args)
00337     {
00338         // parse arguments
00339         int[] featdim = MEAPUtil.parseFeatDim(args,"i:"); 
00340         String filename = args[args.length-1];
00341         
00342         DataDisplayPanel p = new DataDisplayPanel(filename, featdim);
00343 
00344         try
00345         { 
00346             p.setup();
00347         }
00348         catch(Exception e)
00349         {
00350             e.printStackTrace();
00351             System.exit(1);
00352         }
00353 
00354         JFrame jframe = new JFrame(p.getClass().getName() + ": " + filename);
00355         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00356         jframe.setContentPane(p);
00357         jframe.pack();
00358         jframe.setVisible(true);
00359     }
00360 }

Generated on Thu May 11 15:04:10 2006 for MEAPsoft by doxygen1.2.18