Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Ikayaki.java

Go to the documentation of this file.
00001 /*
00002  * Ikayaki.java
00003  *
00004  * Copyright (C) 2005 Project SQUID, http://www.cs.helsinki.fi/group/squid/
00005  *
00006  * This file is part of Ikayaki.
00007  *
00008  * Ikayaki is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Ikayaki is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Ikayaki; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00021  */
00022 
00023 package ikayaki;
00024 
00025 import com.jgoodies.looks.plastic.Plastic3DLookAndFeel;
00026 import com.jgoodies.looks.plastic.PlasticLookAndFeel;
00027 import com.jgoodies.looks.plastic.theme.SkyBlue;
00028 import ikayaki.gui.MainViewPanel;
00029 import ikayaki.util.LoggerPrintStream;
00030 import jutil.JUtil;
00031 
00032 import javax.swing.*;
00033 import java.awt.*;
00034 import java.awt.event.*;
00035 import java.io.File;
00036 import java.io.FileNotFoundException;
00037 import java.io.FileOutputStream;
00038 import java.io.PrintStream;
00039 import java.util.Date;
00040 
00046 public class Ikayaki extends JFrame {
00047 
00048     /* Application information */
00049 
00050     public static final String APP_NAME = "Ikayaki";
00051     public static final String APP_VERSION = "1.0";
00052     public static final String APP_BUILD = "2005-05-06";
00053     public static final String APP_HOME_PAGE = "http://www.cs.helsinki.fi/group/squid/";
00054 
00055     public static final String FILE_TYPE = ".ika";
00056     public static final String FILE_DESCRIPTION = "Ikayaki Project File";
00057 
00058     public static final String[] AUTHORS = new String[]{
00059         "Mikko Jormalainen",
00060         "Samuli Kaipiainen",
00061         "Aki Korpua",
00062         "Esko Luontola",
00063         "Aki Sysmäläinen"
00064     };
00065 
00066     /* Application files and directories */
00067 
00068     public static final File STARTUP_DIRECTORY = new File(System.getProperty("user.dir")).getAbsoluteFile();
00069     public static final String PROGRAM_JAR_NAME = "ikayaki.jar";
00070 
00071     static {
00072         // change to the program directory if it was started somewhere else
00073         String[] paths = System.getProperty("java.class.path").split(System.getProperty("path.separator"));
00074         for (String s : paths) {
00075             File file = new File(s);
00076             if (file.getName().equals(PROGRAM_JAR_NAME)) {
00077                 file = file.getAbsoluteFile();
00078                 System.setProperty("user.dir", file.getParent());
00079                 break;
00080             }
00081         }
00082     }
00083 
00084     public static final File PROPERTIES_FILE = new File("ikayaki.config").getAbsoluteFile();
00085     public static final File SEQUENCES_FILE = new File("ikayaki.sequences").getAbsoluteFile();
00086     public static final File CALIBRATION_PROJECT_DIR = new File("calibration").getAbsoluteFile();
00087     public static final File DEBUG_LOG_DIR = new File("logs").getAbsoluteFile();
00088     public static final File DEBUG_LOG_FILE = new File("debug.log").getAbsoluteFile();
00089     public static final String HELP_PAGES = new File("manual/index.html").getAbsolutePath();
00090 
00097     public static void main(String[] args) {
00098 
00099         // clean up log files
00100         logFileCleanup(DEBUG_LOG_FILE, 1024 * 1024, 5);
00101         logDirCleanup(DEBUG_LOG_DIR, 30);
00102 
00103         // redirect a copy of System.err to a file
00104         try {
00105             String message = "\n\n----- " + APP_NAME + " " + APP_VERSION
00106                     + " started on " + new Date().toString() + " -----";
00107             PrintStream logger = new LoggerPrintStream(new FileOutputStream(DEBUG_LOG_FILE, true), System.err, message);
00108             System.setErr(logger);
00109 
00110         } catch (FileNotFoundException e) {
00111             System.err.println("Unable to write to: " + DEBUG_LOG_FILE);
00112         }
00113 
00114         // read input parameters and load the optional project file
00115         Project project = null;
00116         if (args.length > 0) {
00117             File file = new File(args[0]);
00118             if (!file.isAbsolute()) {
00119                 file = new File(STARTUP_DIRECTORY, file.getPath());
00120             }
00121             if (file.exists() && file.isFile()) {
00122                 project = Project.loadProject(file.getAbsoluteFile());
00123             }
00124         }
00125 
00126         // the program must be started in the event dispatch thread
00127         final Project p = project;
00128         SwingUtilities.invokeLater(new Runnable() {
00129             public void run() {
00130                 new Ikayaki(p);
00131             }
00132         });
00133     }
00134 
00143     private static void logFileCleanup(File logFile, long maxLength, int maxFiles) {
00144         if (!logFile.isFile() || logFile.length() <= maxLength) {
00145             return;
00146         }
00147 
00148         // rename the files to file.1, file.2 and so on
00149         for (int i = maxFiles - 1; i >= 0; i--) {
00150             File from = new File(logFile.getAbsolutePath() + (i == 0 ? "" : "." + i));
00151             File to = new File(logFile.getAbsolutePath() + "." + (i + 1));
00152             if (from.isFile() && !to.exists()) {
00153                 from.renameTo(to);
00154             }
00155         }
00156 
00157         // delete the oldest file
00158         File f = new File(logFile.getAbsolutePath() + "." + maxFiles);
00159         if (f.isFile()) {
00160             f.delete();
00161         }
00162     }
00163 
00170     private static void logDirCleanup(File directory, int maxDays) {
00171         if (!directory.isDirectory()) {
00172             return;
00173         }
00174 
00175         // get the directory contents and set time limits
00176         File[] files = directory.listFiles();
00177         long now = System.currentTimeMillis();
00178         long maxTime = (long) (maxDays) * 24 * 3600 * 1000;
00179 
00180         // delete all files which are more than maxDays old
00181         for (File file : files) {
00182             if (file.isFile() && (now - file.lastModified()) > maxTime) {
00183                 file.delete();
00184             }
00185         }
00186     }
00187 
00194     public Ikayaki(Project project) throws HeadlessException {
00195 
00196         // set look and feel
00197         PlasticLookAndFeel.setMyCurrentTheme(new SkyBlue());
00198         try {
00199             UIManager.setLookAndFeel(new Plastic3DLookAndFeel());
00200         } catch (UnsupportedLookAndFeelException e) {
00201             System.err.println(e);
00202         }
00203 
00204         // do layout
00205         final MainViewPanel main = new MainViewPanel(project);
00206         setTitle(null);
00207         setIconImage(new ImageIcon(ClassLoader.getSystemResource("resources/icon.png")).getImage());
00208         setLayout(new BorderLayout());
00209         setJMenuBar(main.getMenuBar());
00210         add(main, "Center");
00211         //add(main.getStatusBar(), "South");    // TODO: there is no status bar
00212 
00213         setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
00214         pack();
00215 
00216         // monitor the size of this frame
00217         addComponentListener(new ComponentAdapter() {
00218             @Override public void componentResized(ComponentEvent e) {
00219                 if ((getExtendedState() & MAXIMIZED_BOTH) == 0) {
00220                     Settings.setWindowWidth(getWidth());
00221                     Settings.setWindowHeight(getHeight());
00222                 }
00223             }
00224         });
00225         addWindowStateListener(new WindowStateListener() {
00226             public void windowStateChanged(WindowEvent e) {
00227                 if ((getExtendedState() & MAXIMIZED_BOTH) != 0) {
00228                     Settings.setWindowMaximized(true);
00229                 } else {
00230                     Settings.setWindowMaximized(false);
00231                 }
00232             }
00233         });
00234 
00235         // set the window close operation
00236         addWindowListener(new WindowAdapter() {
00237             @Override public void windowClosing(WindowEvent e) {
00238                 main.exitProgram();
00239             }
00240         });
00241 
00242         // restore size and position
00243         setSize(Settings.getWindowWidth(), Settings.getWindowHeight());
00244         setLocationByPlatform(true);
00245         setVisible(true);
00246 
00247         Rectangle maxBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
00248         setLocation(getX() + getWidth() > maxBounds.width ? maxBounds.width - getWidth() : getX(),
00249                 getY() + getHeight() > maxBounds.height ? maxBounds.height - getHeight() : getY());
00250         setLocation(getX() < 0 ? 0 : getX(), getY() < 0 ? 0 : getY());
00251 
00252         if (Settings.getWindowMaximized() && System.getProperty("os.name").startsWith("Windows")) {
00253             try {
00254                 // native code for maximizing the window
00255                 int hwnd = JUtil.getHwnd(getTitle());
00256                 JUtil.setWindowMaximized(hwnd);
00257             } catch (UnsatisfiedLinkError e) {
00258                 e.printStackTrace();
00259             }
00260         }
00261     }
00262 
00268     @Override public void setTitle(String title) {
00269         if (title != null) {
00270             super.setTitle(APP_NAME + " " + APP_VERSION + " - " + title);
00271         } else {
00272             super.setTitle(APP_NAME + " " + APP_VERSION);
00273         }
00274     }
00275 }

Generated on Fri May 6 16:00:32 2005 for Squid by  doxygen 1.4.1