PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/org/gjt/sp/jedit/gui/AbbrevEditor.java

#
Java | 232 lines | 173 code | 26 blank | 33 comment | 10 complexity | 9728617f072720db5133d42e71e67b12 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * AbbrevEditor.java - Panel for editing abbreviations
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.gui;
  23. //{{{ Imports
  24. import javax.swing.border.*;
  25. import javax.swing.*;
  26. import java.awt.event.*;
  27. import java.awt.*;
  28. import org.gjt.sp.jedit.*;
  29. //}}}
  30. public class AbbrevEditor extends JPanel
  31. {
  32. //{{{ AbbrevEditor constructor
  33. public AbbrevEditor()
  34. {
  35. GridBagLayout layout = new GridBagLayout();
  36. setLayout(layout);
  37. GridBagConstraints cons = new GridBagConstraints();
  38. cons.anchor = cons.WEST;
  39. cons.fill = cons.BOTH;
  40. cons.weightx = 0.0f;
  41. cons.gridx = 1;
  42. cons.gridy = 1;
  43. JLabel label = new JLabel(jEdit.getProperty("abbrev-editor.abbrev"),
  44. SwingConstants.RIGHT);
  45. label.setBorder(new EmptyBorder(0,0,0,12));
  46. layout.setConstraints(label,cons);
  47. add(label);
  48. cons.gridx++;
  49. cons.weightx = 1.0f;
  50. abbrev = new JTextField();
  51. layout.setConstraints(abbrev,cons);
  52. add(abbrev);
  53. cons.gridx = 1;
  54. cons.weightx = 0.0f;
  55. cons.gridwidth = 2;
  56. cons.gridy++;
  57. label = new JLabel(jEdit.getProperty("abbrev-editor.before"));
  58. label.setBorder(new EmptyBorder(6,0,3,0));
  59. layout.setConstraints(label,cons);
  60. add(label);
  61. cons.gridy++;
  62. cons.weighty = 1.0f;
  63. beforeCaret = new JTextArea(4,40);
  64. JScrollPane scroller = new JScrollPane(beforeCaret);
  65. layout.setConstraints(scroller,cons);
  66. add(scroller);
  67. cons.gridy++;
  68. cons.weighty = 0.0f;
  69. label = new JLabel(jEdit.getProperty("abbrev-editor.after"));
  70. label.setBorder(new EmptyBorder(6,0,3,0));
  71. layout.setConstraints(label,cons);
  72. add(label);
  73. cons.gridy++;
  74. cons.weighty = 1.0f;
  75. afterCaret = new JTextArea(4,40);
  76. scroller = new JScrollPane(afterCaret);
  77. layout.setConstraints(scroller,cons);
  78. add(scroller);
  79. } //}}}
  80. //{{{ getAbbrev() method
  81. public String getAbbrev()
  82. {
  83. return abbrev.getText();
  84. } //}}}
  85. //{{{ setAbbrev() method
  86. public void setAbbrev(String abbrev)
  87. {
  88. this.abbrev.setText(abbrev);
  89. } //}}}
  90. //{{{ getExpansion() method
  91. public String getExpansion()
  92. {
  93. StringBuffer buf = new StringBuffer();
  94. String beforeCaretText = beforeCaret.getText();
  95. String afterCaretText = afterCaret.getText();
  96. for(int i = 0; i < beforeCaretText.length(); i++)
  97. {
  98. char ch = beforeCaretText.charAt(i);
  99. switch(ch)
  100. {
  101. case '\n':
  102. buf.append("\\n");
  103. break;
  104. case '\t':
  105. buf.append("\\t");
  106. break;
  107. case '\\':
  108. buf.append("\\\\");
  109. break;
  110. default:
  111. buf.append(ch);
  112. break;
  113. }
  114. }
  115. if(afterCaretText.length() != 0)
  116. {
  117. buf.append("\\|");
  118. for(int i = 0; i < afterCaretText.length(); i++)
  119. {
  120. char ch = afterCaretText.charAt(i);
  121. switch(ch)
  122. {
  123. case '\n':
  124. buf.append("\\n");
  125. break;
  126. case '\t':
  127. buf.append("\\t");
  128. break;
  129. case '\\':
  130. buf.append("\\\\");
  131. break;
  132. default:
  133. buf.append(ch);
  134. break;
  135. }
  136. }
  137. }
  138. return buf.toString();
  139. } //}}}
  140. //{{{ setExpansion() method
  141. public void setExpansion(String expansion)
  142. {
  143. if(expansion == null)
  144. {
  145. beforeCaret.setText(null);
  146. afterCaret.setText(null);
  147. return;
  148. }
  149. String beforeCaretText = null;
  150. String afterCaretText = null;
  151. StringBuffer buf = new StringBuffer();
  152. for(int i = 0; i < expansion.length(); i++)
  153. {
  154. char ch = expansion.charAt(i);
  155. if(ch == '\\' && i != expansion.length() - 1)
  156. {
  157. ch = expansion.charAt(++i);
  158. switch(ch)
  159. {
  160. case 't':
  161. buf.append('\t');
  162. break;
  163. case 'n':
  164. buf.append('\n');
  165. break;
  166. case '|':
  167. beforeCaretText = buf.toString();
  168. buf.setLength(0);
  169. break;
  170. default:
  171. buf.append(ch);
  172. break;
  173. }
  174. }
  175. else
  176. buf.append(ch);
  177. }
  178. if(beforeCaretText == null)
  179. beforeCaretText = buf.toString();
  180. else
  181. afterCaretText = buf.toString();
  182. beforeCaret.setText(beforeCaretText);
  183. afterCaret.setText(afterCaretText);
  184. } //}}}
  185. //{{{ getAbbrevField() method
  186. public JTextField getAbbrevField()
  187. {
  188. return abbrev;
  189. } //}}}
  190. //{{{ getBeforeCaretTextArea() method
  191. public JTextArea getBeforeCaretTextArea()
  192. {
  193. return beforeCaret;
  194. } //}}}
  195. //{{{ getAfterCaretTextArea() method
  196. public JTextArea getAfterCaretTextArea()
  197. {
  198. return afterCaret;
  199. } //}}}
  200. //{{{ Private members
  201. private JTextField abbrev;
  202. private JTextArea beforeCaret, afterCaret;
  203. //}}}
  204. }