PageRenderTime 23ms CodeModel.GetById 9ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/gui/EditAbbrevDialog.java

#
Java | 176 lines | 115 code | 25 blank | 36 comment | 15 complexity | 5f3f8f6afef8d6751767db49bbe0d0e4 MD5 | raw file
  1/*
  2 * EditAbbrevDialog.java - Displayed when editing abbrevs
  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 java.util.*;
 31import org.gjt.sp.jedit.*;
 32//}}}
 33
 34public class EditAbbrevDialog extends JDialog
 35{
 36	//{{{ EditAbbrevDialog constructor
 37	/**
 38	 * @since jEdit 4.2pre3
 39	 */
 40	public EditAbbrevDialog(Frame frame, String abbrev, String expansion,
 41		Map abbrevs)
 42	{
 43		super(frame,jEdit.getProperty("edit-abbrev.title"),true);
 44		init(abbrev, expansion, abbrevs);
 45	} //}}}
 46
 47	//{{{ EditAbbrevDialog constructor
 48	public EditAbbrevDialog(Dialog dialog, String abbrev, String expansion,
 49		Map abbrevs)
 50	{
 51		super(dialog,jEdit.getProperty("edit-abbrev.title"),true);
 52		init(abbrev, expansion, abbrevs);
 53	} //}}}
 54
 55	//{{{ getAbbrev() method
 56	public String getAbbrev()
 57	{
 58		if(!isOK)
 59			return null;
 60
 61		return editor.getAbbrev();
 62	} //}}}
 63
 64	//{{{ getExpansion() method
 65	public String getExpansion()
 66	{
 67		if(!isOK)
 68			return null;
 69
 70		return editor.getExpansion();
 71	} //}}}
 72
 73	//{{{ Private members
 74	private AbbrevEditor editor;
 75	private JButton ok;
 76	private JButton cancel;
 77	private boolean isOK;
 78	private String originalAbbrev;
 79	private Map abbrevs;
 80
 81	//{{{ init() method
 82	private void init(String abbrev, String expansion, Map abbrevs)
 83	{
 84		this.abbrevs = abbrevs;
 85
 86		this.originalAbbrev = abbrev;
 87
 88		JPanel content = new JPanel(new BorderLayout());
 89		content.setBorder(new EmptyBorder(12,12,12,12));
 90		setContentPane(content);
 91
 92		editor = new AbbrevEditor();
 93		editor.setAbbrev(abbrev);
 94		editor.setExpansion(expansion);
 95		editor.setBorder(new EmptyBorder(0,0,12,0));
 96		content.add(BorderLayout.CENTER,editor);
 97
 98		Box box = new Box(BoxLayout.X_AXIS);
 99		box.add(Box.createGlue());
100		ok = new JButton(jEdit.getProperty("common.ok"));
101		ok.addActionListener(new ActionHandler());
102		getRootPane().setDefaultButton(ok);
103		box.add(ok);
104		box.add(Box.createHorizontalStrut(6));
105		cancel = new JButton(jEdit.getProperty("common.cancel"));
106		cancel.addActionListener(new ActionHandler());
107		box.add(cancel);
108		box.add(Box.createGlue());
109		content.add(BorderLayout.SOUTH,box);
110
111		KeyListener listener = new KeyHandler();
112		addKeyListener(listener);
113		editor.getBeforeCaretTextArea().addKeyListener(listener);
114		editor.getAfterCaretTextArea().addKeyListener(listener);
115
116		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
117		pack();
118		setLocationRelativeTo(getParent());
119		show();
120	} //}}}
121
122	//{{{ checkForExistingAbbrev() method
123	private boolean checkForExistingAbbrev()
124	{
125		String abbrev = editor.getAbbrev();
126		if(abbrevs.get(abbrev) != null)
127		{
128			if(abbrev.equals(originalAbbrev))
129				return true;
130
131			int result = GUIUtilities.confirm(this,
132				"edit-abbrev.duplicate",null,
133				JOptionPane.YES_NO_OPTION,
134				JOptionPane.WARNING_MESSAGE);
135			return (result == JOptionPane.YES_OPTION);
136		}
137
138		return true;
139	} //}}}
140
141	//}}}
142
143	//{{{ ActionHandler class
144	class ActionHandler implements ActionListener
145	{
146		public void actionPerformed(ActionEvent evt)
147		{
148			if(evt.getSource() == ok)
149			{
150				if(editor.getAbbrev() == null
151					|| editor.getAbbrev().length() == 0)
152				{
153					getToolkit().beep();
154					return;
155				}
156
157				if(!checkForExistingAbbrev())
158					return;
159
160				isOK = true;
161			}
162
163			dispose();
164		}
165	} //}}}
166
167	//{{{ KeyHandler class
168	class KeyHandler extends KeyAdapter
169	{
170		public void keyPressed(KeyEvent evt)
171		{
172			if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
173				dispose();
174		}
175	} //}}}
176}