Skip to content
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

Improve Snackbar Dialogs for Copy to Clipboard Menu #4884

Merged
merged 5 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ public BasePanel(JabRefFrame frame, BasePanelPreferences preferences, BibDatabas

this.preview = new PreviewPanel(this, getBibDatabaseContext(), preferences.getKeyBindings(), preferences.getPreviewPreferences(), dialogService, externalFileTypes);
frame().getGlobalSearchBar().getSearchQueryHighlightObservable().addSearchListener(preview);

}

@Subscribe
Expand Down Expand Up @@ -464,11 +463,12 @@ private void copyTitle() {
output(Localization.lang("None of the selected entries have titles."));
return;
}
Globals.clipboardManager.setContent(String.join("\n", titles));
final String copiedTitles = String.join("\n", titles);
Globals.clipboardManager.setContent(copiedTitles);

if (titles.size() == selectedBibEntries.size()) {
// All entries had titles.
output((selectedBibEntries.size() > 1 ? Localization.lang("Copied titles") : Localization.lang("Copied title")) + '.');
output(Localization.lang("Copied") + " '" + shortenDialogMessage(copiedTitles) + "'.");
} else {
output(Localization.lang("Warning: %0 out of %1 entries have undefined title.", Integer.toString(selectedBibEntries.size() - titles.size()), Integer.toString(selectedBibEntries.size())));
}
Expand All @@ -488,15 +488,15 @@ private void copyCiteKey() {
return;
}

String sb = String.join(",", keys);
String citeCommand = Optional.ofNullable(Globals.prefs.get(JabRefPreferences.CITE_COMMAND))
.filter(cite -> cite.contains("\\")) // must contain \
.orElse("\\cite");
Globals.clipboardManager.setContent(citeCommand + "{" + sb + '}');
final String copiedCiteCommand = citeCommand + "{" + String.join(",", keys) + '}';
Globals.clipboardManager.setContent(copiedCiteCommand);

if (keys.size() == bes.size()) {
// All entries had keys.
output(bes.size() > 1 ? Localization.lang("Copied keys") : Localization.lang("Copied key") + '.');
output(Localization.lang("Copied") + " '" + shortenDialogMessage(copiedCiteCommand) + "'.");
} else {
output(Localization.lang("Warning: %0 out of %1 entries have undefined BibTeX key.", Integer.toString(bes.size() - keys.size()), Integer.toString(bes.size())));
}
Expand All @@ -516,11 +516,12 @@ private void copyKey() {
return;
}

Globals.clipboardManager.setContent(String.join(",", keys));
final String copiedKeys = String.join(",", keys);
Globals.clipboardManager.setContent(copiedKeys);

if (keys.size() == bes.size()) {
// All entries had keys.
output((bes.size() > 1 ? Localization.lang("Copied keys") : Localization.lang("Copied key")) + '.');
output(Localization.lang("Copied") + " '" + shortenDialogMessage(copiedKeys) + "'.");
} else {
output(Localization.lang("Warning: %0 out of %1 entries have undefined BibTeX key.", Integer.toString(bes.size() - keys.size()), Integer.toString(bes.size())));
}
Expand Down Expand Up @@ -557,17 +558,25 @@ private void copyKeyAndTitle() {
return;
}

Globals.clipboardManager.setContent(sb.toString());
final String copiedKeysAndTitles = sb.toString();
Globals.clipboardManager.setContent(copiedKeysAndTitles);

if (copied == bes.size()) {
// All entries had keys.
output((bes.size() > 1 ? Localization.lang("Copied keys") : Localization.lang("Copied key")) + '.');
output(Localization.lang("Copied") + " '" + shortenDialogMessage(copiedKeysAndTitles) + "'.");
} else {
output(Localization.lang("Warning: %0 out of %1 entries have undefined BibTeX key.", Integer.toString(bes.size() - copied), Integer.toString(bes.size())));
}
}
}

private String shortenDialogMessage(String dialogMessage) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a JavaFX util method that shortens text based on its display size (e.g taking the current font into account). The difference is probably negligible through.

https://github.com/javafxports/openjdk-jfx/blob/develop/modules/javafx.controls/src/main/java/com/sun/javafx/scene/control/skin/Utils.java#L212

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, the alternative would be to directly hack a hard limit into the snackbar dialog system. I didn't do that, because maybe another type of messages should be independent of this 300 char limit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move shortenDialogMessage to a helper class (add OverrunStyle as an additional paramater) and reuse it below in CopyBibTeXKeyAndLinkAction ?

