1
1
package org .jabref .gui .filelist ;
2
2
3
3
import java .io .IOException ;
4
- import java .nio .file .Path ;
5
- import java .nio .file .Paths ;
6
- import java .util .List ;
7
4
import java .util .Optional ;
8
- import java .util .regex .Pattern ;
9
-
10
5
import javax .inject .Inject ;
11
6
import javafx .event .ActionEvent ;
12
7
import javafx .fxml .FXML ;
13
8
import javafx .scene .control .Button ;
14
9
import javafx .scene .control .ComboBox ;
15
10
import javafx .scene .control .TextField ;
16
-
17
- import org .jabref .Globals ;
18
11
import org .jabref .gui .AbstractController ;
19
12
import org .jabref .gui .DialogService ;
20
13
import org .jabref .gui .StateManager ;
21
14
import org .jabref .gui .desktop .JabRefDesktop ;
22
15
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 ;
27
16
import org .jabref .model .entry .LinkedFile ;
28
- import org .jabref .model .util .FileHelper ;
29
- import org .jabref .preferences .JabRefPreferences ;
30
17
import org .jabref .preferences .PreferencesService ;
31
18
32
19
import org .apache .commons .logging .Log ;
35
22
public class FileListDialogController extends AbstractController <FileListDialogViewModel > {
36
23
37
24
private static final Log LOGGER = LogFactory .getLog (FileListDialogController .class );
38
- private static final Pattern REMOTE_LINK_PATTERN = Pattern .compile ("[a-z]+://.*" );
39
25
40
26
@ FXML private TextField tfLink ;
41
27
@ FXML private Button btnBrowse ;
@@ -49,11 +35,9 @@ public class FileListDialogController extends AbstractController<FileListDialogV
49
35
@ Inject private DialogService dialogService ;
50
36
@ Inject private StateManager stateManager ;
51
37
52
- private boolean showSaveDialog ;
53
-
54
38
@ FXML
55
39
private void initialize () {
56
- viewModel = new FileListDialogViewModel (stateManager .getActiveDatabase ().get ());
40
+ viewModel = new FileListDialogViewModel (stateManager .getActiveDatabase ().get (), dialogService );
57
41
setBindings ();
58
42
59
43
}
@@ -63,40 +47,16 @@ private void setBindings() {
63
47
cmbFileType .itemsProperty ().bindBidirectional (viewModel .externalFileTypeProperty ());
64
48
tfDescription .textProperty ().bindBidirectional (viewModel .descriptionProperty ());
65
49
tfLink .textProperty ().bindBidirectional (viewModel .linkProperty ());
50
+
51
+ cmbFileType .valueProperty ().bindBidirectional (viewModel .getSelectedExternalFileType ());
66
52
}
67
53
68
54
@ FXML
69
55
void browseFileDialog (ActionEvent event ) {
70
56
71
- String fileText = viewModel .linkProperty ().get ();
57
+ viewModel .browseFileDialog ();
58
+ tfLink .requestFocus ();
72
59
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
- });
100
60
}
101
61
102
62
@ FXML
@@ -110,36 +70,7 @@ void ok_clicked(ActionEvent event) {
110
70
}
111
71
112
72
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 );
143
74
}
144
75
145
76
@ FXML
0 commit comments