001: /**
002: * Copyright 2004 Carlos Silva A.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */package com.csa.lgantt;
017:
018: import java.awt.Dimension;
019: import java.awt.Image;
020: import java.awt.Toolkit;
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.net.URL;
026: import java.util.ArrayList;
027: import java.util.List;
028: import java.util.Properties;
029:
030: import javax.swing.ImageIcon;
031: import javax.swing.JFrame;
032: import javax.swing.JOptionPane;
033: import javax.swing.SwingUtilities;
034: import javax.swing.UIManager;
035: import javax.swing.plaf.metal.DefaultMetalTheme;
036: import javax.swing.plaf.metal.MetalLookAndFeel;
037:
038: import com.csa.lgantt.model.IOManager;
039: import com.csa.lgantt.model.Project;
040: import com.csa.lgantt.view.GanttActionListener;
041: import com.csa.lgantt.view.MainFrame;
042:
043: /**
044: * Aplicacion principal, lee la linea de comandos y carga un archivo si lo hay.
045: *
046: * @version $Header: /cvs/java/App_JGantt/source/jgantt/JGanttMain.java,v 1.5
047: * 2004/12/05 04:06:45 csilva Exp $
048: * @author Carlos Silva
049: */
050: public class LGanttMain implements Runnable {
051: String fileName = null;
052:
053: public static final String LAST_USED_REF = "abcdefg";
054:
055: /**
056: * Properties file, normally on the user.dir folder
057: */
058: public static final String PROPERTIES_FILE = ".lgantt.properties";
059:
060: /**
061: * Crea una ventana de gantt, editando un cierto archivo.
062: *
063: * @param fn
064: */
065: public LGanttMain(String fn) {
066: fileName = fn;
067: }
068:
069: /**
070: * Crea la ventana de Gantt y comienza la ejecucion... Esta funcion se llama
071: * desde el thread de swing.
072: */
073: public void run() {
074: try {
075: JFrame mainFrame = null;
076: if (fileName == null)
077: mainFrame = new MainFrame();
078: else {
079: File source = new File(fileName);
080: Project p = IOManager.loadFrom(source);
081: mainFrame = new MainFrame(p, source);
082: }
083: URL res = getClass().getClassLoader().getResource(
084: Messages.getString("MainFrame.icon.file"));
085: ImageIcon icon = new ImageIcon(res);
086: mainFrame.setIconImage(icon.getImage());
087: mainFrame.pack();
088: mainFrame.setSize(new Dimension(680, 350));
089: mainFrame.setVisible(true);
090:
091: File config = getConfigFile();
092: if (!config.exists()) {
093: GanttActionListener.cmdHelpHints();
094: config.createNewFile();
095: }
096:
097: } catch (Exception e) {
098: reportError("Excepcion al iniciar jGantt", e);
099: System.exit(1);
100: }
101: }
102:
103: /**
104: * Shows an exception on the interface
105: *
106: * @param title
107: * @param ex
108: */
109: public static void reportError(String title, Exception ex) {
110: ex.printStackTrace();
111: JOptionPane.showMessageDialog(null, ex, title,
112: JOptionPane.ERROR_MESSAGE);
113: }
114:
115: public static File getConfigFile() {
116: String userDir = System.getProperty("user.home");
117: return new File(userDir, PROPERTIES_FILE);
118: }
119:
120: static Properties properties = null;
121:
122: /**
123: * Returns the content of a property on the properties file
124: *
125: * @param name
126: * @return
127: * @throws IOException
128: */
129: public static String getProperty(String name) {
130: if (properties == null) {
131: properties = new Properties();
132: File f = getConfigFile();
133: if (f.exists()) {
134: try {
135: FileInputStream fis = new FileInputStream(f);
136: properties.load(fis);
137: fis.close();
138: } catch (IOException ex) {
139: reportError("Loading the properties file", ex);
140: }
141: }
142: }
143: return properties.getProperty(name);
144: }
145:
146: /**
147: * Saves the config into the config properties file
148: *
149: * @throws IOException
150: */
151: public static void saveConfig() {
152: if (properties == null)
153: return;
154: try {
155: File f = getConfigFile();
156: FileOutputStream out = new FileOutputStream(f);
157: properties.store(out, "");
158: out.close();
159: } catch (IOException ex) {
160: reportError("Saving configuration properties...", ex);
161: }
162: }
163:
164: /**
165: * Agrega un archivo abierto recientemente a la configuracion.
166: *
167: * TODO: Verificar que no sea igual a otro archivo ya abierto.
168: *
169: * @param f
170: * @throws IOException
171: */
172: public static void addLastUsed(File f) {
173: String lastUsed = getProperty("last.used");
174: if (lastUsed == null)
175: lastUsed = "";
176: StringBuffer lu = new StringBuffer(lastUsed);
177: int l = lastUsed.length();
178: char newKey = 'a';
179: if (l == 0) {
180: lu.insert(0, newKey);
181: } else {
182: // do not repeat a file reference
183: for (int i = 0; i < lastUsed.length(); i++) {
184: char k = lastUsed.charAt(i);
185: String filePath = getProperty("lru." + k);
186: if (filePath.equals(f.getAbsolutePath())) {
187: if (i > 0) { // move the option to the top.
188: lu.deleteCharAt(i);
189: lu.insert(0, k);
190: properties.setProperty("last.used", lu
191: .toString());
192: }
193: return;
194: }
195: }
196: if (l < LAST_USED_REF.length()) {
197: newKey = LAST_USED_REF.charAt(lastUsed.length());
198: lu.insert(0, newKey);
199: } else {
200: newKey = lastUsed.charAt(lastUsed.length() - 1);
201: lu.insert(0, newKey);
202: lu.setLength(l);
203: }
204: }
205: properties.setProperty("last.used", lu.toString());
206: properties.setProperty("lru." + newKey, f.getAbsolutePath());
207: saveConfig();
208: }
209:
210: /**
211: * retrieves the list of the last used files
212: *
213: * @return
214: */
215: public static List getLastUsed() {
216: List l = new ArrayList();
217: String lastUsed = getProperty("last.used");
218: if (lastUsed != null)
219: for (int i = 0; i < lastUsed.length(); i++) {
220: String ref = "lru." + lastUsed.charAt(i);
221: String fileName = getProperty(ref);
222: l.add(new File(fileName));
223: }
224: return l;
225: }
226:
227: /**
228: * Funcion principal utiliza MetalLookAndFeel.
229: *
230: * @param args
231: * @throws Exception
232: */
233: public static void main(String[] args) throws Exception {
234: try {
235: MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
236: UIManager
237: .setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
238: } catch (Exception e) {
239: System.err
240: .println("ERROR: LookAndFeel could not be loaded: "
241: + e.toString());
242: }
243: String fileName = null;
244: for (int i = 0; i < args.length; i++) {
245: if (!args[i].startsWith("-")) {
246: fileName = args[i];
247: }
248: }
249:
250: SwingUtilities.invokeLater(new LGanttMain(fileName));
251: }
252: }
|