/plugins/LaTeXTools/tags/latextools_release_0-5/latextools/uk/co/antroy/latextools/BibTeXTablePanel.java

#
Java | 166 lines | 106 code | 33 blank | 27 comment | 8 complexity | 5734746a6a80b6743e10d87f2100251e MD5 | raw file

✨ Summary
  1. /*:folding=indent:
  2. * BibTeXTablePanel.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.ActionEvent;
  23. import java.awt.event.ActionListener;
  24. import java.awt.event.KeyEvent;
  25. import java.awt.event.MouseAdapter;
  26. import java.awt.event.MouseEvent;
  27. import javax.swing.AbstractAction;
  28. import javax.swing.JLabel;
  29. import javax.swing.JScrollPane;
  30. import javax.swing.JTable;
  31. import javax.swing.KeyStroke;
  32. import javax.swing.table.TableModel;
  33. import org.gjt.sp.jedit.Buffer;
  34. import org.gjt.sp.jedit.View;
  35. import org.gjt.sp.jedit.jEdit;
  36. import tableutils.TableSorter;
  37. import uk.co.antroy.latextools.macros.ProjectMacros;
  38. import uk.co.antroy.latextools.parsers.BibEntry;
  39. import uk.co.antroy.latextools.parsers.BibTeXParser;
  40. import uk.co.antroy.latextools.parsers.BibTeXTableModel;
  41. public class BibTeXTablePanel
  42. extends AbstractToolPanel {
  43. //~ Instance/static variables .............................................
  44. private JTable table;
  45. private TableModel model;
  46. private ActionListener insert;
  47. private boolean enableInsert = true;
  48. //~ Constructors ..........................................................
  49. /**
  50. * Creates a new BibTeXTablePanel object.
  51. *
  52. * @param view the current view
  53. * @param buff the active buffer
  54. */
  55. public BibTeXTablePanel(View view, Buffer buff) {
  56. super(view, buff, "Bib");
  57. buildPanel();
  58. }
  59. //~ Methods ...............................................................
  60. public void refresh() {
  61. removeAll();
  62. if (!ProjectMacros.isTeXFile(buffer) &&
  63. !ProjectMacros.isBibFile(buffer)) {
  64. displayNotTeX(BorderLayout.CENTER);
  65. } else {
  66. enableInsert = !ProjectMacros.isBibFile(buffer);
  67. buildPanel();
  68. }
  69. super.refresh();
  70. }
  71. public void reload() {
  72. }
  73. private void _buildPanel() {
  74. JLabel parsingLabel = new JLabel("<html><font color='#dd0000'>Parsing...");
  75. add(parsingLabel);
  76. BibTeXParser parser = new BibTeXParser(view, buffer);
  77. model = new BibTeXTableModel(parser.getBibEntries());
  78. TableSorter sorter = new TableSorter(model);
  79. table = new JTable(sorter);
  80. table.addMouseListener(new MouseAdapter() {
  81. public void mouseClicked(MouseEvent e) {
  82. if (e.getClickCount() == 2) {
  83. insert();
  84. }
  85. }
  86. });
  87. String key = "Enter";
  88. table.getActionMap().put(key,
  89. new AbstractAction() {
  90. public void actionPerformed(ActionEvent e) {
  91. insert();
  92. }
  93. });
  94. table.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
  95. key);
  96. sorter.addMouseListenerToHeaderInTable(table);
  97. JScrollPane scp = new JScrollPane(table,
  98. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  99. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  100. setLayout(new BorderLayout());
  101. setPreferredSize(new Dimension(400, 400));
  102. remove(parsingLabel);
  103. add(scp, BorderLayout.CENTER);
  104. sendUpdateEvent("latextools-bibliography-table-dock");
  105. }
  106. private void buildPanel() {
  107. Thread parseThread = new Thread(new Runnable() {
  108. public void run() {
  109. _buildPanel();
  110. }
  111. });
  112. parseThread.start();
  113. }
  114. private void insert() {
  115. if (!enableInsert) {
  116. return;
  117. }
  118. int[] sels = table.getSelectedRows();
  119. StringBuffer sb = new StringBuffer();
  120. for (int i = 0; i < sels.length; i++) {
  121. BibTeXTableModel mod = (BibTeXTableModel)model;
  122. BibEntry bi = mod.getRowEntry(sels[i]);
  123. sb.append(bi.getRef());
  124. sb.append((i < sels.length - 1) ? "," : "");
  125. }
  126. if (jEdit.getBooleanProperty("bibtex.inserttags")) {
  127. sb.insert(0, "\\cite{");
  128. sb.append("}");
  129. }
  130. int posn = view.getTextArea().getCaretPosition();
  131. buffer.insert(posn, sb.toString());
  132. }
  133. }