Skip to content

Commit 982b475

Browse files
committed
Asssign combobox selected value property
1 parent 2296ea5 commit 982b475

File tree

2 files changed

+92
-76
lines changed

2 files changed

+92
-76
lines changed

src/main/java/org/jabref/gui/filelist/FileListDialogController.java

+6-75
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,19 @@
11
package org.jabref.gui.filelist;
22

33
import java.io.IOException;
4-
import java.nio.file.Path;
5-
import java.nio.file.Paths;
6-
import java.util.List;
74
import java.util.Optional;
8-
import java.util.regex.Pattern;
9-
105
import javax.inject.Inject;
116
import javafx.event.ActionEvent;
127
import javafx.fxml.FXML;
138
import javafx.scene.control.Button;
149
import javafx.scene.control.ComboBox;
1510
import javafx.scene.control.TextField;
16-
17-
import org.jabref.Globals;
1811
import org.jabref.gui.AbstractController;
1912
import org.jabref.gui.DialogService;
2013
import org.jabref.gui.StateManager;
2114
import org.jabref.gui.desktop.JabRefDesktop;
2215
import org.jabref.gui.externalfiletype.ExternalFileType;
23-
import org.jabref.gui.externalfiletype.ExternalFileTypes;
24-
import org.jabref.gui.externalfiletype.UnknownExternalFileType;
25-
import org.jabref.gui.util.FileDialogConfiguration;
26-
import org.jabref.logic.util.io.FileUtil;
2716
import org.jabref.model.entry.LinkedFile;
28-
import org.jabref.model.util.FileHelper;
29-
import org.jabref.preferences.JabRefPreferences;
3017
import org.jabref.preferences.PreferencesService;
3118

3219
import org.apache.commons.logging.Log;
@@ -35,7 +22,6 @@
3522
public class FileListDialogController extends AbstractController<FileListDialogViewModel> {
3623

3724
private static final Log LOGGER = LogFactory.getLog(FileListDialogController.class);
38-
private static final Pattern REMOTE_LINK_PATTERN = Pattern.compile("[a-z]+://.*");
3925

4026
@FXML private TextField tfLink;
4127
@FXML private Button btnBrowse;
@@ -49,11 +35,9 @@ public class FileListDialogController extends AbstractController<FileListDialogV
4935
@Inject private DialogService dialogService;
5036
@Inject private StateManager stateManager;
5137

52-
private boolean showSaveDialog;
53-
5438
@FXML
5539
private void initialize() {
56-
viewModel = new FileListDialogViewModel(stateManager.getActiveDatabase().get());
40+
viewModel = new FileListDialogViewModel(stateManager.getActiveDatabase().get(), dialogService);
5741
setBindings();
5842

5943
}
@@ -63,40 +47,16 @@ private void setBindings() {
6347
cmbFileType.itemsProperty().bindBidirectional(viewModel.externalFileTypeProperty());
6448
tfDescription.textProperty().bindBidirectional(viewModel.descriptionProperty());
6549
tfLink.textProperty().bindBidirectional(viewModel.linkProperty());
50+
51+
cmbFileType.valueProperty().bindBidirectional(viewModel.getSelectedExternalFileType());
6652
}
6753

6854
@FXML
6955
void browseFileDialog(ActionEvent event) {
7056

71-
String fileText = viewModel.linkProperty().get();
57+
viewModel.browseFileDialog();
58+
tfLink.requestFocus();
7259

73-
Optional<Path> file = FileHelper.expandFilename(stateManager.getActiveDatabase().get(), fileText,
74-
Globals.prefs.getFileDirectoryPreferences());
75-
76-
Path workingDir = file.orElse(Paths.get(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)));
77-
String fileName = Paths.get(fileText).getFileName().toString();
78-
79-
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
80-
.withInitialDirectory(workingDir)
81-
.withInitialFileName(fileName).build();
82-
Optional<Path> path;
83-
if (showSaveDialog) {
84-
path = dialogService.showFileSaveDialog(fileDialogConfiguration);
85-
} else {
86-
path = dialogService.showFileOpenDialog(fileDialogConfiguration);
87-
}
88-
path.ifPresent(newFile -> {
89-
// Store the directory for next time:
90-
Globals.prefs.put(JabRefPreferences.WORKING_DIRECTORY, newFile.toString());
91-
92-
// If the file is below the file directory, make the path relative:
93-
List<Path> fileDirectories = this.stateManager.getActiveDatabase().get()
94-
.getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences());
95-
newFile = FileUtil.shortenFileName(newFile, fileDirectories);
96-
97-
viewModel.linkProperty().set(newFile.toString());
98-
tfLink.requestFocus();
99-
});
10060
}
10161

