/plugins/LaTeXTools/branches/stable_0-5/latextools/uk/co/antroy/latextools/ReferencePanel.java

# · Java · 192 lines · 124 code · 32 blank · 36 comment · 16 complexity · 0c5fc6c8e7d91951ece060b2ef4c460b MD5 · raw file

  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 gnu.regexp.*;
  21. import uk.co.antroy.latextools.parsers.*;
  22. import java.awt.BorderLayout;
  23. import java.awt.Dimension;
  24. import java.awt.event.ActionListener;
  25. import java.awt.event.MouseAdapter;
  26. import java.awt.event.MouseEvent;
  27. import java.io.*;
  28. import java.util.*;
  29. import java.util.ArrayList;
  30. import javax.swing.JList;
  31. import javax.swing.JScrollPane;
  32. import javax.swing.ListSelectionModel;
  33. import javax.swing.tree.*;
  34. import org.gjt.sp.jedit.Buffer;
  35. import org.gjt.sp.jedit.View;
  36. import org.gjt.sp.jedit.gui.EnhancedDialog;
  37. import org.gjt.sp.jedit.jEdit;
  38. import org.gjt.sp.jedit.textarea.DisplayManager;
  39. import org.gjt.sp.jedit.textarea.JEditTextArea;
  40. import org.gjt.sp.jedit.textarea.Selection;
  41. import org.gjt.sp.util.*;
  42. public class ReferencePanel
  43. extends AbstractToolPanel {
  44. //~ Instance/static variables ...............................................
  45. public static final String REFERENCE_EXP = "\\\\label\\{(.*?)\\}";
  46. private ActionListener insert;
  47. private ArrayList refEntries = new ArrayList();
  48. private JList refList;
  49. private boolean suppress = false;
  50. //~ Constructors ............................................................
  51. /**
  52. * Creates a new ReferencePanel object.
  53. *
  54. * @param view the current view
  55. * @param buff the active buffer
  56. */
  57. public ReferencePanel(View view, Buffer buff) {
  58. super(view, buff, "Ref");
  59. refList = new JList();
  60. refList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  61. refList.addMouseListener(new MouseAdapter() {
  62. public void mouseClicked(MouseEvent e) {
  63. if (e.getClickCount() == 2) {
  64. insert();
  65. } else if (e.getClickCount() == 1) {
  66. if (!suppress) {
  67. refreshCurrentCursorPosn();
  68. }
  69. if ((e.getModifiers() & e.ALT_MASK) == e.ALT_MASK) {
  70. suppress = true;
  71. } else {
  72. suppress = false;
  73. }
  74. visitLabel();
  75. }
  76. }
  77. public void mouseExited(MouseEvent e) {
  78. suppress = false;
  79. }
  80. });
  81. refresh();
  82. }
  83. //~ Methods .................................................................
  84. /**
  85. * ¤
  86. *
  87. * @param view ¤
  88. * @param buff ¤
  89. */
  90. public static void createReferenceDialog(View view, Buffer buff) {
  91. final ReferencePanel n = new ReferencePanel(view, buff);
  92. EnhancedDialog ed = new EnhancedDialog(view, "Insert Cross Reference",
  93. false) {
  94. public void cancel() {
  95. this.hide();
  96. }
  97. public void ok() {
  98. n.insert();
  99. this.hide();
  100. }
  101. };
  102. ed.setContentPane(n);
  103. ed.pack();
  104. ed.show();
  105. }
  106. /**
  107. * ¤
  108. */
  109. public void refresh() {
  110. if (suppress)
  111. return;
  112. if (bufferChanged) {
  113. removeAll();
  114. bufferChanged = false;
  115. }
  116. if (!LaTeXMacros.isTeXFile(buffer)) {
  117. displayNotTeX(BorderLayout.CENTER);
  118. } else {
  119. LabelParser parser = new LabelParser(view, buffer);
  120. Object[] be = parser.getLabelArray();
  121. refList.setListData(be);
  122. JScrollPane scp = new JScrollPane(refList,
  123. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  124. JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  125. setLayout(new BorderLayout());
  126. setPreferredSize(new Dimension(400, 100));
  127. add(scp, BorderLayout.CENTER);
  128. }
  129. repaint();
  130. }
  131. public void reload() {
  132. }
  133. private void insert() {
  134. LaTeXAsset refTagPair = (LaTeXAsset)refList.getSelectedValue();
  135. String ref = refTagPair.name;
  136. if (ref != null) {
  137. if (jEdit.getBooleanProperty("reference.inserttags")) {
  138. ref = "\\ref{" + ref + "}";
  139. }
  140. view.setBuffer(currentBuffer);
  141. currentBuffer.insert(currentCursorPosn, ref);
  142. view.getTextArea().setCaretPosition(
  143. currentCursorPosn + ref.length());
  144. }
  145. }
  146. private void visitLabel() {
  147. LaTeXAsset asset = (LaTeXAsset)refList.getSelectedValue();
  148. Buffer goToBuff = jEdit.openFile(view, asset.getFile().toString());
  149. view.setBuffer(goToBuff);
  150. int line = goToBuff.getLineOfOffset(asset.start.getOffset());
  151. JEditTextArea textArea = view.getTextArea();
  152. DisplayManager fvm = textArea.getDisplayManager();
  153. fvm.expandFold(line, false);
  154. textArea.setFirstPhysicalLine(line);
  155. int lineStart = view.getTextArea().getLineStartOffset(line);
  156. int lineEnd = view.getTextArea().getLineEndOffset(line);
  157. Selection.Range sel = new Selection.Range(lineStart, lineEnd);
  158. view.getTextArea().setSelection(sel);
  159. }
  160. }