Copy link
Member Author

@LinusDietz LinusDietz Apr 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revisiting the code, I decided to move the method to the JabRefDialogService (formerly FXDialogService)

if (dialogMessage.length() < JabRefPreferences.SNACKBAR_DIALOG_SIZE_LIMIT) {
return dialogMessage;
}
return dialogMessage.substring(0, Math.min(dialogMessage.length(), JabRefPreferences.SNACKBAR_DIALOG_SIZE_LIMIT)) + "...";
}

private void openExternalFile() {
final List<BibEntry> selectedEntries = mainTable.getSelectedEntries();
if (selectedEntries.size() != 1) {
Expand Down Expand Up @@ -952,7 +961,7 @@ public void entryEditorClosing(EntryEditor editor) {
*/
public void ensureNotShowingBottomPanel(BibEntry entry) {
if (((mode == BasePanelMode.SHOWING_EDITOR) && (entryEditor.getEntry() == entry))
|| ((mode == BasePanelMode.SHOWING_PREVIEW) && (preview.getEntry() == entry))) {
|| ((mode == BasePanelMode.SHOWING_PREVIEW) && (preview.getEntry() == entry))) {
closeBottomPane();
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/jabref/gui/FXDialogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public class FXDialogService implements DialogService {

private static final Duration TOAST_MESSAGE_DISPLAY_TIME = Duration.millis(3000);
private static final Logger LOGGER = LoggerFactory.getLogger(FXDialogService.class);

private final Window mainWindow;
private final JFXSnackbar statusLine;

Expand Down Expand Up @@ -118,7 +117,6 @@ public <T> Optional<T> showChoiceDialogAndWait(String title, String content, Str
choiceDialog.setTitle(title);
choiceDialog.setContentText(content);
return choiceDialog.showAndWait();

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.jabref.logic.util.OS;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.FieldName;
import org.jabref.preferences.JabRefPreferences;

/**
* This class will copy each selected entry's BibTeX key as a hyperlink to its url to the clipboard.
Expand Down Expand Up @@ -46,14 +47,14 @@ public void action() throws Exception {
sb.append(url.isEmpty() ? key : String.format("<a href=\"%s\">%s</a>", url, key));
sb.append(OS.NEWLINE);
}

DefaultTaskExecutor.runInJavaFXThread(() -> clipboardManager.setHtmlContent(sb.toString()));
final String keyAndLink = sb.toString();
DefaultTaskExecutor.runInJavaFXThread(() -> clipboardManager.setHtmlContent(keyAndLink));

int copied = entriesWithKey.size();
int toCopy = entries.size();
if (copied == toCopy) {
// All entries had keys.
JabRefGUI.getMainFrame().getDialogService().notify((entries.size() > 1 ? Localization.lang("Copied keys") : Localization.lang("Copied key")) + '.');
JabRefGUI.getMainFrame().getDialogService().notify(Localization.lang("Copied") + " '" + keyAndLink.substring(0, Math.min(keyAndLink.length(), JabRefPreferences.SNACKBAR_DIALOG_SIZE_LIMIT)) + "'.");
} else {
JabRefGUI.getMainFrame().getDialogService().notify(Localization.lang("Warning: %0 out of %1 entries have undefined BibTeX key.",
Long.toString(toCopy - copied), Integer.toString(toCopy)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public CitationStyleToClipboardWorker(BasePanel basePanel, CitationStyleOutputFo
}

public void copyCitationStyleToClipboard(TaskExecutor taskExecutor) {
dialogService.notify(Localization.lang("Copying..."));
BackgroundTask.wrap(this::generateCitations)
.onFailure(ex -> LOGGER.error("Error while copying citations to the clipboard", ex))
.onSuccess(this::setClipBoardContent)
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ public class JabRefPreferences implements PreferencesService {
// Id Entry Generator Preferences
public static final String ID_ENTRY_GENERATOR = "idEntryGenerator";

// Snackbar dialog maximum size
public static final int SNACKBAR_DIALOG_SIZE_LIMIT = 300;

//File linking Options for entry editor
public static final String ENTRY_EDITOR_DRAG_DROP_PREFERENCE_TYPE = "DragDropPreferenceType";
Expand Down