From 951bcc0f85534469768c0d890c0dbc673e268958 Mon Sep 17 00:00:00 2001
From: henrikjust <henrikjust@f0f2a975-2e09-46c8-9428-3b39399b9f3c>
Date: Mon, 29 Dec 2014 12:40:53 +0000
Subject: [PATCH] BibTeX support

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@225 f0f2a975-2e09-46c8-9428-3b39399b9f3c
---
 source/distro/changelog.txt                   |   4 +-
 .../comp/w2lcommon/filter/UNOPublisher.java   |   4 +-
 .../da/comp/w2lcommon/helper/DialogBase.java  |  25 +-
 .../da/comp/writer2latex/BibTeXDialog.java    | 238 +++++++++++++++++-
 .../da/comp/writer2latex/BibTeXReader.java    |  39 ++-
 .../comp/writer2latex/LaTeXUNOPublisher.java  |  53 ++--
 .../da/comp/writer2latex/Writer2LaTeX.java    |  35 +--
 .../writer2latex/bibtex/BibTeXEntryMap.java   |   3 +-
 .../writer2latex/W4LDialogs/BibTeXEntry.xdl   |  30 ++-
 source/oxt/writer2latex/help/en/help.tree     |   1 +
 .../bibliography.xhp                          |  16 +-
 .../bibtex.xhp                                |  66 +++++
 .../menu.xhp                                  |   5 +-
 13 files changed, 445 insertions(+), 74 deletions(-)
 create mode 100644 source/oxt/writer2latex/help/en/org.openoffice.da.writer2latex.oxt/bibtex.xhp

diff --git a/source/distro/changelog.txt b/source/distro/changelog.txt
index 82e6d63..2a7da26 100644
--- a/source/distro/changelog.txt
+++ b/source/distro/changelog.txt
@@ -2,8 +2,8 @@ Changelog for Writer2LaTeX version 1.4 -> 1.6
 
 ---------- version 1.5.2 ----------
 
-[w2l] New BibTeX dialog to insert a bibliographic reference to a BibTeX file. The BibTeX files are located as defined
-  in the configuration, and the reference is inserted as an ordinary reference mark.
+[w2l] Added new BibTeX dialog to insert a bibliographic reference to a BibTeX file. The BibTeX files are located as
+  defined in the configuration, and the reference is inserted as an ordinary reference mark.
 
 [w2l] Various improvements to the log viewer dialog: Reduced height to better accommodate small screen resolutions.
   Added checkbox to filter the LaTeX log to display only errors. Added help page and long tips.
diff --git a/source/java/org/openoffice/da/comp/w2lcommon/filter/UNOPublisher.java b/source/java/org/openoffice/da/comp/w2lcommon/filter/UNOPublisher.java
index a9c0412..3f48d9a 100644
--- a/source/java/org/openoffice/da/comp/w2lcommon/filter/UNOPublisher.java
+++ b/source/java/org/openoffice/da/comp/w2lcommon/filter/UNOPublisher.java
@@ -20,7 +20,7 @@
  *
  *  All Rights Reserved.
  *  
- *  Version 1.6 (2014-11-08)
+ *  Version 1.6 (2014-12-27)
  *  
  */
 package org.openoffice.da.comp.w2lcommon.filter;
@@ -173,7 +173,7 @@ public class UNOPublisher {
         String sDocumentUrl = xModel.getURL();
         if (sDocumentUrl.length()==0) { // The document has no location
             MessageBox msgBox = new MessageBox(xContext, xFrame);
-            msgBox.showMessage(sAppName,"Please save the document before publishing the file");
+            msgBox.showMessage(sAppName,"Please save the document first");
             return false;
         }
         else if (!".odt".equals(Misc.getFileExtension(sDocumentUrl)) && !".fodt".equals(Misc.getFileExtension(sDocumentUrl)) && !".ods".equals(Misc.getFileExtension(sDocumentUrl)) && !".fods".equals(Misc.getFileExtension(sDocumentUrl))) {
diff --git a/source/java/org/openoffice/da/comp/w2lcommon/helper/DialogBase.java b/source/java/org/openoffice/da/comp/w2lcommon/helper/DialogBase.java
index 8dacc93..7495095 100644
--- a/source/java/org/openoffice/da/comp/w2lcommon/helper/DialogBase.java
+++ b/source/java/org/openoffice/da/comp/w2lcommon/helper/DialogBase.java
@@ -16,11 +16,11 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  *  MA  02111-1307  USA
  *
- *  Copyright: 2002-2011 by Henrik Just
+ *  Copyright: 2002-2014 by Henrik Just
  *
  *  All Rights Reserved.
  * 
- *  Version 1.2 (2011-02-25)
+ *  Version 1.6 (2014-12-16)
  *
  */ 
  
@@ -392,6 +392,27 @@ public abstract class DialogBase implements
         }
     }
     
