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