/illaeasynpc/src/main/java/illarion/easynpc/gui/SearchDialog.java

http://github.com/Illarion-eV/Illarion-Java · Java · 175 lines · 115 code · 32 blank · 28 comment · 0 complexity · cccad4e94850457acc2ae32197712e12 MD5 · raw file

  1. /*
  2. * This file is part of the Illarion project.
  3. *
  4. * Copyright © 2015 - Illarion e.V.
  5. *
  6. * Illarion is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Illarion is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. package illarion.easynpc.gui;
  17. import illarion.easynpc.Lang;
  18. import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
  19. import org.fife.ui.rtextarea.SearchContext;
  20. import org.fife.ui.rtextarea.SearchEngine;
  21. import javax.annotation.Nonnull;
  22. import javax.swing.*;
  23. import java.awt.*;
  24. /**
  25. * This is the advanced search dialog that can be used to search and replace contents of the script that is currently
  26. * load.
  27. *
  28. * @author Martin Karing <nitram@illarion.org>
  29. */
  30. public final class SearchDialog extends JDialog {
  31. /**
  32. * The serialization UID of this dialog.
  33. */
  34. private static final long serialVersionUID = 1L;
  35. /**
  36. * The constructor of the dialog that prepares the dialog for proper
  37. * display.
  38. */
  39. public SearchDialog(@Nonnull MainFrame frame) {
  40. super(frame, Lang.getMsg(SearchDialog.class, "title"), false);
  41. SearchDialog dialog = this;
  42. JPanel mainPanel = new JPanel(new GridBagLayout());
  43. getContentPane().add(mainPanel);
  44. GridBagConstraints generalConstraints = new GridBagConstraints();
  45. generalConstraints.gridheight = 1;
  46. generalConstraints.gridwidth = 1;
  47. generalConstraints.insets.bottom = 5;
  48. generalConstraints.insets.right = 5;
  49. generalConstraints.insets.left = 5;
  50. generalConstraints.insets.top = 5;
  51. generalConstraints.anchor = GridBagConstraints.WEST;
  52. JLabel searchLabel = new JLabel(Lang.getMsg(SearchDialog.class, "searchForLabel"));
  53. JLabel replaceLabel = new JLabel(Lang.getMsg(SearchDialog.class, "replaceWithLabel"));
  54. JTextField searchText = new JTextField();
  55. JTextField replaceField = new JTextField();
  56. searchText.setPreferredSize(new Dimension(300, searchText.getPreferredSize().height));
  57. replaceField.setPreferredSize(new Dimension(300, replaceField.getPreferredSize().height));
  58. JCheckBox caseSensitiveCheck = new JCheckBox(Lang.getMsg(SearchDialog.class, "caseCheck"));
  59. JCheckBox regExpCheck = new JCheckBox(Lang.getMsg(SearchDialog.class, "regExpCheck"));
  60. JButton findNextBtn = new JButton(Lang.getMsg(SearchDialog.class, "findNextButton"));
  61. JButton replaceOneBtn = new JButton(Lang.getMsg(SearchDialog.class, "replaceButton"));
  62. JButton replaceAllBtn = new JButton(Lang.getMsg(SearchDialog.class, "replaceAllButton"));
  63. JButton closeBtn = new JButton(Lang.getMsg(SearchDialog.class, "closeButton"));
  64. Dimension buttonDim = findNextBtn.getPreferredSize();
  65. buttonDim.width = Math.max(buttonDim.width, replaceOneBtn.getPreferredSize().width);
  66. buttonDim.width = Math.max(buttonDim.width, replaceAllBtn.getPreferredSize().width);
  67. buttonDim.width = Math.max(buttonDim.width, closeBtn.getPreferredSize().width);
  68. buttonDim.width += 10;
  69. findNextBtn.setPreferredSize(buttonDim);
  70. replaceOneBtn.setPreferredSize(buttonDim);
  71. replaceAllBtn.setPreferredSize(buttonDim);
  72. closeBtn.setPreferredSize(buttonDim);
  73. findNextBtn.addActionListener(e -> {
  74. Editor scriptEditor = frame.getCurrentScriptEditor();
  75. RSyntaxTextArea editor = scriptEditor.getEditor();
  76. SearchContext search = new SearchContext();
  77. search.setSearchFor(searchText.getText());
  78. search.setMatchCase(caseSensitiveCheck.isSelected());
  79. search.setRegularExpression(regExpCheck.isSelected());
  80. search.setSearchForward(true);
  81. SearchEngine.find(editor, search);
  82. });
  83. replaceOneBtn.addActionListener(e -> {
  84. Editor scriptEditor = frame.getCurrentScriptEditor();
  85. RSyntaxTextArea editor = scriptEditor.getEditor();
  86. SearchContext search = new SearchContext();
  87. search.setSearchFor(searchText.getText());
  88. search.setReplaceWith(replaceField.getText());
  89. search.setMatchCase(caseSensitiveCheck.isSelected());
  90. search.setRegularExpression(regExpCheck.isSelected());
  91. search.setSearchForward(true);
  92. SearchEngine.replace(editor, search);
  93. });
  94. replaceAllBtn.addActionListener(e -> {
  95. Editor scriptEditor = frame.getCurrentScriptEditor();
  96. RSyntaxTextArea editor = scriptEditor.getEditor();
  97. SearchContext search = new SearchContext();
  98. search.setSearchFor(searchText.getText());
  99. search.setReplaceWith(replaceField.getText());
  100. search.setMatchCase(caseSensitiveCheck.isSelected());
  101. search.setRegularExpression(regExpCheck.isSelected());
  102. search.setSearchForward(true);
  103. SearchEngine.replaceAll(editor, search);
  104. });
  105. closeBtn.addActionListener(e -> dialog.setVisible(false));
  106. generalConstraints.gridwidth = GridBagConstraints.REMAINDER;
  107. mainPanel.add(searchLabel, generalConstraints);
  108. generalConstraints.gridwidth = 1;
  109. generalConstraints.insets.bottom = 20;
  110. mainPanel.add(searchText, generalConstraints);
  111. generalConstraints.gridwidth = GridBagConstraints.REMAINDER;
  112. mainPanel.add(findNextBtn, generalConstraints);
  113. generalConstraints.gridwidth = GridBagConstraints.REMAINDER;
  114. generalConstraints.insets.bottom = 5;
  115. mainPanel.add(replaceLabel, generalConstraints);
  116. generalConstraints.gridwidth = 1;
  117. mainPanel.add(replaceField, generalConstraints);
  118. generalConstraints.gridwidth = GridBagConstraints.REMAINDER;
  119. mainPanel.add(replaceOneBtn, generalConstraints);
  120. generalConstraints.gridwidth = 1;
  121. generalConstraints.insets.bottom = 20;
  122. mainPanel.add(new JLabel(), generalConstraints);
  123. generalConstraints.gridwidth = GridBagConstraints.REMAINDER;
  124. mainPanel.add(replaceAllBtn, generalConstraints);
  125. generalConstraints.gridwidth = 1;
  126. generalConstraints.insets.bottom = 5;
  127. mainPanel.add(caseSensitiveCheck, generalConstraints);
  128. generalConstraints.gridwidth = GridBagConstraints.REMAINDER;
  129. mainPanel.add(new JLabel(), generalConstraints);
  130. generalConstraints.gridwidth = 1;
  131. mainPanel.add(regExpCheck, generalConstraints);
  132. generalConstraints.gridwidth = GridBagConstraints.REMAINDER;
  133. mainPanel.add(closeBtn, generalConstraints);
  134. setAlwaysOnTop(true);
  135. validate();
  136. pack();
  137. Dimension parentDim = getOwner().getSize();
  138. Point parentPos = getOwner().getLocation();
  139. setLocation(((parentDim.width - getSize().width) / 2) + parentPos.x,
  140. ((parentDim.height - getSize().height) / 2) + parentPos.y);
  141. setResizable(false);
  142. }
  143. }