AddObjectPopUp.java 11 KB

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