+    public String getLabelText(String sControlName) {
+        XPropertySet xPropertySet = getControlProperties(sControlName);
+        try {
+            return (String) xPropertySet.getPropertyValue("Label");
+        }
+        catch (Exception e) {
+            // Will fail if the control does not exist or is not a label
+            return "";
+        }
+    }
+	
+    public void setLabelText(String sControlName, String sLabel) {
+        XPropertySet xPropertySet = getControlProperties(sControlName);
+        try {
+            xPropertySet.setPropertyValue("Label",sLabel);
+        }
+        catch (Exception e) {
+            // Will fail if the control does not exist or is not a label
+        }
+    }    
+	    
     protected String getFormattedFieldText(String sControlName) {
         XPropertySet xPropertySet = getControlProperties(sControlName);
         try {
diff --git a/source/java/org/openoffice/da/comp/writer2latex/BibTeXDialog.java b/source/java/org/openoffice/da/comp/writer2latex/BibTeXDialog.java
index 696bf54..fdf72f7 100644
--- a/source/java/org/openoffice/da/comp/writer2latex/BibTeXDialog.java
+++ b/source/java/org/openoffice/da/comp/writer2latex/BibTeXDialog.java
@@ -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");
+		        }
+		    }
+    	}
+    }
+
 }
\ No newline at end of file
diff --git a/source/java/org/openoffice/da/comp/writer2latex/BibTeXReader.java b/source/java/org/openoffice/da/comp/writer2latex/BibTeXReader.java
index d9141a4..e85d9bb 100644
--- a/source/java/org/openoffice/da/comp/writer2latex/BibTeXReader.java
+++ b/source/java/org/openoffice/da/comp/writer2latex/BibTeXReader.java
@@ -20,7 +20,7 @@
  *
  *  All Rights Reserved.
  * 
- *  Version 1.6 (2014-11-24) 
+ *  Version 1.6 (2014-12-27) 
  *
  */
 
