PageRenderTime 75ms CodeModel.GetById 66ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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

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