AddObjectPopUp.java 12 KB

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