Skip to content

Commit

Permalink
Fixed JabRef#492: All text is copied if nothing is marked and preview…
Browse files Browse the repository at this point in the history
… of pasted text
  • Loading branch information
oscargus committed Feb 20, 2016
1 parent 702913b commit 39322ec
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ to [sourceforge feature requests](https://sourceforge.net/p/jabref/features/) by
- Moved all quality-related database actions inside a new quality menu
- [#684](https://github.com/JabRef/jabref/issues/684): ISBNtoBibTex Error Message is now more clear
- Moved default bibliography mode to general preferences tab

- [#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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("<html>");
if (red) {
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/net/sf/jabref/gui/actions/CopyAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
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;

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();
}

Expand Down Expand Up @@ -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());
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/net/sf/jabref/logic/util/strings/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) + "...";
}
}
7 changes: 2 additions & 5 deletions src/main/java/net/sf/jabref/pdfimport/ImportDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/net/sf/jabref/logic/util/strings/StringUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}

0 comments on commit 39322ec

Please sign in to comment.