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

# · Java · 175 lines · 107 code · 35 blank · 33 comment · 16 complexity · f88a74b7506d7ae514c961976af72eb8 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 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.util.ArrayList;
  26. import javax.swing.JList;
  27. import javax.swing.JScrollPane;
  28. import javax.swing.ListSelectionModel;
  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.macros.TextMacros;
  35. import uk.co.antroy.latextools.parsers.LabelParser;
  36. import uk.co.antroy.latextools.parsers.LaTeXAsset;
  37. public class ReferencePanel
  38. extends AbstractToolPanel {
  39. //~ Instance/static variables .............................................
  40. public static final String REFERENCE_EXP = "\\\\label\\{(.*?)\\}";
  41. private ActionListener insert;
  42. private ArrayList refEntries = new ArrayList();
  43. private JList refList;
  44. private boolean suppress = false;
  45. //~ Constructors ..........................................................
  46. /**
  47. * Creates a new ReferencePanel object.
  48. *
  49. * @param view the current view
  50. * @param buff the active buffer
  51. */
  52. public ReferencePanel(final View view, Buffer buff) {
  53. super(view, buff, "Ref");
  54. refList = new JList();
  55. refList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  56. refList.addMouseListener(new MouseAdapter() {
  57. public void mouseClicked(MouseEvent e) {
  58. if (e.getClickCount() == 2) {
  59. insert();
  60. } else if (e.getClickCount() == 1) {
  61. if (!suppress) {
  62. refreshCurrentCursorPosn();
  63. }
  64. if ((e.getModifiers() & e.ALT_MASK) == e.ALT_MASK) {
  65. suppress = true;
  66. } else {
  67. suppress = false;
  68. }
  69. LaTeXAsset asset = (LaTeXAsset)refList.getSelectedValue();
  70. TextMacros.visitAsset(view, asset);
  71. }
  72. }
  73. public void mouseExited(MouseEvent e) {
  74. suppress = false;
  75. }
  76. });
  77. refresh();
  78. }
  79. //~ Methods ...............................................................
  80. /**
  81. * ¤
  82. *
  83. * @param view ¤
  84. * @param buff ¤
  85. */
  86. public static void createReferenceDialog(View view, Buffer buff) {
  87. final ReferencePanel n = new ReferencePanel(view, buff);
  88. EnhancedDialog ed = new EnhancedDialog(view, "Insert Cross Reference",
  89. false) {
  90. public void cancel() {
  91. this.hide();
  92. }
  93. public void ok() {
  94. n.insert();
  95. this.hide();
  96. }
  97. };
  98. ed.setContentPane(n);
  99. ed.pack();
  100. ed.show();
  101. }
  102. public void refresh() {
  103. if (suppress) {
  104. return;
  105. }
  106. if (bufferChanged) {
  107. removeAll();
  108. bufferChanged = false;
  109. }
  110. if (!ProjectMacros.isTeXFile(buffer)) {
  111. displayNotTeX(BorderLayout.CENTER);
  112. } else {
  113. LabelParser parser = new LabelParser(view, buffer);
  114. Object[] be = parser.getLabelArray();
  115. refList.setListData(be);
  116. JScrollPane scp = new JScrollPane(refList,
  117. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  118. JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  119. setLayout(new BorderLayout());
  120. setPreferredSize(new Dimension(400, 100));
  121. add(scp, BorderLayout.CENTER);
  122. }
  123. repaint();
  124. }
  125. public void reload() {
  126. }
  127. private void insert() {
  128. LaTeXAsset refTagPair = (LaTeXAsset)refList.getSelectedValue();
  129. String ref = refTagPair.name;
  130. if (ref != null) {
  131. if (jEdit.getBooleanProperty("reference.inserttags")) {
  132. ref = "\\ref{" + ref + "}";
  133. }
  134. view.setBuffer(currentBuffer);
  135. currentBuffer.insert(currentCursorPosn, ref);
  136. view.getTextArea().setCaretPosition(
  137. currentCursorPosn + ref.length());
  138. }
  139. }
  140. }