/plugins/LaTeXTools/tags/plugin_release_0-5-3/uk/co/antroy/latextools/BibTeXPanel.java

# · Java · 166 lines · 98 code · 32 blank · 36 comment · 10 complexity · e3f42aa6ad7d72c4603b8025f103ac29 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 java.awt.BorderLayout;
  21. import java.awt.Dimension;
  22. import java.awt.event.ActionListener;
  23. import java.awt.event.KeyAdapter;
  24. import java.awt.event.KeyEvent;
  25. import java.awt.event.MouseAdapter;
  26. import java.awt.event.MouseEvent;
  27. import javax.swing.JList;
  28. import javax.swing.JScrollPane;
  29. import org.gjt.sp.jedit.Buffer;
  30. import org.gjt.sp.jedit.View;
  31. import org.gjt.sp.jedit.gui.EnhancedDialog;
  32. import org.gjt.sp.jedit.jEdit;
  33. import uk.co.antroy.latextools.macros.ProjectMacros;
  34. import uk.co.antroy.latextools.parsers.BibEntry;
  35. import uk.co.antroy.latextools.parsers.BibTeXParser;
  36. public class BibTeXPanel
  37. extends AbstractToolPanel {
  38. //~ Instance/static variables .............................................
  39. private JList bibList = new JList();
  40. private ActionListener insert;
  41. //~ Constructors ..........................................................
  42. /**
  43. * Creates a new BibTeXPanel object.
  44. *
  45. * @param view the current view
  46. * @param buff the active buffer
  47. */
  48. public BibTeXPanel(View view, Buffer buff) {
  49. super(view, buff, "Bib");
  50. refresh();
  51. }
  52. //~ Methods ...............................................................
  53. /**
  54. * ¤
  55. *
  56. * @param view ¤
  57. * @param buff ¤
  58. */
  59. public static void createBibTeXDialog(View view, Buffer buff) {
  60. final BibTeXPanel n = new BibTeXPanel(view, buff);
  61. EnhancedDialog ed = new EnhancedDialog(view, "Insert Citation", false) {
  62. public void cancel() {
  63. this.hide();
  64. }
  65. public void ok() {
  66. n.insert();
  67. this.hide();
  68. }
  69. };
  70. ed.setContentPane(n);
  71. ed.pack();
  72. ed.show();
  73. }
  74. /**
  75. * ¤
  76. */
  77. public void refresh() {
  78. removeAll();
  79. if (!ProjectMacros.isTeXFile(buffer)) {
  80. displayNotTeX(BorderLayout.CENTER);
  81. } else {
  82. buildList();
  83. JScrollPane scp = new JScrollPane(bibList,
  84. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  85. JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  86. setLayout(new BorderLayout());
  87. setPreferredSize(new Dimension(400, 400));
  88. add(scp, BorderLayout.CENTER);
  89. }
  90. repaint();
  91. }
  92. public void reload() {
  93. }
  94. private void buildList() {
  95. BibTeXParser parser = new BibTeXParser(view, buffer);
  96. Object[] be = parser.getBibEntryArray();
  97. bibList = null;
  98. bibList = new JList(be);
  99. bibList.addMouseListener(new MouseAdapter() {
  100. public void mouseClicked(MouseEvent e) {
  101. if (e.getClickCount() == 2) {
  102. insert();
  103. }
  104. }
  105. });
  106. bibList.addKeyListener(new KeyAdapter() {
  107. public void keyReleased(KeyEvent e) {
  108. if (e.getKeyCode() == e.VK_ENTER) {
  109. insert();
  110. }
  111. }
  112. });
  113. }
  114. private void insert() {
  115. Object[] sels = bibList.getSelectedValues();
  116. if (sels.length == 0) {
  117. System.err.println("Code should have returned by now!");
  118. return;
  119. }
  120. BibEntry bi = (BibEntry)sels[0];
  121. StringBuffer sb = new StringBuffer(bi.getRef());
  122. for (int i = 1; i < sels.length; i++) {
  123. bi = (BibEntry)sels[i];
  124. sb.append(",");
  125. sb.append(bi.getRef());
  126. }
  127. if (jEdit.getBooleanProperty("bibtex.inserttags")) {
  128. sb.insert(0, "\\cite{");
  129. sb.append("}");
  130. }
  131. int posn = view.getTextArea().getCaretPosition();
  132. buffer.insert(posn, sb.toString());
  133. }
  134. }