PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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