PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/gui/AddAbbrevDialog.java

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