SearchPopUp.java 8.5 KB

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