KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * AddAbbrevDialog.java - Displayed when expanding unknown abbrev
3  * Copyright (C) 2001 Slava Pestov
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */

19
20 package org.gjt.sp.jedit.gui;
21
22 import javax.swing.border.*;
23 import javax.swing.*;
24 import java.awt.event.*;
25 import java.awt.*;
26 import org.gjt.sp.jedit.*;
27
28 public class AddAbbrevDialog extends JDialog
29 {
30     public AddAbbrevDialog(View view, String JavaDoc abbrev)
31     {
32         super(view,jEdit.getProperty("add-abbrev.title"),true);
33
34         this.view = view;
35
36         JPanel content = new JPanel(new BorderLayout());
37         content.setBorder(new EmptyBorder(12,12,12,12));
38         setContentPane(content);
39
40         editor = new AbbrevEditor();
41         editor.setAbbrev(abbrev);
42         editor.setBorder(new EmptyBorder(6,0,12,0));
43         content.add(BorderLayout.CENTER,editor);
44
45         Box box = new Box(BoxLayout.X_AXIS);
46         box.add(Box.createGlue());
47         global = new JButton(jEdit.getProperty("add-abbrev.global"));
48         global.addActionListener(new ActionHandler());
49         box.add(global);
50         box.add(Box.createHorizontalStrut(6));
51         modeSpecific = new JButton(jEdit.getProperty("add-abbrev.mode"));
52         modeSpecific.addActionListener(new ActionHandler());
53         box.add(modeSpecific);
54         box.add(Box.createHorizontalStrut(6));
55         cancel = new JButton(jEdit.getProperty("common.cancel"));
56         cancel.addActionListener(new ActionHandler());
57         box.add(cancel);
58         box.add(Box.createGlue());
59         content.add(BorderLayout.SOUTH,box);
60
61         KeyListener listener = new KeyHandler();
62         addKeyListener(listener);
63         editor.getBeforeCaretTextArea().addKeyListener(listener);
64         editor.getAfterCaretTextArea().addKeyListener(listener);
65
66         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
67
68         if(abbrev == null)
69             GUIUtilities.requestFocus(this,editor.getAbbrevField());
70         else
71             GUIUtilities.requestFocus(this,editor.getBeforeCaretTextArea());
72
73         pack();
74         setLocationRelativeTo(view);
75         setVisible(true);
76     }
77
78     // private members
79
private View view;
80     private AbbrevEditor editor;
81     private JButton global;
82     private JButton modeSpecific;
83     private JButton cancel;
84
85     class ActionHandler implements ActionListener
86     {
87         public void actionPerformed(ActionEvent evt)
88         {
89             Object JavaDoc source = evt.getSource();
90             if(source == global)
91             {
92                 String JavaDoc _abbrev = editor.getAbbrev();
93                 if(_abbrev == null || _abbrev.length() == 0)
94                 {
95                     getToolkit().beep();
96                     return;
97                 }
98                 Abbrevs.addGlobalAbbrev(_abbrev,editor.getExpansion());
99                 Abbrevs.expandAbbrev(view,false);
100             }
101             else if(source == modeSpecific)
102             {
103                 String JavaDoc _abbrev = editor.getAbbrev();
104                 if(_abbrev == null || _abbrev.length() == 0)
105                 {
106                     getToolkit().beep();
107                     return;
108                 }
109                 Abbrevs.addModeAbbrev(view.getBuffer().getMode().getName(),
110                     _abbrev,editor.getExpansion());
111                 Abbrevs.expandAbbrev(view,false);
112             }
113
114             dispose();
115         }
116     }
117
118     class KeyHandler extends KeyAdapter
119     {
120         public void keyPressed(KeyEvent evt)
121         {
122             if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
123                 dispose();
124         }
125     }
126 }
127
Popular Tags