KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > AbbrevEditor


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
23 package org.gjt.sp.jedit.gui;
24
25 //{{{ Imports
26
import javax.swing.border.*;
27 import javax.swing.*;
28 import java.awt.*;
29 import org.gjt.sp.jedit.*;
30 //}}}
31

32 public class AbbrevEditor extends JPanel
33 {
34     //{{{ AbbrevEditor constructor
35
public AbbrevEditor()
36     {
37         GridBagLayout layout = new GridBagLayout();
38         setLayout(layout);
39
40         GridBagConstraints cons = new GridBagConstraints();
41         cons.anchor = cons.WEST;
42         cons.fill = cons.BOTH;
43         cons.weightx = 0.0f;
44         cons.gridx = 1;
45         cons.gridy = 1;
46
47         JLabel label = new JLabel(jEdit.getProperty("abbrev-editor.abbrev"),
48             SwingConstants.RIGHT);
49         label.setBorder(new EmptyBorder(0,0,0,12));
50         layout.setConstraints(label,cons);
51         add(label);
52         cons.gridx++;
53         cons.weightx = 1.0f;
54         abbrev = new JTextField();
55         layout.setConstraints(abbrev,cons);
56         add(abbrev);
57
58         cons.gridx = 1;
59         cons.weightx = 0.0f;
60         cons.gridwidth = 2;
61
62         cons.gridy++;
63         label = new JLabel(jEdit.getProperty("abbrev-editor.before"));
64         label.setBorder(new EmptyBorder(6,0,3,0));
65         layout.setConstraints(label,cons);
66         add(label);
67
68         cons.gridy++;
69         cons.weighty = 1.0f;
70         beforeCaret = new JTextArea(4,40);
71         JScrollPane scroller = new JScrollPane(beforeCaret);
72         layout.setConstraints(scroller,cons);
73         add(scroller);
74
75         cons.gridy++;
76         cons.weighty = 0.0f;
77         label = new JLabel(jEdit.getProperty("abbrev-editor.after"));
78         label.setBorder(new EmptyBorder(6,0,3,0));
79         layout.setConstraints(label,cons);
80         add(label);
81
82         cons.gridy++;
83         cons.weighty = 1.0f;
84         afterCaret = new JTextArea(4,40);
85         scroller = new JScrollPane(afterCaret);
86         layout.setConstraints(scroller,cons);
87         add(scroller);
88     } //}}}
89

90     //{{{ getAbbrev() method
91
public String JavaDoc getAbbrev()
92     {
93         return abbrev.getText();
94     } //}}}
95

96     //{{{ setAbbrev() method
97
public void setAbbrev(String JavaDoc abbrev)
98     {
99         this.abbrev.setText(abbrev);
100     } //}}}
101

102     //{{{ getExpansion() method
103
public String JavaDoc getExpansion()
104     {
105         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
106
107         String JavaDoc beforeCaretText = beforeCaret.getText();
108         String JavaDoc afterCaretText = afterCaret.getText();
109
110         for(int i = 0; i < beforeCaretText.length(); i++)
111         {
112             char ch = beforeCaretText.charAt(i);
113             switch(ch)
114             {
115             case '\n':
116                 buf.append("\\n");
117                 break;
118             case '\t':
119                 buf.append("\\t");
120                 break;
121             case '\\':
122                 buf.append("\\\\");
123                 break;
124             default:
125                 buf.append(ch);
126                 break;
127             }
128         }
129
130         if(afterCaretText.length() != 0)
131         {
132             buf.append("\\|");
133
134             for(int i = 0; i < afterCaretText.length(); i++)
135             {
136                 char ch = afterCaretText.charAt(i);
137                 switch(ch)
138                 {
139                 case '\n':
140                     buf.append("\\n");
141                     break;
142                 case '\t':
143                     buf.append("\\t");
144                     break;
145                 case '\\':
146                     buf.append("\\\\");
147                     break;
148                 default:
149                     buf.append(ch);
150                     break;
151                 }
152             }
153         }
154
155         return buf.toString();
156     } //}}}
157

158     //{{{ setExpansion() method
159
public void setExpansion(String JavaDoc expansion)
160     {
161         if(expansion == null)
162         {
163             beforeCaret.setText(null);
164             afterCaret.setText(null);
165             return;
166         }
167
168         String JavaDoc beforeCaretText = null;
169         String JavaDoc afterCaretText = null;
170         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
171
172         for(int i = 0; i < expansion.length(); i++)
173         {
174             char ch = expansion.charAt(i);
175
176             if(ch == '\\' && i != expansion.length() - 1)
177             {
178                 ch = expansion.charAt(++i);
179                 switch(ch)
180                 {
181                 case 't':
182                     buf.append('\t');
183                     break;
184                 case 'n':
185                     buf.append('\n');
186                     break;
187                 case '|':
188                     beforeCaretText = buf.toString();
189                     buf.setLength(0);
190                     break;
191                 default:
192                     buf.append(ch);
193                     break;
194                 }
195             }
196             else
197                 buf.append(ch);
198         }
199
200         if(beforeCaretText == null)
201             beforeCaretText = buf.toString();
202         else
203             afterCaretText = buf.toString();
204
205         beforeCaret.setText(beforeCaretText);
206         afterCaret.setText(afterCaretText);
207     } //}}}
208

209     //{{{ getAbbrevField() method
210
public JTextField getAbbrevField()
211     {
212         return abbrev;
213     } //}}}
214

215     //{{{ getBeforeCaretTextArea() method
216
public JTextArea getBeforeCaretTextArea()
217     {
218         return beforeCaret;
219     } //}}}
220

221     //{{{ getAfterCaretTextArea() method
222
public JTextArea getAfterCaretTextArea()
223     {
224         return afterCaret;
225     } //}}}
226

227     //{{{ Private members
228
private JTextField abbrev;
229     private JTextArea beforeCaret, afterCaret;
230     //}}}
231
}
232
Popular Tags