@@ -55,18 +55,34 @@ import writer2latex.office.BibMark.EntryType;
  */
 public class BibTeXReader {
 
+	private File file;
 	private Map<String, BibMark> entries;
-
-	/**
-	 * Construct a new <code>BibTeXReader</code> based on a file
+	
+	/** Construct a new <code>BibTeXReader</code> based on a file
 	 * 
 	 * @param file the file to read
 	 * @throws IOException if any error occurs reading the file
 	 * @throws ParseException if any error occurs interpreting the contents of the file
 	 */
 	public BibTeXReader(File file) throws IOException, ParseException {
+		this.file = file;
+		reload();
+	}
+
+	/** Parse the contents of the file, replacing any previous entries in this <code>BibTeXReader</code>
+	 */
+	public void reload() throws IOException, ParseException {
+		entries = new HashMap<String, BibMark>();
 		BibTeXDatabase database = parseBibTeX(file);
-		readEntries(database);
+		readEntries(database);		
+	}
+	
+	/** Get the file associated with this <code>BibTeXReader</code>
+	 * 
+	 * @return the file
+	 */
+	public File getFile() {
+		return file;
 	}
 	
 	/** Get the entries of this BibTeX file
@@ -77,7 +93,7 @@ public class BibTeXReader {
 		return entries;
 	}
 
-	private static BibTeXDatabase parseBibTeX(File file) throws IOException, ParseException {
+	private static BibTeXDatabase parseBibTeX(File file) throws ParseException, IOException {
 		Reader reader = new FileReader(file);
 		try {
 			BibTeXParser parser = new BibTeXParser() {
@@ -102,8 +118,7 @@ public class BibTeXReader {
 		}
 	}
 
-	private void readEntries(BibTeXDatabase database) throws IOException {
-		entries = new HashMap<String, BibMark>();
+	private void readEntries(BibTeXDatabase database) {
 		Map<Key, BibTeXEntry> entryMap = database.getEntries();
 
 		Collection<BibTeXEntry> bibentries = entryMap.values();
@@ -124,7 +139,7 @@ public class BibTeXReader {
 		}	
 	}
 
-	private static String parseLaTeX(String string) throws IOException {
+	private static String parseLaTeX(String string) {
 		Reader reader = new StringReader(string);
 		try {
 			LaTeXParser parser = new LaTeXParser();
@@ -134,7 +149,11 @@ public class BibTeXReader {
 			// If parsing fails, return the original string
 			return string;
 		} finally {
-			reader.close();
+			try {
+				reader.close();
+			} catch (IOException e) {
+				// Reading from a String will not fail :-)
+			}
 		}
 	}
 
diff --git a/source/java/org/openoffice/da/comp/writer2latex/LaTeXUNOPublisher.java b/source/java/org/openoffice/da/comp/writer2latex/LaTeXUNOPublisher.java
index 56f5adb..1cfdc4c 100644
--- a/source/java/org/openoffice/da/comp/writer2latex/LaTeXUNOPublisher.java
+++ b/source/java/org/openoffice/da/comp/writer2latex/LaTeXUNOPublisher.java
@@ -20,7 +20,7 @@
  *
  *  All Rights Reserved.
  *  
- *  Version 1.6 (2014-11-03)
+ *  Version 1.6 (2014-12-27)
  *  
  */
 package org.openoffice.da.comp.writer2latex;
@@ -55,7 +55,26 @@ public class LaTeXUNOPublisher extends UNOPublisher {
     	super(xContext, xFrame, sAppName);
     }
     
-    /** Make a file name LaTeX friendly 
+    /** Get the directory containing the BibTeX files (as defined in the registry)
+     * 
+     * @return the directory
+     */
+    public File getBibTeXDirectory() {
+        // Get the BibTeX settings from the registry
+    	RegistryHelper registry = new RegistryHelper(xContext);
+		Object view;
+		try {
+			view = registry.getRegistryView(BibliographyDialog.REGISTRY_PATH, false);
+		} catch (Exception e) {
+			// Failed to get registry settings
+			return null;
+		}
+		XPropertySet xProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,view);
+		return getDirectory(XPropertySetHelper.getPropertyValueAsShort(xProps, "BibTeXLocation"),
+				XPropertySetHelper.getPropertyValueAsString(xProps, "BibTeXDir"));
+    }
+    
+    /** Make a file name LaTeX friendly
      */
     @Override protected String filterFileName(String sFileName) {
         String sResult = "";
@@ -229,31 +248,35 @@ public class LaTeXUNOPublisher extends UNOPublisher {
         }
     }
     
-    private String getFileList(short nType, String sDirectory) {
-    	File dir;
+    private File getDirectory(short nType, String sDirectory) {
     	switch (nType) {
     	case 0: // absolute path
-        	dir = new File(sDirectory);
-        	break;
+        	return new File(sDirectory);
     	case 1: // relative path
-    		dir = new File(Misc.urlToFile(getTargetPath()),sDirectory);
-    		break;
+    		return new File(Misc.urlToFile(getTargetPath()),sDirectory);
     	default: // document directory
-    		dir = Misc.urlToFile(getTargetPath());
+    		return Misc.urlToFile(getTargetPath());
     	}
-    	
-    	CSVList filelist = new CSVList(",");
+    }
+
+    private String getFileList(short nType, String sDirectory) {
+    	File dir = getDirectory(nType,sDirectory);
+    	File[] files;
     	if (dir.isDirectory()) {
-    		File[] files = dir.listFiles();
+    		files = dir.listFiles();
+    	}
+    	else {
+    		return null;
+    	}
+    	CSVList filelist = new CSVList(",");
+    	if (files!=null) {
     		for (File file : files) {
     			if (file.isFile() && file.getName().endsWith(".bib")) {
-    				//filelist.addValue(file.getAbsolutePath());
     				filelist.addValue(Misc.removeExtension(file.getName()));
     			}
     		}
     	}
-    	String sFileList = filelist.toString();
-    	return sFileList.length()>0 ? sFileList : "dummy";
+    	return filelist.toString();
 	}
 
 }
diff --git a/source/java/org/openoffice/da/comp/writer2latex/Writer2LaTeX.java b/source/java/org/openoffice/da/comp/writer2latex/Writer2LaTeX.java
index 97e1b45..c1472c6 100644
--- a/source/java/org/openoffice/da/comp/writer2latex/Writer2LaTeX.java
+++ b/source/java/org/openoffice/da/comp/writer2latex/Writer2LaTeX.java
@@ -20,7 +20,7 @@
  *
  *  All Rights Reserved.
  * 
