AddObjectPopUp.java 11 KB

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