 /*
 * Copyright (C) 17-sept.-2003  Jean-Yves Beaujean 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,  59
 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * 
 */
package jxtray;


import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JFileChooser;
import java.io.File;

import jxtray.io.TextFilePreview;

/**
 * 
 * @author Jean-Yves Beaujean
 * Messages class use JOptionPane functions to show messages (error, info, warning,...) to screen  
 * 
 */

public class Messages 
{
		/** Show an information message contains <code> msg String</code>*/
		public static void info(String msg ) 
		{
				JLabel jmsg = new JLabel(msg);
				jmsg.setFont(Jxtray.getFont());
				JOptionPane.showMessageDialog(null,jmsg,JxtrayConstants.APPNAME+ " - "+Jxtray.getLang("dialog.information"),JOptionPane.INFORMATION_MESSAGE);
		}
		
		/** Show an error message contains <code> msg String</code>*/
		public static void error(String msg ) 
		{
				System.err.println(msg);
				JLabel jmsg = new JLabel(msg);
				jmsg.setFont(Jxtray.getFont());
				JOptionPane.showMessageDialog(null,jmsg,JxtrayConstants.APPNAME+ " - "+Jxtray.getLang("dialog.error"),JOptionPane.ERROR_MESSAGE);
		}
		/** Show a question message contains <code> msg String</code>
		 *  @return int JOptionPane response.
		 */
		public static int question(String msg ) 
		{
				JLabel jmsg = new JLabel(msg);
				jmsg.setFont(Jxtray.getFont());
				return JOptionPane.showConfirmDialog(null,jmsg,JxtrayConstants.APPNAME+ " - "+Jxtray.getLang("dialog.question"),JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
		}
		
		/** Show a warning message contains <code> msg String</code>*/
		public static void warning(String msg ) 
		{
				JLabel jmsg = new JLabel(msg);
				jmsg.setFont(Jxtray.getFont());
				JOptionPane.showMessageDialog(null,jmsg,JxtrayConstants.APPNAME+ " - "+Jxtray.getLang("dialog.warning"),JOptionPane.WARNING_MESSAGE);
		}
		
		public static String input(String msg ) 
		{
				JLabel jmsg = new JLabel(msg);
				jmsg.setFont(Jxtray.getFont());
				return (String) JOptionPane.showInputDialog(null,msg,JxtrayConstants.APPNAME,JOptionPane.QUESTION_MESSAGE);
				 
		}
		
		public static String chooseFile(CustomFileFilter fileFilter ) 
		{
				
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.setAcceptAllFileFilterUsed(false);
			fileChooser.setFileFilter(fileFilter);
			fileChooser.setAccessory(new TextFilePreview(fileChooser));
			
			boolean canWrite = true;
			while (true)
			{
				canWrite = true;
				int ret = fileChooser.showSaveDialog(null);
				if ( ret == JFileChooser.APPROVE_OPTION)
				{
					StringBuffer name = new StringBuffer();
					
					try {name.append(fileChooser.getSelectedFile().getCanonicalPath());	}
					catch ( Exception ex )	
					{	Messages.error("Messages.chooseFile : " +ex );}
					
					if ( name.toString().toLowerCase().lastIndexOf("."+fileFilter.getExtension().toLowerCase()) <= 0)
						name.append("."+fileFilter.getExtension().toLowerCase());	
					
					
					if ( "TRUE".equals(Jxtray.getProperty("confirm.overwrite").toUpperCase()) && (new File(name.toString())).exists() )
					{
						int retOw = question(Jxtray.getLang("output.overwrite"));
						if ( retOw == JOptionPane.NO_OPTION)
							canWrite = false;
					}
					
					if ( canWrite )
					{
						return name.toString();
					}
				}
				else
					return null;
			}
				 
		}	
		
		
		public static String chooseFile(CustomFileFilter fileFilter[] ) 
		{
				
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.setAcceptAllFileFilterUsed(false);

			for ( int i =0; i < fileFilter.length;i++)
				fileChooser.setFileFilter(fileFilter[i]);
			
			boolean canWrite = true;
			while (true)
			{
				canWrite = true;
				int ret = fileChooser.showSaveDialog(null);
				if ( ret == JFileChooser.APPROVE_OPTION)
				{
					StringBuffer name = new StringBuffer();
					String extension = ((CustomFileFilter)fileChooser.getFileFilter()).getExtension();
					
					try {name.append(fileChooser.getSelectedFile().getCanonicalPath());	}
					catch ( Exception ex )	
					{	Messages.error("Messages.chooseFile : " +ex );}
					
					if ( name.toString().toLowerCase().lastIndexOf("."+extension.toLowerCase()) <= 0)
						name.append("."+extension.toLowerCase());	
					
					
					if ( "TRUE".equals(Jxtray.getProperty("confirm.overwrite").toUpperCase()) && (new File(name.toString())).exists() )
					{
						int retOw = question(Jxtray.getLang("output.overwrite"));
						if ( retOw == JOptionPane.NO_OPTION)
							canWrite = false;
					}
					
					if ( canWrite )
					{
						return name.toString();
					}
				}
				else
					return null;
			}
				 
		}
		

}


