/plugins/LaTeXTools/tags/release-0-2/src/uk/co/antroy/latextools/ReferencePanel.java

#
Java | 208 lines | 120 code | 42 blank | 46 comment | 13 complexity | 36924398a6f34459be77c9470a6fb9ec MD5 | raw file

✨ Summary
  1. /*:folding=indent:
  2. * ReferencePanel.java - Reference Panel
  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.MouseAdapter;
  24. import java.awt.event.MouseEvent;
  25. //import java.io.*;
  26. import java.util.ArrayList;
  27. import java.util.Collections;
  28. import javax.swing.JList;
  29. import javax.swing.JScrollPane;
  30. import javax.swing.ListSelectionModel;
  31. import org.gjt.sp.jedit.Buffer;
  32. import org.gjt.sp.jedit.View;
  33. import org.gjt.sp.jedit.gui.EnhancedDialog;
  34. import org.gjt.sp.jedit.jEdit;
  35. import org.gjt.sp.jedit.textarea.JEditTextArea;
  36. import org.gjt.sp.jedit.textarea.FoldVisibilityManager;
  37. import org.gjt.sp.jedit.textarea.Selection;
  38. public class ReferencePanel
  39. extends DefaultToolPanel {
  40. //~ Instance/static variables ...............................................
  41. private ActionListener insert;
  42. private ArrayList refEntries = new ArrayList();
  43. private JList refList;
  44. //~ Constructors ............................................................
  45. /**
  46. * Creates a new ReferencePanel object.
  47. *
  48. * @param view the current view
  49. * @param buff the active buffer
  50. */
  51. public ReferencePanel(View view, Buffer buff) {
  52. super(view, buff, "Ref");
  53. refresh();
  54. }
  55. //~ Methods .................................................................
  56. /**
  57. * ¤
  58. *
  59. * @param view ¤
  60. * @param buff ¤
  61. */
  62. public static void createReferenceDialog(View view, Buffer buff) {
  63. final ReferencePanel n = new ReferencePanel(view, buff);
  64. EnhancedDialog ed = new EnhancedDialog(view, "Insert Cross Reference",
  65. false) {
  66. public void cancel() {
  67. this.hide();
  68. }
  69. public void ok() {
  70. n.insert();
  71. this.hide();
  72. }
  73. };
  74. ed.setContentPane(n);
  75. ed.pack();
  76. ed.show();
  77. }
  78. /**
  79. * ¤
  80. */
  81. public void refresh() {
  82. if (bufferChanged) {
  83. removeAll();
  84. bufferChanged = false;
  85. }
  86. if (!isTeXFile(buffer)) {
  87. displayNotTeX(BorderLayout.CENTER);
  88. } else {
  89. loadReferences();
  90. refList = new JList(refEntries.toArray());
  91. refList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  92. refList.addMouseListener(new MouseAdapter() {
  93. public void mouseClicked(MouseEvent e) {
  94. if (e.getClickCount() == 2) {
  95. insert();
  96. } else if (e.getClickCount() == 1) {
  97. refreshCurrentCursorPosn();
  98. visitLabel();
  99. }
  100. }
  101. });
  102. JScrollPane scp = new JScrollPane(refList,
  103. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  104. JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  105. setLayout(new BorderLayout());
  106. setPreferredSize(new Dimension(400, 100));
  107. add(scp, BorderLayout.CENTER);
  108. add(createButtonPanel(REFRESH), BorderLayout.SOUTH);
  109. }
  110. repaint();
  111. }
  112. private void insert() {
  113. LaTeXAsset refTagPair = (LaTeXAsset) refList.getSelectedValue();
  114. String ref = refTagPair.name;
  115. if (ref != null) {
  116. if (jEdit.getBooleanProperty("reference.inserttags")) {
  117. ref = "\\ref{" + ref + "}";
  118. }
  119. buffer.insert(currentCursorPosn, ref);
  120. view.getTextArea().setCaretPosition(currentCursorPosn + ref.length());
  121. }
  122. }
  123. private void loadReferences() {
  124. refEntries.clear();
  125. int index = buffer.getLineCount() - 1;
  126. // boolean last = false;
  127. //TODO: Optimize the algorithm below. In particular find the last entry first
  128. // and then begin the main loop from that point.
  129. while (index > 0) {
  130. String line = buffer.getLineText(index);
  131. int refStart = line.indexOf("\\label{");
  132. // if (!last && refStart>=0) {
  133. // last = true;
  134. // String lastLine = index+"";
  135. // maxLineLen = lastLine.length();
  136. // }
  137. //
  138. while (refStart >= 0) {
  139. int refEnd = line.indexOf("}", refStart);
  140. String ref = line.substring(refStart + 7, refEnd);
  141. LaTeXAsset asset = LaTeXAsset.createAsset(ref.trim(),
  142. buffer.createPosition(buffer.getLineStartOffset(index)),
  143. buffer.createPosition(buffer.getLineEndOffset(index)),
  144. 0,
  145. 0); //,maxLineLen);
  146. refEntries.add(asset);
  147. refStart = line.indexOf("\\label{", refEnd);
  148. }
  149. index--;
  150. }
  151. Collections.sort(refEntries);
  152. }
  153. private void visitLabel() {
  154. LaTeXAsset asset = (LaTeXAsset) refList.getSelectedValue();
  155. int line = buffer.getLineOfOffset(asset.start.getOffset());
  156. JEditTextArea textArea = view.getTextArea();
  157. FoldVisibilityManager fvm = textArea.getFoldVisibilityManager();
  158. fvm.expandFold(line,false);
  159. int virtualLine = fvm.physicalToVirtual(line);
  160. textArea.setFirstLine(virtualLine);
  161. int lineStart = view.getTextArea().getLineStartOffset(line);
  162. int lineEnd = view.getTextArea().getLineEndOffset(line);
  163. Selection.Range sel = new Selection.Range(lineStart, lineEnd);
  164. view.getTextArea().setSelection(sel);
  165. }
  166. }