- *  Version 1.6 (2014-12-11)
+ *  Version 1.6 (2014-12-27)
  *
  */ 
  
@@ -175,19 +175,26 @@ public final class Writer2LaTeX extends WeakBase
     }
 	
 	private void insertBibTeX() {
-		// Execute the BibTeX dialog
-        try {
-            Object dialog = m_xContext.getServiceManager()
-                .createInstanceWithContext(
-                "org.openoffice.da.writer2latex.BibTeXDialog", m_xContext);
-            XExecutableDialog xDialog = (XExecutableDialog)
-                UnoRuntime.queryInterface(XExecutableDialog.class, dialog);
-            if (xDialog.execute()==ExecutableDialogResults.OK) {
-                // Closed with the close button
-            }
-        }
-        catch (com.sun.star.uno.Exception e) {
-        }
+		createUNOPublisher();
+    	if (unoPublisher.documentSaved()) {
+			// Execute the BibTeX dialog
+	        try {
+	        	// The dialog needs the current frame and the path to the BibTeX directory
+	            Object[] args = new Object[2];
+	            args[0] = m_xFrame;
+	            args[1] = unoPublisher.getBibTeXDirectory().getPath();
+	            Object dialog = m_xContext.getServiceManager()
+	                    .createInstanceWithArgumentsAndContext(
+	                    "org.openoffice.da.writer2latex.BibTeXDialog", args, m_xContext);
+	            XExecutableDialog xDialog = (XExecutableDialog)
+	                UnoRuntime.queryInterface(XExecutableDialog.class, dialog);
+	            if (xDialog.execute()==ExecutableDialogResults.OK) {
+	                // Closed with the close button
+	            }
+	        }
+	        catch (com.sun.star.uno.Exception e) {
+	        }
+	    }
 	}
 	
 	private void createUNOPublisher() {
diff --git a/source/java/writer2latex/bibtex/BibTeXEntryMap.java b/source/java/writer2latex/bibtex/BibTeXEntryMap.java
index e3ebf28..383c2d8 100644
--- a/source/java/writer2latex/bibtex/BibTeXEntryMap.java
+++ b/source/java/writer2latex/bibtex/BibTeXEntryMap.java
@@ -20,7 +20,7 @@
  *
  *  All Rights Reserved.
  * 
- *  Version 1.6 (2014-11-28)
+ *  Version 1.6 (2014-12-16)
  *
  */
 package writer2latex.bibtex;
@@ -104,6 +104,7 @@ public class BibTeXEntryMap {
 		if (bibTeXFields == null) {
 			createMaps();
 		}
+		sFieldName = sFieldName.toLowerCase();
 		return entryTypes.containsKey(sFieldName) ? entryTypes.get(sFieldName) : null;
 	}
 }
diff --git a/source/oxt/writer2latex/W4LDialogs/BibTeXEntry.xdl b/source/oxt/writer2latex/W4LDialogs/BibTeXEntry.xdl
index aff8a93..1857c85 100644
--- a/source/oxt/writer2latex/W4LDialogs/BibTeXEntry.xdl
+++ b/source/oxt/writer2latex/W4LDialogs/BibTeXEntry.xdl
@@ -1,14 +1,26 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE dlg:window PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "dialog.dtd">
-<dlg:window xmlns:dlg="http://openoffice.org/2000/dialog" xmlns:script="http://openoffice.org/2000/script" dlg:id="BibTeXEntry" dlg:left="117" dlg:top="53" dlg:width="210" dlg:height="100" dlg:closeable="true" dlg:moveable="true" dlg:title="Insert BibTeX reference">
+<dlg:window xmlns:dlg="http://openoffice.org/2000/dialog" xmlns:script="http://openoffice.org/2000/script" dlg:id="BibTeXEntry" dlg:left="117" dlg:top="53" dlg:width="250" dlg:height="100" dlg:closeable="true" dlg:moveable="true" dlg:title="Insert BibTeX reference">
  <dlg:bulletinboard>
