-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
minor refactor to JabRefDialogService #11767
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
This file contains 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 |
---|---|---|
|
@@ -6,11 +6,9 @@ | |
import java.nio.file.FileSystems; | ||
import java.nio.file.Path; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Collectors; | ||
|
||
import javafx.concurrent.Task; | ||
import javafx.geometry.Pos; | ||
|
@@ -99,9 +97,9 @@ private FXDialog createDialog(AlertType type, String title, String content) { | |
return alert; | ||
} | ||
|
||
private FXDialog createDialogWithOptOut(AlertType type, String title, String content, | ||
private FXDialog createDialogWithOptOut(String title, String content, | ||
String optOutMessage, Consumer<Boolean> optOutAction) { | ||
FXDialog alert = new FXDialog(type, title, true); | ||
FXDialog alert = new FXDialog(AlertType.CONFIRMATION, title, true); | ||
// Need to force the alert to layout in order to grab the graphic as we are replacing the dialog pane with a custom pane | ||
alert.getDialogPane().applyCss(); | ||
Node graphic = alert.getDialogPane().getGraphic(); | ||
|
@@ -135,7 +133,7 @@ public static String shortenDialogMessage(String dialogMessage) { | |
if (dialogMessage.length() < JabRefDialogService.DIALOG_SIZE_LIMIT) { | ||
return dialogMessage.trim(); | ||
} | ||
return (dialogMessage.substring(0, Math.min(dialogMessage.length(), JabRefDialogService.DIALOG_SIZE_LIMIT)) + "...").trim(); | ||
return (dialogMessage.substring(0, JabRefDialogService.DIALOG_SIZE_LIMIT) + "...").trim(); | ||
} | ||
|
||
private <T> ChoiceDialog<T> createChoiceDialog(String title, String content, String okButtonLabel, T defaultChoice, Collection<T> choices) { | ||
|
@@ -226,16 +224,7 @@ public void showErrorDialogAndWait(FetcherException fetcherException) { | |
String localizedMessage = fetcherException.getLocalizedMessage(); | ||
Optional<SimpleHttpResponse> httpResponse = fetcherException.getHttpResponse(); | ||
if (httpResponse.isPresent()) { | ||
int statusCode = httpResponse.get().statusCode(); | ||
if (statusCode == 401) { | ||
this.showInformationDialogAndWait(failedTitle, Localization.lang("Access denied. You are not authorized to access this resource. Please check your credentials and try again. If you believe you should have access, please contact the administrator for assistance.") + "\n\n" + localizedMessage); | ||
} else if (statusCode == 403) { | ||
this.showInformationDialogAndWait(failedTitle, Localization.lang("Access denied. You do not have permission to access this resource. Please contact the administrator for assistance or try a different action.") + "\n\n" + localizedMessage); | ||
} else if (statusCode == 404) { | ||
this.showInformationDialogAndWait(failedTitle, Localization.lang("The requested resource could not be found. It seems that the file you are trying to download is not available or has been moved. Please verify the URL and try again. If you believe this is an error, please contact the administrator for further assistance.") + "\n\n" + localizedMessage); | ||
} else { | ||
this.showErrorDialogAndWait(failedTitle, Localization.lang("Something is wrong on JabRef side. Please check the URL and try again.") + "\n\n" + localizedMessage); | ||
} | ||
this.showInformationDialogAndWait(failedTitle, Localization.lang(getContentByCode(httpResponse.get().statusCode())) + "\n\n" + localizedMessage); | ||
} else if (fetcherException instanceof FetcherClientException) { | ||
this.showErrorDialogAndWait(failedTitle, Localization.lang("Something is wrong on JabRef side. Please check the URL and try again.") + "\n\n" + localizedMessage); | ||
} else if (fetcherException instanceof FetcherServerException) { | ||
|
@@ -246,6 +235,19 @@ public void showErrorDialogAndWait(FetcherException fetcherException) { | |
} | ||
} | ||
|
||
private String getContentByCode(int statusCode) { | ||
return switch (statusCode) { | ||
case 401 -> | ||
"Access denied. You are not authorized to access this resource. Please check your credentials and try again. If you believe you should have access, please contact the administrator for assistance."; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please wrap in |
||
case 403 -> | ||
"Access denied. You do not have permission to access this resource. Please contact the administrator for assistance or try a different action."; | ||
case 404 -> | ||
"The requested resource could not be found. It seems that the file you are trying to download is not available or has been moved. Please verify the URL and try again. If you believe this is an error, please contact the administrator for further assistance."; | ||
default -> | ||
"Something is wrong on JabRef side. Please check the URL and try again."; | ||
}; | ||
} | ||
|
||
@Override | ||
public void showErrorDialogAndWait(String title, String content, Throwable exception) { | ||
ExceptionDialog exceptionDialog = new ExceptionDialog(exception); | ||
|
@@ -288,7 +290,7 @@ public boolean showConfirmationDialogAndWait(String title, String content, | |
@Override | ||
public boolean showConfirmationDialogWithOptOutAndWait(String title, String content, | ||
String optOutMessage, Consumer<Boolean> optOutAction) { | ||
FXDialog alert = createDialogWithOptOut(AlertType.CONFIRMATION, title, content, optOutMessage, optOutAction); | ||
FXDialog alert = createDialogWithOptOut(title, content, optOutMessage, optOutAction); | ||
alert.getButtonTypes().setAll(ButtonType.YES, ButtonType.NO); | ||
return alert.showAndWait().filter(buttonType -> buttonType == ButtonType.YES).isPresent(); | ||
} | ||
|
@@ -297,7 +299,7 @@ public boolean showConfirmationDialogWithOptOutAndWait(String title, String cont | |
public boolean showConfirmationDialogWithOptOutAndWait(String title, String content, | ||
String okButtonLabel, String cancelButtonLabel, | ||
String optOutMessage, Consumer<Boolean> optOutAction) { | ||
FXDialog alert = createDialogWithOptOut(AlertType.CONFIRMATION, title, content, optOutMessage, optOutAction); | ||
FXDialog alert = createDialogWithOptOut(title, content, optOutMessage, optOutAction); | ||
ButtonType okButtonType = new ButtonType(okButtonLabel, ButtonBar.ButtonData.YES); | ||
ButtonType cancelButtonType = new ButtonType(cancelButtonLabel, ButtonBar.ButtonData.NO); | ||
alert.getButtonTypes().setAll(okButtonType, cancelButtonType); | ||
|
@@ -388,7 +390,7 @@ public <V> void showProgressDialogAndWait(String title, String content, Task<V> | |
} | ||
|
||
@Override | ||
public <V> Optional<ButtonType> showBackgroundProgressDialogAndWait(String title, String content, StateManager stateManager) { | ||
public Optional<ButtonType> showBackgroundProgressDialogAndWait(String title, String content, StateManager stateManager) { | ||
TaskProgressView<Task<?>> taskProgressView = new TaskProgressView<>(); | ||
EasyBind.bindContent(taskProgressView.getTasks(), stateManager.getRunningBackgroundTasks()); | ||
taskProgressView.setRetainTasks(false); | ||
|
@@ -424,25 +426,24 @@ public void notify(String message) { | |
// The event log is not that user friendly (different purpose). | ||
LOGGER.info(message); | ||
|
||
UiTaskExecutor.runInJavaFXThread(() -> { | ||
UiTaskExecutor.runInJavaFXThread(() -> | ||
Notifications.create() | ||
.text(message) | ||
.position(Pos.BOTTOM_CENTER) | ||
.hideAfter(TOAST_MESSAGE_DISPLAY_TIME) | ||
.owner(mainWindow) | ||
.threshold(5, | ||
Notifications.create() | ||
.title(Localization.lang("Last notification")) | ||
.text( | ||
"(" + Localization.lang("Check the event log to see all notifications") + ")" | ||
+ "\n\n" + message) | ||
.onAction(e -> { | ||
ErrorConsoleAction ec = new ErrorConsoleAction(); | ||
ec.execute(); | ||
})) | ||
Notifications.create() | ||
.title(Localization.lang("Last notification")) | ||
.text( | ||
"(" + Localization.lang("Check the event log to see all notifications") + ")" | ||
+ "\n\n" + message) | ||
.onAction(e -> { | ||
ErrorConsoleAction ec = new ErrorConsoleAction(); | ||
ec.execute(); | ||
})) | ||
.hideCloseButton() | ||
.show(); | ||
}); | ||
.show()); | ||
} | ||
|
||
@Override | ||
|
@@ -472,7 +473,7 @@ public Optional<Path> showDirectorySelectionDialog(DirectoryDialogConfiguration | |
public List<Path> showFileOpenDialogAndGetMultipleFiles(FileDialogConfiguration fileDialogConfiguration) { | ||
FileChooser chooser = getConfiguredFileChooser(fileDialogConfiguration); | ||
List<File> files = chooser.showOpenMultipleDialog(mainWindow); | ||
return files != null ? files.stream().map(File::toPath).collect(Collectors.toList()) : Collections.emptyList(); | ||
return files != null ? files.stream().map(File::toPath).toList() : List.of(); | ||
} | ||
|
||
private DirectoryChooser getConfiguredDirectoryChooser(DirectoryDialogConfiguration directoryDialogConfiguration) { | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that does not work.
Localization.lang has to be parameterized with a literal (!) string. Otherwise, our tooling does nt work. Please see https://devdocs.jabref.org/code-howtos/localization.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found this TODO in
org.jabref.logic.l10n.LocalizationConsistencyTest
:In my code is a
var1 + "test" + var2
case, so it failed. maybe I should finish this TODO to pass the unit test?sorry I made a mistake, my code is not
var1 + "test" + var2
case, it just avar
case which is illegal inLocalizationConsistencyTest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finishing that ToDo would require several hours of work and design planning. Localization requires full sentences to be translated as in different languages word order and grammar changes d pending on what that var may be. So please just do as @koppor said, fix your strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did as @koppor said, have fixed my strings before I commented. I just want to make this unit test better by finishing ToDo. And I think it may not require several hours work, such as this simple way:
LocalizationConsistencyTest
now:StringUtils.countMatches(e.getKey(), "\"") >= 2
to coverLocalization.lang(var1 + "test" + var2)
case:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussion follow-up at #11784