From 7575e555bfb214c91577b698cc3b103c72cc10c1 Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson Date: Sat, 20 Feb 2016 10:49:19 +0100 Subject: [PATCH] Fixed #492: All text is copied if nothing is marked and preview of pasted text --- CHANGELOG.md | 1 + .../jabref/groups/GroupTreeCellRenderer.java | 5 +--- .../net/sf/jabref/gui/actions/CopyAction.java | 9 ++++--- .../contextmenu/FieldTextMenu.java | 25 ++++++++++++++++--- .../jabref/logic/util/strings/StringUtil.java | 12 +++++++++ .../net/sf/jabref/pdfimport/ImportDialog.java | 7 ++---- .../logic/util/strings/StringUtilTest.java | 16 ++++++++++++ 7 files changed, 60 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f093e6a29d..52bc3c4f14e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ to [sourceforge feature requests](https://sourceforge.net/p/jabref/features/) by - Moved default bibliography mode to general preferences tab - Added Ordinal formatter (1 -> 1st etc) +- [#492](https://github.com/JabRef/jabref/issues/492): If no text is marked, the whole field is copied. Preview of pasted text in tool tip ### Fixed - Fixed [#621](https://github.com/JabRef/jabref/issues/621) and [#669](https://github.com/JabRef/jabref/issues/669): Encoding and preamble now end with newline. diff --git a/src/main/java/net/sf/jabref/groups/GroupTreeCellRenderer.java b/src/main/java/net/sf/jabref/groups/GroupTreeCellRenderer.java index 26a2322abcd..3c7dafd5f95 100644 --- a/src/main/java/net/sf/jabref/groups/GroupTreeCellRenderer.java +++ b/src/main/java/net/sf/jabref/groups/GroupTreeCellRenderer.java @@ -94,10 +94,7 @@ public Component getTreeCellRendererComponent(JTree tree, Object value, boolean } } } - String name = group.getName(); - if (name.length() > GroupTreeCellRenderer.MAX_DISPLAYED_LETTERS) { - name = name.substring(0, GroupTreeCellRenderer.MAX_DISPLAYED_LETTERS - 2) + "..."; - } + String name = StringUtil.limitStringLength(group.getName(), GroupTreeCellRenderer.MAX_DISPLAYED_LETTERS); StringBuilder sb = new StringBuilder(60); sb.append(""); if (red) { diff --git a/src/main/java/net/sf/jabref/gui/actions/CopyAction.java b/src/main/java/net/sf/jabref/gui/actions/CopyAction.java index 3352f42dd7a..89e89c1b2ac 100644 --- a/src/main/java/net/sf/jabref/gui/actions/CopyAction.java +++ b/src/main/java/net/sf/jabref/gui/actions/CopyAction.java @@ -23,9 +23,12 @@ public CopyAction(JTextComponent field) { @Override public void actionPerformed(ActionEvent e) { if (field != null) { - String data = field.getSelectedText(); - if ((data != null) && !data.isEmpty()) { - ClipBoardManager.CLIPBOARD.setClipboardContents(data); + String selectedText = field.getSelectedText(); + String allText = field.getText(); + if ((selectedText != null) && !selectedText.isEmpty()) { + ClipBoardManager.CLIPBOARD.setClipboardContents(selectedText); + } else if ((allText != null) && !allText.isEmpty()) { + ClipBoardManager.CLIPBOARD.setClipboardContents(allText); } } } diff --git a/src/main/java/net/sf/jabref/gui/fieldeditors/contextmenu/FieldTextMenu.java b/src/main/java/net/sf/jabref/gui/fieldeditors/contextmenu/FieldTextMenu.java index c2992866501..8e7200a115c 100644 --- a/src/main/java/net/sf/jabref/gui/fieldeditors/contextmenu/FieldTextMenu.java +++ b/src/main/java/net/sf/jabref/gui/fieldeditors/contextmenu/FieldTextMenu.java @@ -1,6 +1,5 @@ package net.sf.jabref.gui.fieldeditors.contextmenu; -import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; @@ -8,20 +7,27 @@ import javax.swing.*; import javax.swing.text.JTextComponent; +import net.sf.jabref.gui.ClipBoardManager; import net.sf.jabref.gui.actions.CopyAction; import net.sf.jabref.gui.actions.PasteAction; import net.sf.jabref.gui.fieldeditors.FieldEditor; import net.sf.jabref.logic.l10n.Localization; +import net.sf.jabref.logic.util.strings.StringUtil; import net.sf.jabref.logic.formatter.bibtexfields.AuthorsFormatter; public class FieldTextMenu implements MouseListener { private final FieldEditor field; private final JPopupMenu inputMenu = new JPopupMenu(); private final CopyAction copyAct; + private final PasteAction pasteAct; + + private static final int MAX_PASTE_PREVIEW_LENGTH = 20; + public FieldTextMenu(FieldEditor fieldComponent) { field = fieldComponent; copyAct = new CopyAction((JTextComponent) field); + pasteAct = new PasteAction((JTextComponent) field); initMenu(); } @@ -54,18 +60,31 @@ private void maybeShowPopup(MouseEvent e) { // enable/disable copy to clipboard if selected text available String txt = field.getSelectedText(); + String allTxt = field.getText(); boolean cStat = false; - if ((txt != null) && !txt.isEmpty()) { + if (((txt != null) && (!txt.isEmpty())) || ((allTxt != null) && !allTxt.isEmpty())) { cStat = true; } + copyAct.setEnabled(cStat); + + String data = ClipBoardManager.CLIPBOARD.getClipboardContents(); + boolean pStat = false; + if (!data.isEmpty()) { + pStat = true; + pasteAct.putValue(Action.SHORT_DESCRIPTION, Localization.lang("Paste from clipboard") + ": " + + StringUtil.limitStringLength(data, MAX_PASTE_PREVIEW_LENGTH)); + } else { + pasteAct.putValue(Action.SHORT_DESCRIPTION, Localization.lang("Paste from clipboard")); + } + pasteAct.setEnabled(pStat); inputMenu.show(e.getComponent(), e.getX(), e.getY()); } } } private void initMenu() { - inputMenu.add(new PasteAction((Component) field)); + inputMenu.add(pasteAct); inputMenu.add(copyAct); inputMenu.addSeparator(); inputMenu.add(new ReplaceAction()); diff --git a/src/main/java/net/sf/jabref/logic/util/strings/StringUtil.java b/src/main/java/net/sf/jabref/logic/util/strings/StringUtil.java index 76dec4f08ac..f4560b6b554 100644 --- a/src/main/java/net/sf/jabref/logic/util/strings/StringUtil.java +++ b/src/main/java/net/sf/jabref/logic/util/strings/StringUtil.java @@ -553,4 +553,16 @@ public static String quote(String toQuote, String specials, char quoteChar) { } return result.toString(); } + + public static String limitStringLength(String s, int maxLength) { + if (s == null) { + return null; + } + + if (s.length() <= maxLength) { + return s; + } + + return s.substring(0, maxLength - 3) + "..."; + } } diff --git a/src/main/java/net/sf/jabref/pdfimport/ImportDialog.java b/src/main/java/net/sf/jabref/pdfimport/ImportDialog.java index 9572147fae2..ad0fa2ab400 100644 --- a/src/main/java/net/sf/jabref/pdfimport/ImportDialog.java +++ b/src/main/java/net/sf/jabref/pdfimport/ImportDialog.java @@ -22,6 +22,7 @@ import net.sf.jabref.Globals; import net.sf.jabref.gui.preftabs.ImportSettingsTab; import net.sf.jabref.logic.l10n.Localization; +import net.sf.jabref.logic.util.strings.StringUtil; import javax.swing.*; @@ -104,11 +105,7 @@ public ImportDialog(boolean targetIsARow, String fileName) { this.radioButtononlyAttachPDF.setEnabled(false); } String name = new File(fileName).getName(); - if (name.length() < 34) { - labelFileName.setText(name); - } else { - labelFileName.setText(new File(fileName).getName().substring(0, 33) + "..."); - } + labelFileName.setText(StringUtil.limitStringLength(name, 34)); this.setTitle(Localization.lang("Import_Metadata_From_PDF")); setModal(true); diff --git a/src/test/java/net/sf/jabref/logic/util/strings/StringUtilTest.java b/src/test/java/net/sf/jabref/logic/util/strings/StringUtilTest.java index a38b622bed4..9b52d12f268 100644 --- a/src/test/java/net/sf/jabref/logic/util/strings/StringUtilTest.java +++ b/src/test/java/net/sf/jabref/logic/util/strings/StringUtilTest.java @@ -258,4 +258,20 @@ public void testQuoteMoreComplicated() { assertEquals("a::b:%c:;", StringUtil.quote("a:b%c;", "%;", ':')); } + @Test + public void testLimitStringLengthShort() { + assertEquals("Test", StringUtil.limitStringLength("Test", 20)); + } + + @Test + public void testLimitStringLengthLimiting() { + assertEquals("TestTes...", StringUtil.limitStringLength("TestTestTestTestTest", 10)); + assertEquals(10, StringUtil.limitStringLength("TestTestTestTestTest", 10).length()); + } + + @Test + public void testLimitStringLengthNullInput() { + assertNull(StringUtil.limitStringLength(null, 10)); + } + } \ No newline at end of file