/jEdit/tags/jedit-4-1-pre6/org/gjt/sp/jedit/gui/EditAbbrevDialog.java

# · Java · 133 lines · 85 code · 18 blank · 30 comment · 10 complexity · 9fbb84409498090eaf2db222d156fb2b MD5 · raw file

  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 org.gjt.sp.jedit.*;
  29. //}}}
  30. public class EditAbbrevDialog extends JDialog
  31. {
  32. //{{{ EditAbbrevDialog constructor
  33. public EditAbbrevDialog(Component comp, String abbrev, String expansion)
  34. {
  35. super(JOptionPane.getFrameForComponent(comp),
  36. jEdit.getProperty("edit-abbrev.title"),true);
  37. this.comp = comp;
  38. JPanel content = new JPanel(new BorderLayout());
  39. content.setBorder(new EmptyBorder(12,12,12,12));
  40. setContentPane(content);
  41. editor = new AbbrevEditor();
  42. editor.setAbbrev(abbrev);
  43. editor.setExpansion(expansion);
  44. editor.setBorder(new EmptyBorder(0,0,12,0));
  45. content.add(BorderLayout.CENTER,editor);
  46. Box box = new Box(BoxLayout.X_AXIS);
  47. box.add(Box.createGlue());
  48. ok = new JButton(jEdit.getProperty("common.ok"));
  49. ok.addActionListener(new ActionHandler());
  50. getRootPane().setDefaultButton(ok);
  51. box.add(ok);
  52. box.add(Box.createHorizontalStrut(6));
  53. cancel = new JButton(jEdit.getProperty("common.cancel"));
  54. cancel.addActionListener(new ActionHandler());
  55. box.add(cancel);
  56. box.add(Box.createGlue());
  57. content.add(BorderLayout.SOUTH,box);
  58. KeyListener listener = new KeyHandler();
  59. addKeyListener(listener);
  60. editor.getBeforeCaretTextArea().addKeyListener(listener);
  61. editor.getAfterCaretTextArea().addKeyListener(listener);
  62. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  63. pack();
  64. setLocationRelativeTo(comp);
  65. show();
  66. } //}}}
  67. //{{{ getAbbrev() method
  68. public String getAbbrev()
  69. {
  70. if(!isOK)
  71. return null;
  72. return editor.getAbbrev();
  73. } //}}}
  74. //{{{ getExpansion() method
  75. public String getExpansion()
  76. {
  77. if(!isOK)
  78. return null;
  79. return editor.getExpansion();
  80. } //}}}
  81. //{{{ Private members
  82. private Component comp;
  83. private AbbrevEditor editor;
  84. private JButton ok;
  85. private JButton cancel;
  86. private boolean isOK;
  87. //}}}
  88. //{{{ ActionHandler class
  89. class ActionHandler implements ActionListener
  90. {
  91. public void actionPerformed(ActionEvent evt)
  92. {
  93. if(evt.getSource() == ok)
  94. {
  95. if(editor.getAbbrev() == null
  96. || editor.getAbbrev().length() == 0)
  97. {
  98. getToolkit().beep();
  99. return;
  100. }
  101. isOK = true;
  102. }
  103. dispose();
  104. }
  105. } //}}}
  106. //{{{ KeyHandler class
  107. class KeyHandler extends KeyAdapter
  108. {
  109. public void keyPressed(KeyEvent evt)
  110. {
  111. if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
  112. dispose();
  113. }
  114. } //}}}
  115. }