-  <dlg:menulist dlg:id="File" dlg:tab-index="0" dlg:left="80" dlg:top="10" dlg:width="120" dlg:height="12" dlg:spin="true"/>
-  <dlg:text dlg:id="FileLabel" dlg:tab-index="3" dlg:left="10" dlg:top="10" dlg:width="60" dlg:height="12" dlg:value="BibTeX file"/>
-  <dlg:text dlg:id="EntryLabel" dlg:tab-index="4" dlg:left="10" dlg:top="24" dlg:width="60" dlg:height="12" dlg:value="BibTeX entry"/>
-  <dlg:menulist dlg:id="Entry" dlg:tab-index="1" dlg:left="80" dlg:top="24" dlg:width="120" dlg:height="12" dlg:spin="true"/>
-  <dlg:text dlg:id="EntryInformation" dlg:tab-index="5" dlg:left="10" dlg:top="40" dlg:width="170" dlg:height="34"/>
-  <dlg:button dlg:id="Insert" dlg:tab-index="2" dlg:left="10" dlg:top="78" dlg:width="60" dlg:height="12" dlg:value="Insert reference" dlg:button-type="ok"/>
-  <dlg:button dlg:id="Cancel" dlg:tab-index="6" dlg:left="75" dlg:top="78" dlg:width="60" dlg:height="12" dlg:value="Cancel" dlg:button-type="cancel"/>
-  <dlg:button dlg:id="Help" dlg:tab-index="7" dlg:left="140" dlg:top="78" dlg:width="60" dlg:height="12" dlg:value="Help" dlg:button-type="help"/>
+  <dlg:text dlg:id="FileLabel" dlg:tab-index="0" dlg:left="10" dlg:top="10" dlg:width="60" dlg:height="12" dlg:value="BibTeX file"/>
+  <dlg:menulist dlg:id="File" dlg:tab-index="1" dlg:left="75" dlg:top="10" dlg:width="95" dlg:height="12" dlg:spin="true" dlg:help-url="org.openoffice.da.writer2latex.oxt:BibTeXFile">
+    <script:event script:event-name="on-itemstatechange" script:macro-name="vnd.sun.star.UNO:FileChange" script:language="UNO"/>
+  </dlg:menulist>
+  <dlg:button dlg:id="Edit" dlg:tab-index="2" dlg:left="180" dlg:top="10" dlg:width="60" dlg:height="12" dlg:value="Edit..." dlg:help-url="org.openoffice.da.writer2latex.oxt:BibTeXEdit">
+    <script:event script:event-name="on-mouseup" script:macro-name="vnd.sun.star.UNO:Edit" script:language="UNO"/>
+  </dlg:button>
+  <dlg:text dlg:id="EntryLabel" dlg:tab-index="3" dlg:left="10" dlg:top="24" dlg:width="60" dlg:height="12" dlg:value="BibTeX entry"/>
+  <dlg:menulist dlg:id="Entry" dlg:tab-index="4" dlg:left="75" dlg:top="24" dlg:width="95" dlg:height="12" dlg:spin="true" dlg:help-url="org.openoffice.da.writer2latex.oxt:BibTeXEntry">
+    <script:event script:event-name="on-itemstatechange" script:macro-name="vnd.sun.star.UNO:EntryChange" script:language="UNO"/>
+  </dlg:menulist>
+  <dlg:button dlg:id="Refresh" dlg:tab-index="5" dlg:left="180" dlg:top="24" dlg:width="60" dlg:height="12" dlg:value="Refresh" dlg:help-url="org.openoffice.da.writer2latex.oxt:BibTeXRefresh">
+    <script:event script:event-name="on-mouseup" script:macro-name="vnd.sun.star.UNO:Refresh" script:language="UNO"/>
+  </dlg:button>
+  <dlg:text dlg:id="EntryInformation" dlg:tab-index="6" dlg:left="10" dlg:top="40" dlg:width="210" dlg:height="34"/>
+  <dlg:button dlg:id="Insert" dlg:tab-index="7" dlg:left="10" dlg:top="78" dlg:width="60" dlg:height="12" dlg:value="Insert reference" dlg:button-type="ok" dlg:help-url="org.openoffice.da.writer2latex.oxt:BibTeXInsert">
+    <script:event script:event-name="on-mouseup" script:macro-name="vnd.sun.star.UNO:InsertReference" script:language="UNO"/>
+  </dlg:button>
+  <dlg:button dlg:id="Cancel" dlg:tab-index="8" dlg:left="75" dlg:top="78" dlg:width="60" dlg:height="12" dlg:value="Cancel" dlg:button-type="cancel"  dlg:help-url="org.openoffice.da.writer2latex.oxt:BibTeXCancel"/>
+  <dlg:button dlg:id="Help" dlg:tab-index="9" dlg:left="180" dlg:top="78" dlg:width="60" dlg:height="12" dlg:value="Help" dlg:button-type="help" dlg:help-url="org.openoffice.da.writer2latex.oxt:BibTeXDialog"/>
  </dlg:bulletinboard>
 </dlg:window>
