001: /*
002: * AddAbbrevDialog.java - Displayed when expanding unknown abbrev
003: * Copyright (C) 2001 Slava Pestov
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019:
020: package org.gjt.sp.jedit.gui;
021:
022: import javax.swing.border.*;
023: import javax.swing.*;
024: import java.awt.event.*;
025: import java.awt.*;
026: import org.gjt.sp.jedit.*;
027:
028: public class AddAbbrevDialog extends JDialog {
029: public AddAbbrevDialog(View view, String abbrev) {
030: super (view, jEdit.getProperty("add-abbrev.title"), true);
031:
032: this .view = view;
033:
034: JPanel content = new JPanel(new BorderLayout());
035: content.setBorder(new EmptyBorder(12, 12, 12, 12));
036: setContentPane(content);
037:
038: editor = new AbbrevEditor();
039: editor.setAbbrev(abbrev);
040: editor.setBorder(new EmptyBorder(6, 0, 12, 0));
041: content.add(BorderLayout.CENTER, editor);
042:
043: Box box = new Box(BoxLayout.X_AXIS);
044: box.add(Box.createGlue());
045: global = new JButton(jEdit.getProperty("add-abbrev.global"));
046: global.addActionListener(new ActionHandler());
047: box.add(global);
048: box.add(Box.createHorizontalStrut(6));
049: modeSpecific = new JButton(jEdit.getProperty("add-abbrev.mode"));
050: modeSpecific.addActionListener(new ActionHandler());
051: box.add(modeSpecific);
052: box.add(Box.createHorizontalStrut(6));
053: cancel = new JButton(jEdit.getProperty("common.cancel"));
054: cancel.addActionListener(new ActionHandler());
055: box.add(cancel);
056: box.add(Box.createGlue());
057: content.add(BorderLayout.SOUTH, box);
058:
059: KeyListener listener = new KeyHandler();
060: addKeyListener(listener);
061: editor.getBeforeCaretTextArea().addKeyListener(listener);
062: editor.getAfterCaretTextArea().addKeyListener(listener);
063:
064: setDefaultCloseOperation(DISPOSE_ON_CLOSE);
065:
066: if (abbrev == null)
067: GUIUtilities.requestFocus(this , editor.getAbbrevField());
068: else
069: GUIUtilities.requestFocus(this , editor
070: .getBeforeCaretTextArea());
071:
072: pack();
073: setLocationRelativeTo(view);
074: setVisible(true);
075: }
076:
077: // private members
078: private View view;
079: private AbbrevEditor editor;
080: private JButton global;
081: private JButton modeSpecific;
082: private JButton cancel;
083:
084: class ActionHandler implements ActionListener {
085: public void actionPerformed(ActionEvent evt) {
086: Object source = evt.getSource();
087: if (source == global) {
088: String _abbrev = editor.getAbbrev();
089: if (_abbrev == null || _abbrev.length() == 0) {
090: getToolkit().beep();
091: return;
092: }
093: Abbrevs.addGlobalAbbrev(_abbrev, editor.getExpansion());
094: Abbrevs.expandAbbrev(view, false);
095: } else if (source == modeSpecific) {
096: String _abbrev = editor.getAbbrev();
097: if (_abbrev == null || _abbrev.length() == 0) {
098: getToolkit().beep();
099: return;
100: }
101: Abbrevs.addModeAbbrev(view.getBuffer().getMode()
102: .getName(), _abbrev, editor.getExpansion());
103: Abbrevs.expandAbbrev(view, false);
104: }
105:
106: dispose();
107: }
108: }
109:
110: class KeyHandler extends KeyAdapter {
111: public void keyPressed(KeyEvent evt) {
112: if (evt.getKeyCode() == KeyEvent.VK_ESCAPE)
113: dispose();
114: }
115: }
116: }
|