Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

GUIUtils.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 /*
00024  * Created on Nov 28, 2006
00025  *
00026  * Various GUI utilities
00027  * see com.meapsoft.MEAPUtil for non-gui-related utilities
00028  * 
00029  */
00030 package com.meapsoft.gui;
00031 
00032 import java.beans.PropertyChangeEvent;
00033 import java.beans.PropertyChangeListener;
00034 import java.io.File;
00035 import java.io.PrintWriter;
00036 import java.io.StringWriter;
00037 
00038 import javax.swing.BoxLayout;
00039 import javax.swing.JDialog;
00040 import javax.swing.JFileChooser;
00041 import javax.swing.JFrame;
00042 import javax.swing.JOptionPane;
00043 import javax.swing.JPanel;
00044 import javax.swing.JScrollPane;
00045 import javax.swing.JTextArea;
00046 
00052 public class GUIUtils
00053 {       
00054         public static final int OPEN = 0;
00055         public static final int SAVE = 1;
00056         public static final int TARGET = 2;
00057         public static final int DIR = 3;
00058         public static final int OPENWAV = 4;
00059         public static final int OPENFEAT = 5;
00060         public static final int OPENEDL = 6;
00061         public static final int SAVEEDL = 7;
00062         public static final int SAVEFEAT = 8;
00063         
00064         public static final int FATAL_ERROR = 0;
00065         public static final int MESSAGE = 1;
00066         
00067         public static String[] FileSelector(int mode, String defaultDirectory, JFrame jframe)
00068         {       
00069                 if (jframe == null)
00070                         jframe = new JFrame();
00071                         
00072                 JFileChooser chooser = new JFileChooser();
00073 
00074                 chooser.setCurrentDirectory(new File(defaultDirectory));
00075                 int returnVal = 0;
00076                 
00077                 if (mode == OPENWAV)
00078                 {
00079                         chooser.addChoosableFileFilter(new WavFileFilter());
00080                         returnVal = chooser.showOpenDialog(null);
00081                 }
00082                 else if (mode == OPEN)
00083                 {
00084                         returnVal = chooser.showOpenDialog(null);
00085                 }
00086                 else if (mode == SAVE)
00087                         returnVal = chooser.showSaveDialog(null);
00088                 else if (mode == DIR)
00089                 {
00090                         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
00091                         returnVal = chooser.showOpenDialog(jframe);
00092                 }
00093                 else if (mode == OPENFEAT)
00094                 {
00095                         chooser.addChoosableFileFilter(new FeatFileFilter());
00096                         returnVal = chooser.showOpenDialog(null);
00097                 }
00098                 else if (mode == OPENEDL)
00099                 {
00100                         chooser.addChoosableFileFilter(new EDLFileFilter());
00101                         returnVal = chooser.showOpenDialog(null);
00102                 }
00103                 else if (mode == SAVEEDL)
00104                         returnVal = chooser.showSaveDialog(null);
00105                 else if (mode == SAVEFEAT)
00106                         returnVal = chooser.showSaveDialog(null);
00107                 else
00108                         return null;
00109                         
00110                 String[] name = new String[2];
00111                 
00112                 if(returnVal == JFileChooser.APPROVE_OPTION) 
00113                 {
00114                         try
00115                         {
00116                                 name[0] = chooser.getSelectedFile().getAbsolutePath();
00117                                 name[1] = chooser.getSelectedFile().getName();
00118                         }
00119                         catch (Exception e)
00120                         {
00121                                 ShowDialog(e, "", MESSAGE, jframe);
00122                                 return null;
00123                         }
00124                 }
00125                 
00126                 return name;
00127         }
00128 
00129         public static void ShowDialog(String message, int status, JFrame jframe)
00130         {               
00131                 if (jframe == null)
00132                         jframe = new JFrame();
00133                         
00134                 if (status == FATAL_ERROR)
00135                 {
00136                         JOptionPane.showMessageDialog(jframe, 
00137                                 "MEAPsoft has encountered a fatal error: " + message);
00138                         System.exit(-1);
00139                 }
00140                 else
00141                         JOptionPane.showMessageDialog(jframe, message);
00142         }
00143                 
00144         public static void ShowDialog(Exception e, String message, int status, JFrame jframe)
00145         {
00146                 if (jframe == null)
00147                         jframe = new JFrame();
00148                         
00149                 message += ":\n" + e.getMessage();
00150 
00151                 if (status == FATAL_ERROR)
00152                         message = "MEAPsoft has encountered a fatal error: " + message;
00153 
00154                 //if(message.length() > 70)
00155                 //    message = message.substring(0, 70);
00156                         
00157                 final JOptionPane optionPane = 
00158                         new JOptionPane(message, JOptionPane.QUESTION_MESSAGE);
00159 
00160                 final JDialog dialog = new JDialog(jframe, "Exception!", true);
00161                 //dialog.setContentPane(optionPane);
00162                 dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
00163 
00164                 optionPane.addPropertyChangeListener(
00165                         new PropertyChangeListener() {
00166                                 public void propertyChange(PropertyChangeEvent e) {
00167                                         String prop = e.getPropertyName();
00168 
00169                                         if (dialog.isVisible() 
00170                                          && (e.getSource() == optionPane)
00171                                          && (prop.equals(JOptionPane.VALUE_PROPERTY))) {
00172                                                 dialog.setVisible(false);
00173                                         }
00174                                 }
00175                         });
00176                 
00177                 JPanel errorPanel = new JPanel();
00178                 BoxLayout bl = new BoxLayout(errorPanel, BoxLayout.Y_AXIS);
00179                 errorPanel.setLayout(bl);
00180                 
00181                 StringWriter sw = new StringWriter(); 
00182                 e.printStackTrace(new PrintWriter(sw));
00183                 JTextArea textbox = new JTextArea(sw.toString());
00184                 textbox.setEditable(false);
00185                 textbox.setColumns(50);
00186                 textbox.setLineWrap(true);
00187                 textbox.setWrapStyleWord(true);
00188                 JScrollPane scrollPane = new JScrollPane(textbox);
00189                 
00190                 errorPanel.add(optionPane);
00191                 errorPanel.add(scrollPane);
00192 
00193                 dialog.setContentPane(errorPanel);
00194                 
00195                 dialog.pack();
00196                 dialog.setVisible(true);
00197 
00198                 if (status == FATAL_ERROR)
00199                         System.exit(-1);
00200         }
00201 
00202 
00203 }
00204 
00205 
00206 
00207 

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