/plugins/LaTeXTools/tags/Plugin-release-0_2_0/src/uk/co/antroy/latextools/BibTeXPanel.java

# · Java · 427 lines · 262 code · 114 blank · 51 comment · 45 complexity · bfbeb6f270332f49634112b9676527cb MD5 · raw file

  1. /*:folding=indent:
  2. * BibTeXPanel.java - BibTeX Dialog
  3. * Copyright (C) 2002 Anthony Roy
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package uk.co.antroy.latextools;
  20. import gnu.regexp.RE;
  21. import gnu.regexp.REException;
  22. import gnu.regexp.REMatch;
  23. import java.awt.BorderLayout;
  24. import java.awt.Dimension;
  25. import java.awt.event.ActionListener;
  26. import java.awt.event.MouseAdapter;
  27. import java.awt.event.MouseEvent;
  28. import java.io.BufferedReader;
  29. import java.io.File;
  30. import java.io.FileReader;
  31. import java.util.ArrayList;
  32. import java.util.Collections;
  33. import java.util.Iterator;
  34. import java.util.StringTokenizer;
  35. import javax.swing.JList;
  36. import javax.swing.JOptionPane;
  37. import javax.swing.JScrollPane;
  38. import org.gjt.sp.jedit.Buffer;
  39. import org.gjt.sp.jedit.View;
  40. import org.gjt.sp.jedit.gui.EnhancedDialog;
  41. import org.gjt.sp.jedit.jEdit;
  42. public class BibTeXPanel
  43. extends DefaultToolPanel {
  44. //~ Instance/static variables ...............................................
  45. private ArrayList bibEntries = new ArrayList();
  46. private ArrayList bibFiles = new ArrayList();
  47. // private JTable bibTable;
  48. private JList bibList = new JList();
  49. private ActionListener insert;
  50. //~ Constructors ............................................................
  51. /**
  52. * Creates a new BibTeXPanel object.
  53. *
  54. * @param view the current view
  55. * @param buff the active buffer
  56. */
  57. public BibTeXPanel(View view, Buffer buff) {
  58. super(view, buff, "Bib");
  59. refresh();
  60. }
  61. //~ Methods .................................................................
  62. /**
  63. * ¤
  64. *
  65. * @param view ¤
  66. * @param buff ¤
  67. */
  68. public static void createBibTeXDialog(View view, Buffer buff) {
  69. final BibTeXPanel n = new BibTeXPanel(view, buff);
  70. EnhancedDialog ed = new EnhancedDialog(view, "Insert Citation", false) {
  71. public void cancel() {
  72. this.hide();
  73. }
  74. public void ok() {
  75. n.insert();
  76. this.hide();
  77. }
  78. };
  79. ed.setContentPane(n);
  80. ed.pack();
  81. ed.show();
  82. }
  83. /**
  84. * ¤
  85. */
  86. public void refresh() {
  87. if (bufferChanged) {
  88. removeAll();
  89. bufferChanged = false;
  90. }
  91. if (!isTeXFile(buffer)) {
  92. displayNotTeX(BorderLayout.CENTER);
  93. } else {
  94. loadBibFiles();
  95. buildList();
  96. JScrollPane scp = new JScrollPane(bibList,
  97. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  98. JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  99. setLayout(new BorderLayout());
  100. setPreferredSize(new Dimension(400, 400));
  101. add(scp, BorderLayout.CENTER);
  102. add(createButtonPanel(REFRESH), BorderLayout.SOUTH);
  103. }
  104. repaint();
  105. }
  106. private void buildList() {
  107. Object[] be = bibEntries.toArray();
  108. bibList.setListData(be);// = new JList(be);
  109. bibList.addMouseListener(new MouseAdapter() {
  110. public void mouseClicked(MouseEvent e) {
  111. if (e.getClickCount() == 2) {
  112. insert();
  113. }
  114. }
  115. });
  116. }
  117. private void insert() {
  118. Object[] sels = bibList.getSelectedValues();
  119. if (sels.length == 0) {
  120. System.err.println("Code should have returned by now!");
  121. return;
  122. }
  123. BibEntry bi = (BibEntry) sels[0];
  124. StringBuffer sb = new StringBuffer(bi.getRef());
  125. for (int i = 1; i < sels.length; i++) {
  126. bi = (BibEntry) sels[i];
  127. sb.append(",");
  128. sb.append(bi.getRef());
  129. }
  130. if (jEdit.getBooleanProperty("bibtex.inserttags")) {
  131. sb.insert(0, "\\cite{");
  132. sb.append("}");
  133. }
  134. int posn = view.getTextArea().getCaretPosition();
  135. buffer.insert(posn, sb.toString());
  136. }
  137. private void loadBibEntries() {
  138. RE refRe;
  139. RE titleRe;
  140. try {
  141. refRe = new RE("@\\w+?\\{([a-zA-Z0-9]+?),");
  142. titleRe = new RE("title\\s*=\\s*\\{(.+?)\\}");
  143. } catch (REException e) {
  144. return;
  145. }
  146. Iterator it = bibFiles.iterator();
  147. File bib;
  148. while (it.hasNext()) {
  149. bib = (File) it.next();
  150. //log(0);
  151. try {
  152. // log(1);
  153. // log("Bib: " + bib.toString());
  154. FileReader reader = new FileReader(bib);
  155. // log(2);
  156. BufferedReader in = new BufferedReader(reader);
  157. // log(3);
  158. String nextLine = in.readLine();
  159. // log("nextLine=" + nextLine);
  160. boolean newEntry = false;
  161. int count = 0;
  162. String bibref = "";
  163. while (nextLine != null) {
  164. if (newEntry) {
  165. REMatch rm = titleRe.getMatch(nextLine);
  166. if (rm != null) {
  167. newEntry = false;
  168. // log("entry: " + rm.toString(1));
  169. BibEntry bibEntry = new BibEntry(bibref.trim(),
  170. rm.toString(1).trim());
  171. bibEntries.add(bibEntry);
  172. }
  173. } else {
  174. REMatch rm = refRe.getMatch(nextLine);
  175. if (rm != null) {
  176. count++;
  177. newEntry = true;
  178. bibref = rm.toString(1);
  179. int l = bibref.length();
  180. }
  181. }
  182. nextLine = in.readLine();
  183. }
  184. } catch (Exception e) {
  185. }
  186. }
  187. //log("size"+bibEntries.size());
  188. Collections.sort(bibEntries);
  189. }
  190. private void loadBibFiles() {
  191. bibEntries.clear();
  192. bibFiles.clear();
  193. File texFile = new File(tex);
  194. for (int i = buffer.getLineCount() - 1; i > 0; i--) {
  195. String nextLine = buffer.getLineText(i);
  196. if (nextLine.indexOf("%Bibliography") != -1 ||
  197. nextLine.indexOf("\\begin{document}") != -1) {
  198. break;
  199. }
  200. int index = nextLine.indexOf("\\bibliography{");
  201. int tbindex = nextLine.indexOf("\\begin{thebibliography}");
  202. if (index >= 0) {
  203. StringBuffer sb = new StringBuffer("");
  204. int start = index + 14;
  205. int end = nextLine.indexOf("}", start);
  206. while (end == -1) {
  207. sb.append(nextLine.substring(start));
  208. nextLine = buffer.getLineText(++i);
  209. end = nextLine.indexOf("}");
  210. }
  211. sb.append(nextLine.substring(start, end));
  212. StringTokenizer st = new StringTokenizer(sb.toString(), ",");
  213. //Add referenced bibtex files to bibFiles list.
  214. while (st.hasMoreTokens()) {
  215. String s = st.nextToken().trim();
  216. if (!s.endsWith(".bib"))
  217. s = s + ".bib";
  218. File f = new File(texFile.getParentFile(), s);
  219. //log(f.toString());
  220. if (!f.exists())
  221. f = new File(s);
  222. bibFiles.add(f);
  223. }
  224. loadBibEntries();
  225. return;
  226. } else if (tbindex >= 0) {
  227. loadTheBibliography(i);
  228. }
  229. }
  230. }
  231. private void loadTheBibliography(int startIndex) {
  232. int index = startIndex;
  233. int refStart;
  234. int end = buffer.getLineText(index).indexOf("\\end{thebibliography}");
  235. while (end == -1) {
  236. String line = buffer.getLineText(index);
  237. refStart = line.indexOf("\\bibitem{");
  238. if (refStart != -1) {
  239. int refEnd = line.indexOf("}");
  240. String bibRef = line.substring(refStart + 9, refEnd);
  241. BibEntry be = new BibEntry(bibRef.trim(), "");
  242. bibEntries.add(be);
  243. }
  244. end = buffer.getLineText(++index).indexOf("\\end{thebibliography}");
  245. }
  246. Collections.sort(bibEntries);
  247. }
  248. private void popup(String s) {
  249. JOptionPane.showMessageDialog(view, s);
  250. }
  251. private void popup(int s) {
  252. JOptionPane.showMessageDialog(view, "" + s);
  253. }
  254. private void popup() {
  255. popup("Green Eggs and Ham");
  256. }
  257. //~ Inner classes ...........................................................
  258. private class BibEntry
  259. implements Comparable {
  260. //~ Instance/static variables .............................................
  261. private String ref;
  262. private String title;
  263. //~ Constructors ..........................................................
  264. public BibEntry() {
  265. this("", "");
  266. }
  267. public BibEntry(String r, String t) {
  268. ref = r;
  269. setTitle(t);
  270. }
  271. //~ Methods ...............................................................
  272. public void setRef(String s) {
  273. this.ref = s;
  274. }
  275. public String getRef() {
  276. return ref;
  277. }
  278. public void setTitle(String s) {
  279. int bibLength = jEdit.getIntegerProperty("bibtex.bibtitle.wordlength", 0);
  280. int bibCount = jEdit.getIntegerProperty("bibtex.bibtitle.wordcount", 0);
  281. StringTokenizer st = new StringTokenizer(s, " ");
  282. StringBuffer sb = new StringBuffer("");
  283. int count = bibCount;
  284. int length = st.countTokens();
  285. if (count == 0) {
  286. count = length;
  287. }
  288. while (st.hasMoreTokens() && count > 0) {
  289. count--;
  290. String ss = st.nextToken();
  291. if (bibLength != 0 && ss.length() > bibLength)
  292. ss = ss.substring(0, bibLength) + ".";
  293. sb.append(" " + ss);
  294. }
  295. if (bibCount != 0 && length > bibCount) {
  296. sb.append("...");
  297. }
  298. this.title = sb.toString();
  299. }
  300. public String getTitle() {
  301. return title;
  302. }
  303. public int compareTo(BibEntry be) {
  304. return ref.compareTo(be.getRef());
  305. }
  306. public int compareTo(Object o) {
  307. return compareTo((BibEntry) o);
  308. }
  309. public String toString() {
  310. return ref + " : " + title;
  311. }
  312. }
  313. }