e0c8110eb1fd3844efcc94f33f09a1e3cbdce2bf
[debian/jabref.git] / src / java / net / sf / jabref / GUIGlobals.java
1 /*
2   Copyright (C) 2003 Morten O. Alver
3
4   All programs in this directory and
5   subdirectories are published under the GNU General Public License as
6   described below.
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or (at
11   your option) any later version.
12
13   This program is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21   USA
22
23   Further information about the GNU GPL is available at:
24   http://www.gnu.org/copyleft/gpl.ja.html
25
26   Note:
27   Modified for use in JabRef.
28
29  */
30
31 package net.sf.jabref;
32
33 import java.awt.*;
34 import java.util.*;
35 //import java.util.List;
36 import java.net.URL;
37 import java.net.MalformedURLException;
38 import javax.swing.*;
39 import java.io.InputStream;
40 import java.io.IOException;
41 import java.io.File;
42
43 /**
44  * Static variables for graphics files and keyboard shortcuts.
45  */
46 public class GUIGlobals {
47
48         // Frame titles.
49         public static String
50         frameTitle = "JabRef",
51 //      version = "1.8b",
52         version = Globals.VERSION,
53         stringsTitle = "Strings for database",
54         //untitledStringsTitle = stringsTitle + Globals.lang("untitled"),
55         untitledTitle = "untitled",
56         helpTitle = "JabRef help",
57         TYPE_HEADER = "entrytype",
58         NUMBER_COL = "#",
59         encPrefix = "Encoding: ", // Part of the signature in written bib files.
60         linuxDefaultLookAndFeel = "com.jgoodies.looks.plastic.Plastic3DLookAndFeel",
61         //"com.shfarr.ui.plaf.fh.FhLookAndFeel",
62 //      "net.sourceforge.mlf.metouia.MetouiaLookAndFeel",
63 //      "org.compiere.plaf.CompiereLookAndFeel",
64         windowsDefaultLookAndFeel = "com.jgoodies.looks.windows.WindowsLookAndFeel";
65
66         public static Font CURRENTFONT,
67         typeNameFont,
68         jabRefFont,
69         fieldNameFont;
70
71         // Signature written at the top of the .bib file.
72         public static final String SIGNATURE =
73                 "This file was created with JabRef";
74
75         // Size of help window.
76         static Dimension
77         helpSize = new Dimension(700, 600),
78         aboutSize = new Dimension(600, 265),
79         searchPaneSize = new Dimension(430, 70),
80         searchFieldSize = new Dimension(215, 25);
81
82         // Divider size for BaseFrame split pane. 0 means non-resizable.
83         public static final int
84         SPLIT_PANE_DIVIDER_SIZE = 4,
85         SPLIT_PANE_DIVIDER_LOCATION = 145 + 15, // + 15 for possible scrollbar.
86         TABLE_ROW_PADDING = 4,
87         KEYBIND_COL_0 = 200,
88         KEYBIND_COL_1 = 80, // Added to the font size when determining table
89         PREVIEW_PANEL_PADDING = 15, // Extra room given to the preview editor, in addition to its own
90         PREVIEW_PANEL_HEIGHT = 200,
91         MAX_CONTENT_SELECTOR_WIDTH = 240; // The max width of the combobox for content selectors.
92         // calculated preferred size
93         //public static final int[] PREVIEW_HEIGHT = {115, 300};
94         // row height
95         public static final double
96         VERTICAL_DIVIDER_LOCATION = 0.4;
97
98         // File names.
99         public static String //configFile = "preferences.dat",
100         backupExt = ".bak",
101         tempExt = ".tmp",
102         defaultDir = ".";
103
104         // Image paths.
105         public static String
106         imageSize = "24",
107         extension = ".gif",
108         ex = imageSize + extension,
109         pre = "/images/",
110         helpPre = "/help/",
111         fontPath = "/images/font/";
112
113         static HashMap tableIcons = new HashMap(); // Contains table icon mappings. Set up
114         // further below.
115         public static Color activeEditor = new Color(230, 230, 255);
116
117         static HashMap iconMap;
118
119         public static JLabel getTableIcon(String fieldType) {
120                 Object o = tableIcons.get(fieldType);
121                 if (o == null) {
122                         Globals.logger("Error: no table icon defined for type '"+fieldType+"'.");
123                         return null;
124                 } else return (JLabel)o;
125         }
126
127
128         //Help files (in HTML format):
129         public static String
130         baseFrameHelp = "BaseFrameHelp.html",
131         entryEditorHelp = "EntryEditorHelp.html",
132         stringEditorHelp = "StringEditorHelp.html",
133         helpContents = "Contents.html",
134         searchHelp = "SearchHelp.html",
135         groupsHelp = "GroupsHelp.html",
136         customEntriesHelp = "CustomEntriesHelp.html",
137         contentSelectorHelp = "ContentSelectorHelp.html",
138         labelPatternHelp = "LabelPatterns.html",
139         ownerHelp = "OwnerHelp.html",
140         timeStampHelp = "TimeStampHelp.html",
141         pdfHelp = "ExternalFiles.html",
142         exportCustomizationHelp = "CustomExports.html",
143         importCustomizationHelp = "CustomImports.html",
144         medlineHelp = "MedlineHelp.html",
145         citeSeerHelp = "CiteSeerHelp.html",
146         generalFieldsHelp = "GeneralFields.html",
147 //      searchHelp = "SearchHelp.html",
148         aboutPage = "About.html",
149         shortPlainImport="ShortPlainImport.html",
150         importInspectionHelp = "ImportInspectionDialog.html",
151         shortIntegrityCheck="ShortIntegrityCheck.html",
152         shortAuxImport="ShortAuxImport.html",
153         remoteHelp = "RemoteHelp.html",
154         journalAbbrHelp = "JournalAbbreviations.html",
155         regularExpressionSearchHelp = "ExternalFiles.html#RegularExpressionSearch",
156         nameFormatterHelp = "CustomExports.html#NameFormatter",
157         previewHelp = "PreviewHelp.html";
158
159 //      Colors.
160         public static Color
161         lightGray = new Color(230, 30, 30), // Light gray background
162         validFieldColor = new Color(100, 100, 150), // Empty field, blue.
163         nullFieldColor = new Color(75, 130, 95), // Valid field, green.
164         invalidFieldColor = new Color(141, 0, 61), // Invalid field, red.
165 //      invalidFieldColor = new Color(210, 70, 70), // Invalid field, red.
166         validFieldBackground = Color.white, // Valid field backgnd.
167 //      invalidFieldBackground = new Color(210, 70, 70), // Invalid field backgnd.
168         invalidFieldBackground = new Color(255, 100, 100), // Invalid field backgnd.
169         gradientGray = new Color(112, 121, 165),  // Title bar gradient color, sidepaneheader
170         gradientBlue = new Color(0, 27, 102),  // Title bar gradient color, sidepaneheader
171         //activeTabbed = Color.black,  // active Database (JTabbedPane)
172         //inActiveTabbed = Color.gray.darker(),  // inactive Database
173         activeTabbed = validFieldColor.darker(),  // active Database (JTabbedPane)
174         inActiveTabbed = Color.black,  // inactive Database
175         infoField = new Color(254, 255, 225) // color for an info field
176         ;
177
178         public static String META_FLAG = "jabref-meta: ";
179         public static String META_FLAG_OLD = "bibkeeper-meta: ";
180         public static String ENTRYTYPE_FLAG = "jabref-entrytype: ";
181
182         // some fieldname constants
183         public static final double
184         DEFAULT_FIELD_WEIGHT = 1,
185         MAX_FIELD_WEIGHT = 2;
186
187     // constants for editor types:
188     public static final int
189         STANDARD_EDITOR=1,
190         FILE_LIST_EDITOR=2;
191
192     public static final String FILE_FIELD = "file";
193
194     public static final double
195         SMALL_W = 0.30,
196         MEDIUM_W = 0.5,
197         LARGE_W = 1.5 ;
198
199         public static final double PE_HEIGHT = 2;
200
201 //      Size constants for EntryTypeForm; small, medium and large.
202         public static int[] FORM_WIDTH = new int[] { 500, 650, 820};
203         public static int[] FORM_HEIGHT = new int[] { 90, 110, 130};
204
205 //      Constants controlling formatted bibtex output.
206         public static final int
207         INDENT = 4,
208         LINE_LENGTH = 65; // Maximum
209
210         public static int DEFAULT_FIELD_LENGTH = 100,
211         NUMBER_COL_LENGTH = 32,
212         WIDTH_ICON_COL = 19;
213
214         // Column widths for export customization dialog table:
215         public static final int
216         EXPORT_DIALOG_COL_0_WIDTH = 50,
217         EXPORT_DIALOG_COL_1_WIDTH = 200,
218         EXPORT_DIALOG_COL_2_WIDTH = 30;
219
220         // Column widths for import customization dialog table:
221         public static final int
222         IMPORT_DIALOG_COL_0_WIDTH = 200,
223         IMPORT_DIALOG_COL_1_WIDTH = 80,
224         IMPORT_DIALOG_COL_2_WIDTH = 200,
225         IMPORT_DIALOG_COL_3_WIDTH = 200;
226
227         public static final Map LANGUAGES;
228
229         static {
230                 LANGUAGES = new HashMap();
231                 // LANGUAGES contains mappings for supported languages.
232                 LANGUAGES.put("English", "en");
233                 LANGUAGES.put("Deutsch", "de");
234                 LANGUAGES.put("Fran\u00E7ais", "fr");
235                 LANGUAGES.put("Italiano", "it");
236         LANGUAGES.put("Nederlands", "du");
237         LANGUAGES.put("Norsk", "no");
238         LANGUAGES.put("EspaƱol", "es");
239         }
240
241         /**
242          * Read either the default icon theme, or a custom one. If loading of the custom theme
243          * fails, try to fall back on the default theme.
244          */
245         public static void setUpIconTheme() {
246                 String defaultPrefix = "/images/crystal_16/", prefix = defaultPrefix;
247
248                 URL defaultResource = GUIGlobals.class.getResource(prefix+"Icons.properties");
249                 URL resource = defaultResource;
250
251                 if (Globals.prefs.getBoolean("useCustomIconTheme")) {
252                         String filename = Globals.prefs.get("customIconThemeFile");
253                         if (filename != null)
254                                 try {
255                                         File file = new File(filename);
256                                         String parent = file.getParentFile().getAbsolutePath();
257                                         prefix = "file://"+parent+System.getProperty("file.separator");
258                                         resource = new URL("file://"+file.getAbsolutePath());
259                                 } catch (MalformedURLException e) {
260                                         e.printStackTrace();
261                                 }
262                 }
263                 try {
264                         iconMap = readIconThemeFile(resource, prefix);
265                 } catch (IOException e) {
266                         System.err.println(Globals.lang("Unable to read icon theme file")+" '"+
267                                 resource.toString()+"'");
268                         // If we were trying to load a custom theme, try the default one as a fallback:
269                         if (resource != defaultResource)
270                                 try {
271                                         iconMap = readIconThemeFile(defaultResource, defaultPrefix);
272                                 } catch (IOException e2) {
273                                         System.err.println(Globals.lang("Unable to read default icon theme."));
274                                 }
275
276                 }
277
278
279         }
280
281         /**
282          * Looks up the URL for the image representing the given function, in the resource
283          * file listing images.
284          * @param name The name of the icon, such as "open", "save", "saveAs" etc.
285          * @return The URL to the actual image to use.
286          */
287         public static URL getIconUrl(String name) {
288         if (iconMap.containsKey(name)) {
289                         String path = (String)iconMap.get(name);
290                         URL url = GUIGlobals.class.getResource(path);
291                         if (url == null)
292                                 // This may be a resource outside of the jar file, so we try a general URL:
293                                 try {
294                                         url = new URL(path);
295                                 } catch (MalformedURLException e) {
296                                         url = null;
297                                 }
298                                 if (url == null)
299                                         System.err.println(Globals.lang("Could not find image file")+" '"+path+"'");
300                                 return url;
301                 }
302                 else return null;
303         }
304
305         /**
306          * Constructs an ImageIcon for the given function, using the image specified in
307          * the resource files resource/Icons_en.properties.
308          * @param name The name of the icon, such as "open", "save", "saveAs" etc.
309          * @return The ImageIcon for the function.
310          */
311         public static ImageIcon getImage(String name) {
312                 URL u = getIconUrl(name);
313                 return u != null ? new ImageIcon(getIconUrl(name)) : null;
314         }
315
316     /**
317      * Get a Map of all application icons mapped from their keys.
318      * @return A Map containing all icons used in the application.
319      */
320     public static Map getAllIcons() {
321         return Collections.unmodifiableMap(iconMap);
322     }
323
324     /**
325          * Read a typical java property file into a HashMap. Currently doesn't support escaping
326          * of the '=' character - it simply looks for the first '=' to determine where the key ends.
327          * Both the key and the value is trimmed for whitespace at the ends.
328          * @param file The URL to read information from.
329          * @param prefix A String to prefix to all values read. Can represent e.g. the directory
330          * where icon files are to be found.
331          * @return A HashMap containing all key-value pairs found.
332          * @throws IOException
333          */
334         private static HashMap readIconThemeFile(URL file, String prefix) throws IOException {
335                 HashMap map = new HashMap();
336                 InputStream in = null;
337                 try {
338                         in = file.openStream();
339                         StringBuffer buffer = new StringBuffer();
340                         int c;
341                         while ((c = in.read()) != -1)
342                                 buffer.append((char)c);
343                         String[] lines = buffer.toString().split("\n");
344                         for (int i=0; i<lines.length; i++) {
345                                 String line = lines[i].trim();
346                                 int index = line.indexOf("=");
347                                 if (index >= 0) {
348                                         String key = line.substring(0, index).trim();
349                                         String value = prefix+line.substring(index+1).trim();
350                                         map.put(key, value);
351                                 }
352                         }
353                 } catch (IOException ex) {
354                         throw ex;
355                 } finally {
356                         try {
357                                 if (in != null) in.close();
358                         } catch (IOException ex) {
359                                 ex.printStackTrace();
360                         }
361                 }
362                 return map;
363         }
364
365         /** returns the path to language independent help files */
366         public static String getLocaleHelpPath()
367         {
368                 JabRefPreferences prefs = JabRefPreferences.getInstance() ;
369                 String middle = prefs.get("language")+"/";
370                 if (middle.equals("en/")) middle = ""; // english in base help dir.
371
372                 return (helpPre + middle );
373         }
374
375
376         /**
377          * Perform initializations that are only used in graphical mode. This is to prevent
378          * the "Xlib: connection to ":0.0" refused by server" error when access to the X server
379          * on Un*x is unavailable.
380          */
381         public static void init() {
382                 typeNameFont = new Font("arial", Font.ITALIC+Font.BOLD, 24);
383                 fieldNameFont = new Font("arial", Font.ITALIC+Font.BOLD, 14);
384                 JLabel lab;
385                 lab = new JLabel(getImage("pdfSmall"));
386                 lab.setToolTipText(Globals.lang("Open")+" PDF");
387                 tableIcons.put("pdf", lab);
388                 lab = new JLabel(getImage("wwwSmall"));
389                 lab.setToolTipText(Globals.lang("Open")+" URL");
390                 tableIcons.put("url", lab);
391                 lab = new JLabel(getImage("citeseer"));
392                 lab.setToolTipText(Globals.lang("Open")+" CiteSeer URL");
393                 tableIcons.put("citeseerurl", lab);
394                 lab = new JLabel(getImage("doiSmall"));
395                 lab.setToolTipText(Globals.lang("Open")+" DOI "+Globals.lang("web link"));
396                 tableIcons.put("doi", lab);
397                 lab = new JLabel(getImage("psSmall"));
398                 lab.setToolTipText(Globals.lang("Open")+" PS");
399                 tableIcons.put("ps", lab);
400         lab = new JLabel(getImage("psSmall"));
401         lab.setToolTipText(Globals.lang("Open file"));
402         tableIcons.put(GUIGlobals.FILE_FIELD, lab);
403
404
405         //jabRefFont = new Font("arial", Font.ITALIC/*+Font.BOLD*/, 20);
406         }
407
408 }