SearchPopUp.java 8.4 KB

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