11package org .jabref .gui .importer ;
22
3+ import java .util .ArrayList ;
4+ import java .util .List ;
35import java .util .Optional ;
46
57import javax .swing .undo .UndoManager ;
4951import org .jabref .model .database .BibDatabaseContext ;
5052import org .jabref .model .entry .BibEntry ;
5153import org .jabref .model .entry .BibEntryTypesManager ;
54+ import org .jabref .model .groups .AllEntriesGroup ;
55+ import org .jabref .model .groups .GroupTreeNode ;
5256import org .jabref .model .util .FileUpdateMonitor ;
5357
5458import com .airhacks .afterburner .views .ViewLoader ;
@@ -62,6 +66,7 @@ public class ImportEntriesDialog extends BaseDialog<Boolean> {
6266 @ FXML private Label pageNumberLabel ;
6367 @ FXML private CheckListView <BibEntry > entriesListView ;
6468 @ FXML private ComboBox <BibDatabaseContext > libraryListView ;
69+ @ FXML private ComboBox <GroupTreeNode > groupListView ;
6570 @ FXML private ButtonType importButton ;
6671 @ FXML private Label totalItems ;
6772 @ FXML private Label selectedItems ;
@@ -146,6 +151,7 @@ private void initialize() {
146151 });
147152
148153 libraryListView .setEditable (false );
154+ groupListView .setEditable (false );
149155 libraryListView .getItems ().addAll (stateManager .getOpenDatabases ());
150156 new ViewModelListCellFactory <BibDatabaseContext >()
151157 .withText (database -> {
@@ -162,6 +168,7 @@ private void initialize() {
162168 .install (libraryListView );
163169 viewModel .selectedDbProperty ().bind (libraryListView .getSelectionModel ().selectedItemProperty ());
164170 stateManager .getActiveDatabase ().ifPresent (database1 -> libraryListView .getSelectionModel ().select (database1 ));
171+ setupGroupListView ();
165172
166173 PseudoClass entrySelected = PseudoClass .getPseudoClass ("selected" );
167174 new ViewModelListCellFactory <BibEntry >()
@@ -220,6 +227,48 @@ private void initialize() {
220227 }
221228 }
222229
230+ private void setupGroupListView () {
231+ groupListView .setVisibleRowCount (5 );
232+ updateGroupList ();
233+ libraryListView .getSelectionModel ().selectedItemProperty ()
234+ .addListener ((_ , _ , _ ) -> {
235+ updateGroupList ();
236+ });
237+
238+ new ViewModelListCellFactory <GroupTreeNode >()
239+ .withText (group -> group != null ? group .getName () : Localization .lang ("No group" ))
240+ .install (groupListView );
241+ }
242+
243+ private void updateGroupList () {
244+ groupListView .getItems ().clear ();
245+
246+ BibDatabaseContext selectedDb = libraryListView .getSelectionModel ().getSelectedItem ();
247+ if (selectedDb .getMetaData ().getGroups ().isPresent ()) {
248+ GroupTreeNode rootGroup = selectedDb .getMetaData ().getGroups ().get ();
249+ groupListView .getItems ().add (rootGroup );
250+
251+ List <GroupTreeNode > allGroups = new ArrayList <>();
252+ collectGroupsFromTree (rootGroup , allGroups );
253+ allGroups .sort ((g1 , g2 ) -> g1 .getName ().compareToIgnoreCase (g2 .getName ()));
254+
255+ groupListView .getItems ().addAll (allGroups );
256+ groupListView .getSelectionModel ().select (stateManager .getSelectedGroups (selectedDb ).getFirst ());
257+ } else {
258+ // No groups defined -> only "All entries"
259+ GroupTreeNode noGroup = new GroupTreeNode (new AllEntriesGroup (Localization .lang ("All entries" )));
260+ groupListView .getItems ().add (noGroup );
261+ groupListView .getSelectionModel ().select (noGroup );
262+ }
263+ }
264+
265+ private void collectGroupsFromTree (GroupTreeNode parent , List <GroupTreeNode > groupList ) {
266+ for (GroupTreeNode child : parent .getChildren ()) {
267+ groupList .add (child );
268+ collectGroupsFromTree (child , groupList );
269+ }
270+ }
271+
223272 private void initializeDialog () {
224273 ViewLoader .view (this )
225274 .load ()
@@ -236,7 +285,15 @@ private void initializeDialog() {
236285
237286 setResultConverter (button -> {
238287 if (button == importButton ) {
239- viewModel .importEntries (viewModel .getCheckedEntries ().stream ().toList (), downloadLinkedOnlineFiles .isSelected ());
288+ if (groupListView .getItems ().size () > 1 ) {
289+ // 1 is the "All entries" group, so if more than 1, we have groups defined
290+ GroupTreeNode prevSelectedGroup = stateManager .getSelectedGroups (stateManager .getActiveDatabase ().orElse (null )).getFirst ();
291+ stateManager .setSelectedGroups (libraryListView .getSelectionModel ().getSelectedItem (), List .of (groupListView .getSelectionModel ().getSelectedItem ()));
292+ viewModel .importEntries (viewModel .getCheckedEntries ().stream ().toList (), downloadLinkedOnlineFiles .isSelected ());
293+ stateManager .setSelectedGroups (stateManager .getActiveDatabase ().orElse (null ), List .of (prevSelectedGroup ));
294+ } else {
295+ viewModel .importEntries (viewModel .getCheckedEntries ().stream ().toList (), downloadLinkedOnlineFiles .isSelected ());
296+ }
240297 } else {
241298 dialogService .notify (Localization .lang ("Import canceled" ));
242299 }
0 commit comments