00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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
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
00080 float currHZoom = 1.0f;
00081 float currVZoom = 1.0f;
00082
00083
00084 boolean zoomIn = true;
00085 boolean hZoom = true;
00086 boolean vZoom = false;
00087
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
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 scroller = new JScrollPane(dataPanel);
00138 add(scroller, BorderLayout.CENTER);
00139
00140
00141
00142
00143
00144 toolbar = new JToolBar(JToolBar.HORIZONTAL);
00145 add(toolbar, BorderLayout.PAGE_START);
00146
00147 toolbar.setFloatable(false);
00148
00149
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
00184 statusbar = new JLabel(featFile.filename);
00185 add(statusbar, BorderLayout.SOUTH);
00186 dataPanel.addMouseMotionListener(this);
00187
00188
00189 dataPanel.addMouseListener(this);
00190
00191
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
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
00222
00223
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
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
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
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
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 }