1 package net.sf.jabref.external;
5 import net.sf.jabref.GUIGlobals;
8 * This class defines a type of external files that can be linked to from JabRef.
9 * The class contains enough information to provide an icon, a standard extension
10 * and a link to which application handles files of this type.
12 public class ExternalFileType implements Comparable<ExternalFileType> {
14 protected String name, extension, openWith, iconName;
15 protected ImageIcon icon;
16 protected JLabel label = new JLabel();
18 public ExternalFileType(String name, String extension, String openWith,
22 label.setToolTipText(this.name);
23 this.extension = extension;
24 this.openWith = openWith;
25 setIconName(iconName);
29 * Construct an ExternalFileType from a String array. This constructor is used when
30 * reading file type definitions from Preferences, where the available data types are
31 * limited. We assume that the array contains the same values as the main constructor,
34 * TODO: The icon argument needs special treatment. At the moment, we assume that the fourth
35 * element of the array contains the icon keyword to be looked up in the current icon theme.
36 * To support icons found elsewhere on the file system we simply need to prefix the icon name
39 * @param val Constructor arguments.
41 public ExternalFileType(String[] val) {
42 if ((val == null) || (val.length < 4))
43 throw new IllegalArgumentException("Cannot contruct ExternalFileType without four elements in String[] argument.");
45 label.setToolTipText(this.name);
46 this.extension = val[1];
47 this.openWith = val[2];
53 * Return a String array representing this file type. This is used for storage into
54 * Preferences, and the same array can be used to construct the file type later,
55 * using the String[] constructor.
57 * @return A String[] containing all information about this file type.
59 public String[] getStringArrayRepresentation() {
60 return new String[] {name, extension, openWith, iconName};
63 public String getName() {
67 public void setName(String name) {
69 label.setToolTipText(this.name);
72 public String getExtension() {
76 public void setExtension(String extension) {
77 this.extension = extension;
81 * Get the bibtex field name used to extension to this file type.
82 * Currently we assume that field name equals filename extension.
83 * @return The field name.
85 public String getFieldName() {
89 public String getOpenWith() {
93 public void setOpenWith(String openWith) {
94 this.openWith = openWith;
98 * Set the string associated with this file type's icon. The string is used
99 * to get the actual icon by the method GUIGlobals.getIcon(String)
100 * @param name The icon name to use.
102 public void setIconName(String name) {
103 this.iconName = name;
105 this.icon = GUIGlobals.getImage(iconName);
106 } catch (NullPointerException ex) {
107 // Loading the icon failed. This could be because the icons have not been
108 // initialized, which will be the case if we are operating from the command
109 // line and the graphical interface hasn't been initialized. In that case
110 // we will do without the icon:
113 label.setIcon(this.icon);
117 * Obtain a JLabel instance set with this file type's icon. The same JLabel
118 * is returned from each call of this method.
121 public JLabel getIconLabel() {
126 * Get the string associated with this file type's icon. The string is used
127 * to get the actual icon by the method GUIGlobals.getIcon(String)
128 * @return The icon name.
130 public String getIconName() {
134 public ImageIcon getIcon() {
138 public void setIcon(ImageIcon icon) {
142 public String toString() {
146 public int compareTo(ExternalFileType o) {
147 return getName().compareTo(o.getName());
150 public ExternalFileType copy() {
151 return new ExternalFileType(name, extension, openWith, iconName);
155 public int hashCode() {
156 return name.hashCode();
160 * We define two file type objects as equal if their name, extension, openWith and
161 * iconName are equal.
163 * @param object The file type to compare with.
164 * @return true if the file types are equal.
166 public boolean equals(Object object) {
167 ExternalFileType other = (ExternalFileType)object;
170 return (name == null ? other.name == null : name.equals(other.name))
171 && (extension == null ? other.extension == null : extension.equals(other.extension))
172 && (openWith== null ? other.openWith == null : openWith.equals(other.openWith))
173 && (iconName== null ? other.iconName == null : iconName.equals(other.iconName));