\ No newline at end of file
diff --git a/source/oxt/writer2latex/help/en/help.tree b/source/oxt/writer2latex/help/en/help.tree
index a1548af..179ed87 100644
--- a/source/oxt/writer2latex/help/en/help.tree
+++ b/source/oxt/writer2latex/help/en/help.tree
@@ -3,6 +3,7 @@
   <help_section application="writer2latex" id="w2l01_writer" title="Writer2LaTeX">
       <topic id="writer2latex/org.openoffice.da.writer2latex.oxt/introduction.xhp">Introduction</topic>
       <topic id="writer2latex/org.openoffice.da.writer2latex.oxt/menu.xhp">Menu and toolbar</topic>
+      <topic id="writer2latex/org.openoffice.da.writer2latex.oxt/bibtex.xhp">BibTeX references</topic>
       <topic id="writer2latex/org.openoffice.da.writer2latex.oxt/export.xhp">LaTeX export</topic>
       <topic id="writer2latex/org.openoffice.da.writer2latex.oxt/logviewer.xhp">View log files</topic>
       <!--<topic id="writer2latex/org.openoffice.da.writer2latex.oxt/import.xhp">LaTeX import</topic>-->
diff --git a/source/oxt/writer2latex/help/en/org.openoffice.da.writer2latex.oxt/bibliography.xhp b/source/oxt/writer2latex/help/en/org.openoffice.da.writer2latex.oxt/bibliography.xhp
index 54732f9..8153364 100644
--- a/source/oxt/writer2latex/help/en/org.openoffice.da.writer2latex.oxt/bibliography.xhp
+++ b/source/oxt/writer2latex/help/en/org.openoffice.da.writer2latex.oxt/bibliography.xhp
@@ -20,18 +20,20 @@
 		If you use the standard bibliographic tool provided by %PRODUCTNAME, Writer2LaTeX can convert the references to BibTeX if you
 		choose to do so in the export dialog. In this case you should not make any settings on this page.</paragraph>
 		
+		<paragraph role="paragraph" xml-lang="en-US">You may also use the Writer2LaTeX toolbar to insert bibliographic references from
+		BibTeX files. In this case Writer2LaTeX can be configured to use the original BibTeX files.
+		To use this feature, you have to define the location of your BibTeX files.</paragraph>
+		
 		<paragraph role="paragraph" xml-lang="en-US">You may also use other tools such as Zotero or JabRef to handle your citations.
 		If you want to use BibTeX to format references inserted by these tools, Writer2LaTeX can convert them for you. In this case
 		you have to define the location of the BibTeX files produced by Zotero or JabRef.</paragraph>
 
 		<bookmark xml-lang="en-US" branch="hid/org.openoffice.da.writer2latex.oxt:BibliographyUseExternalBibTeXFiles" id="bm_useexternalbibtexfiles"/>
 		<paragraph role="heading" level="3" xml-lang="en-US">Use external BibTeX files for ordinary citations</paragraph>
-		<paragraph role="paragraph" xml-lang="en-US">Check this if the citations in your document refers to one or more external BibTeX files.
-		(The keys used in the document must match up with the keys in the external BibTeX files.)</paragraph>
-		<paragraph role="paragraph" xml-lang="en-US">This feature is for advanced users that maintain their references in BibTeX.
-		It enables you to use the original BibTeX files in the LaTeX document rather than converting the inserted
-		references back into BibTeX.</paragraph>
-		<paragraph role="paragraph" xml-lang="en-US"><ahelp hid="org.openoffice.da.writer2latex.oxt:BibliographyUseExternalBibTeXFiles" visibility="hidden">Check this if your citations refer to external BibTeX file(s)</ahelp></paragraph>
+		<paragraph role="paragraph" xml-lang="en-US">Check this if you use the Writer2LaTeX toolbar to insert bibliographic references from BibTeX files.</paragraph>
+		<paragraph role="paragraph" xml-lang="en-US">This enables you to use the original BibTeX files in the LaTeX document rather than converting the
+		inserted references back into BibTeX.</paragraph>
+		<paragraph role="paragraph" xml-lang="en-US"><ahelp hid="org.openoffice.da.writer2latex.oxt:BibliographyUseExternalBibTeXFiles" visibility="hidden">Check this if your citatations are inserted from BibTeX file(s)</ahelp></paragraph>
 
 		<bookmark xml-lang="en-US" branch="hid/org.openoffice.da.writer2latex.oxt:BibliographyConvertZoteroCitations" id="bm_convertzoterocitations"/>
 		<paragraph role="heading" level="3" xml-lang="en-US">Convert Zotero citations</paragraph>