10262
@FXML
@@ -110,36 +70,7 @@ void ok_clicked(ActionEvent event) {
11070
}
11171

11272
private void setValues(LinkedFile entry) {
113-
tfDescription.setText(entry.getDescription());
114-
tfLink.setText(entry.getLink());
115-
116-
cmbFileType.getSelectionModel().clearSelection();
117-
// See what is a reasonable selection for the type combobox:
118-
Optional<ExternalFileType> fileType = ExternalFileTypes.getInstance().fromLinkedFile(entry, false);
119-
if (fileType.isPresent() && !(fileType.get() instanceof UnknownExternalFileType)) {
120-
cmbFileType.getSelectionModel().select(fileType.get());
121-
} else if ((entry.getLink() != null) && (!entry.getLink().isEmpty())) {
122-
checkExtension();
123-
}
124-
}
125-
126-
private void checkExtension() {
127-
if (cmbFileType.getSelectionModel().isEmpty() && (!tfLink.getText().trim().isEmpty())) {
128-
129-
// Check if this looks like a remote link:
130-
if (REMOTE_LINK_PATTERN.matcher(tfLink.getText()).matches()) {
131-
Optional<ExternalFileType> type = ExternalFileTypes.getInstance().getExternalFileTypeByExt("html");
132-
if (type.isPresent()) {
133-
cmbFileType.getSelectionModel().select(type.get());
134-
return;
135-
}
136-
}
137-
138-
// Try to guess the file type:
139-
String theLink = tfLink.getText().trim();
140-
ExternalFileTypes.getInstance().getExternalFileTypeForName(theLink).ifPresent(type -> cmbFileType.getSelectionModel().select(type));
141-
}
142-
73+
viewModel.setValues(entry);
14374
}
14475

14576
@FXML

src/main/java/org/jabref/gui/filelist/FileListDialogViewModel.java

+86-1
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,114 @@
11
package org.jabref.gui.filelist;
22

3+
import java.nio.file.Path;
4+
import java.nio.file.Paths;
5+
import java.util.List;
6+
import java.util.Optional;
7+
import java.util.regex.Pattern;
8+
39
import javafx.beans.property.ListProperty;
10+
import javafx.beans.property.ObjectProperty;
411
import javafx.beans.property.SimpleListProperty;
12+
import javafx.beans.property.SimpleObjectProperty;
513
import javafx.beans.property.SimpleStringProperty;
614
import javafx.beans.property.StringProperty;
715
import javafx.collections.FXCollections;
816

17+
import org.jabref.Globals;
918
import org.jabref.gui.AbstractViewModel;
19+
import org.jabref.gui.DialogService;
1020
import org.jabref.gui.externalfiletype.ExternalFileType;
1121
import org.jabref.gui.externalfiletype.ExternalFileTypes;
22+
import org.jabref.gui.externalfiletype.UnknownExternalFileType;
23+
import org.jabref.gui.util.FileDialogConfiguration;
24+
import org.jabref.logic.util.io.FileUtil;
1225
import org.jabref.model.database.BibDatabaseContext;
26+
import org.jabref.model.entry.LinkedFile;
27+
import org.jabref.model.util.FileHelper;
28+
import org.jabref.preferences.JabRefPreferences;
1329

