SearchPopUp.java 8.1 KB

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