001: /*
002: * EditAbbrevDialog.java - Displayed when editing abbrevs
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.event.*;
029: import java.awt.*;
030: import java.util.*;
031: import org.gjt.sp.jedit.*;
032:
033: //}}}
034:
035: public class EditAbbrevDialog extends JDialog {
036: //{{{ EditAbbrevDialog constructor
037: /**
038: * @since jEdit 4.2pre3
039: */
040: public EditAbbrevDialog(Frame frame, String abbrev,
041: String expansion, Map abbrevs) {
042: super (frame, jEdit.getProperty("edit-abbrev.title"), true);
043: init(abbrev, expansion, abbrevs);
044: } //}}}
045:
046: //{{{ EditAbbrevDialog constructor
047: public EditAbbrevDialog(Dialog dialog, String abbrev,
048: String expansion, Map abbrevs) {
049: super (dialog, jEdit.getProperty("edit-abbrev.title"), true);
050: init(abbrev, expansion, abbrevs);
051: } //}}}
052:
053: //{{{ getAbbrev() method
054: public String getAbbrev() {
055: if (!isOK)
056: return null;
057:
058: return editor.getAbbrev();
059: } //}}}
060:
061: //{{{ getExpansion() method
062: public String getExpansion() {
063: if (!isOK)
064: return null;
065:
066: return editor.getExpansion();
067: } //}}}
068:
069: //{{{ Private members
070: private AbbrevEditor editor;
071: private JButton ok;
072: private JButton cancel;
073: private boolean isOK;
074: private String originalAbbrev;
075: private Map abbrevs;
076:
077: //{{{ init() method
078: private void init(String abbrev, String expansion, Map abbrevs) {
079: this .abbrevs = abbrevs;
080:
081: this .originalAbbrev = abbrev;
082:
083: JPanel content = new JPanel(new BorderLayout());
084: content.setBorder(new EmptyBorder(12, 12, 12, 12));
085: setContentPane(content);
086:
087: editor = new AbbrevEditor();
088: editor.setAbbrev(abbrev);
089: editor.setExpansion(expansion);
090: editor.setBorder(new EmptyBorder(0, 0, 12, 0));
091: content.add(BorderLayout.CENTER, editor);
092:
093: Box box = new Box(BoxLayout.X_AXIS);
094: box.add(Box.createGlue());
095: ok = new JButton(jEdit.getProperty("common.ok"));
096: ok.addActionListener(new ActionHandler());
097: getRootPane().setDefaultButton(ok);
098: box.add(ok);
099: box.add(Box.createHorizontalStrut(6));
100: cancel = new JButton(jEdit.getProperty("common.cancel"));
101: cancel.addActionListener(new ActionHandler());
102: box.add(cancel);
103: box.add(Box.createGlue());
104: content.add(BorderLayout.SOUTH, box);
105:
106: KeyListener listener = new KeyHandler();
107: addKeyListener(listener);
108: editor.getBeforeCaretTextArea().addKeyListener(listener);
109: editor.getAfterCaretTextArea().addKeyListener(listener);
110:
111: setDefaultCloseOperation(DISPOSE_ON_CLOSE);
112: pack();
113: setLocationRelativeTo(getParent());
114: setVisible(true);
115: } //}}}
116:
117: //{{{ checkForExistingAbbrev() method
118: private boolean checkForExistingAbbrev() {
119: String abbrev = editor.getAbbrev();
120: if (abbrevs.get(abbrev) != null) {
121: if (abbrev.equals(originalAbbrev))
122: return true;
123:
124: int result = GUIUtilities.confirm(this ,
125: "edit-abbrev.duplicate", null,
126: JOptionPane.YES_NO_OPTION,
127: JOptionPane.WARNING_MESSAGE);
128: return (result == JOptionPane.YES_OPTION);
129: }
130:
131: return true;
132: } //}}}
133:
134: //}}}
135:
136: //{{{ ActionHandler class
137: class ActionHandler implements ActionListener {
138: public void actionPerformed(ActionEvent evt) {
139: if (evt.getSource() == ok) {
140: if (editor.getAbbrev() == null
141: || editor.getAbbrev().length() == 0) {
142: getToolkit().beep();
143: return;
144: }
145:
146: if (!checkForExistingAbbrev())
147: return;
148:
149: isOK = true;
150: }
151:
152: dispose();
153: }
154: } //}}}
155:
156: //{{{ KeyHandler class
157: class KeyHandler extends KeyAdapter {
158: public void keyPressed(KeyEvent evt) {
159: if (evt.getKeyCode() == KeyEvent.VK_ESCAPE)
160: dispose();
161: }
162: } //}}}
163: }
|