SearchPopUp.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package holeg.ui.view.dialog;
  2. import java.awt.BorderLayout;
  3. import java.awt.Font;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import javax.swing.ButtonGroup;
  7. import javax.swing.JButton;
  8. import javax.swing.JDialog;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JPanel;
  12. import javax.swing.JRadioButton;
  13. import javax.swing.JTextField;
  14. import javax.swing.border.EmptyBorder;
  15. import holeg.model.AbstractCanvasObject;
  16. import holeg.ui.controller.Control;
  17. import holeg.ui.model.GuiSettings;
  18. import holeg.ui.view.canvas.Canvas;
  19. /**
  20. * This Class represents a popup to seatch for Objects on the Canvas.
  21. *
  22. * @author Gruppe14
  23. */
  24. public class SearchPopUp extends JDialog {
  25. private static final long serialVersionUID = 1L;
  26. private final JPanel contentPanel = new JPanel();
  27. private JTextField replaceTextField;
  28. private JTextField findTextField;
  29. private Control control;
  30. private Canvas canvas;
  31. private JRadioButton rdbtnForward;
  32. private JRadioButton rdbtnBackward;
  33. private JRadioButton rdbtnAll;
  34. private JRadioButton rdbtnSingle;
  35. private int idx;
  36. /**
  37. * Constructor.
  38. *
  39. * @param contr
  40. * Controller
  41. * @param can
  42. * Canvas
  43. */
  44. public SearchPopUp(Control contr, Canvas can, JFrame parentFrame) {
  45. super((java.awt.Frame) null, true);
  46. idx = -1;
  47. setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
  48. this.setTitle("Search for Objects");
  49. setBounds(100, 100, 250, 360);
  50. setLocationRelativeTo(parentFrame);
  51. getContentPane().setLayout(new BorderLayout());
  52. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  53. getContentPane().add(contentPanel, BorderLayout.CENTER);
  54. contentPanel.setLayout(null);
  55. this.control = contr;
  56. this.canvas = can;
  57. JLabel lblFind = new JLabel("Find" + ":");
  58. lblFind.setFont(new Font("Tahoma", Font.PLAIN, 13));
  59. lblFind.setBounds(10, 11, 46, 19);
  60. contentPanel.add(lblFind);
  61. JLabel lblReplace = new JLabel("Replace" + ":");
  62. lblReplace.setFont(new Font("Tahoma", Font.PLAIN, 13));
  63. lblReplace.setBounds(10, 41, 80, 14);
  64. contentPanel.add(lblReplace);
  65. // ReplaceTest
  66. replaceTextField = new JTextField();
  67. replaceTextField.setBounds(90, 39, 101, 20);
  68. contentPanel.add(replaceTextField);
  69. replaceTextField.setColumns(10);
  70. // FindText
  71. findTextField = new JTextField();
  72. findTextField.setBounds(90, 11, 101, 20);
  73. contentPanel.add(findTextField);
  74. findTextField.setColumns(10);
  75. JLabel lblNewLabel = new JLabel("Direction");
  76. lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
  77. lblNewLabel.setBounds(10, 90, 82, 14);
  78. contentPanel.add(lblNewLabel);
  79. rdbtnForward = new JRadioButton("Forward");
  80. rdbtnForward.setFont(new Font("Tahoma", Font.PLAIN, 13));
  81. rdbtnForward.setBounds(10, 111, 109, 23);
  82. contentPanel.add(rdbtnForward);
  83. rdbtnForward.setSelected(true);
  84. rdbtnBackward = new JRadioButton("Backward");
  85. rdbtnBackward.setFont(new Font("Tahoma", Font.PLAIN, 13));
  86. rdbtnBackward.setBounds(10, 137, 109, 23);
  87. contentPanel.add(rdbtnBackward);
  88. JLabel lblScope = new JLabel("Scope");
  89. lblScope.setFont(new Font("Tahoma", Font.BOLD, 13));
  90. lblScope.setBounds(122, 90, 60, 14);
  91. contentPanel.add(lblScope);
  92. rdbtnAll = new JRadioButton("All");
  93. rdbtnAll.setFont(new Font("Tahoma", Font.PLAIN, 13));
  94. rdbtnAll.setBounds(121, 112, 109, 23);
  95. contentPanel.add(rdbtnAll);
  96. rdbtnAll.setSelected(true);
  97. rdbtnSingle = new JRadioButton("Single");
  98. rdbtnSingle.setFont(new Font("Tahoma", Font.PLAIN, 13));
  99. rdbtnSingle.setBounds(121, 138, 109, 23);
  100. contentPanel.add(rdbtnSingle);
  101. // FindButton
  102. JButton btnFind = new JButton("Find");
  103. btnFind.addActionListener(new ActionListener() {
  104. public void actionPerformed(ActionEvent e) {
  105. if (rdbtnAll.isSelected()) {
  106. for (AbstractCanvasObject cps : control.getModel().getObjectsOnCanvas()) {
  107. if (cps.getName().equals(findTextField.getText())
  108. && !GuiSettings.getSelectedObjects().contains(cps)) {
  109. GuiSettings.getSelectedObjects().add(cps);
  110. }
  111. }
  112. }
  113. if (rdbtnSingle.isSelected()) {
  114. GuiSettings.getSelectedObjects().clear();
  115. if (!control.getModel().getObjectsOnCanvas().isEmpty() && !findTextField.getText().isEmpty()) {
  116. if (rdbtnForward.isSelected()) {
  117. if ((idx = getNext(++idx)) != -1)
  118. GuiSettings.getSelectedObjects().add(getObj(idx));
  119. } else if (rdbtnBackward.isSelected()) {
  120. if ((idx = getPrev(--idx)) != -1)
  121. GuiSettings.getSelectedObjects().add(getObj(idx));
  122. }
  123. }
  124. }
  125. canvas.repaint();
  126. }
  127. });
  128. btnFind.setFont(new Font("Tahoma", Font.PLAIN, 13));
  129. btnFind.setBounds(10, 186, 110, 23);
  130. contentPanel.add(btnFind);
  131. // ReplaceButton
  132. JButton btnReplace = new JButton("Replace");
  133. btnReplace.addActionListener(new ActionListener() {
  134. public void actionPerformed(ActionEvent e) {
  135. //TODO(Tom2021-12-1) fix or remove SearchPopUp
  136. System.out.println("REPLACE");
  137. canvas.repaint();
  138. }
  139. });
  140. btnReplace.setFont(new Font("Tahoma", Font.PLAIN, 13));
  141. btnReplace.setBounds(110, 187, 110, 23);
  142. contentPanel.add(btnReplace);
  143. // ReplaceAllButton
  144. JButton btnReplaceAll = new JButton("Replace All");
  145. btnReplaceAll.addActionListener(new ActionListener() {
  146. public void actionPerformed(ActionEvent e) {
  147. canvas.tempCps = null;
  148. GuiSettings.getSelectedObjects().clear();
  149. for (AbstractCanvasObject cps : control.getModel().getObjectsOnCanvas()) {
  150. if (cps.getName().equals(findTextField.getText())
  151. && !GuiSettings.getSelectedObjects().contains(cps))
  152. selectObj(cps);
  153. }
  154. for (AbstractCanvasObject cps : GuiSettings.getSelectedObjects()) {
  155. renameObj(cps, replaceTextField.getText());
  156. }
  157. canvas.repaint();
  158. }
  159. });
  160. btnReplaceAll.setFont(new Font("Tahoma", Font.PLAIN, 11));
  161. btnReplaceAll.setBounds(110, 218, 110, 23);
  162. contentPanel.add(btnReplaceAll);
  163. // CloseButton
  164. JButton btnClose = new JButton("Close");
  165. btnClose.addActionListener(new ActionListener() {
  166. public void actionPerformed(ActionEvent arg0) {
  167. dispose();
  168. }
  169. });
  170. btnClose.setFont(new Font("Tahoma", Font.PLAIN, 13));
  171. btnClose.setBounds(110, 287, 110, 23);
  172. contentPanel.add(btnClose);
  173. ButtonGroup directionbtns = new ButtonGroup();
  174. ButtonGroup scopebtns = new ButtonGroup();
  175. directionbtns.add(rdbtnBackward);
  176. directionbtns.add(rdbtnForward);
  177. scopebtns.add(rdbtnSingle);
  178. scopebtns.add(rdbtnAll);
  179. }
  180. /**
  181. * add the searched Objects to the Selected Objects.
  182. *
  183. * @param obj
  184. * The Object
  185. */
  186. public void selectObj(AbstractCanvasObject obj) {
  187. GuiSettings.getSelectedObjects().add(obj);
  188. }
  189. /**
  190. * Rename an Object.
  191. *
  192. * @param obj
  193. * the Object
  194. * @param name
  195. * the new name
  196. */
  197. public void renameObj(AbstractCanvasObject obj, String name) {
  198. obj.setName(name);
  199. }
  200. /**
  201. * get the Object with the specific ID.
  202. *
  203. * @param idx
  204. * the ID
  205. * @return the Object with that ID
  206. */
  207. public AbstractCanvasObject getObj(int idx) {
  208. return control.getModel().getObjectsOnCanvas().get(idx);
  209. }
  210. /**
  211. * Get the next Object ID.
  212. *
  213. * @param idx
  214. * the Index
  215. * @return the next Index
  216. */
  217. public int getNext(int idx) {
  218. for (int i = idx; i < control.getModel().getObjectsOnCanvas().size(); i++) {
  219. if (getObj(i).getName().equals(findTextField.getText())
  220. && !GuiSettings.getSelectedObjects().contains(getObj(i)))
  221. return i;
  222. }
  223. for (int i = 0; i < idx; i++) {
  224. if (getObj(i).getName().equals(findTextField.getText())
  225. && !GuiSettings.getSelectedObjects().contains(getObj(i)))
  226. return i;
  227. }
  228. return -1;
  229. }
  230. /**
  231. * Get the previous Index.
  232. *
  233. * @param idx
  234. * the Index
  235. * @return the previousIndex
  236. */
  237. public int getPrev(int idx) {
  238. for (int i = idx; i >= 0; i--) {
  239. if (getObj(i).getName().equals(findTextField.getText())
  240. && !GuiSettings.getSelectedObjects().contains(getObj(i)))
  241. return i;
  242. }
  243. for (int i = control.getModel().getObjectsOnCanvas().size() - 1; i > idx; i--) {
  244. if (getObj(i).getName().equals(findTextField.getText())
  245. && !GuiSettings.getSelectedObjects().contains(getObj(i)))
  246. return i;
  247. }
  248. return -1;
  249. }
  250. }