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 6117695
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 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
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,13 +1,13 @@
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;
Expand All @@ -18,10 +18,12 @@ public class FieldTextMenu implements MouseListener {
private final FieldEditor field;
private final JPopupMenu inputMenu = new JPopupMenu();
private final CopyAction copyAct;
private final PasteAction pasteAct;

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

Expand Down Expand Up @@ -54,18 +56,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") + ": " + limitStringLength(data, 20));
} 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 All @@ -76,6 +91,14 @@ private void initMenu() {
}
}

private String limitStringLength(String s, int maxLength) {
if (s.length() <= maxLength) {
return s;
}

return s.substring(0, maxLength - 4) + "...";
}

@SuppressWarnings("serial")
class ReplaceAction extends AbstractAction {
public ReplaceAction() {
Expand Down

0 comments on commit 6117695

Please sign in to comment.