1430
public class FileListDialogViewModel extends AbstractViewModel {
1531

32+
private static final Pattern REMOTE_LINK_PATTERN = Pattern.compile("[a-z]+://.*");
1633
private final StringProperty linkProperty = new SimpleStringProperty("");
1734
private final StringProperty descriptionProperty = new SimpleStringProperty("");
1835
private final ListProperty<ExternalFileType> externalfilesTypes = new SimpleListProperty<>(FXCollections.emptyObservableList());
36+
private final ObjectProperty<ExternalFileType> selectedExternalFileType = new SimpleObjectProperty<>();
1937
private final BibDatabaseContext bibDatabaseContext;
38+
private final DialogService dialogService;
39+
40+
private boolean showSaveDialog;
2041

21-
public FileListDialogViewModel(BibDatabaseContext bibDatabaseContext) {
42+
public FileListDialogViewModel(BibDatabaseContext bibDatabaseContext, DialogService dialogService) {
2243

2344
this.bibDatabaseContext = bibDatabaseContext;
45+
this.dialogService = dialogService;
2446
externalfilesTypes.set(FXCollections.observableArrayList(ExternalFileTypes.getInstance().getExternalFileTypeSelection()));
47+
}
48+
49+
private void checkExtension() {
50+
51+
if (!linkProperty.getValueSafe().isEmpty()) {
2552

53+
// Check if this looks like a remote link:
54+
if (REMOTE_LINK_PATTERN.matcher(linkProperty.get()).matches()) {
55+
ExternalFileTypes.getInstance().getExternalFileTypeByExt("html").ifPresent(selectedExternalFileType::setValue);
56+
}
2657

58+
// Try to guess the file type:
59+
String theLink = linkProperty.get().trim();
60+
ExternalFileTypes.getInstance().getExternalFileTypeForName(theLink).ifPresent(selectedExternalFileType::setValue);
61+
}
2762
}
63+
64+
public void browseFileDialog() {
65+
String fileText = linkProperty().get();
66+
67+
Optional<Path> file = FileHelper.expandFilename(bibDatabaseContext, fileText,
68+
Globals.prefs.getFileDirectoryPreferences());
69+
70+
Path workingDir = file.orElse(Paths.get(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)));
71+
String fileName = Paths.get(fileText).getFileName().toString();
72+
73+
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
74+
.withInitialDirectory(workingDir)
75+
.withInitialFileName(fileName).build();
76+
Optional<Path> path;
77+
78+
if (showSaveDialog) {
79+
path = dialogService.showFileSaveDialog(fileDialogConfiguration);
80+
} else {
81+
path = dialogService.showFileOpenDialog(fileDialogConfiguration);
82+
}
83+
path.ifPresent(newFile -> {
84+
// Store the directory for next time:
85+
Globals.prefs.put(JabRefPreferences.WORKING_DIRECTORY, newFile.toString());
86+
87+
// If the file is below the file directory, make the path relative:
88+
List<Path> fileDirectories = bibDatabaseContext
89+
.getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences());
90+
newFile = FileUtil.shortenFileName(newFile, fileDirectories);
91+
92+
linkProperty().set(newFile.toString());
93+
checkExtension();
94+
});
95+
}
96+
2897
//
98+
public void setValues(LinkedFile entry) {
99+
descriptionProperty.set(entry.getDescription());
100+
linkProperty.set(entry.getLink());
101+
102+
selectedExternalFileType.setValue(null);
29103

104+
// See what is a reasonable selection for the type combobox:
105+
Optional<ExternalFileType> fileType = ExternalFileTypes.getInstance().fromLinkedFile(entry, false);
106+
if (fileType.isPresent() && !(fileType.get() instanceof UnknownExternalFileType)) {
107+
selectedExternalFileType.setValue(fileType.get());
108+
} else if ((entry.getLink() != null) && (!entry.getLink().isEmpty())) {
109+
checkExtension();
110+
}
111+
}
30112

31113
public StringProperty linkProperty() {
32114
return linkProperty;
@@ -40,4 +122,7 @@ public ListProperty<ExternalFileType> externalFileTypeProperty() {
40122
return externalfilesTypes;
41123
}
42124

125+
public ObjectProperty<ExternalFileType> getSelectedExternalFileType() {
126+
return selectedExternalFileType;
127+
}
43128
}

0 commit comments

Comments
 (0)