Skip to content

Commit 06514bd

Browse files
authored
Improvements around external file types (#3887)
* Refactor external file types and extract interface * Fix ExternalFileType dialog * Catch exception for not valid paths * Fix build * Fix checkstyle
1 parent 5bdc4ad commit 06514bd

16 files changed

+431
-425
lines changed

src/main/java/org/jabref/gui/actions/EditExternalFileTypesAction.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@
22

33
import org.jabref.gui.externalfiletype.ExternalFileTypeEditor;
44

5-
//TODO: DOES NOT SHOW UP
65
public class EditExternalFileTypesAction extends SimpleCommand {
76

87
private ExternalFileTypeEditor editor;
98

109
@Override
1110
public void execute() {
1211
if (editor == null) {
13-
editor = new ExternalFileTypeEditor(null);
14-
editor.setVisible(true);
15-
12+
editor = new ExternalFileTypeEditor();
1613
}
17-
14+
editor.show();
1815
}
19-
2016
}

src/main/java/org/jabref/gui/desktop/JabRefDesktop.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,13 @@ public static boolean openExternalFileAnyFormat(final BibDatabaseContext databas
136136
openExternalFilePlatformIndependent(type, filePath);
137137
return true;
138138
} else {
139-
// No file matched the name, or we did not know the file type.
139+
// No file matched the name, try to open it directly using the given app
140+
if (type.isPresent()) {
141+
openExternalFilePlatformIndependent(type, link);
142+
return true;
143+
}
144+
145+
// Run out of ideas what to do...
140146
return false;
141147
}
142148
}
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
package org.jabref.gui.externalfiletype;
2+
3+
import java.util.Objects;
4+
5+
import javax.swing.JLabel;
6+
7+
import org.jabref.gui.icon.IconTheme;
8+
import org.jabref.gui.icon.JabRefIcon;
9+
10+
/**
11+
* This class defines a type of external files that can be linked to from JabRef.
12+
* The class contains enough information to provide an icon, a standard extension
13+
* and a link to which application handles files of this type.
14+
*/
15+
public class CustomExternalFileType implements ExternalFileType {
16+
17+
private final JLabel label = new JLabel();
18+
private String name;
19+
private String extension;
20+
private String openWith;
21+
private String iconName;
22+
private String mimeType;
23+
private JabRefIcon icon;
24+
25+
public CustomExternalFileType(String name, String extension, String mimeType,
26+
String openWith, String iconName, JabRefIcon icon) {
27+
label.setText(null);
28+
this.name = name;
29+
label.setToolTipText(this.name);
30+
this.extension = extension;
31+
this.mimeType = mimeType;
32+
this.openWith = openWith;
33+
34+
setIconName(iconName);
35+
setIcon(icon);
36+
}
37+
38+
public CustomExternalFileType(ExternalFileType type) {
39+
this(type.getName(), type.getExtension(), type.getMimeType(), type.getOpenWithApplication(), "", type.getIcon());
40+
}
41+
42+
/**
43+
* Construct an ExternalFileType from a String array. This is used when
44+
* reading file type definitions from Preferences, where the available data types are
45+
* limited. We assume that the array contains the same values as the main constructor,
46+
* in the same order.
47+
*
48+
* @param val arguments.
49+
*/
50+
public static ExternalFileType buildFromArgs(String[] val) {
51+
if ((val == null) || (val.length < 4) || (val.length > 5)) {
52+
throw new IllegalArgumentException("Cannot construct ExternalFileType without four elements in String[] argument.");
53+
}
54+
String name = val[0];
55+
String extension = val[1];
56+
String openWith;
57+
String mimeType;
58+
String iconName;
59+
60+
if (val.length == 4) {
61+
// Up to version 2.4b the mime type is not included:
62+
mimeType = "";
63+
openWith = val[2];
64+
iconName = val[3];
65+
} else {
66+
// When mime type is included, the array length should be 5:
67+
mimeType = val[2];
68+
openWith = val[3];
69+
iconName = val[4];
70+
}
71+
72+
// set icon to default first
73+
JabRefIcon icon = IconTheme.JabRefIcons.FILE;
74+
75+
// check whether there is another icon defined for this file type
76+
for (ExternalFileType fileType : ExternalFileTypes.getDefaultExternalFileTypes()) {
77+
if (fileType.getName().equals(name)) {
78+
icon = fileType.getIcon();
79+
break;
80+
}
81+
}
82+
83+
return new CustomExternalFileType(name, extension, mimeType, openWith, iconName, icon);
84+
}
85+
86+
@Override
87+
public String getName() {
88+
return name;
89+
}
90+
91+
public void setName(String name) {
92+
this.name = name;
93+
label.setToolTipText(this.name);
94+
}
95+
96+
@Override
97+
public String getExtension() {
98+
if (extension == null) {
99+
return "";
100+
}
101+
return extension;
102+
}
103+
104+
public void setExtension(String extension) {
105+
this.extension = extension;
106+
}
107+
108+
@Override
109+
public String getMimeType() {
110+
if (mimeType == null) {
111+
return "";
112+
}
113+
return mimeType;
114+
}
115+
116+
public void setMimeType(String mimeType) {
117+
this.mimeType = mimeType;
118+
}
119+
120+
@Override
121+
public String getOpenWithApplication() {
122+
if (openWith == null) {
123+
return "";
124+
}
125+
return openWith;
126+
}
127+
128+
public void setOpenWith(String openWith) {
129+
this.openWith = openWith;
130+
}
131+
132+
/**
133+
* Obtain a JLabel instance set with this file type's icon. The same JLabel
134+
* is returned from each call of this method.
135+
*
136+
* @return the label.
137+
*/
138+
public JLabel getIconLabel() {
139+
return label;
140+
}
141+
142+
/**
143+
* Get the string associated with this file type's icon.
144+
*
145+
* @return The icon name.
146+
*/
147+
public String getIconName() {
148+
return iconName;
149+
}
150+
151+
/**
152+
* Set the string associated with this file type's icon.
153+
*
154+
* @param name The icon name to use.
155+
*/
156+
public void setIconName(String name) {
157+
this.iconName = name;
158+
}
159+
160+
@Override
161+
public JabRefIcon getIcon() {
162+
return icon;
163+
}
164+
165+
public void setIcon(JabRefIcon icon) {
166+
Objects.requireNonNull(icon);
167+
this.icon = icon;
168+
label.setIcon(this.icon.getSmallIcon());
169+
}
170+
171+
@Override
172+
public String toString() {
173+
return getName();
174+
}
175+
176+
public ExternalFileType copy() {
177+
return new CustomExternalFileType(name, extension, mimeType, openWith, iconName, icon);
178+
}
179+
180+
@Override
181+
public int hashCode() {
182+
return Objects.hash(name, extension, mimeType, openWith, iconName);
183+
}
184+
185+
/**
186+
* We define two file type objects as equal if their name, extension, openWith and
187+
* iconName are equal.
188+
*
189+
* @param object The file type to compare with.
190+
* @return true if the file types are equal.
191+
*/
192+
@Override
193+
public boolean equals(Object object) {
194+
if (this == object) {
195+
return true;
196+
}
197+
198+
if (object instanceof CustomExternalFileType) {
199+
CustomExternalFileType other = (CustomExternalFileType) object;
200+
return Objects.equals(name, other.name) && Objects.equals(extension, other.extension) &&
201+
Objects.equals(mimeType, other.mimeType) && Objects.equals(openWith, other.openWith) && Objects.equals(iconName, other.iconName);
202+
}
203+
return false;
204+
}
205+
}

0 commit comments

Comments
 (0)