AddObjectPopUp.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. package ui.view;
  2. import classes.AbstractCpsObject;
  3. import classes.HolonElement;
  4. import classes.HolonObject;
  5. import classes.Pair;
  6. import ui.controller.Control;
  7. import javax.swing.*;
  8. import javax.swing.border.EmptyBorder;
  9. import javax.swing.filechooser.FileNameExtensionFilter;
  10. import java.awt.*;
  11. import java.awt.event.KeyEvent;
  12. import java.awt.event.KeyListener;
  13. import java.awt.event.MouseAdapter;
  14. import java.awt.event.MouseEvent;
  15. import java.io.*;
  16. import java.util.ArrayList;
  17. /**
  18. * Popup for adding a Holon Object to a Category.
  19. *
  20. * @author Gruppe14
  21. */
  22. public class AddObjectPopUp extends JDialog {
  23. private static final long serialVersionUID = 1L;
  24. private AddElementPopUp addElement;
  25. private JTextField objectName;
  26. private JTextField sourcePath;
  27. private ArrayList<HolonElement> hElements;
  28. private DefaultListModel listModel;
  29. private JList list;
  30. private String imagePath;
  31. // private HolonObject theObject;
  32. private Control controller;
  33. private File selectedFile = null;
  34. private String filePath = " ";
  35. private String givenCategory;
  36. private JLabel lblImagePreview;
  37. private AbstractCpsObject toEdit;
  38. private boolean editState;
  39. private boolean imageChanged = false;
  40. // /**
  41. // * Launch the application.
  42. // *
  43. // * @param args
  44. // * standard
  45. // */
  46. // public static void main(String[] args) {
  47. // try {
  48. // AddObjectPopUp dialog = new AddObjectPopUp(false, null, null);
  49. // dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  50. // dialog.setVisible(true);
  51. // } catch (Exception e) {
  52. // e.printStackTrace();
  53. // }
  54. // }
  55. /**
  56. * Create the dialog.
  57. *
  58. * @param edit
  59. * true if edit
  60. * @param obj
  61. * the object
  62. * @param cat
  63. * the categorie
  64. */
  65. AddObjectPopUp(boolean edit, AbstractCpsObject obj, String cat, JFrame parentFrame) {
  66. toEdit = obj;
  67. editState = edit;
  68. this.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
  69. .getScaledInstance(30, 30, Image.SCALE_SMOOTH));
  70. setBounds(100, 100, 450, 342);
  71. setLocationRelativeTo(parentFrame);
  72. getContentPane().setLayout(new BorderLayout());
  73. JPanel contentPanel = new JPanel();
  74. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  75. getContentPane().add(contentPanel, BorderLayout.CENTER);
  76. contentPanel.setLayout(null);
  77. hElements = new ArrayList<>();
  78. this.setTitle(Languages.getLanguage()[58]);
  79. {
  80. JLabel lblName = new JLabel(Languages.getLanguage()[59]);
  81. lblName.setHorizontalAlignment(SwingConstants.CENTER);
  82. lblName.setBounds(28, 21, 76, 14);
  83. contentPanel.add(lblName);
  84. }
  85. {
  86. objectName = new JTextField();
  87. objectName.addKeyListener(new KeyListener() {
  88. @Override
  89. public void keyPressed(KeyEvent arg0) {
  90. }
  91. @Override
  92. public void keyReleased(KeyEvent e) {
  93. }
  94. @Override
  95. public void keyTyped(KeyEvent e) {
  96. objectName.setBackground(Color.WHITE);
  97. }
  98. });
  99. if (edit) {
  100. objectName.setText(obj.getName());
  101. }
  102. objectName.setBounds(98, 18, 172, 20);
  103. contentPanel.add(objectName);
  104. objectName.setColumns(10);
  105. }
  106. {
  107. JButton btnBrowseImage = new JButton(Languages.getLanguage()[60]);
  108. btnBrowseImage.setBounds(10, 75, 134, 23);
  109. contentPanel.add(btnBrowseImage);
  110. btnBrowseImage.addMouseListener(new MouseAdapter() {
  111. public void mouseClicked(MouseEvent e) {
  112. fileChooser();
  113. }
  114. });
  115. }
  116. {
  117. lblImagePreview = new JLabel("");
  118. lblImagePreview.setBounds(295, 3, 50, 50);
  119. contentPanel.add(lblImagePreview);
  120. }
  121. {
  122. sourcePath = new JTextField();
  123. sourcePath.addKeyListener(new KeyListener() {
  124. @Override
  125. public void keyPressed(KeyEvent arg0) {
  126. }
  127. @Override
  128. public void keyReleased(KeyEvent e) {
  129. }
  130. @Override
  131. public void keyTyped(KeyEvent e) {
  132. sourcePath.setBackground(Color.WHITE);
  133. }
  134. });
  135. if (edit) {
  136. File checkPath = new File(obj.getImage());
  137. if (checkPath.exists()) {
  138. selectedFile = new File(obj.getImage());
  139. } else {
  140. selectedFile = new File(this.getClass().getResource(obj.getImage()).getFile());
  141. }
  142. filePath = selectedFile.getAbsolutePath();
  143. sourcePath.setText(filePath);
  144. ImageIcon icon = new ImageIcon(
  145. new ImageIcon(filePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
  146. lblImagePreview.setIcon(icon);
  147. }
  148. sourcePath.setBounds(148, 77, 271, 20);
  149. contentPanel.add(sourcePath);
  150. sourcePath.setColumns(10);
  151. }
  152. {
  153. JButton btnAddDefaultElement = new JButton(Languages.getLanguage()[61]);
  154. btnAddDefaultElement.addActionListener(actionEvent -> {
  155. addElement = new AddElementPopUp(parentFrame);
  156. addElement.setActualCps(toEdit);
  157. addElement.setVisible(true);
  158. HolonElement hl = addElement.getElement();
  159. hl.setSaving(new Pair<>(givenCategory, objectName.getText()));
  160. // if (hl != null) {
  161. // hl.setSav(givenCategory);
  162. // }
  163. // hl.setObj(objectName.getText());
  164. addElement(hl);
  165. });
  166. btnAddDefaultElement.setBounds(270, 144, 142, 23);
  167. contentPanel.add(btnAddDefaultElement);
  168. }
  169. {
  170. JScrollPane scrollPane = new JScrollPane();
  171. scrollPane.setBounds(10, 114, 236, 150);
  172. contentPanel.add(scrollPane);
  173. {
  174. listModel = new DefaultListModel();
  175. /*
  176. * HolonElement hel = new HolonElement("Test", 100, 5); String
  177. * name = hel.getEleName(); for (int i = 0; i < 11; i++) {
  178. * hel.setEleName(name + i); addElement(hel); }
  179. */
  180. list = new JList(listModel);
  181. scrollPane.setViewportView(list);
  182. }
  183. }
  184. if (edit) {
  185. for (HolonElement e : ((HolonObject) obj).getElements()) {
  186. addElement(e);
  187. }
  188. }
  189. {
  190. JButton btnNewButton = new JButton(Languages.getLanguage()[62]);
  191. btnNewButton.addActionListener(actionEvent -> {
  192. int selectedIndex = list.getSelectedIndex();
  193. if (selectedIndex != -1) {
  194. listModel.remove(selectedIndex);
  195. hElements.remove(selectedIndex);
  196. }
  197. });
  198. btnNewButton.setBounds(270, 182, 142, 27);
  199. contentPanel.add(btnNewButton);
  200. }
  201. {
  202. JPanel buttonPane = new JPanel();
  203. buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
  204. getContentPane().add(buttonPane, BorderLayout.SOUTH);
  205. {
  206. JButton okButton = new JButton("OK");
  207. okButton.addMouseListener(new MouseAdapter() {
  208. public void mouseClicked(MouseEvent e) {
  209. // Component frame = null;
  210. if (objectName.getText().length() > 0) {
  211. if (sourcePath.getText().equals(filePath)) {
  212. imagePath = filePath;
  213. if (imageChanged)
  214. copieFile();
  215. imageChanged = false;
  216. // theObject = new
  217. // HolonObject(objectName.getText());
  218. // theObject.setElements(hElements);
  219. // theObject.setImage(imagePath);
  220. try {
  221. if (editState) {
  222. controller.delObjectCategory(givenCategory, toEdit.getName());
  223. hElements.forEach(ele -> ele
  224. .setSaving(new Pair<>(ele.getSaving().getKey(), objectName.getText())));
  225. controller.addObject(controller.searchCategory(givenCategory),
  226. objectName.getText(), hElements, imagePath);
  227. } else {
  228. controller.addObject(controller.searchCategory(givenCategory),
  229. objectName.getText(), hElements, imagePath);
  230. }
  231. } catch (Exception e2) {
  232. }
  233. // controller.addObjectCategory(controller.searchCategory(givenCategory),
  234. // theObject);
  235. //
  236. // System.out.println(theObject.getImage());
  237. dispose();
  238. } else {
  239. sourcePath.setBackground(new Color(255, 50, 50));
  240. }
  241. } else {
  242. objectName.setBackground(new Color(255, 50, 50));
  243. if (!sourcePath.getText().equals(filePath))
  244. sourcePath.setBackground(new Color(255, 50, 50));
  245. }
  246. }
  247. });
  248. okButton.setActionCommand("OK");
  249. buttonPane.add(okButton);
  250. getRootPane().setDefaultButton(okButton);
  251. }
  252. {
  253. JButton cancelButton = new JButton(Languages.getLanguage()[63]);
  254. cancelButton.setActionCommand("Cancel");
  255. buttonPane.add(cancelButton);
  256. cancelButton.addActionListener(e -> dispose());
  257. }
  258. }
  259. }
  260. // /**
  261. // * Get Jar Containing Folder.
  262. // *
  263. // * @param aclass aClass
  264. // * @return String
  265. // * @throws Exception Exception
  266. // */
  267. // public static String getJarContainingFolder(Class aclass) throws Exception {
  268. // CodeSource codeSource = aclass.getProtectionDomain().getCodeSource();
  269. //
  270. // File jarFile;
  271. //
  272. // if (codeSource.getLocation() != null) {
  273. // jarFile = new File(codeSource.getLocation().toURI());
  274. // } else {
  275. // String path = aclass.getResource(aclass.getSimpleName() + ".class").getPath();
  276. // String jarFilePath = path.substring(path.indexOf(":") + 1, path.indexOf("!"));
  277. // jarFilePath = URLDecoder.decode(jarFilePath, "UTF-8");
  278. // jarFile = new File(jarFilePath);
  279. // }
  280. // return jarFile.getParentFile().getAbsolutePath();
  281. // }
  282. /**
  283. * adds a Holon Element.
  284. *
  285. * @param hl
  286. * the HolonElement
  287. */
  288. private void addElement(HolonElement hl) {
  289. hElements.add(hl);
  290. listModel.addElement(hl.getAmount() + "x: " + hl.getEleName() + " " + hl.getEnergyPerElement() + "U");
  291. }
  292. /**
  293. * Choose the file.
  294. */
  295. private void fileChooser() {
  296. JFileChooser fileChooser = new JFileChooser();
  297. FileNameExtensionFilter filter = new FileNameExtensionFilter("png, jpg or jpeg", "png", "jpg", "jpeg");
  298. fileChooser.setFileFilter(filter);
  299. int returnValue = fileChooser.showOpenDialog(null);
  300. if (returnValue == JFileChooser.APPROVE_OPTION) {
  301. selectedFile = fileChooser.getSelectedFile();
  302. filePath = selectedFile.getAbsolutePath();
  303. sourcePath.setText(filePath);
  304. ImageIcon icon = new ImageIcon(
  305. new ImageIcon(filePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
  306. lblImagePreview.setIcon(icon);
  307. imageChanged = true;
  308. } else {
  309. System.out.println("Failed to Load");
  310. }
  311. }
  312. /**
  313. * Copies the File.
  314. */
  315. private void copieFile() {
  316. InputStream inStream;
  317. OutputStream outStream;
  318. try {
  319. File source = new File(filePath);
  320. File dest = new File(System.getProperty("user.home") + "/.config/HolonGUI/Images/");
  321. dest.mkdirs();
  322. dest = new File(dest, selectedFile.getName());
  323. imagePath = "" + dest;
  324. inStream = new FileInputStream(source);
  325. outStream = new FileOutputStream(dest);
  326. byte[] buffer = new byte[1024];
  327. int length;
  328. while ((length = inStream.read(buffer)) > 0) {
  329. outStream.write(buffer, 0, length);
  330. }
  331. inStream.close();
  332. outStream.close();
  333. System.out.println("File Copied..");
  334. } catch (IOException eex) {
  335. eex.printStackTrace();
  336. }
  337. }
  338. // /**
  339. // * Edit the Information.
  340. // *
  341. // * @param obj
  342. // * the CpsObject
  343. // */
  344. // public void editInformation(HolonObject obj) {
  345. // objectName.setText(obj.getName());
  346. // }
  347. // /**
  348. // * Return the Object.
  349. // *
  350. // * @return the CpsObject
  351. // */
  352. // public HolonObject getObject() {
  353. // return theObject;
  354. // }
  355. /**
  356. * Sets the Controller.
  357. *
  358. * @param controller
  359. * the controller
  360. */
  361. public void setController(Control controller) {
  362. this.controller = controller;
  363. }
  364. /**
  365. * Set the Category.
  366. *
  367. * @param cat
  368. * the Category
  369. */
  370. public void setCategory(String cat) {
  371. givenCategory = cat;
  372. }
  373. }