001: /*
002: * AbbrevEditor.java - Panel for editing abbreviations
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2001 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.gui;
024:
025: //{{{ Imports
026: import javax.swing.border.*;
027: import javax.swing.*;
028: import java.awt.*;
029: import org.gjt.sp.jedit.*;
030:
031: //}}}
032:
033: public class AbbrevEditor extends JPanel {
034: //{{{ AbbrevEditor constructor
035: public AbbrevEditor() {
036: GridBagLayout layout = new GridBagLayout();
037: setLayout(layout);
038:
039: GridBagConstraints cons = new GridBagConstraints();
040: cons.anchor = cons.WEST;
041: cons.fill = cons.BOTH;
042: cons.weightx = 0.0f;
043: cons.gridx = 1;
044: cons.gridy = 1;
045:
046: JLabel label = new JLabel(jEdit
047: .getProperty("abbrev-editor.abbrev"),
048: SwingConstants.RIGHT);
049: label.setBorder(new EmptyBorder(0, 0, 0, 12));
050: layout.setConstraints(label, cons);
051: add(label);
052: cons.gridx++;
053: cons.weightx = 1.0f;
054: abbrev = new JTextField();
055: layout.setConstraints(abbrev, cons);
056: add(abbrev);
057:
058: cons.gridx = 1;
059: cons.weightx = 0.0f;
060: cons.gridwidth = 2;
061:
062: cons.gridy++;
063: label = new JLabel(jEdit.getProperty("abbrev-editor.before"));
064: label.setBorder(new EmptyBorder(6, 0, 3, 0));
065: layout.setConstraints(label, cons);
066: add(label);
067:
068: cons.gridy++;
069: cons.weighty = 1.0f;
070: beforeCaret = new JTextArea(4, 40);
071: JScrollPane scroller = new JScrollPane(beforeCaret);
072: layout.setConstraints(scroller, cons);
073: add(scroller);
074:
075: cons.gridy++;
076: cons.weighty = 0.0f;
077: label = new JLabel(jEdit.getProperty("abbrev-editor.after"));
078: label.setBorder(new EmptyBorder(6, 0, 3, 0));
079: layout.setConstraints(label, cons);
080: add(label);
081:
082: cons.gridy++;
083: cons.weighty = 1.0f;
084: afterCaret = new JTextArea(4, 40);
085: scroller = new JScrollPane(afterCaret);
086: layout.setConstraints(scroller, cons);
087: add(scroller);
088: } //}}}
089:
090: //{{{ getAbbrev() method
091: public String getAbbrev() {
092: return abbrev.getText();
093: } //}}}
094:
095: //{{{ setAbbrev() method
096: public void setAbbrev(String abbrev) {
097: this .abbrev.setText(abbrev);
098: } //}}}
099:
100: //{{{ getExpansion() method
101: public String getExpansion() {
102: StringBuffer buf = new StringBuffer();
103:
104: String beforeCaretText = beforeCaret.getText();
105: String afterCaretText = afterCaret.getText();
106:
107: for (int i = 0; i < beforeCaretText.length(); i++) {
108: char ch = beforeCaretText.charAt(i);
109: switch (ch) {
110: case '\n':
111: buf.append("\\n");
112: break;
113: case '\t':
114: buf.append("\\t");
115: break;
116: case '\\':
117: buf.append("\\\\");
118: break;
119: default:
120: buf.append(ch);
121: break;
122: }
123: }
124:
125: if (afterCaretText.length() != 0) {
126: buf.append("\\|");
127:
128: for (int i = 0; i < afterCaretText.length(); i++) {
129: char ch = afterCaretText.charAt(i);
130: switch (ch) {
131: case '\n':
132: buf.append("\\n");
133: break;
134: case '\t':
135: buf.append("\\t");
136: break;
137: case '\\':
138: buf.append("\\\\");
139: break;
140: default:
141: buf.append(ch);
142: break;
143: }
144: }
145: }
146:
147: return buf.toString();
148: } //}}}
149:
150: //{{{ setExpansion() method
151: public void setExpansion(String expansion) {
152: if (expansion == null) {
153: beforeCaret.setText(null);
154: afterCaret.setText(null);
155: return;
156: }
157:
158: String beforeCaretText = null;
159: String afterCaretText = null;
160: StringBuffer buf = new StringBuffer();
161:
162: for (int i = 0; i < expansion.length(); i++) {
163: char ch = expansion.charAt(i);
164:
165: if (ch == '\\' && i != expansion.length() - 1) {
166: ch = expansion.charAt(++i);
167: switch (ch) {
168: case 't':
169: buf.append('\t');
170: break;
171: case 'n':
172: buf.append('\n');
173: break;
174: case '|':
175: beforeCaretText = buf.toString();
176: buf.setLength(0);
177: break;
178: default:
179: buf.append(ch);
180: break;
181: }
182: } else
183: buf.append(ch);
184: }
185:
186: if (beforeCaretText == null)
187: beforeCaretText = buf.toString();
188: else
189: afterCaretText = buf.toString();
190:
191: beforeCaret.setText(beforeCaretText);
192: afterCaret.setText(afterCaretText);
193: } //}}}
194:
195: //{{{ getAbbrevField() method
196: public JTextField getAbbrevField() {
197: return abbrev;
198: } //}}}
199:
200: //{{{ getBeforeCaretTextArea() method
201: public JTextArea getBeforeCaretTextArea() {
202: return beforeCaret;
203: } //}}}
204:
205: //{{{ getAfterCaretTextArea() method
206: public JTextArea getAfterCaretTextArea() {
207: return afterCaret;
208: } //}}}
209:
210: //{{{ Private members
211: private JTextField abbrev;
212: private JTextArea beforeCaret, afterCaret;
213: //}}}
214: }
|