AddObjectPopUp.java 11 KB

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