Skip to content

Commit

Permalink
Merge branch 'maintable-beta' of https://github.com/JabRef/jabref int…
Browse files Browse the repository at this point in the history
…o javafxGlobalEverything
  • Loading branch information
tobiasdiez committed Feb 20, 2018
2 parents 840bdf5 + 0141abb commit 8cbe6e7
Show file tree
Hide file tree
Showing 23 changed files with 458 additions and 213 deletions.
5 changes: 4 additions & 1 deletion src/main/java/org/jabref/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Optional;
import java.util.UUID;

import org.jabref.gui.ClipBoardManager;
import org.jabref.gui.GlobalFocusListener;
import org.jabref.gui.StateManager;
import org.jabref.gui.keyboard.KeyBindingRepository;
Expand Down Expand Up @@ -49,6 +50,9 @@ public class Globals {
/**
* Manager for the state of the GUI.
*/

public static ClipBoardManager clipboardManager = new ClipBoardManager();

public static StateManager stateManager = new StateManager();
public static ExporterFactory exportFactory;
// Key binding preferences
Expand All @@ -69,7 +73,6 @@ public static synchronized KeyBindingRepository getKeyPrefs() {
return keyBindingRepository;
}


// Background tasks
public static void startBackgroundTasks() {
Globals.focusListener = new GlobalFocusListener();
Expand Down
92 changes: 11 additions & 81 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.nio.charset.UnsupportedCharsetException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -163,10 +162,11 @@ public class BasePanel extends StackPane implements ClipboardOwner {
private final BasePanelPreferences preferences;
private final ExternalFileTypes externalFileTypes;

private final EntryEditor entryEditor;
private MainTable mainTable;
// To contain instantiated entry editors. This is to save time
// As most enums, this must not be null
private BasePanelMode mode = BasePanelMode.SHOWING_NOTHING;
private final EntryEditor entryEditor;
private SplitPane splitPane;
private boolean saving;

Expand All @@ -175,7 +175,6 @@ public class BasePanel extends StackPane implements ClipboardOwner {
private boolean baseChanged;
private boolean nonUndoableChange;
// Used to track whether the base has changed since last save.
private MainTable mainTable;
private BibEntry showing;

// in switching between entries.
Expand Down Expand Up @@ -344,11 +343,11 @@ private void setupActions() {
actions.put(Actions.SAVE_SELECTED_AS_PLAIN, new SaveSelectedAction(SavePreferences.DatabaseSaveType.PLAIN_BIBTEX));

// The action for copying selected entries.
actions.put(Actions.COPY, (BaseAction) this::copy);
actions.put(Actions.COPY, (BaseAction) mainTable::copy);

actions.put(Actions.PRINT_PREVIEW, new PrintPreviewAction());

actions.put(Actions.CUT, (BaseAction) this::cut);
actions.put(Actions.CUT, (BaseAction) mainTable::cut);

//when you modify this action be sure to adjust Actions.CUT,
//they are the same except of the Localization, delete confirmation and Actions.COPY call
Expand All @@ -360,7 +359,7 @@ private void setupActions() {
// This allows you to (a) paste entire bibtex entries from a text editor, web browser, etc
// (b) copy and paste entries between multiple instances of JabRef (since
// only the text representation seems to get as far as the X clipboard, at least on my system)
actions.put(Actions.PASTE, (BaseAction) this::paste);
actions.put(Actions.PASTE, (BaseAction) mainTable::paste);

actions.put(Actions.SELECT_ALL, (BaseAction) mainTable.getSelectionModel()::selectAll);

Expand Down Expand Up @@ -672,30 +671,13 @@ private void copyCitationToClipboard(CitationStyleOutputFormat outputFormat) {
new CitationStyleToClipboardWorker(this, outputFormat).execute();
}

private void copy() {
List<BibEntry> bes = mainTable.getSelectedEntries();

if (!bes.isEmpty()) {
TransferableBibtexEntry trbe = new TransferableBibtexEntry(bes);
// ! look at ClipBoardManager
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trbe, BasePanel.this);
output(formatOutputMessage(Localization.lang("Copied"), bes.size()));
}
}

private void cut() {
runCommand(Actions.COPY);
// cannot call runCommand(Actions.DELETE), b/c it will call delete(false) with the wrong parameter
delete(true);
}

/**
* Removes the selected entries from the database
*
* @param cut If false the user will get asked if he really wants to delete the entries, and it will be localized as
* "deleted". If true the action will be localized as "cut"
*/
private void delete(boolean cut) {
public void delete(boolean cut) {
delete(cut, mainTable.getSelectedEntries());
}

Expand Down Expand Up @@ -749,54 +731,6 @@ public void delete(BibEntry entry) {
delete(false, Collections.singletonList(entry));
}

private void paste() {
Collection<BibEntry> bes = new ClipBoardManager().extractBibEntriesFromClipboard();

// finally we paste in the entries (if any), which either came from TransferableBibtexEntries
// or were parsed from a string
if (!bes.isEmpty()) {

NamedCompound ce = new NamedCompound(
(bes.size() > 1 ? Localization.lang("paste entries") : Localization.lang("paste entry")));

// Store the first inserted bibtexentry.
// bes[0] does not work as bes[0] is first clonded,
// then inserted.
// This entry is used to open up an entry editor
// for the first inserted entry.
BibEntry firstBE = null;

for (BibEntry be1 : bes) {

BibEntry be = (BibEntry) be1.clone();
if (firstBE == null) {
firstBE = be;
}
UpdateField.setAutomaticFields(be, Globals.prefs.getUpdateFieldPreferences());

// We have to clone the
// entries, since the pasted
// entries must exist
// independently of the copied
// ones.
bibDatabaseContext.getDatabase().insertEntry(be);

ce.addEdit(new UndoableInsertEntry(bibDatabaseContext.getDatabase(), be, BasePanel.this));
}
ce.end();
getUndoManager().addEdit(ce);
output(formatOutputMessage(Localization.lang("Pasted"), bes.size()));
markBaseChanged();

clearAndSelect(firstBE);
mainTable.requestFocus();

if (Globals.prefs.getBoolean(JabRefPreferences.AUTO_OPEN_FORM)) {
showAndEdit(firstBE);
}
}
}

private void copyTitle() {
List<BibEntry> selectedBibEntries = mainTable.getSelectedEntries();
if (!selectedBibEntries.isEmpty()) {
Expand Down Expand Up @@ -1095,7 +1029,7 @@ public BibEntry newEntry(EntryType type) {
UpdateField.setAutomaticFields(list, true, true, Globals.prefs.getUpdateFieldPreferences());

// Create an UndoableInsertEntry object.
getUndoManager().addEdit(new UndoableInsertEntry(bibDatabaseContext.getDatabase(), be, BasePanel.this));
getUndoManager().addEdit(new UndoableInsertEntry(bibDatabaseContext.getDatabase(), be));
output(Localization.lang("Added new '%0' entry.", actualType.getName().toLowerCase(Locale.ROOT)));

// We are going to select the new entry. Before that, make sure that we are in
Expand Down Expand Up @@ -1135,7 +1069,7 @@ public void insertEntry(final BibEntry bibEntry) {
UpdateField.setAutomaticFields(bibEntry, true, true, Globals.prefs.getUpdateFieldPreferences());
}
// Create an UndoableInsertEntry object.
getUndoManager().addEdit(new UndoableInsertEntry(bibDatabaseContext.getDatabase(), bibEntry, BasePanel.this));
getUndoManager().addEdit(new UndoableInsertEntry(bibDatabaseContext.getDatabase(), bibEntry));
output(Localization.lang("Added new '%0' entry.", bibEntry.getType()));

markBaseChanged(); // The database just changed.
Expand Down Expand Up @@ -1163,7 +1097,7 @@ public void updateTableFont() {
}

private void createMainTable() {
bibDatabaseContext.getDatabase().registerListener(SpecialFieldDatabaseChangeListener.getInstance());
bibDatabaseContext.getDatabase().registerListener(SpecialFieldDatabaseChangeListener.INSTANCE);

mainTable = new MainTable(tableModel, frame, this, bibDatabaseContext, preferences.getTablePreferences(), externalFileTypes, Globals.getKeyPrefs());

Expand Down Expand Up @@ -1467,11 +1401,7 @@ public void closeBottomPane() {
* given focus afterwards.
*/
public void clearAndSelect(final BibEntry bibEntry) {
mainTable.findEntry(bibEntry)
.ifPresent(entry -> {
mainTable.getSelectionModel().clearSelection();
mainTable.getSelectionModel().select(entry);
});
mainTable.clearAndSelect(bibEntry);
}

/**
Expand Down Expand Up @@ -1763,7 +1693,7 @@ private BibEntry getShowing() {
return showing;
}

private String formatOutputMessage(String start, int count) {
public String formatOutputMessage(String start, int count) {
return String.format("%s %d %s.", start, count,
(count > 1 ? Localization.lang("entries") : Localization.lang("entry")));
}
Expand Down
39 changes: 38 additions & 1 deletion src/main/java/org/jabref/gui/ClipBoardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javafx.scene.input.ClipboardContent;

import org.jabref.Globals;
import org.jabref.logic.bibtex.BibEntryWriter;
import org.jabref.logic.bibtex.LatexFieldFormatter;
import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.ImportException;
import org.jabref.logic.importer.ImportFormatReader;
import org.jabref.logic.importer.ImportFormatReader.UnknownFormatImport;
import org.jabref.logic.importer.fetcher.DoiFetcher;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.identifier.DOI;

Expand Down Expand Up @@ -77,10 +83,40 @@ public String getClipboardContents() {
return result;
}

public void setClipboardHtmlContent(String html) {
// TODO: This works on Mac and Windows 10, but not on Ubuntu 16.04
final javafx.scene.input.Clipboard clipboard = javafx.scene.input.Clipboard.getSystemClipboard();
final ClipboardContent content = new ClipboardContent();
content.putHtml(html);
clipboard.setContent(content);
}

public void setClipboardContent(String string) {
// TODO: This works on Mac and Windows 10, but not on Ubuntu 16.04
final javafx.scene.input.Clipboard clipboard = javafx.scene.input.Clipboard.getSystemClipboard();
final ClipboardContent content = new ClipboardContent();
content.putString(string);
clipboard.setContent(content);
}

public void setClipboardContent(List<BibEntry> entries) throws IOException {
// TODO: This works on Mac and Windows 10, but not on Ubuntu 16.04
final javafx.scene.input.Clipboard clipboard = javafx.scene.input.Clipboard.getSystemClipboard();
final ClipboardContent content = new ClipboardContent();
BibEntryWriter writer = new BibEntryWriter(new LatexFieldFormatter(Globals.prefs.getLatexFieldFormatterPreferences()), false);
String serializedEntries = writer.serializeAll(entries, BibDatabaseMode.BIBTEX);
content.put(DragAndDropDataFormats.ENTRIES, serializedEntries);
content.putString(serializedEntries);
clipboard.setContent(content);
}

/**
* Place a String on the clipboard, and make this class the
* owner of the Clipboard's contents.
*
* @deprecated use {@link #setClipboardContent(String)} instead
*/
@Deprecated
public void setClipboardContents(String aString) {
StringSelection stringSelection = new StringSelection(aString);
clipboard.setContents(stringSelection, this);
Expand All @@ -96,7 +132,8 @@ public List<BibEntry> extractBibEntriesFromClipboard() {
try {
@SuppressWarnings("unchecked")
List<BibEntry> contents = (List<BibEntry>) content.getTransferData(TransferableBibtexEntry.ENTRY_FLAVOR);
result = contents;
// We clone the entries to make sure we don't accidentally paste references
result = contents.stream().map(entry -> (BibEntry) entry.clone()).collect(Collectors.toList());
} catch (UnsupportedFlavorException | ClassCastException ex) {
LOGGER.warn("Could not paste this type", ex);
} catch (IOException ex) {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/jabref/gui/DefaultInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import org.jabref.Globals;
import org.jabref.gui.keyboard.KeyBindingRepository;
import org.jabref.gui.util.DefaultFileUpdateMonitor;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.journals.JournalAbbreviationLoader;
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.injection.Injector;
Expand Down Expand Up @@ -36,8 +36,10 @@ private static Object createDependency(Class<?> clazz) {
return Globals.journalAbbreviationLoader;
} else if (clazz == StateManager.class) {
return Globals.stateManager;
} else if (clazz == DefaultFileUpdateMonitor.class) {
} else if (clazz == FileUpdateMonitor.class) {
return Globals.getFileUpdateMonitor();
} else if (clazz == ClipBoardManager.class) {
return Globals.clipboardManager;
} else {
try {
return clazz.newInstance();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/DragAndDropDataFormats.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ public class DragAndDropDataFormats {

public static final DataFormat GROUP = new DataFormat("dnd/org.jabref.model.groups.GroupTreeNode");
public static final DataFormat LINKED_FILE = new DataFormat("dnd/org.jabref.model.entry.LinkedFile");
public static final DataFormat ENTRIES = new DataFormat("application/x-java-jvm-local-objectref");
public static final DataFormat ENTRIES = new DataFormat("dnd/org.jabref.model.entry.BibEntries");
}
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/DuplicateSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void run() {
if (!toAdd.isEmpty()) {
for (BibEntry entry : toAdd) {
panel.getDatabase().insertEntry(entry);
ce.addEdit(new UndoableInsertEntry(panel.getDatabase(), entry, panel));
ce.addEdit(new UndoableInsertEntry(panel.getDatabase(), entry));
}
panel.markBaseChanged();
}
Expand Down
Loading

0 comments on commit 8cbe6e7

Please sign in to comment.