AddObjectPopUp.java 11 KB

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