KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 package org.gjt.sp.jedit.gui;
24
25 //{{{ Imports
26
import javax.swing.border.*;
27 import javax.swing.*;
28 import java.awt.event.*;
29 import java.awt.*;
30 import java.util.*;
31 import org.gjt.sp.jedit.*;
32 //}}}
33

34 public class EditAbbrevDialog extends JDialog
35 {
36     //{{{ EditAbbrevDialog constructor
37
/**
38      * @since jEdit 4.2pre3
39      */

40     public EditAbbrevDialog(Frame frame, String JavaDoc abbrev, String JavaDoc 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 JavaDoc abbrev, String JavaDoc 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 JavaDoc getAbbrev()
57     {
58         if(!isOK)
59             return null;
60
61         return editor.getAbbrev();
62     } //}}}
63

64     //{{{ getExpansion() method
65
public String JavaDoc 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 JavaDoc originalAbbrev;
79     private Map abbrevs;
80
81     //{{{ init() method
82
private void init(String JavaDoc abbrev, String JavaDoc 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         setVisible(true);
120     } //}}}
121

122     //{{{ checkForExistingAbbrev() method
123
private boolean checkForExistingAbbrev()
124     {
125         String JavaDoc 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
}
177
Popular Tags