Skip to content

Commit

Permalink
Merge pull request #842 from oscargus/betterfieldcopypaste
Browse files Browse the repository at this point in the history
Fixed #492: All text is copied if nothing is marked and preview of pa…
  • Loading branch information
simonharrer committed Feb 22, 2016
2 parents 3b3fc0d + e8fc08d commit 5e78a79
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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 CopyAction copyAction;
private final PasteAction pasteAction;

private static final int MAX_PASTE_PREVIEW_LENGTH = 20;


public FieldTextMenu(FieldEditor fieldComponent) {
field = fieldComponent;
copyAct = new CopyAction((JTextComponent) field);
copyAction = new CopyAction((JTextComponent) field);
pasteAction = new PasteAction((JTextComponent) field);
initMenu();
}

Expand Down Expand Up @@ -54,19 +60,32 @@ private void maybeShowPopup(MouseEvent e) {

// enable/disable copy to clipboard if selected text available
String txt = field.getSelectedText();
boolean cStat = false;
if ((txt != null) && !txt.isEmpty()) {
cStat = true;
String allTxt = field.getText();
boolean copyStatus = false;
if (((txt != null) && (!txt.isEmpty())) || ((allTxt != null) && !allTxt.isEmpty())) {
copyStatus = true;
}

copyAction.setEnabled(copyStatus);

String data = ClipBoardManager.CLIPBOARD.getClipboardContents();
boolean pasteStatus = false;
if (!data.isEmpty()) {
pasteStatus = true;
pasteAction.putValue(Action.SHORT_DESCRIPTION, Localization.lang("Paste from clipboard") + ": "
+ StringUtil.limitStringLength(data, MAX_PASTE_PREVIEW_LENGTH));
} else {
pasteAction.putValue(Action.SHORT_DESCRIPTION, Localization.lang("Paste from clipboard"));
}
copyAct.setEnabled(cStat);
pasteAction.setEnabled(pasteStatus);
inputMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
}

private void initMenu() {
inputMenu.add(new PasteAction((Component) field));
inputMenu.add(copyAct);
inputMenu.add(pasteAction);
inputMenu.add(copyAction);
inputMenu.addSeparator();
inputMenu.add(new ReplaceAction());

Expand Down
20 changes: 16 additions & 4 deletions src/main/java/net/sf/jabref/logic/util/strings/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -533,24 +533,36 @@ public static List<String> tokenizeToList(String buf, String delimstr)
*/
public static String quote(String toQuote, String specials, char quoteChar) {
if (toQuote == null) {
return null;
return "";
}

StringBuilder result = new StringBuilder();
char c;
boolean isSpecial;
for (int i = 0; i < toQuote.length(); ++i) {
c = toQuote.charAt(i);

isSpecial = (c == quoteChar);
// If non-null specials performs logic-or with specials.indexOf(c) >= 0
isSpecial |= ((specials != null) && (specials.indexOf(c) >= 0));

if (isSpecial) {
result.append(quoteChar);
}
result.append(c);
}
return result.toString();
}

public static String limitStringLength(String s, int maxLength) {
if (s == null) {
return "";
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public void testQuoteNullQuotation() {

@Test
public void testQuoteNullString() {
assertNull(StringUtil.quote(null, ";", ':'));
assertEquals("", StringUtil.quote(null, ";", ':'));
}

@Test
Expand All @@ -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() {
assertEquals("", StringUtil.limitStringLength(null, 10));
}

}

0 comments on commit 5e78a79

Please sign in to comment.