@@ -122,7 +124,5 @@
 		<paragraph role="paragraph" xml-lang="en-US">Enter any options to the package natbib.sty here.</paragraph>
 		<paragraph role="paragraph" xml-lang="en-US"><ahelp hid="org.openoffice.da.writer2latex.oxt:BibliographyNatbibOptions" visibility="hidden">Enter any options to the package natbib.sty here</ahelp></paragraph>
 
-		<paragraph role="paragraph" xml-lang="en-US"></paragraph>
-
 	</body>
 </helpdocument>
\ No newline at end of file
diff --git a/source/oxt/writer2latex/help/en/org.openoffice.da.writer2latex.oxt/bibtex.xhp b/source/oxt/writer2latex/help/en/org.openoffice.da.writer2latex.oxt/bibtex.xhp
new file mode 100644
index 0000000..723e8af
--- /dev/null
+++ b/source/oxt/writer2latex/help/en/org.openoffice.da.writer2latex.oxt/bibtex.xhp
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<helpdocument version="1.0">
+	<meta>
+        <topic id="writer2latex-bibtex" indexer="include">
+            <title xml-lang="en-US">Insert BibTeX reference</title>
+            <filename>org.openoffice.da.writer2latex.oxt/bibtex.xhp</filename>
+        </topic>
+    </meta>
+    <body>
+    	<bookmark xml-lang="en-US" branch="hid/org.openoffice.da.writer2latex.oxt:BibTeXDialog" id="bm_bibtexdialog"/>
+        <paragraph role="heading" level="1" xml-lang="en-US">Insert BibTeX reference</paragraph>
+        <paragraph role="paragraph" xml-lang="en-US">Insert a bibliographic reference to an item in a BibTeX file.</paragraph>
+        <section id="howtoget" xml-lang="en-US">
+        	Click the <emph>Bib</emph>-button on the toolbar
+        </section>
+        
+        <paragraph role="paragraph" xml-lang="en-US">BibTeX is the standard database format for bibliographic references in LaTeX.
+        This is used to insert bibliographic references in %PRODUCTNAME Writer from BibTeX files.
+        This enables you to maintain your references in BibTeX, rather that using the biblipography database in %PRODUCTNAME Writer.
+        </paragraph>
+        
+        <paragraph role="paragraph" xml-lang="en-US">The dialog presents a list of BibTeX files from a specific folder.
+        The folder to search is configured in the <link href="org.openoffice.da.writer2latex.oxt/bibliography.xhp"
+          name="Bibliography settings">bibliography settings</link>.</paragraph>
+       
+        <paragraph role="heading" level="2" xml-lang="en-US">Inserting BibTeX references</paragraph>
+        <paragraph role="paragraph" xml-lang="en-US">To insert a bibliographic reference, select an item and
+        click the <emph>Insert reference</emph> button.</paragraph>
+        
+		<bookmark xml-lang="en-US" branch="hid/org.openoffice.da.writer2latex.oxt:BibTeXFile" id="bm_bibtexfile"/>
+        <paragraph role="heading" level="3" xml-lang="en-US">File</paragraph>
+        <paragraph role="paragraph" xml-lang="en-US">Select the BibTeX file to use in this list.</paragraph>
+        <paragraph role="paragraph" xml-lang="en-US"><ahelp hid="org.openoffice.da.writer2latex.oxt:BibTeXFile" visibility="hidden">Select the BibTeX file to use</ahelp></paragraph>
+                
+		<bookmark xml-lang="en-US" branch="hid/org.openoffice.da.writer2latex.oxt:BibTeXEntry" id="bm_bibtexentry"/>
+        <paragraph role="heading" level="3" xml-lang="en-US">Entry</paragraph>
+        <paragraph role="paragraph" xml-lang="en-US">Select the entry to use in this list. The list contains the keys of all entries
+        in the selected BibTeX file.
+        Basic information about the entry is displayed in the field below the list.</paragraph>
+        <paragraph role="paragraph" xml-lang="en-US"><ahelp hid="org.openoffice.da.writer2latex.oxt:BibTeXEntry" visibility="hidden">Select the entry to use</ahelp></paragraph>
+        
+		<bookmark xml-lang="en-US" branch="hid/org.openoffice.da.writer2latex.oxt:BibTeXInsert" id="bm_bibtexinsert"/>
+        <paragraph role="heading" level="3" xml-lang="en-US">Insert reference</paragraph>
+        <paragraph role="paragraph" xml-lang="en-US">Click this button to insert a bibliographic reference to
+        the currently selected entry.</paragraph>
+        <paragraph role="paragraph" xml-lang="en-US"><ahelp hid="org.openoffice.da.writer2latex.oxt:BibTeXInsert" visibility="hidden">Click to insert a reference to the current entry</ahelp></paragraph>
+
+		<bookmark xml-lang="en-US" branch="hid/org.openoffice.da.writer2latex.oxt:BibTeXCancel" id="bm_bibtexcancel"/>
+        <paragraph role="heading" level="3" xml-lang="en-US">Cancel</paragraph>
+        <paragraph role="paragraph" xml-lang="en-US">Click this button to close the dialog without inserting any references.</paragraph>
+        <paragraph role="paragraph" xml-lang="en-US"><ahelp hid="org.openoffice.da.writer2latex.oxt:BibTeXCancel" visibility="hidden">Click to close the dialog</ahelp></paragraph>
+
+		<bookmark xml-lang="en-US" branch="hid/org.openoffice.da.writer2latex.oxt:BibTeXEdit" id="bm_bibtexedit"/>
+        <paragraph role="heading" level="3" xml-lang="en-US">Edit...</paragraph>
+        <paragraph role="paragraph" xml-lang="en-US">Click this button to edit the currently selected BibTeX file.
+        The file will open in your default BibTeX editor. If no default BibTeX editor was found on your system,
+        an error message is displayed.</paragraph>
+        <paragraph role="paragraph" xml-lang="en-US"><ahelp hid="org.openoffice.da.writer2latex.oxt:BibTeXEdit" visibility="hidden">Click to edit the current file in your BibTeX editor</ahelp></paragraph>
+        
+		<bookmark xml-lang="en-US" branch="hid/org.openoffice.da.writer2latex.oxt:BibTeXRefresh" id="bm_bibtexrefresh"/>
+        <paragraph role="heading" level="3" xml-lang="en-US">Refresh</paragraph>
+        <paragraph role="paragraph" xml-lang="en-US">Click this button to reload the BibTeX
+        files. You should do this if you have edited a BibTeX file.</paragraph>
+        <paragraph role="paragraph" xml-lang="en-US"><ahelp hid="org.openoffice.da.writer2latex.oxt:BibTeXRefresh" visibility="hidden">Click to refresh the contents of the lists</ahelp></paragraph>
+    </body>      
+</helpdocument>
\ No newline at end of file
diff --git a/source/oxt/writer2latex/help/en/org.openoffice.da.writer2latex.oxt/menu.xhp b/source/oxt/writer2latex/help/en/org.openoffice.da.writer2latex.oxt/menu.xhp
index f352e8f..a541884 100644
--- a/source/oxt/writer2latex/help/en/org.openoffice.da.writer2latex.oxt/menu.xhp
+++ b/source/oxt/writer2latex/help/en/org.openoffice.da.writer2latex.oxt/menu.xhp
@@ -32,10 +32,11 @@
         <paragraph role="paragraph" xml-lang="en-US">Writer2LaTeX adds a toolbar in Writer,
         providing the following commands.</paragraph>
 
-        <paragraph role="heading" level="3" xml-lang="en-US">Insert BibTeX reference</paragraph>
+        <paragraph role="heading" level="3" xml-lang="en-US"><link href="org.openoffice.da.writer2latex.oxt/bibtex.xhp"
+          name="Insert BibTeX reference">Insert BibTeX reference</link></paragraph>
 
 		<bookmark xml-lang="en-US" branch="hid/org.openoffice.da.writer2latex:InsertBibTeX" id="bm_insertbibtex"/>
-        <paragraph role="paragraph" xml-lang="en-US">Insert a reference to an item in a BibTeX file.</paragraph>
+        <paragraph role="paragraph" xml-lang="en-US">Insert a bibliographic reference to an item in a BibTeX file.</paragraph>
 		<paragraph role="paragraph" xml-lang="en-US"><ahelp hid="org.openoffice.da.writer2latex:InsertBibTeX" visibility="hidden">Insert reference to an item in a BibTeX file</ahelp></paragraph>
 
         <paragraph role="heading" level="3" xml-lang="en-US"><link href="org.openoffice.da.writer2latex.oxt/export.xhp"