Skip to content

Commit

Permalink
Use default file icon for custom external file types, fixes #598
Browse files Browse the repository at this point in the history
  • Loading branch information
simonharrer committed Dec 30, 2015
1 parent a118905 commit 3dd15b8
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 54 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ to [sourceforge feature requests](https://sourceforge.net/p/jabref/features/) by
- Changes in customized entry types are now directly reflected in the table when clicking "Apply" or "OK"
- Reference list generation works for OpenOffice/LibreOffice again, fixes #593
- ACM fetcher works again, fixes #545
- Use default file icon for custom external file types, fixes #598

### Removed

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/net/sf/jabref/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

public final class JabRefPreferences {
private static final Log LOGGER = LogFactory.getLog(JabRefPreferences.class);
private static final String EXTERNAL_FILE_TYPES = "externalFileTypes";

/**
* HashMap that contains all preferences which are set by default
Expand Down Expand Up @@ -1403,13 +1404,13 @@ public void updateExternalFileTypes() {
// First get a list of the default file types as a starting point:
List<ExternalFileType> types = getDefaultExternalFileTypes();
// If no changes have been stored, simply use the defaults:
if (prefs.get("externalFileTypes", null) == null) {
if (prefs.get(EXTERNAL_FILE_TYPES, null) == null) {
externalFileTypes.clear();
externalFileTypes.addAll(types);
return;
}
// Read the prefs information for file types:
String[][] vals = StringUtil.decodeStringDoubleArray(prefs.get("externalFileTypes", ""));
String[][] vals = StringUtil.decodeStringDoubleArray(prefs.get(EXTERNAL_FILE_TYPES, ""));
for (String[] val : vals) {
if ((val.length == 2) && val[1].equals(JabRefPreferences.FILE_TYPE_REMOVED_FLAG)) {
// This entry indicates that a default entry type should be removed:
Expand All @@ -1426,7 +1427,7 @@ public void updateExternalFileTypes() {
}
} else {
// A new or modified entry type. Construct it from the string array:
ExternalFileType type = new ExternalFileType(val);
ExternalFileType type = ExternalFileType.buildFromArgs(val);
// Check if there is a default type with the same name. If so, this is a
// modification of that type, so remove the default one:
ExternalFileType toRemove = null;
Expand Down
56 changes: 31 additions & 25 deletions src/main/java/net/sf/jabref/external/ExternalFileType.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ExternalFileType implements Comparable<ExternalFileType> {
private final JLabel label = new JLabel();

public ExternalFileType(String name, String extension, String mimeType,
String openWith, String iconName, Icon icon) {
String openWith, String iconName, Icon icon) {
label.setText(null);
this.name = name;
label.setToolTipText(this.name);
Expand All @@ -48,39 +48,43 @@ public ExternalFileType(String name, String extension, String mimeType,
}

/**
* Construct an ExternalFileType from a String array. This constructor is used when
* Construct an ExternalFileType from a String array. This is used when
* reading file type definitions from Preferences, where the available data types are
* limited. We assume that the array contains the same values as the main constructor,
* in the same order.
*
* TODO: The icon argument needs special treatment. At the moment, we assume that the fourth
* element of the array contains the icon keyword to be looked up in the current icon theme.
* To support icons found elsewhere on the file system we simply need to prefix the icon name
* with a marker.
*
* @param val Constructor arguments.
* @param val arguments.
*/
public ExternalFileType(String[] val) {
if ((val == null) || (val.length < 4)) {
public static ExternalFileType buildFromArgs(String[] val) {
if ((val == null) || (val.length < 4) || val.length > 5) {
throw new IllegalArgumentException("Cannot construct ExternalFileType without four elements in String[] argument.");
}
this.name = val[0];
label.setToolTipText(this.name);
this.extension = val[1];
label.setText(null);
// Up to version 2.4b the mime type is not included:
String name = val[0];
String extension = val[1];
String openWith;
String mimeType;
String iconName;
Icon icon;

if (val.length == 4) {
this.openWith = val[2];
setIconName(val[3]);
setIcon(IconTheme.getImage(getIconName()));
// Up to version 2.4b the mime type is not included:
mimeType = "";
openWith = val[2];
iconName = val[3];
} else {
// When mime type is included, the array length should be 5:
mimeType = val[2];
openWith = val[3];
iconName = val[4];
}
// When mime type is included, the array length should be 5:
else if (val.length == 5) {
this.mimeType = val[2];
this.openWith = val[3];
setIconName(val[4]);
setIcon(IconTheme.getImage(getIconName()));

if ("new".equals(iconName)) {
icon = IconTheme.JabRefIcon.FILE.getSmallIcon();
} else {
icon = IconTheme.getImage(iconName);
}

return new ExternalFileType(name, extension, mimeType, openWith, iconName, icon);
}

/**
Expand All @@ -91,7 +95,7 @@ else if (val.length == 5) {
* @return A String[] containing all information about this file type.
*/
public String[] getStringArrayRepresentation() {
return new String[] {name, extension, mimeType, openWith, iconName};
return new String[]{name, extension, mimeType, openWith, iconName};
}

public String getName() {
Expand Down Expand Up @@ -122,6 +126,7 @@ public void setMimeType(String mimeType) {
/**
* Get the bibtex field name used to extension to this file type.
* Currently we assume that field name equals filename extension.
*
* @return The field name.
*/
public String getFieldName() {
Expand All @@ -148,6 +153,7 @@ public void setIconName(String name) {
/**
* Obtain a JLabel instance set with this file type's icon. The same JLabel
* is returned from each call of this method.
*
* @return the label.
*/
public JLabel getIconLabel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
Expand All @@ -44,7 +45,7 @@
public class ExternalFileTypeEditor extends JDialog {
private JFrame frame;
private JDialog dialog;
private ArrayList<ExternalFileType> fileTypes;
private List<ExternalFileType> fileTypes;
private JTable table;
private ExternalFileTypeEntryEditor entryEditor;
private FileTypeTableModel tableModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public class ExternalFileTypeEntryEditor {
private final JButton cancel = new JButton(Localization.lang("Cancel"));
private final JRadioButton useDefault = new JRadioButton(Localization.lang("Default"));
private final JRadioButton other = new JRadioButton("");
final String emptyMessage = "<" + Localization.lang("Use default viewer") + ">";
private final String editFileTitle = Localization.lang("Edit file type");
private final String newFileTitle = Localization.lang("Add new file type");

Expand Down Expand Up @@ -83,7 +82,7 @@ private void init(ExternalFileType inEntry) {
builder.layout(new FormLayout
("left:pref, 4dlu, fill:150dlu, 4dlu, fill:pref", "p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p"));
builder.add(Localization.lang("Icon")).xy(1, 1);
builder.add(icon).xy(3, 1);
builder.add(icon).xy(3, 1);
builder.add(Localization.lang("Name")).xy(1, 3);
builder.add(name).xy(3, 3);
builder.add(Localization.lang("Extension")).xy(1, 5);
Expand All @@ -94,10 +93,10 @@ private void init(ExternalFileType inEntry) {
builder.add(Localization.lang("Application")).xy(1, 9);
JButton browseBut = new JButton(Localization.lang("Browse"));
if (OS.WINDOWS) {
builder.add(useDefault).xy(3,9);
builder.add(useDefault).xy(3, 9);
builder.appendRows("2dlu, p");
JPanel p1 = new JPanel();
builder.add(p1).xy(1,11);
builder.add(p1).xy(1, 11);
JPanel p2 = new JPanel();
application.setPreferredSize(new Dimension(300, application.getPreferredSize().height));
BorderLayout bl = new BorderLayout();
Expand All @@ -117,23 +116,13 @@ private void init(ExternalFileType inEntry) {
bb.addButton(cancel);
bb.addGlue();

ok.addActionListener(new ActionListener() {
ok.addActionListener(e -> {
okPressed = true;
storeSettings(ExternalFileTypeEntryEditor.this.entry);
diag.dispose();

@Override
public void actionPerformed(ActionEvent e) {
okPressed = true;
storeSettings(ExternalFileTypeEntryEditor.this.entry);
diag.dispose();

}
});
cancel.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
diag.dispose();
}
});
cancel.addActionListener(e -> diag.dispose());

if (OS.WINDOWS) {
application.getDocument().addDocumentListener(new DocumentListener() {
Expand Down Expand Up @@ -183,18 +172,16 @@ public void changedUpdate(DocumentEvent documentEvent) {

if (dParent != null) {
diag.setLocationRelativeTo(dParent);
}
else {
} else {
diag.setLocationRelativeTo(fParent);
//Util.placeDialog(diag, parent);
}

setValues(entry);
}

public void setEntry(ExternalFileType entry) {
this.entry = entry;
if(entry.getName().isEmpty()) {
if (entry.getName().isEmpty()) {
diag.setTitle(newFileTitle);
} else {
diag.setTitle(editFileTitle);
Expand Down Expand Up @@ -267,7 +254,7 @@ public void actionPerformed(ActionEvent e) {
// Nothing in the field. Go to the last file dir used:
initial = new File(Globals.prefs.get(JabRefPreferences.FILE_WORKING_DIRECTORY));
}
String chosen = FileDialogs.getNewFile(/*parent*/null, initial, Globals.NONE,
String chosen = FileDialogs.getNewFile(null, initial, Globals.NONE,
JFileChooser.OPEN_DIALOG, false);
if (chosen != null) {
File newFile = new File(chosen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public void run() {
// User doesn't want to handle this unknown link type.
} else if (answer == JOptionPane.YES_OPTION) {
// User wants to define the new file type. Show the dialog:
ExternalFileType newType = new ExternalFileType(flEntry.getType().getName(), "", "", "", "new", IconTheme.getImage("new"));
ExternalFileType newType = new ExternalFileType(flEntry.getType().getName(), "", "", "", "new", IconTheme.JabRefIcon.FILE.getSmallIcon());
ExternalFileTypeEntryEditor editor = new ExternalFileTypeEntryEditor(panel.frame(), newType);
editor.setVisible(true);
if (editor.okPressed()) {
Expand Down

0 comments on commit 3dd15b8

Please sign in to comment.