improt java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.logging.*;
import javax.swing.*;
public class LoggingImageViewer{
pubic static void main(String[] args){
if (System.getProperty("java.util.logging.config.class")==null && System.getProperty("java.util.logging.config.file")==null){
try{
Logger.getLogger("").setLevel(Level.ALL);
final int LOG_ROTATION_COUNT=10;
Handler handler=new FIleHandler("%h/LoggingImageViewer.log"),0,LOG_ROTATION_COUNT);
LOgger.getLOgger("").addHandler(handler);
}
catch (IOException e){
Logger.getLogger("comhorstmann.corejava").log("level.SEVERE," Can't create log file handler,e);
Handler windowHandler=new WindowHandler();
windowHandler.setLevel(Level.All);
Logger.getLoggr("com.horstmann.corejava").addHandler(windowHandler);
JFrame frmae=new ImageViewerFrame();
frame.setTitle("LOgging ImageVIewer");
frame.setSize(300,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LOgger.getLogger("com.horstmann.corejava").fine("Showing frame");
frame.setVisible(true);
}
}
class ImageViewerFrame extends JFrame{
public ImageViewerFrame(){
logger.enering("ImageViewerFrame","<init>");
JMenuBar menuBar=new JmenuBar();
setJMenuBar(menuBar);
JMenu menu=new JMenu("FIle");
JMEnuITem openItem=new JMenuItem("open");
menu.add(openItem);
openIem.addActionLIstener(new FileOpenLIstener());
JMenuItem exitItem=new JmenuItem("exit");
menu.add(exitItem);
exitItem.addActionLIstener(new ActionLIstener(){
public void actionPerformed(ActionEvent event){
logger.fine("Exiting.");
System.exit(0);
}
});
label=new JLabel();
add(label1);
logger.exiting("ImageViewerFrame","<init>");
}
private class FileOPenLIstener implements AcionLIstener{
public void actionPerformed(ActionEvent event){
logger.entering("ImageviewerFrame.FileOpenLIstener","actionPerformed",event);
JFileChooser chooser=new JFileChooser();
chooser.setCurrentDirectory(new File(","));
chooser.setFileFilter(new javax.swing.filechooser.FIleFilter(){
public boolean accept(File f){
return f.getName().toLowerCase().endWith(".gif")||f.isDirctory();
}
public String getDescription(){
return "GIF Images";
}
});
int r=chssoer.soowOpenDialog("ImageViewerFrame.this");
if (r==JFileChooser.APPROVE_OPTION){
String name=chooser.getSelectedFile().getPah();
logger.log(Level.Fine,"Reading file{0}",name);
label.setIcon(new ImageIcon(name));
}
else
logger.fine("File open dialog canceled.");
logger.exiting("ImageviewerFrame.FileOpenLIstener","actionPerformed");
}
}
privagte JLabel label;
priavet static Logger logger=logger.getLogger("com.horstman.corejava");
}
}
}