CreateTemplatePopUp.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package ui.view;
  2. import java.awt.BorderLayout;
  3. import java.awt.Choice;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import javax.swing.DefaultListModel;
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JButton;
  9. import javax.swing.JDialog;
  10. import javax.swing.JFileChooser;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JList;
  14. import javax.swing.JPanel;
  15. import javax.swing.JScrollPane;
  16. import javax.swing.JTextField;
  17. import javax.swing.border.EmptyBorder;
  18. import javax.swing.filechooser.FileNameExtensionFilter;
  19. import classes.Category;
  20. import classes.HolonElement;
  21. import classes.HolonObject;
  22. import classes.Pair;
  23. import ui.controller.Control;
  24. import ui.model.Model;
  25. import util.ImageImport;
  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. * @param edit
  76. * true if edit
  77. * @param obj
  78. * the object
  79. * @param model
  80. * @param cat
  81. * the categorie
  82. */
  83. public CreateTemplatePopUp(HolonObject obj, Model model,
  84. JFrame parentFrame, Control controller) {
  85. setResizable(false);
  86. /*
  87. * use Category Controller an stuff lul
  88. */
  89. /*
  90. * initialize Data
  91. */
  92. template = new HolonObject(obj);
  93. this.parent = parentFrame;
  94. this.controller = controller;
  95. /*
  96. * create Frame and GUI
  97. */
  98. setIconImage(ImageImport.loadImage("/Images/Holeg.png", 30, 30));
  99. setBounds(100, 100, 476, 344);
  100. setLocationRelativeTo(parentFrame);
  101. getContentPane().setLayout(new BorderLayout());
  102. /**
  103. * Content Panel
  104. */
  105. JPanel contentPanel = new JPanel();
  106. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  107. getContentPane().add(contentPanel, BorderLayout.CENTER);
  108. contentPanel.setLayout(null);
  109. /**
  110. * Category Label
  111. */
  112. JLabel lblCategory = new JLabel("Category:");
  113. lblCategory.setBounds(12, 13, 68, 22);
  114. contentPanel.add(lblCategory);
  115. /**
  116. * Category Choice
  117. */
  118. choice = new Choice();
  119. choice.setBounds(86, 13, 172, 22);
  120. contentPanel.add(choice);
  121. // add categories
  122. if (model.getCategories().size() == 0)// if no categorie exist: create
  123. // new Template Category
  124. try {
  125. this.controller.addCategory("Template");
  126. } catch (IOException e1) {
  127. e1.printStackTrace();
  128. }
  129. // add Categories to the choice
  130. for (Category c : model.getCategories())
  131. choice.add(c.getName());
  132. /**
  133. * Name Label
  134. */
  135. JLabel lblName = new JLabel("Name:");
  136. lblName.setBounds(12, 48, 56, 16);
  137. contentPanel.add(lblName);
  138. /**
  139. * Name Textfield
  140. */
  141. textField_name = new JTextField();
  142. textField_name.setBounds(86, 48, 172, 22);
  143. contentPanel.add(textField_name);
  144. textField_name.setColumns(10);
  145. textField_name.setText(template.getName());
  146. /**
  147. * Image Path Lable
  148. */
  149. JLabel lblImage = new JLabel("Image:");
  150. lblImage.setBounds(12, 89, 56, 16);
  151. contentPanel.add(lblImage);
  152. /**
  153. * Image Path Text Field
  154. */
  155. textField_imagePath = new JTextField();
  156. textField_imagePath.setBounds(86, 86, 172, 22);
  157. contentPanel.add(textField_imagePath);
  158. textField_imagePath.setColumns(10);
  159. textField_imagePath.setText(template.getImage());
  160. /**
  161. * Browse Image Button
  162. */
  163. JButton btnBrowseImage = new JButton("BrowseImage");
  164. btnBrowseImage.setBounds(268, 85, 117, 25);
  165. contentPanel.add(btnBrowseImage);
  166. btnBrowseImage.addActionListener(actionevent -> {
  167. fileChooser();
  168. });
  169. /**
  170. * Image Preview
  171. */
  172. lblImagePreview = new JLabel("Image Preview");
  173. lblImagePreview.setIcon(new ImageIcon(ImageImport.loadImage(
  174. template.getImage(), 62, 62)));
  175. lblImagePreview.setBounds(298, 13, 62, 62);
  176. contentPanel.add(lblImagePreview);
  177. /**
  178. * Holon Element List
  179. */
  180. listModel = new DefaultListModel<String>();
  181. /**
  182. * Add Elements to List
  183. */
  184. for (HolonElement he : template.getElements())
  185. listModel.addElement(he.getAmount() + " * " + he.getName()
  186. + ": " + he.getMaximumEnergy() + "U");
  187. /**
  188. * Add ScrollPane to List
  189. */
  190. JScrollPane scrollPane = new JScrollPane();
  191. scrollPane.setBounds(22, 118, 236, 150);
  192. contentPanel.add(scrollPane);
  193. list = new JList<String>(listModel);
  194. scrollPane.setViewportView(list);
  195. /**
  196. * Delete Element Button
  197. */
  198. JButton btnDeleteElement = new JButton("Delete Element");
  199. btnDeleteElement.setBounds(268, 228, 140, 25);
  200. contentPanel.add(btnDeleteElement);
  201. btnDeleteElement.addActionListener(e -> removeElement());
  202. /**
  203. * Edit Element Button
  204. */
  205. JButton btnEditElement = new JButton("Edit Element");
  206. btnEditElement.setBounds(268, 190, 140, 25);
  207. contentPanel.add(btnEditElement);
  208. btnEditElement.addActionListener(e -> editElement());
  209. /**
  210. * Add Element Button
  211. */
  212. JButton btnAddElement = new JButton("Add Element");
  213. btnAddElement.setBounds(268, 152, 140, 25);
  214. contentPanel.add(btnAddElement);
  215. btnAddElement.addActionListener(e -> addElement());
  216. /**
  217. * Cancel Button
  218. */
  219. JButton btnCancel = new JButton("Cancel");
  220. btnCancel.setBounds(384, 277, 74, 25);
  221. contentPanel.add(btnCancel);
  222. btnCancel.addActionListener(e -> dispose());
  223. /**
  224. * Add Template Button
  225. */
  226. JButton btnAddTemplate = new JButton("Add Template");
  227. btnAddTemplate.setBounds(75, 271, 113, 25);
  228. contentPanel.add(btnAddTemplate);
  229. btnAddTemplate.addActionListener(e -> createTemplate());
  230. /**
  231. * Title
  232. */
  233. setTitle("Create Template Menu");
  234. }
  235. /**
  236. * Choose the file.
  237. */
  238. private void fileChooser() {
  239. JFileChooser fileChooser = new JFileChooser();
  240. FileNameExtensionFilter filter = new FileNameExtensionFilter(
  241. "png, jpg or jpeg", "png", "jpg", "jpeg");
  242. fileChooser.setFileFilter(filter);
  243. int returnValue = fileChooser.showOpenDialog(null);
  244. if (returnValue == JFileChooser.APPROVE_OPTION) {
  245. File selectedFile = fileChooser.getSelectedFile();
  246. String filePath = selectedFile.getAbsolutePath();
  247. textField_imagePath.setText(filePath);
  248. ImageIcon icon = new ImageIcon(ImageImport.loadImage(filePath, 62,
  249. 62));
  250. lblImagePreview.setIcon(icon);
  251. } else {
  252. System.out.println("Failed to Load");
  253. }
  254. }
  255. /**
  256. * create the template and add it to the category
  257. */
  258. private void createTemplate() {
  259. try {
  260. template.setName(textField_name.getText());
  261. template.setImage(textField_imagePath.getText());
  262. template.getElements().forEach(
  263. ele -> ele.setSaving(new Pair<String, String>(choice
  264. .getSelectedItem(), template.getName())));
  265. controller.addObject(controller.searchCategory(choice
  266. .getItem(choice.getSelectedIndex())), template.getName(),
  267. template.getElements(), template.getImage());
  268. this.dispose();
  269. } catch (IOException e) {
  270. System.out
  271. .println("Could not create Template: Category not found!");
  272. e.printStackTrace();
  273. }
  274. }
  275. /**
  276. * Add an Holon Element to the template
  277. */
  278. private void addElement() {
  279. AddElementPopUp popUp = new AddElementPopUp(parent);
  280. popUp.setActualCps(template);
  281. popUp.setVisible(true);
  282. HolonElement he = popUp.getElement();
  283. if (he != null) {
  284. listModel.addElement(he.getAmount() + " * " + he.getName()
  285. + ": " + he.getMaximumEnergy() + "U");
  286. template.addElement(he);
  287. he.setSaving(new Pair<>(category, textField_name.getText()));
  288. }
  289. }
  290. /**
  291. * Removes the Selected Element from the template
  292. */
  293. private void removeElement() {
  294. int index = list.getSelectedIndex();
  295. if (index == -1)
  296. return;
  297. template.deleteElement(index);
  298. listModel.remove(index);
  299. }
  300. /**
  301. * Edits the selected HolonElement
  302. */
  303. private void editElement() {
  304. int index = list.getSelectedIndex();
  305. if (index == -1)
  306. return;
  307. AddElementPopUp popUp = new AddElementPopUp(parent);
  308. popUp.setActualCps(template);
  309. popUp.setElement(template.getElements().get(index));
  310. popUp.setVisible(true);
  311. HolonElement he = popUp.getElement();
  312. if (he != null) {
  313. listModel.remove(index);
  314. listModel.addElement(he.getAmount() + " * " + he.getName()
  315. + ": " + he.getMaximumEnergy() + "U");
  316. template.deleteElement(index);
  317. template.addElement(he);
  318. he.setSaving(new Pair<>(category, textField_name.getText()));
  319. }
  320. }
  321. }