AddObjectPopUp.java 12 KB

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