CreateTemplatePopUp.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. package holeg.ui.view.dialog;
  2. import java.awt.BorderLayout;
  3. import java.awt.Choice;
  4. import java.io.File;
  5. import javax.swing.DefaultListModel;
  6. import javax.swing.ImageIcon;
  7. import javax.swing.JButton;
  8. import javax.swing.JDialog;
  9. import javax.swing.JFileChooser;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JList;
  13. import javax.swing.JPanel;
  14. import javax.swing.JScrollPane;
  15. import javax.swing.JTextField;
  16. import javax.swing.border.EmptyBorder;
  17. import javax.swing.filechooser.FileNameExtensionFilter;
  18. import holeg.model.HolonElement;
  19. import holeg.model.HolonObject;
  20. import holeg.preferences.ImagePreference;
  21. import holeg.ui.controller.Control;
  22. import holeg.ui.model.GuiSettings;
  23. import holeg.model.Model;
  24. import holeg.ui.view.category.Category;
  25. import holeg.ui.view.image.Import;
  26. /**
  27. * PopUp for creating Holon Object Template.
  28. *
  29. * @author Gruppe 07 (A.T.M-B)
  30. */
  31. public class CreateTemplatePopUp extends JDialog {
  32. /**
  33. * Template HolonObject
  34. */
  35. private HolonObject template;
  36. /**
  37. * HolonElementList
  38. */
  39. DefaultListModel<String> listModel;
  40. /**
  41. * HolonElement List
  42. */
  43. JList<String> list;
  44. /**
  45. * Category the Template should be inserted into
  46. */
  47. private String category;
  48. // Template Attributes
  49. // PopUp Parts
  50. private Control controller;
  51. /**
  52. * Category Selection
  53. */
  54. Choice choice;
  55. /**
  56. * name textfield
  57. */
  58. private JTextField textField_name;
  59. /**
  60. * textField for path
  61. */
  62. private JTextField textField_imagePath;
  63. /**
  64. * Image Preview
  65. */
  66. JLabel lblImagePreview;
  67. /**
  68. * parent Frame
  69. */
  70. JFrame parent;
  71. /**
  72. * Create the dialog.
  73. *
  74. * true if edit
  75. * @param obj
  76. * the object
  77. * @param model
  78. * the categorie
  79. */
  80. public CreateTemplatePopUp(HolonObject obj, Model model,
  81. JFrame parentFrame, Control controller) {
  82. setResizable(false);
  83. /*
  84. * use Category Controller an stuff lul
  85. */
  86. /*
  87. * initialize Data
  88. */
  89. template = new HolonObject(obj);
  90. this.parent = parentFrame;
  91. this.controller = controller;
  92. /*
  93. * create Frame and GUI
  94. */
  95. setIconImage(Import.loadImage(ImagePreference.Logo, 30, 30));
  96. setBounds(100, 100, 476, 344);
  97. setLocationRelativeTo(parentFrame);
  98. getContentPane().setLayout(new BorderLayout());
  99. /**
  100. * Content Panel
  101. */
  102. JPanel contentPanel = new JPanel();
  103. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  104. getContentPane().add(contentPanel, BorderLayout.CENTER);
  105. contentPanel.setLayout(null);
  106. /**
  107. * Category Label
  108. */
  109. JLabel lblCategory = new JLabel("Category:");
  110. lblCategory.setBounds(12, 13, 68, 22);
  111. contentPanel.add(lblCategory);
  112. /**
  113. * Category Choice
  114. */
  115. choice = new Choice();
  116. choice.setBounds(86, 13, 172, 22);
  117. contentPanel.add(choice);
  118. // add categories
  119. if (GuiSettings.getCategories().isEmpty()){
  120. this.controller.createCategoryWithName("Template");
  121. }
  122. // add Categories to the choice
  123. for (Category c : GuiSettings.getCategories())
  124. choice.add(c.getName());
  125. /**
  126. * Name Label
  127. */
  128. JLabel lblName = new JLabel("Name:");
  129. lblName.setBounds(12, 48, 56, 16);
  130. contentPanel.add(lblName);
  131. /**
  132. * Name Textfield
  133. */
  134. textField_name = new JTextField();
  135. textField_name.setBounds(86, 48, 172, 22);
  136. contentPanel.add(textField_name);
  137. textField_name.setColumns(10);
  138. textField_name.setText(template.getName());
  139. /**
  140. * Image Path Lable
  141. */
  142. JLabel lblImage = new JLabel("Image:");
  143. lblImage.setBounds(12, 89, 56, 16);
  144. contentPanel.add(lblImage);
  145. /**
  146. * Image Path Text Field
  147. */
  148. textField_imagePath = new JTextField();
  149. textField_imagePath.setBounds(86, 86, 172, 22);
  150. contentPanel.add(textField_imagePath);
  151. textField_imagePath.setColumns(10);
  152. textField_imagePath.setText(template.getImagePath());
  153. /**
  154. * Browse Image Button
  155. */
  156. JButton btnBrowseImage = new JButton("BrowseImage");
  157. btnBrowseImage.setBounds(268, 85, 117, 25);
  158. contentPanel.add(btnBrowseImage);
  159. btnBrowseImage.addActionListener(actionevent -> {
  160. fileChooser();
  161. });
  162. /**
  163. * Image Preview
  164. */
  165. lblImagePreview = new JLabel("Image Preview");
  166. lblImagePreview.setIcon(new ImageIcon(Import.loadImage(
  167. template.getImagePath(), 62, 62)));
  168. lblImagePreview.setBounds(298, 13, 62, 62);
  169. contentPanel.add(lblImagePreview);
  170. /**
  171. * Holon Element List
  172. */
  173. listModel = new DefaultListModel<String>();
  174. /**
  175. * Add Elements to List
  176. */
  177. template.elementsStream().forEach(hE -> {
  178. listModel.addElement(hE.getName()
  179. + ": " + hE.getEnergy() + "U");
  180. });
  181. /**
  182. * Add ScrollPane to List
  183. */
  184. JScrollPane scrollPane = new JScrollPane();
  185. scrollPane.setBounds(22, 118, 236, 150);
  186. contentPanel.add(scrollPane);
  187. list = new JList<String>(listModel);
  188. scrollPane.setViewportView(list);
  189. /**
  190. * Delete Element Button
  191. */
  192. JButton btnDeleteElement = new JButton("Delete Element");
  193. btnDeleteElement.setBounds(268, 228, 140, 25);
  194. contentPanel.add(btnDeleteElement);
  195. btnDeleteElement.addActionListener(e -> removeElement());
  196. /**
  197. * Edit Element Button
  198. */
  199. JButton btnEditElement = new JButton("Edit Element");
  200. btnEditElement.setBounds(268, 190, 140, 25);
  201. contentPanel.add(btnEditElement);
  202. btnEditElement.addActionListener(e -> editElement());
  203. /**
  204. * Add Element Button
  205. */
  206. JButton btnAddElement = new JButton("Add Element");
  207. btnAddElement.setBounds(268, 152, 140, 25);
  208. contentPanel.add(btnAddElement);
  209. btnAddElement.addActionListener(e -> addElement());
  210. /**
  211. * Cancel Button
  212. */
  213. JButton btnCancel = new JButton("Cancel");
  214. btnCancel.setBounds(384, 277, 74, 25);
  215. contentPanel.add(btnCancel);
  216. btnCancel.addActionListener(e -> dispose());
  217. /**
  218. * Add Template Button
  219. */
  220. JButton btnAddTemplate = new JButton("Add Template");
  221. btnAddTemplate.setBounds(75, 271, 113, 25);
  222. contentPanel.add(btnAddTemplate);
  223. btnAddTemplate.addActionListener(e -> createTemplate());
  224. /**
  225. * Title
  226. */
  227. setTitle("Create Template Menu");
  228. }
  229. /**
  230. * Choose the file.
  231. */
  232. private void fileChooser() {
  233. JFileChooser fileChooser = new JFileChooser();
  234. FileNameExtensionFilter filter = new FileNameExtensionFilter(
  235. "png, jpg or jpeg", "png", "jpg", "jpeg");
  236. fileChooser.setFileFilter(filter);
  237. int returnValue = fileChooser.showOpenDialog(null);
  238. if (returnValue == JFileChooser.APPROVE_OPTION) {
  239. File selectedFile = fileChooser.getSelectedFile();
  240. String filePath = selectedFile.getAbsolutePath();
  241. textField_imagePath.setText(filePath);
  242. ImageIcon icon = new ImageIcon(Import.loadImage(filePath, 62,
  243. 62));
  244. lblImagePreview.setIcon(icon);
  245. } else {
  246. System.out.println("Failed to Load");
  247. }
  248. }
  249. /**
  250. * create the template and add it to the category
  251. */
  252. private void createTemplate() {
  253. template.setName(textField_name.getText());
  254. template.setImagePath(textField_imagePath.getText());
  255. controller.findCategoryWithName(choice
  256. .getItem(choice.getSelectedIndex())).ifPresent(cat -> {
  257. controller.addObject(cat, template.getName(),
  258. template.elementsStream().toList(), template.getImagePath());
  259. });
  260. this.dispose();
  261. }
  262. /**
  263. * Add an Holon Element to the template
  264. */
  265. private void addElement() {
  266. AddElementPopUp popUp = new AddElementPopUp(parent);
  267. popUp.setActualHolonObject(template);
  268. popUp.setVisible(true);
  269. HolonElement he = popUp.getElement();
  270. if (he != null) {
  271. listModel.addElement(he.getName()
  272. + ": " + he.getEnergy() + "U");
  273. template.add(he);
  274. }
  275. }
  276. /**
  277. * Removes the Selected Element from the template
  278. */
  279. private void removeElement() {
  280. int index = list.getSelectedIndex();
  281. if (index == -1)
  282. return;
  283. //TODO(Tom2022-01-27): template.remove(index);
  284. listModel.remove(index);
  285. }
  286. /**
  287. * Edits the selected HolonElement
  288. */
  289. private void editElement() {
  290. int index = list.getSelectedIndex();
  291. if (index == -1)
  292. return;
  293. AddElementPopUp popUp = new AddElementPopUp(parent);
  294. popUp.setActualHolonObject(template);
  295. popUp.setElement(template.elementsStream().toList().get(index));
  296. popUp.setVisible(true);
  297. HolonElement he = popUp.getElement();
  298. if (he != null) {
  299. listModel.remove(index);
  300. listModel.addElement(he.getName()
  301. + ": " + he.getEnergy() + "U");
  302. //TODO(Tom2022-01-27): template.removeElement(index);
  303. template.add(he);
  304. }
  305. }
  306. }