-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: Update WebSearchTab #13791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: Update WebSearchTab #13791
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
48172c7
feat: Update WebSearchTab
Yubo-Cao 563d293
docs: Update CHANGELOG.md
Yubo-Cao a64e154
fix: Apply Trag suggestion
Yubo-Cao e677079
fix: Typo in the JabRef_en.properties
Yubo-Cao 653232c
docs: Update CHANGELOG.md for another PR which I forgot to update the…
Yubo-Cao 737070c
docs: Update CHANGELOG.md for another and another PR which I forgot t…
Yubo-Cao fd9d760
fix: Apply Subhramit's suggestions
Yubo-Cao 97fe32c
docs: Update CHANGELOG.md
Yubo-Cao ec28110
Fix checkstyle
subhramit ac64607
Merge branch 'main' into fix/online-tab
subhramit 292c626
fix brace
subhramit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
jabgui/src/main/java/org/jabref/gui/preferences/websearch/ApiKeyDialog.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package org.jabref.gui.preferences.websearch; | ||
|
||
import javafx.animation.Animation; | ||
import javafx.animation.Interpolator; | ||
import javafx.animation.RotateTransition; | ||
import javafx.application.Platform; | ||
import javafx.beans.property.BooleanProperty; | ||
import javafx.beans.property.SimpleBooleanProperty; | ||
import javafx.fxml.FXML; | ||
import javafx.scene.control.Button; | ||
import javafx.scene.control.ButtonType; | ||
import javafx.scene.control.CheckBox; | ||
import javafx.scene.control.TextField; | ||
import javafx.util.Duration; | ||
|
||
import org.jabref.gui.FXDialog; | ||
import org.jabref.gui.icon.IconTheme; | ||
import org.jabref.gui.preferences.websearch.WebSearchTabViewModel.FetcherViewModel; | ||
import org.jabref.gui.util.DelayedExecution; | ||
import org.jabref.logic.l10n.Localization; | ||
|
||
import com.airhacks.afterburner.views.ViewLoader; | ||
|
||
public class ApiKeyDialog extends FXDialog { | ||
/// The length at which the status message is shown after testing the API key | ||
public static final Duration STATUS_DURATION = Duration.seconds(5); | ||
|
||
@FXML private TextField apiKeyField; | ||
@FXML private CheckBox persistApiKeysCheckBox; | ||
@FXML private Button testButton; | ||
|
||
private final WebSearchTabViewModel viewModel; | ||
private final FetcherViewModel fetcherViewModel; | ||
private final BooleanProperty apiKeyValid = new SimpleBooleanProperty(); | ||
private RotateTransition rotation; | ||
|
||
public ApiKeyDialog(WebSearchTabViewModel viewModel, FetcherViewModel fetcherViewModel) { | ||
super(AlertType.NONE, Localization.lang("API Key for %0", fetcherViewModel.getName())); | ||
this.viewModel = viewModel; | ||
this.fetcherViewModel = fetcherViewModel; | ||
|
||
ViewLoader.view(this) | ||
.load() | ||
.setAsDialogPane(this); | ||
|
||
setResultConverter(this::convertResult); | ||
} | ||
|
||
@FXML | ||
private void initialize() { | ||
apiKeyField.setText(fetcherViewModel.getApiKey()); | ||
|
||
apiKeyValid.set(apiKeyField.getText().isEmpty() || fetcherViewModel.shouldUseCustomApiKey()); | ||
|
||
Button okButton = (Button) getDialogPane().lookupButton(ButtonType.OK); | ||
calixtus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (okButton != null) { | ||
okButton.disableProperty().bind(apiKeyValid.not()); | ||
} | ||
|
||
apiKeyField.textProperty().addListener((obs, oldValue, newValue) -> { | ||
Yubo-Cao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
apiKeyValid.set(newValue.isEmpty()); | ||
testButton.setGraphic(null); | ||
testButton.setText(Localization.lang("Test connection")); | ||
testButton.getStyleClass().removeAll("success", "error"); | ||
}); | ||
|
||
persistApiKeysCheckBox.selectedProperty().bindBidirectional(viewModel.getApikeyPersistProperty()); | ||
persistApiKeysCheckBox.disableProperty().bind(viewModel.apiKeyPersistAvailable().not()); | ||
|
||
rotation = new RotateTransition(Duration.seconds(1), IconTheme.JabRefIcons.REFRESH.getGraphicNode()); | ||
rotation.setByAngle(360); | ||
rotation.setCycleCount(Animation.INDEFINITE); | ||
rotation.setInterpolator(Interpolator.LINEAR); | ||
} | ||
|
||
@FXML | ||
private void testApiKey() { | ||
testButton.setDisable(true); | ||
testButton.setText(Localization.lang("Testing...")); | ||
testButton.setGraphic(rotation.getNode()); | ||
rotation.play(); | ||
viewModel.checkApiKey(fetcherViewModel, apiKeyField.getText(), this::handleTestResult); | ||
} | ||
|
||
private void handleTestResult(boolean success) { | ||
Platform.runLater(() -> { | ||
rotation.stop(); | ||
testButton.setDisable(false); | ||
|
||
testButton.setGraphic(success ? IconTheme.JabRefIcons.SUCCESS.getGraphicNode() : IconTheme.JabRefIcons.ERROR.getGraphicNode()); | ||
testButton.getStyleClass().removeAll("success", "error"); | ||
testButton.getStyleClass().add(success ? "success" : "error"); | ||
testButton.setText(success ? Localization.lang("API Key Valid") : Localization.lang("API Key Invalid")); | ||
apiKeyValid.set(success); | ||
|
||
new DelayedExecution(STATUS_DURATION, () -> { | ||
testButton.setGraphic(null); | ||
testButton.setText(Localization.lang("Test connection")); | ||
}).start(); | ||
}); | ||
} | ||
|
||
private ButtonType convertResult(ButtonType button) { | ||
if (button == ButtonType.OK) { | ||
String apiKey = apiKeyField.getText().trim(); | ||
fetcherViewModel.apiKeyProperty().set(apiKey); | ||
fetcherViewModel.useCustomApiKeyProperty().set(!apiKey.isEmpty()); | ||
} | ||
return button; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.