CreateTemplatePopUp.java 8.5 KB

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