|
| 1 | +package org.jabref.gui.edit.automaticfiededitor.clearcontent; |
| 2 | + |
| 3 | +import java.util.Set; |
| 4 | + |
| 5 | +import javafx.application.Platform; |
| 6 | +import javafx.fxml.FXML; |
| 7 | +import javafx.scene.control.Button; |
| 8 | +import javafx.scene.control.CheckBox; |
| 9 | +import javafx.scene.control.ComboBox; |
| 10 | + |
| 11 | +import org.jabref.gui.StateManager; |
| 12 | +import org.jabref.gui.edit.automaticfiededitor.AbstractAutomaticFieldEditorTabView; |
| 13 | +import org.jabref.gui.edit.automaticfiededitor.AutomaticFieldEditorTab; |
| 14 | +import org.jabref.logic.l10n.Localization; |
| 15 | +import org.jabref.model.entry.field.Field; |
| 16 | + |
| 17 | +import com.airhacks.afterburner.views.ViewLoader; |
| 18 | +import com.tobiasdiez.easybind.EasyBind; |
| 19 | + |
| 20 | +import static org.jabref.gui.util.FieldsUtil.FIELD_STRING_CONVERTER; |
| 21 | + |
| 22 | +public class ClearContentTabView extends AbstractAutomaticFieldEditorTabView implements AutomaticFieldEditorTab { |
| 23 | + |
| 24 | + @FXML private ComboBox<Field> fieldComboBox; |
| 25 | + @FXML private CheckBox showOnlySetFieldsCheckBox; |
| 26 | + @FXML private Button clearButton; |
| 27 | + private final StateManager stateManager; |
| 28 | + private ClearContentViewModel viewModel; |
| 29 | + |
| 30 | + public ClearContentTabView(StateManager stateManager) { |
| 31 | + this.stateManager = stateManager; |
| 32 | + ViewLoader.view(this) |
| 33 | + .root(this) |
| 34 | + .load(); |
| 35 | + } |
| 36 | + |
| 37 | + @FXML |
| 38 | + public void initialize() { |
| 39 | + viewModel = new ClearContentViewModel(stateManager); |
| 40 | + |
| 41 | + fieldComboBox.setConverter(FIELD_STRING_CONVERTER); |
| 42 | + fieldComboBox.getItems().setAll(viewModel.getAllFields()); |
| 43 | + if (!fieldComboBox.getItems().isEmpty()) { |
| 44 | + fieldComboBox.getSelectionModel().selectFirst(); |
| 45 | + } |
| 46 | + |
| 47 | + EasyBind.subscribe(showOnlySetFieldsCheckBox.selectedProperty(), selected -> { |
| 48 | + Set<Field> items = selected ? viewModel.getSetFieldsOnly() |
| 49 | + : viewModel.getAllFields(); |
| 50 | + fieldComboBox.getItems().setAll(items); |
| 51 | + if (!items.isEmpty()) { |
| 52 | + fieldComboBox.getSelectionModel().selectFirst(); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + clearButton.disableProperty().bind(fieldComboBox.valueProperty().isNull()); |
| 57 | + |
| 58 | + Platform.runLater(fieldComboBox::requestFocus); |
| 59 | + } |
| 60 | + |
| 61 | + @FXML |
| 62 | + private void onClear() { |
| 63 | + Field chosen = fieldComboBox.getValue(); |
| 64 | + if (chosen != null) { |
| 65 | + viewModel.clearField(chosen); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public String getTabName() { |
| 71 | + return Localization.lang("Clear content"); |
| 72 | + } |
| 73 | +} |
0 commit comments