PageRenderTime 39ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 231 lines | 172 code | 26 blank | 33 comment | 10 complexity | f02960aaa2694b7a6df5771de043c6b6 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.*;
  27. import org.gjt.sp.jedit.*;
  28. //}}}
  29. public class AbbrevEditor extends JPanel
  30. {
  31. //{{{ AbbrevEditor constructor
  32. public AbbrevEditor()
  33. {
  34. GridBagLayout layout = new GridBagLayout();
  35. setLayout(layout);
  36. GridBagConstraints cons = new GridBagConstraints();
  37. cons.anchor = cons.WEST;
  38. cons.fill = cons.BOTH;
  39. cons.weightx = 0.0f;
  40. cons.gridx = 1;
  41. cons.gridy = 1;
  42. JLabel label = new JLabel(jEdit.getProperty("abbrev-editor.abbrev"),
  43. SwingConstants.RIGHT);
  44. label.setBorder(new EmptyBorder(0,0,0,12));
  45. layout.setConstraints(label,cons);
  46. add(label);
  47. cons.gridx++;
  48. cons.weightx = 1.0f;
  49. abbrev = new JTextField();
  50. layout.setConstraints(abbrev,cons);
  51. add(abbrev);
  52. cons.gridx = 1;
  53. cons.weightx = 0.0f;
  54. cons.gridwidth = 2;
  55. cons.gridy++;
  56. label = new JLabel(jEdit.getProperty("abbrev-editor.before"));
  57. label.setBorder(new EmptyBorder(6,0,3,0));
  58. layout.setConstraints(label,cons);
  59. add(label);
  60. cons.gridy++;
  61. cons.weighty = 1.0f;
  62. beforeCaret = new JTextArea(4,40);
  63. JScrollPane scroller = new JScrollPane(beforeCaret);
  64. layout.setConstraints(scroller,cons);
  65. add(scroller);
  66. cons.gridy++;
  67. cons.weighty = 0.0f;
  68. label = new JLabel(jEdit.getProperty("abbrev-editor.after"));
  69. label.setBorder(new EmptyBorder(6,0,3,0));
  70. layout.setConstraints(label,cons);
  71. add(label);
  72. cons.gridy++;
  73. cons.weighty = 1.0f;
  74. afterCaret = new JTextArea(4,40);
  75. scroller = new JScrollPane(afterCaret);
  76. layout.setConstraints(scroller,cons);
  77. add(scroller);
  78. } //}}}
  79. //{{{ getAbbrev() method
  80. public String getAbbrev()
  81. {
  82. return abbrev.getText();
  83. } //}}}
  84. //{{{ setAbbrev() method
  85. public void setAbbrev(String abbrev)
  86. {
  87. this.abbrev.setText(abbrev);
  88. } //}}}
  89. //{{{ getExpansion() method
  90. public String getExpansion()
  91. {
  92. StringBuffer buf = new StringBuffer();
  93. String beforeCaretText = beforeCaret.getText();
  94. String afterCaretText = afterCaret.getText();
  95. for(int i = 0; i < beforeCaretText.length(); i++)
  96. {
  97. char ch = beforeCaretText.charAt(i);
  98. switch(ch)
  99. {
  100. case '\n':
  101. buf.append("\\n");
  102. break;
  103. case '\t':
  104. buf.append("\\t");
  105. break;
  106. case '\\':
  107. buf.append("\\\\");
  108. break;
  109. default:
  110. buf.append(ch);
  111. break;
  112. }
  113. }
  114. if(afterCaretText.length() != 0)
  115. {
  116. buf.append("\\|");
  117. for(int i = 0; i < afterCaretText.length(); i++)
  118. {
  119. char ch = afterCaretText.charAt(i);
  120. switch(ch)
  121. {
  122. case '\n':
  123. buf.append("\\n");
  124. break;
  125. case '\t':
  126. buf.append("\\t");
  127. break;
  128. case '\\':
  129. buf.append("\\\\");
  130. break;
  131. default:
  132. buf.append(ch);
  133. break;
  134. }
  135. }
  136. }
  137. return buf.toString();
  138. } //}}}
  139. //{{{ setExpansion() method
  140. public void setExpansion(String expansion)
  141. {
  142. if(expansion == null)
  143. {
  144. beforeCaret.setText(null);
  145. afterCaret.setText(null);
  146. return;
  147. }
  148. String beforeCaretText = null;
  149. String afterCaretText = null;
  150. StringBuffer buf = new StringBuffer();
  151. for(int i = 0; i < expansion.length(); i++)
  152. {
  153. char ch = expansion.charAt(i);
  154. if(ch == '\\' && i != expansion.length() - 1)
  155. {
  156. ch = expansion.charAt(++i);
  157. switch(ch)
  158. {
  159. case 't':
  160. buf.append('\t');
  161. break;
  162. case 'n':
  163. buf.append('\n');
  164. break;
  165. case '|':
  166. beforeCaretText = buf.toString();
  167. buf.setLength(0);
  168. break;
  169. default:
  170. buf.append(ch);
  171. break;
  172. }
  173. }
  174. else
  175. buf.append(ch);
  176. }
  177. if(beforeCaretText == null)
  178. beforeCaretText = buf.toString();
  179. else
  180. afterCaretText = buf.toString();
  181. beforeCaret.setText(beforeCaretText);
  182. afterCaret.setText(afterCaretText);
  183. } //}}}
  184. //{{{ getAbbrevField() method
  185. public JTextField getAbbrevField()
  186. {
  187. return abbrev;
  188. } //}}}
  189. //{{{ getBeforeCaretTextArea() method
  190. public JTextArea getBeforeCaretTextArea()
  191. {
  192. return beforeCaret;
  193. } //}}}
  194. //{{{ getAfterCaretTextArea() method
  195. public JTextArea getAfterCaretTextArea()
  196. {
  197. return afterCaret;
  198. } //}}}
  199. //{{{ Private members
  200. private JTextField abbrev;
  201. private JTextArea beforeCaret, afterCaret;
  202. //}}}
  203. }