BibTeX support
git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@225 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
parent
86c96621a7
commit
951bcc0f85
13 changed files with 445 additions and 74 deletions
|
@ -20,21 +20,31 @@
|
|||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.6 (2014-12-11)
|
||||
* Version 1.6 (2014-12-27)
|
||||
*
|
||||
*/
|
||||
|
||||
package org.openoffice.da.comp.writer2latex;
|
||||
|
||||
import java.awt.Desktop;
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import com.sun.star.awt.XDialog;
|
||||
import com.sun.star.frame.XFrame;
|
||||
import com.sun.star.uno.UnoRuntime;
|
||||
import com.sun.star.uno.XComponentContext;
|
||||
|
||||
import org.jbibtex.ParseException;
|
||||
import org.openoffice.da.comp.w2lcommon.helper.DialogBase;
|
||||
import org.openoffice.da.comp.w2lcommon.helper.MessageBox;
|
||||
|
||||
import writer2latex.office.BibMark;
|
||||
import writer2latex.office.BibMark.EntryType;
|
||||
|
||||
/** This class provides a UNO dialog to insert a BibTeX bibliographic reference
|
||||
*/
|
||||
public class BibTeXDialog extends DialogBase {
|
||||
//implements com.sun.star.lang.XInitialization {
|
||||
public class BibTeXDialog extends DialogBase implements com.sun.star.lang.XInitialization {
|
||||
|
||||
/** The component will be registered under this name.
|
||||
*/
|
||||
|
@ -49,7 +59,19 @@ public class BibTeXDialog extends DialogBase {
|
|||
public String getDialogLibraryName() {
|
||||
return "W4LDialogs";
|
||||
}
|
||||
|
||||
|
||||
// The current frame (passed at initialization)
|
||||
XFrame xFrame = null;
|
||||
|
||||
// The BibTeX directory (passed at initialization)
|
||||
File bibTeXDirectory = null;
|
||||
|
||||
// Cache of BibTeX files in the BibTeX directory
|
||||
File[] files = null;
|
||||
|
||||
// Cache of the current BibTeX file
|
||||
BibTeXReader currentFile = null;
|
||||
|
||||
/** Return the name of the dialog within the library
|
||||
*/
|
||||
public String getDialogName() {
|
||||
|
@ -57,6 +79,7 @@ public class BibTeXDialog extends DialogBase {
|
|||
}
|
||||
|
||||
public void initialize() {
|
||||
refresh();
|
||||
}
|
||||
|
||||
public void endDialog() {
|
||||
|
@ -68,19 +91,216 @@ public class BibTeXDialog extends DialogBase {
|
|||
}
|
||||
|
||||
// Implement com.sun.star.lang.XInitialization
|
||||
/*public void initialize( Object[] object )
|
||||
// We expect to get the current frame and a comma separated list of BibTeX files to use
|
||||
public void initialize( Object[] objects )
|
||||
throws com.sun.star.uno.Exception {
|
||||
}*/
|
||||
for (Object object : objects) {
|
||||
if (object instanceof XFrame) {
|
||||
xFrame = (XFrame) UnoRuntime.queryInterface(XFrame.class, object);
|
||||
}
|
||||
if (object instanceof String) {
|
||||
bibTeXDirectory = new File((String) object);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Implement XDialogEventHandler
|
||||
public boolean callHandlerMethod(XDialog xDialog, Object event, String sMethod) {
|
||||
if (sMethod.equals("FileChange")) {
|
||||
fileChange();
|
||||
}
|
||||
else if (sMethod.equals("EntryChange")) {
|
||||
entryChange();
|
||||
}
|
||||
else if (sMethod.equals("InsertReference")) {
|
||||
insertReference();
|
||||
}
|
||||
else if (sMethod.equals("Edit")) {
|
||||
edit();
|
||||
}
|
||||
else if (sMethod.equals("Refresh")) {
|
||||
refresh();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public String[] getSupportedMethodNames() {
|
||||
String[] sNames = { };
|
||||
String[] sNames = { "FileChange", "EntryChange", "InsertReference", "Edit", "Refresh" };
|
||||
return sNames;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// (Re)load the list of BibTeX files
|
||||
private void refresh() {
|
||||
// Remember current file selection, if any
|
||||
String sFile = null;
|
||||
short nFile = getListBoxSelectedItem("File");
|
||||
if (nFile>=0 && files[nFile]!=null) {
|
||||
sFile = getListBoxStringItemList("File")[nFile];
|
||||
}
|
||||
|
||||
if (bibTeXDirectory!=null && bibTeXDirectory.isDirectory()) {
|
||||
// Populate the file list based on the BibTeX directory
|
||||
files = bibTeXDirectory.listFiles(
|
||||
new FilenameFilter() {
|
||||
public boolean accept(File file, String sName) { return sName!=null && sName.endsWith(".bib"); }
|
||||
}
|
||||
);
|
||||
int nFileCount = files.length;
|
||||
String[] sFileNames = new String[nFileCount];
|
||||
|
||||
// Select either the first or the previous item
|
||||
nFile = 0;
|
||||
for (short i=0; i<nFileCount; i++) {
|
||||
sFileNames[i] = files[i].getName();
|
||||
if (sFileNames[i].equals(sFile)) { nFile = i; }
|
||||
}
|
||||
|
||||
setListBoxStringItemList("File", sFileNames);
|
||||
setListBoxSelectedItem("File",(short)nFile);
|
||||
|
||||
if (nFileCount>0) {
|
||||
setControlEnabled("FileLabel",true);
|
||||
setControlEnabled("File",true);
|
||||
setControlEnabled("EntryLabel",true);
|
||||
setControlEnabled("Entry",true);
|
||||
setControlEnabled("Edit",true);
|
||||
setControlEnabled("Insert",true);
|
||||
|
||||
fileChange();
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// The directory did not contain any BibTeX files
|
||||
setControlEnabled("FileLabel",false);
|
||||
setControlEnabled("File",false);
|
||||
setControlEnabled("EntryLabel",false);
|
||||
setControlEnabled("Entry",false);
|
||||
setControlEnabled("Edit",false);
|
||||
setControlEnabled("Insert",false);
|
||||
setLabelText("EntryInformation","No BibTeX files were found");
|
||||
}
|
||||
|
||||
// Update the list of entries based on the current selection in the file list
|
||||
private void fileChange() {
|
||||
// Remember current entry selection, if any
|
||||
String sEntry = null;
|
||||
short nEntry = getListBoxSelectedItem("Entry");
|
||||
if (nEntry>=0) {
|
||||
sEntry = getListBoxStringItemList("Entry")[nEntry];
|
||||
}
|
||||
|
||||
// Parse the selected file
|
||||
int nFile = getListBoxSelectedItem("File");
|
||||
if (nFile>=0) {
|
||||
try {
|
||||
currentFile = new BibTeXReader(files[nFile]);
|
||||
} catch (IOException e) {
|
||||
currentFile = null;
|
||||
} catch (ParseException e) {
|
||||
currentFile = null;
|
||||
}
|
||||
|
||||
if (currentFile!=null) {
|
||||
// Populate the entry list with the keys from the current file, if any
|
||||
String[] sCurrentKeys = currentFile.getEntries().keySet().toArray(new String[0]);
|
||||
setListBoxStringItemList("Entry", sCurrentKeys);
|
||||
if (sCurrentKeys.length>0) {
|
||||
// Select either the first or the previous entry
|
||||
nEntry = 0;
|
||||
if (sEntry!=null) {
|
||||
int nEntryCount = sCurrentKeys.length;
|
||||
for (short i=0; i<nEntryCount; i++) {
|
||||
if (sEntry.equals(sCurrentKeys[i])) {
|
||||
nEntry = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
setListBoxSelectedItem("Entry",nEntry);
|
||||
setControlEnabled("EntryLabel",true);
|
||||
setControlEnabled("Entry",true);
|
||||
setControlEnabled("Insert",true);
|
||||
entryChange();
|
||||
}
|
||||
else { // No entries, disable controls
|
||||
setControlEnabled("EntryLabel",false);
|
||||
setControlEnabled("Entry",false);
|
||||
setControlEnabled("Insert",false);
|
||||
setLabelText("EntryInformation","This file does not contain any entries");
|
||||
}
|
||||
setControlEnabled("Edit",true);
|
||||
}
|
||||
else { // Failed to parse, disable controls
|
||||
setListBoxStringItemList("Entry", new String[0]);
|
||||
setControlEnabled("EntryLabel",false);
|
||||
setControlEnabled("Entry",false);
|
||||
setControlEnabled("Edit",false);
|
||||
setControlEnabled("Insert",false);
|
||||
setLabelText("EntryInformation","There was an error reading this file");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update the entry information based on the current selection in the entry list
|
||||
private void entryChange() {
|
||||
BibMark bibMark = getCurrentEntry();
|
||||
if (bibMark!=null) {
|
||||
String sAuthor = bibMark.getField(EntryType.author);
|
||||
if (sAuthor==null) { sAuthor = ""; }
|
||||
String sTitle = bibMark.getField(EntryType.title);
|
||||
if (sTitle==null) { sTitle = ""; }
|
||||
String sPublisher = bibMark.getField(EntryType.publisher);
|
||||
if (sPublisher==null) { sPublisher = ""; }
|
||||
String sYear = bibMark.getField(EntryType.year);
|
||||
if (sYear==null) { sYear = ""; }
|
||||
setLabelText("EntryInformation", sAuthor+"\n"+sTitle+"\n"+sPublisher+"\n"+sYear);
|
||||
}
|
||||
else {
|
||||
setLabelText("EntryInformation", "No information");
|
||||
}
|
||||
}
|
||||
|
||||
// Insert the currently selected entry as a reference in the text document
|
||||
private void insertReference() {
|
||||
if (xFrame!=null) {
|
||||
MessageBox msgBox = new MessageBox(xContext, xFrame);
|
||||
msgBox.showMessage("Writer2LaTeX","This feature has not been implemented yet");
|
||||
}
|
||||
}
|
||||
|
||||
// Get the currently selected entry, or null if none is selected
|
||||
private BibMark getCurrentEntry() {
|
||||
BibMark bibMark = null;
|
||||
int nEntry = getListBoxSelectedItem("Entry");
|
||||
if (nEntry>=0) {
|
||||
String[] sCurrentKeys = getListBoxStringItemList("Entry");
|
||||
String sKey = sCurrentKeys[nEntry];
|
||||
bibMark = currentFile.getEntries().get(sKey);
|
||||
}
|
||||
return bibMark;
|
||||
}
|
||||
|
||||
// Edit the current BibTeX file
|
||||
private void edit() {
|
||||
int nFile = getListBoxSelectedItem("File");
|
||||
if (nFile>=0) {
|
||||
if (files[nFile].exists()) {
|
||||
// Open the file in the default application on this system (if any)
|
||||
if (Desktop.isDesktopSupported()) {
|
||||
Desktop desktop = Desktop.getDesktop();
|
||||
try {
|
||||
desktop.open(files[nFile]);
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
else if (xFrame!=null) {
|
||||
MessageBox msgBox = new MessageBox(xContext, xFrame);
|
||||
msgBox.showMessage("Writer2LaTeX","Error: No BibTeX editor was found");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue