0c758c7e1edca39c041636b4f932777bb41b6eea
[debian/jabref.git] / src / java / net / sf / jabref / JabRef.java
1 /*
2  * Copyright (C) 2003 Morten O. Alver, Nizar N. Batada
3  * 
4  * All programs in this directory and subdirectories are published under the GNU
5  * General Public License as described below.
6  *
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option) any later
10  * version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19  * Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Further information about the GNU GPL is available at:
22  * http://www.gnu.org/copyleft/gpl.ja.html
23  *
24  */
25 package net.sf.jabref;
26
27 import com.jgoodies.plaf.FontSizeHints;
28 import net.sf.jabref.export.*;
29 import net.sf.jabref.imports.*;
30 import net.sf.jabref.wizard.auximport.*;
31 import net.sf.jabref.remote.RemoteListener;
32
33 import gnu.dtools.ritopt.*;
34 import java.awt.Font;
35 import java.awt.Frame;
36
37 import java.io.*;
38 import java.io.File;
39
40 import java.util.*;
41
42 import javax.swing.*;
43
44 import com.jgoodies.plaf.plastic.Plastic3DLookAndFeel;
45 import com.jgoodies.plaf.windows.ExtWindowsLookAndFeel;
46
47
48
49
50 //import javax.swing.UIManager;
51 //import javax.swing.UIDefaults;
52 //import javax.swing.UnsupportedLookAndFeelException;
53 public class JabRef {
54     public static JabRef ths;
55     public static RemoteListener remoteListener = null;
56     public JabRefFrame jrf;
57     public Options options;
58     public Frame splashScreen = null;
59
60     boolean graphicFailure = false;
61
62     StringOption importFile, exportFile, exportPrefs, importPrefs, auxImExport, importToOpenBase;
63     BooleanOption helpO, disableGui, blank, loadSess;
64     /*
65     * class StringArrayOption extends ArrayOption { public public void
66     * modify(String value) { } public void modify(String[] value) { } public
67     * Object[] getObjectArray() { return null; } public String getTypeName() {
68     * return "Strings"; } public String getStringValue() { return ""; } public
69     * Object getObject() { return null; } }
70     */
71     public static void main(String[] args) {
72         new JabRef(args);
73     }
74
75     public JabRef(String[] args) {
76
77         /*
78         String aut = "{Bill and Bob Alver}, Jr., Morten Omholt and Alfredsen, Jo A. and Øie, G. and Yngvar von Olsen";
79         System.out.println("lastnameFirst: "+ImportFormatReader.fixAuthor_lastNameFirst(aut));
80         System.out.println("lastnameFirstCommas: "+ImportFormatReader.fixAuthor_lastNameFirstCommas(aut,false));
81         System.out.println("lastnameFirstCommas (abbr): "+ImportFormatReader.fixAuthor_lastNameFirstCommas(aut,true));
82         System.out.println("firstNameFirst: "+ImportFormatReader.fixAuthor_firstNameFirst(aut));
83         System.out.println("firstNameFirstCommas: "+ImportFormatReader.fixAuthor_firstNameFirstCommas(aut, false));
84         System.out.println("forAlphabetization: "+ImportFormatReader.fixAuthorForAlphabetization(aut));
85         */
86         ths = this;
87         JabRefPreferences prefs = JabRefPreferences.getInstance();
88         Globals.prefs = prefs;
89         Globals.setLanguage(prefs.get("language"), "");
90                 
91         Globals.importFormatReader.resetImportFormats();
92         BibtexEntryType.loadCustomEntryTypes(prefs);
93         // Read list(s) of journal names and abbreviations:
94         //Globals.turnOnFileLogging();
95
96         Globals.initializeJournalNames();
97
98         if (Globals.prefs.getBoolean("useRemoteServer")) {
99             remoteListener = RemoteListener.openRemoteListener(this);
100             if (remoteListener != null) {
101                 remoteListener.start();
102             }
103
104         // Unless we are alone, try to contact already running JabRef:
105             if (remoteListener == null) {
106                     if (RemoteListener.sendToActiveJabRefInstance(args)) {
107                 // We have successfully sent our command line options through the socket to
108                 // another JabRef instance. So we assume it's all taken care of, and quit.
109                 System.out.println(Globals.lang("Arguments passed on to running JabRef instance. Shutting down."));
110                 System.exit(0);
111             }
112         }
113         }
114
115       /**
116        * See if the user has a personal journal list set up. If so, add these
117        * journal names and abbreviations to the list:
118        */
119       String personalJournalList = prefs.get("personalJournalList");
120       if (personalJournalList != null) {
121           try {
122               Globals.journalAbbrev.readJournalList(new File(personalJournalList));
123           } catch (FileNotFoundException e) {
124               e.printStackTrace();
125           }
126       }
127
128         
129         //System.setProperty("sun.awt.noerasebackground", "true");
130         
131         //System.out.println(java.awt.Toolkit.getDefaultToolkit().getDesktopProperty("awt.dynamicLayoutSupported"));
132         // Make sure of a proper cleanup when quitting (e.g. deleting temporary
133         // files).
134         System.runFinalizersOnExit(true);
135
136         Vector loaded = processArguments(args, true);
137         openWindow(loaded);
138         //System.out.println("1");
139     }
140
141     private void setupOptions() {
142
143         importFile = new StringOption("");
144         exportFile = new StringOption("");
145         helpO = new BooleanOption();
146         disableGui = new BooleanOption();
147         blank = new BooleanOption();
148         loadSess = new BooleanOption();
149         exportPrefs = new StringOption("jabref_prefs.xml");
150         importPrefs = new StringOption("jabref_prefs.xml");
151         auxImExport = new StringOption("");
152         importToOpenBase = new StringOption("");
153
154         options = new Options("JabRef "); // Create an options repository.
155         options.setVersion(GUIGlobals.version);
156
157         importFile.setDescription("imopoepuoeu"); //Globals.lang);
158         options.register("nogui", 'n',
159             Globals.lang("No GUI. Only process command line options."), disableGui);
160         options.register("import", 'i',
161             Globals.lang("Import file") + ": " + Globals.lang("filename")
162             + "[,import format]", importFile);
163         options.register("output", 'o',
164             Globals.lang("Output or export file") + ": " + Globals.lang("filename")
165             + "[,export format]", exportFile);
166         options.register("help", 'h',
167             Globals.lang("Display help on command line options"), helpO);
168         options.register("loads", 'l', Globals.lang("Load session"), loadSess);
169         options.register("prexp", 'x', Globals.lang("Export preferences to file"),
170             exportPrefs);
171         options.register("primp", 'p', Globals.lang("Import preferences from file"),
172             importPrefs);
173         options.register("aux", 'a',
174             Globals.lang("Subdatabase from aux") + ": " + Globals.lang("file")+"[.aux]" + ","+Globals.lang("new")+"[.bib]",
175             auxImExport);
176         options.register("blank", 'b', Globals.lang("Do not open any files at startup"), blank);
177
178         options.register("importToOpen", '\0', Globals.lang("Import to open tab"), importToOpenBase);
179
180         options.setUseMenu(false);
181     }
182
183     public Vector processArguments(String[] args, boolean initialStartup) {
184
185         setupOptions();
186         String[] leftOver = options.process(args);
187
188         //Util.pr(": Options processed");
189
190         if (initialStartup && helpO.isInvoked()) {
191             System.out.println("jabref [options] [bibtex-file]\n");
192             System.out.println(options.getHelp());
193
194             String importFormats = Globals.importFormatReader.getImportFormatList();
195             System.out.println(Globals.lang("Available import formats") + ":\n"
196                 + importFormats);
197
198             // + ": biblioscape, bibtexml, endnote, inspec,\n\tisi, medline, ovid,
199             // ris, scifinder, sixpack, jstor, silverplatter.");
200             // To specify export formats, we need to take the custom export formats
201             // into account.
202             // So we iterate through the custom formats and add them.
203             String outFormats = ": bibtexml, docbook, html, simplehtml";
204             int length = outFormats.length();
205
206             for (int i = 0; i < Globals.prefs.customExports.size(); i++) {
207                 String[] format = Globals.prefs.customExports.getElementAt(i);
208
209                 if ((length + format[0].length()) > 50) {
210                     outFormats = outFormats + ",\n\t" + format[0];
211                     length = format[0].length();
212                 } else {
213                     outFormats = outFormats + ", " + format[0];
214                     length += (1 + format[0].length());
215                 }
216             }
217
218             System.out.println(Globals.lang("Available export formats") + outFormats
219                 + ".");
220             System.exit(0);
221         }
222
223         // First we quickly scan the command line parameters for any that signal
224         // that the GUI
225         // should not be opened. This is used to decide whether we should show the
226         // splash screen or not.
227         if (initialStartup && !disableGui.isInvoked()) {
228             try {
229
230                 splashScreen = SplashScreen.splash();
231
232             } catch (Throwable ex) {
233                 graphicFailure = true;
234                 System.err.println(Globals.lang("Unable to create graphical interface")
235                     + ".");
236             }
237         }
238
239         //Util.pr("JabRef "+GUIGlobals.version);
240         // Vector to put imported/loaded database(s) in.
241         Vector loaded = new Vector();
242         Vector toImport = new Vector();
243         if (!blank.isInvoked() && (leftOver.length > 0))  {
244             for (int i = 0; i < leftOver.length; i++) {
245                 // Leftover arguments are interpreted as bib files to open.
246
247                 ParserResult pr = openBibFile(leftOver[i]);
248
249                 if (pr != null) {
250                     if (pr == ParserResult.INVALID_FORMAT)
251                         // We will try to import this file instead:
252                         toImport.add(leftOver[i]);
253                     else
254                         loaded.add(pr);
255                 } else {
256
257                 }
258             }
259         }
260
261         //Util.pr(": Checked blank");
262
263         if (!blank.isInvoked() && importFile.isInvoked()) {
264             toImport.add(importFile.getStringValue());
265         }
266
267         if (toImport.size() > 0) for (int i=0; i<toImport.size(); i++) {
268             String[] data = ((String)toImport.elementAt(i)).split(",");
269
270             /*if (data.length == 1) {
271                 // Load a bibtex file:
272                 ParserResult pr = openBibFile(data[0]);
273
274                 if (pr != null)
275                     loaded.add(pr);
276             } else if (data.length == 2) {*/
277                 // Import a database in a certain format.
278                 try {
279
280                     if ((data.length > 1) && !"*".equals(data[1])) {
281                         System.out.println(Globals.lang("Importing") + ": " + data[0]);
282                         List entries =
283                             Globals.importFormatReader.importFromFile(data[1],
284                                 data[0].replaceAll("~", System.getProperty("user.home")));
285                         BibtexDatabase base = ImportFormatReader.createDatabase(entries);
286                         ParserResult pr = new ParserResult(base, null, new HashMap());
287                         loaded.add(pr);
288                         
289                     } else {
290                         // * means "guess the format":
291                         System.out.println(Globals.lang("Importing in unknown format")
292                             + ": " + data[0]);
293
294                         Object[] o =
295                             Globals.importFormatReader.importUnknownFormat(data[0]
296                                 .replaceAll("~", System.getProperty("user.home")));
297                         String formatName = (String) o[0];
298
299                         if (formatName == null) {
300                             System.err.println(Globals.lang("Error opening file")+" '"+data[0]+"'");    
301                         }
302                         else if (formatName.equals(ImportFormatReader.BIBTEX_FORMAT)) {
303                             ParserResult pr = (ParserResult)o[1];
304                             loaded.add(pr);  
305
306                         }
307                         else {
308                             List entries = (java.util.List) o[1];
309                             if (entries != null)
310                                 System.out.println(Globals.lang("Format used") + ": "
311                                     + formatName);
312                             else
313                                 System.out.println(Globals.lang(
314                                         "Could not find a suitable import format."));
315
316                             if (entries != null) {
317                                 BibtexDatabase base = ImportFormatReader.createDatabase(entries);
318                                 ParserResult pr = new ParserResult(base, null, new HashMap());
319                         
320                                 //pr.setFile(new File(data[0]));
321                                 loaded.add(pr);
322                             }
323                         }
324                     }
325                 } catch (IOException ex) {
326                     System.err.println(Globals.lang("Error opening file") + " '"
327                         + data[0] + "': " + ex.getMessage());
328                 }
329
330
331         }
332
333
334         if (/*!initialStartup && */!blank.isInvoked() && importToOpenBase.isInvoked()) {
335             String[] data = importToOpenBase.getStringValue().split(",");
336
337             /*if (data.length == 1) {
338                 // Load a bibtex file:
339                 ParserResult pr = openBibFile(data[0]);
340
341                 if (pr != null) {
342                     pr.setToOpenTab(true);
343                     loaded.add(pr);
344                 }
345             } else if (data.length == 2) {*/
346                 // Import a database in a certain format.
347                 try {
348
349                     if ((data.length > 1) && !"*".equals(data[1])) {
350                         System.out.println(Globals.lang("Importing") + ": " + data[0]);
351                         List entries =
352                             Globals.importFormatReader.importFromFile(data[1],
353                                 data[0].replaceAll("~", System.getProperty("user.home")));
354                         BibtexDatabase base = ImportFormatReader.createDatabase(entries);
355                         ParserResult pr = new ParserResult(base, null, new HashMap());
356                         pr.setToOpenTab(true);
357                         loaded.add(pr);
358
359                     } else {
360                         // * means "guess the format":
361                         System.out.println(Globals.lang("Importing in unknown format")
362                             + ": " + data[0]);
363
364                         Object[] o =
365                             Globals.importFormatReader.importUnknownFormat(data[0]
366                                 .replaceAll("~", System.getProperty("user.home")));
367                         String formatName = (String) o[0];
368
369                         if (formatName.equals(ImportFormatReader.BIBTEX_FORMAT)) {
370                             ParserResult pr = (ParserResult)o[1];
371                             pr.setToOpenTab(true);
372                             loaded.add(pr);
373
374                         }
375                         else {
376                             List entries = (java.util.List) o[1];
377                             if (entries != null)
378                                 System.out.println(Globals.lang("Format used") + ": "
379                                     + formatName);
380                             else
381                                 System.out.println(Globals.lang(
382                                         "Could not find a suitable import format."));
383
384                             if (entries != null) {
385                                 BibtexDatabase base = ImportFormatReader.createDatabase(entries);
386                                 ParserResult pr = new ParserResult(base, null, new HashMap());
387
388                                 //pr.setFile(new File(data[0]));
389                                 pr.setToOpenTab(true);
390                                 loaded.add(pr);
391                             }
392                         }
393                     }
394                 } catch (IOException ex) {
395                     System.err.println(Globals.lang("Error opening file") + " '"
396                         + data[0] + "': " + ex.getMessage());
397                 }
398
399
400         }
401         //Util.pr(": Finished import");
402
403         if (exportFile.isInvoked()) {
404             if (loaded.size() > 0) {
405                 String[] data = exportFile.getStringValue().split(",");
406
407                 if (data.length == 1) {
408                     // This signals that the latest import should be stored in BibTeX
409                     // format to the given file.
410                     if (loaded.size() > 0) {
411                         ParserResult pr =
412                             (ParserResult) loaded.elementAt(loaded.size() - 1);
413
414                         try {
415                             System.out.println(Globals.lang("Saving") + ": " + data[0]);
416                             SaveSession session = FileActions.saveDatabase(pr.getDatabase(),
417                                 new MetaData(pr.getMetaData(),pr.getDatabase()), new File(data[0]), Globals.prefs,
418                                 false, false, Globals.prefs.get("defaultEncoding"));
419                             // Show just a warning message if encoding didn't work for all characters:
420                             if (!session.getWriter().couldEncodeAll())
421                                 System.err.println(Globals.lang("Warning")+": "+
422                                     Globals.lang("The chosen encoding '%0' could not encode the following characters: ",
423                                     session.getEncoding())+session.getWriter().getProblemCharacters());
424                             session.commit();
425                         } catch (SaveException ex) {
426                             System.err.println(Globals.lang("Could not save file") + " '"
427                                 + data[0] + "': " + ex.getMessage());
428                         }
429                     } else
430                         System.err.println(Globals.lang(
431                                 "The output option depends on a valid import option."));
432                 } else if (data.length == 2) {
433                     // This signals that the latest import should be stored in the given
434                     // format to the given file.
435                     ParserResult pr = (ParserResult) loaded.elementAt(loaded.size() - 1);
436
437                     // We first try to find a matching custom export format.
438                     boolean foundCustom = false;
439
440                     for (int i = 0; i < Globals.prefs.customExports.size(); i++) {
441                         String[] format = Globals.prefs.customExports.getElementAt(i);
442
443                         if (format[0].equals(data[1])) {
444                             // Found the correct export format here.
445                             //System.out.println(format[0]+" "+format[1]+" "+format[2]);
446                             try {
447                                 File lfFile = new File(format[1]);
448
449                                 //System.out.println(lfFile.getName());
450                                 String fname = (lfFile.getName().split("\\."))[0];
451                                 FileActions.exportDatabase(pr.getDatabase(),
452                                     lfFile.getParent() + File.separator, fname,
453                                     new File(data[0]), pr.getEncoding());
454                                 System.out.println(Globals.lang("Exporting") + ": "
455                                     + data[0]);
456                             } catch (Exception ex) {
457                                 //ex.printStackTrace();
458                                 System.err.println(Globals.lang("Could not export file")
459                                     + " '" + data[0] + "': " + ex.getMessage());
460                             }
461
462                             foundCustom = true;
463
464                             break;
465                         }
466                     }
467
468                     if (!foundCustom) {
469                         try {
470                             System.out.println(Globals.lang("Exporting") + ": " + data[0]);
471                             FileActions.exportDatabase(pr.getDatabase(), data[1],
472                                 new File(data[0]), pr.getEncoding());
473                         } catch (NullPointerException ex2) {
474                             System.err.println(Globals.lang("Unknown export format")
475                                 + ": " + data[1]);
476                         } catch (Exception ex) {
477                             System.err.println(Globals.lang("Could not export file")
478                                 + " '" + data[0] + "': " + ex.getMessage());
479                         }
480                     }
481                 }
482             } else
483                 System.err.println(Globals.lang(
484                         "The output option depends on a valid import option."));
485         }
486
487         //Util.pr(": Finished export");
488
489         if (exportPrefs.isInvoked()) {
490             try {
491                 Globals.prefs.exportPreferences(exportPrefs.getStringValue());
492             } catch (IOException ex) {
493                 Util.pr(ex.getMessage());
494             }
495         }
496
497
498         if (importPrefs.isInvoked()) {
499             try {
500                 Globals.prefs.importPreferences(importPrefs.getStringValue());
501                 BibtexEntryType.loadCustomEntryTypes(Globals.prefs);
502             }
503             catch (IOException ex) {
504                 Util.pr(ex.getMessage());
505             }
506         }
507
508         if (!blank.isInvoked() && auxImExport.isInvoked()) {
509             boolean usageMsg = false;
510
511             if (loaded.size() > 0) // bibtex file loaded
512              {
513                 String[] data = auxImExport.getStringValue().split(",");
514
515                 if (data.length == 2) {
516                     ParserResult pr = (ParserResult) loaded.firstElement();
517                     AuxCommandLine acl = new AuxCommandLine(data[0], pr.getDatabase());
518                     BibtexDatabase newBase = acl.perform();
519
520                     boolean notSavedMsg = false;
521
522                     // write an output, if something could be resolved
523                     if (newBase != null) {
524                         if (newBase.getEntryCount() > 0) {
525                             String subName = Util.getCorrectFileName(data[1], "bib");
526
527                             try {
528                                 System.out.println(Globals.lang("Saving") + ": "
529                                     + subName);
530                                 SaveSession session = FileActions.saveDatabase(newBase, new MetaData(), // no Metadata
531                                     new File(subName), Globals.prefs, false, false,
532                                     Globals.prefs.get("defaultEncoding"));
533                                 // Show just a warning message if encoding didn't work for all characters:
534                                 if (!session.getWriter().couldEncodeAll())
535                                     System.err.println(Globals.lang("Warning")+": "+
536                                         Globals.lang("The chosen encoding '%0' could not encode the following characters: ",
537                                         session.getEncoding())+session.getWriter().getProblemCharacters());
538                                 session.commit();
539                             } catch (SaveException ex) {
540                                 System.err.println(Globals.lang("Could not save file")
541                                     + " '" + subName + "': " + ex.getMessage());
542                             }
543
544                             notSavedMsg = true;
545                         }
546                     }
547
548                     if (!notSavedMsg)
549                         System.out.println(Globals.lang("no database generated"));
550                 } else
551                     usageMsg = true;
552             } else
553                 usageMsg = true;
554
555             if (usageMsg) {
556                 System.out.println(Globals.lang("no base-bibtex-file specified"));
557                 System.out.println(Globals.lang("usage") + " :");
558                 System.out.println(
559                     "jabref --aux infile[.aux],outfile[.bib] base-bibtex-file");
560             }
561         }
562
563         return loaded;
564     }
565
566     public void openWindow(Vector loaded) {
567         if (!graphicFailure && !disableGui.isInvoked()) {
568             // Call the method performCompatibilityUpdate(), which does any
569             // necessary changes for users with a preference set from an older
570             // Jabref version.
571             Util.performCompatibilityUpdate();
572
573            // This property is set to make the Mac OSX Java VM move the menu bar to
574             // the top
575             // of the screen, where Mac users expect it to be.
576             System.setProperty("apple.laf.useScreenMenuBar", "true");
577
578             // Set antialiasing on everywhere. This only works in JRE >= 1.5.
579             // Or... it doesn't work, period.
580             //System.setProperty("swing.aatext", "true");
581             // If we are not on Mac, deal with font sizes and LookAndFeels:
582             if (!Globals.ON_MAC) {
583                 int fontSizes = Globals.prefs.getInt("menuFontSize");
584
585                 String defaultLookAndFeel;
586
587                 if (Globals.ON_WIN)
588                     defaultLookAndFeel = GUIGlobals.windowsDefaultLookAndFeel;
589                 else
590                     defaultLookAndFeel = GUIGlobals.linuxDefaultLookAndFeel;
591
592                 String lookAndFeel = null;
593
594                 if (!Globals.prefs.getBoolean("useDefaultLookAndFeel"))
595                     lookAndFeel = Globals.prefs.get("lookAndFeel");
596                 else
597                     lookAndFeel = defaultLookAndFeel;
598
599                 LookAndFeel lnf = null;
600
601                 //Class plastic =
602                 // Class.forName("com.jgoodies.plaf.plastic.PlasticLookAndFeel");
603                 //PlasticLookAndFeel lnf = new
604                 // com.jgoodies.plaf.plastic.Plastic3DLookAndFeel();
605                 Object objLnf = null;
606
607
608                 try {
609                     //lnf2 =
610                     // Class.forName("com.jgoodies.plaf.plastic.Plastic3DLookAndFeel").newInstance();
611                     if (lookAndFeel != null)
612                         objLnf = Class.forName(lookAndFeel).newInstance();
613                     else
614                         objLnf = Class.forName(defaultLookAndFeel).newInstance();
615                 } catch (Exception ex) {
616                     ex.printStackTrace();
617
618                     try {
619                         objLnf = Class.forName(defaultLookAndFeel).newInstance();
620                     } catch (Exception ex2) {
621                     }
622                 }
623
624                 if (objLnf != null)
625                     lnf = (LookAndFeel) objLnf;
626
627                 // Set font sizes if we are using a JGoodies look and feel.
628                 if ((lnf != null) && (lnf instanceof Plastic3DLookAndFeel)) {
629                     //MetalLookAndFeel.setCurrentTheme(new
630                     // com.jgoodies.plaf.plastic.theme.SkyBluer());
631                     Plastic3DLookAndFeel plLnf = (Plastic3DLookAndFeel) lnf;
632                     plLnf.setFontSizeHints(new FontSizeHints(fontSizes, fontSizes,
633                             fontSizes, fontSizes));
634                 } else if ((lnf != null) && (lnf instanceof ExtWindowsLookAndFeel)) {
635                     //System.out.println("ttt");
636                     ExtWindowsLookAndFeel plLnf = (ExtWindowsLookAndFeel) lnf;
637                     plLnf.setFontSizeHints(new FontSizeHints(fontSizes, fontSizes,
638                             fontSizes, fontSizes));
639                 }
640
641                 if (lnf != null) {
642                     try {
643                         UIManager.setLookAndFeel(lnf);
644                         
645                         if (!Globals.ON_WIN) {
646                             UIManager.put("SimpleInternalFrame.activeTitleBackground", GUIGlobals.gradientBlue);
647                             //UIManager.put("TabbedPane.selected", Color.red);
648                         }
649                     } catch (Throwable ex) {
650                         ex.printStackTrace();
651                         System.err.println("Trying to set system default Look&Feel...");
652
653                         // if desired lnf could not be set, try system default
654                         try {
655                             UIManager.setLookAndFeel(UIManager
656                                 .getSystemLookAndFeelClassName());
657                         } catch (Throwable e) {
658                             e.printStackTrace();
659                         }
660                     }
661
662                     //LookAndFeel lnf = new com.sun.java.swing.plaf.gtk.GTKLookAndFeel();
663                     //Look1AndFeel lnf = new
664                     // com.incors.plaf.kunststoff.KunststoffLookAndFeel();
665                     //com.incors.plaf.kunststoff.KunststoffLookAndFeel.setCurrentTheme(new
666                     // com.incors.plaf.kunststoff.themes.KunststoffDesktopTheme());
667                 }
668             }
669
670
671             // If the option is enabled, open the last edited databases, if any.
672             if (!blank.isInvoked() && Globals.prefs.getBoolean("openLastEdited") && (Globals.prefs.get("lastEdited") != null)) {
673                 // How to handle errors in the databases to open?
674                 String[] names = Globals.prefs.getStringArray("lastEdited");
675 lastEdLoop: 
676                 for (int i = 0; i < names.length; i++) {
677                     File fileToOpen = new File(names[i]);
678
679                     for (int j = 0; j < loaded.size(); j++) {
680                         ParserResult pr = (ParserResult) loaded.elementAt(j);
681
682                         if ((pr.getFile() != null) &&pr.getFile().equals(fileToOpen))
683                             continue lastEdLoop;
684                     }
685
686                     if (fileToOpen.exists()) {
687                         ParserResult pr = openBibFile(names[i]);
688
689                         if (pr != null) {
690
691                             if (pr == ParserResult.INVALID_FORMAT) {
692                                 System.out.println(Globals.lang("Error opening file")+" '"+fileToOpen.getPath()+"'");
693                             }
694                             else
695                                 loaded.add(pr);
696
697                         }
698                     }
699                 }
700             }
701
702             GUIGlobals.init();
703             GUIGlobals.CURRENTFONT =
704                 new Font(Globals.prefs.get("fontFamily"), Globals.prefs.getInt("fontStyle"),
705                     Globals.prefs.getInt("fontSize"));
706
707             //Util.pr(": Initializing frame");
708             jrf = new JabRefFrame();
709
710             // Add all loaded databases to the frame:
711             boolean first = true;
712             if (loaded.size() > 0) {
713                 for (Iterator i=loaded.iterator(); i.hasNext();) {
714                     ParserResult pr = (ParserResult)i.next();
715                     jrf.addTab(pr.getDatabase(), pr.getFile(), pr.getMetaData(), pr.getEncoding(), first);
716                     first = false;
717                 }
718             }
719
720             if (loadSess.isInvoked())
721                 jrf.loadSessionAction.actionPerformed(new java.awt.event.ActionEvent(
722                         jrf, 0, ""));
723
724             if (splashScreen != null) {// do this only if splashscreen was actually created
725                 splashScreen.dispose();
726                 splashScreen = null;
727             }
728
729             //Util.pr(": Showing frame");
730             jrf.setVisible(true);
731
732             for (int i = 0; i < loaded.size(); i++) {
733                 ParserResult pr = (ParserResult) loaded.elementAt(i);
734                 if (Globals.prefs.getBoolean("displayKeyWarningDialogAtStartup") && pr.hasWarnings()) {
735                     String[] wrns = pr.warnings();
736                     StringBuffer wrn = new StringBuffer();
737                     for (int j = 0; j<wrns.length; j++)
738                         wrn.append(j + 1).append(". ").append(wrns[j]).append("\n");
739                     if (wrn.length() > 0)
740                         wrn.deleteCharAt(wrn.length() - 1);
741                     jrf.showBaseAt(i);
742                     JOptionPane.showMessageDialog(jrf, wrn.toString(),
743                         Globals.lang("Warnings"),
744                         JOptionPane.WARNING_MESSAGE);
745                 }
746             }
747
748             //Util.pr(": Finished adding panels");
749
750             if (loaded.size() > 0) {
751                 jrf.tabbedPane.setSelectedIndex(0);
752                 new FocusRequester(((BasePanel) jrf.tabbedPane.getComponentAt(0)).mainTable);
753             }
754         } else
755             System.exit(0);
756     }
757
758     public static ParserResult openBibFile(String name) {
759         System.out.println(Globals.lang("Opening") + ": " + name);
760
761         try {
762             File file = new File(name);
763             String encoding = Globals.prefs.get("defaultEncoding");
764             ParserResult pr = OpenDatabaseAction.loadDatabase(file, encoding);
765             if (pr == null)
766                 return ParserResult.INVALID_FORMAT;
767             pr.setFile(file);
768             if (pr.hasWarnings()) {
769                 String[] warn = pr.warnings();
770                 for (int i=0; i<warn.length; i++)
771                     System.out.println(Globals.lang("Warning")+": "+warn[i]);
772
773             }
774             return pr;
775         } catch (Throwable ex) {
776             //System.err.println(Globals.lang("Error opening file")+" '"+ name+"':
777             // "+ex.getMessage());
778             System.err.println(Globals.lang("Error opening file") + ": "
779                 + ex.getMessage());
780         }
781
782         return null;
783     }
784 }