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.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
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
00070 protected double[][] data;
00071
00072 protected String windowTitle = "data";
00073
00074
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
00083 private float currHZoom = 1.0f;
00084 private float currVZoom = 1.0f;
00085
00086
00087 private boolean zoomIn = true;
00088 private boolean hZoom = true;
00089 private boolean vZoom = false;
00090
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
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 scroller = new JScrollPane(dataPanel);
00132 add(scroller, BorderLayout.CENTER);
00133
00134
00135
00136
00137
00138 toolbar = new JToolBar(JToolBar.HORIZONTAL);
00139 add(toolbar, BorderLayout.PAGE_START);
00140
00141 toolbar.setFloatable(false);
00142
00143
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
00178 statusbar = new JLabel(windowTitle);
00179 add(statusbar, BorderLayout.SOUTH);
00180 dataPanel.addMouseMotionListener(this);
00181
00182
00183 dataPanel.addMouseListener(this);
00184
00185
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
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
00216
00217
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
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
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
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
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