SearchPopUp.java 8.4 KB

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