AddObjectPopUp.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. package ui.view;
  2. import classes.AbstractCpsObject;
  3. import classes.HolonBattery;
  4. import classes.HolonElement;
  5. import classes.HolonObject;
  6. import classes.Pair;
  7. import ui.controller.Control;
  8. import java.lang.NumberFormatException;
  9. import ui.model.Model;
  10. import javax.swing.*;
  11. import javax.swing.border.EmptyBorder;
  12. import javax.swing.filechooser.FileNameExtensionFilter;
  13. import java.awt.*;
  14. import java.awt.event.KeyEvent;
  15. import java.awt.event.KeyListener;
  16. import java.awt.event.MouseAdapter;
  17. import java.awt.event.MouseEvent;
  18. import java.io.*;
  19. import java.util.ArrayList;
  20. /**
  21. * Popup for adding a Holon Object to a Category.
  22. *
  23. * @author Gruppe14
  24. */
  25. public class AddObjectPopUp extends JDialog {
  26. private static final long serialVersionUID = 1L;
  27. private AddElementPopUp addElement;
  28. private JTextField objectName;
  29. private JTextField sourcePath;
  30. private ArrayList<HolonElement> hElements;
  31. private DefaultListModel listModel;
  32. private JList list;
  33. private String imagePath;
  34. // private HolonObject theObject;
  35. private Control controller;
  36. private File selectedFile = null;
  37. private String filePath = " ";
  38. private String givenCategory;
  39. private JLabel lblImagePreview;
  40. private AbstractCpsObject toEdit;
  41. private boolean editState;
  42. private boolean imageChanged = false;
  43. private Model model;
  44. /**
  45. * Launch the application.
  46. *
  47. * @param args
  48. * standard
  49. */
  50. public static void main(String[] args) {
  51. try {
  52. AddObjectPopUp dialog = new AddObjectPopUp(false,new HolonBattery("jo") , "hei", null);
  53. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  54. dialog.setVisible(true);
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. /**
  60. * Create the dialog.
  61. *
  62. * @param edit
  63. * true if edit
  64. * @param obj
  65. * the object
  66. * @param cat
  67. * the categorie
  68. */
  69. AddObjectPopUp(boolean edit, AbstractCpsObject obj, String cat, JFrame parentFrame) {
  70. if(obj instanceof HolonBattery)
  71. {
  72. BatteryPopUp(edit, obj, cat, parentFrame);
  73. return;
  74. }
  75. toEdit = obj;
  76. editState = edit;
  77. this.model=model;
  78. this.setIconImage(Util.loadImage(this, "/Images/Holeg.png",30,30));
  79. setBounds(100, 100, 450, 342);
  80. setLocationRelativeTo(parentFrame);
  81. getContentPane().setLayout(new BorderLayout());
  82. JPanel contentPanel = new JPanel();
  83. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  84. getContentPane().add(contentPanel, BorderLayout.CENTER);
  85. contentPanel.setLayout(null);
  86. hElements = new ArrayList<>();
  87. this.setTitle(Languages.getLanguage()[58]);
  88. {
  89. JLabel lblName = new JLabel(Languages.getLanguage()[59]);
  90. lblName.setHorizontalAlignment(SwingConstants.CENTER);
  91. lblName.setBounds(28, 21, 76, 14);
  92. contentPanel.add(lblName);
  93. }
  94. {
  95. objectName = new JTextField();
  96. objectName.addKeyListener(new KeyListener() {
  97. @Override
  98. public void keyPressed(KeyEvent arg0) {
  99. }
  100. @Override
  101. public void keyReleased(KeyEvent e) {
  102. }
  103. @Override
  104. public void keyTyped(KeyEvent e) {
  105. objectName.setBackground(Color.WHITE);
  106. }
  107. });
  108. if (edit) {
  109. objectName.setText(obj.getName());
  110. }
  111. objectName.setBounds(98, 18, 172, 20);
  112. contentPanel.add(objectName);
  113. objectName.setColumns(10);
  114. }
  115. {
  116. JButton btnBrowseImage = new JButton(Languages.getLanguage()[60]);
  117. btnBrowseImage.setBounds(10, 75, 134, 23);
  118. contentPanel.add(btnBrowseImage);
  119. btnBrowseImage.addMouseListener(new MouseAdapter() {
  120. public void mouseClicked(MouseEvent e) {
  121. fileChooser();
  122. }
  123. });
  124. }
  125. {
  126. lblImagePreview = new JLabel("");
  127. lblImagePreview.setBounds(295, 3, 50, 50);
  128. contentPanel.add(lblImagePreview);
  129. }
  130. {
  131. sourcePath = new JTextField();
  132. sourcePath.addKeyListener(new KeyListener() {
  133. @Override
  134. public void keyPressed(KeyEvent arg0) {
  135. }
  136. @Override
  137. public void keyReleased(KeyEvent e) {
  138. }
  139. @Override
  140. public void keyTyped(KeyEvent e) {
  141. sourcePath.setBackground(Color.WHITE);
  142. }
  143. });
  144. if (edit) {
  145. //TODO: check why this needs a file?
  146. /*
  147. selectedFile = Util.loadStream(this,obj.getImage());
  148. filePath = selectedFile.getAbsolutePath();
  149. sourcePath.setText(filePath);
  150. ImageIcon icon = new ImageIcon(
  151. new ImageIcon(filePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
  152. */
  153. lblImagePreview.setIcon(new ImageIcon(Util.loadImage(this, obj.getImage(), 50, 50)));
  154. sourcePath.setText(obj.getImage());
  155. filePath = obj.getImage();
  156. }
  157. sourcePath.setBounds(148, 77, 271, 20);
  158. contentPanel.add(sourcePath);
  159. sourcePath.setColumns(10);
  160. }
  161. {
  162. JButton btnAddDefaultElement = new JButton(Languages.getLanguage()[61]);
  163. btnAddDefaultElement.addActionListener(actionEvent -> {
  164. addElement = new AddElementPopUp(parentFrame, model);
  165. addElement.setActualCps(toEdit);
  166. addElement.setVisible(true);
  167. HolonElement hl = addElement.getElement();
  168. hl.setSaving(new Pair<>(givenCategory, objectName.getText()));
  169. // if (hl != null) {
  170. // hl.setSav(givenCategory);
  171. // }
  172. // hl.setObj(objectName.getText());
  173. addElement(hl);
  174. });
  175. btnAddDefaultElement.setBounds(270, 144, 142, 23);
  176. contentPanel.add(btnAddDefaultElement);
  177. }
  178. {
  179. JScrollPane scrollPane = new JScrollPane();
  180. scrollPane.setBounds(10, 114, 236, 150);
  181. contentPanel.add(scrollPane);
  182. {
  183. listModel = new DefaultListModel();
  184. /*
  185. * HolonElement hel = new HolonElement("Test", 100, 5); String
  186. * name = hel.getEleName(); for (int i = 0; i < 11; i++) {
  187. * hel.setEleName(name + i); addElement(hel); }
  188. */
  189. list = new JList(listModel);
  190. scrollPane.setViewportView(list);
  191. }
  192. }
  193. if (edit) {
  194. for (HolonElement e : ((HolonObject) obj).getElements()) {
  195. addElement(e);
  196. }
  197. }
  198. {
  199. JButton btnNewButton = new JButton(Languages.getLanguage()[62]);
  200. btnNewButton.addActionListener(actionEvent -> {
  201. int selectedIndex = list.getSelectedIndex();
  202. if (selectedIndex != -1) {
  203. listModel.remove(selectedIndex);
  204. hElements.remove(selectedIndex);
  205. }
  206. });
  207. btnNewButton.setBounds(270, 182, 142, 27);
  208. contentPanel.add(btnNewButton);
  209. }
  210. {
  211. JPanel buttonPane = new JPanel();
  212. buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
  213. getContentPane().add(buttonPane, BorderLayout.SOUTH);
  214. {
  215. JButton okButton = new JButton("OK");
  216. okButton.addMouseListener(new MouseAdapter() {
  217. public void mouseClicked(MouseEvent e) {
  218. // Component frame = null;
  219. if (objectName.getText().length() > 0) {
  220. if (sourcePath.getText().equals(filePath)) {
  221. imagePath = filePath;
  222. if (imageChanged)
  223. copieFile();
  224. imageChanged = false;
  225. // theObject = new
  226. // HolonObject(objectName.getText());
  227. // theObject.setElements(hElements);
  228. // theObject.setImage(imagePath);// TODO Auto-generated catch block
  229. try {
  230. if (editState) {
  231. controller.delObjectCategory(givenCategory, toEdit.getName());
  232. hElements.forEach(ele -> ele
  233. .setSaving(new Pair<>(ele.getSaving().getKey(), objectName.getText())));
  234. controller.addObject(controller.searchCategory(givenCategory),
  235. objectName.getText(), hElements, imagePath);
  236. } else {
  237. controller.addObject(controller.searchCategory(givenCategory),
  238. objectName.getText(), hElements, imagePath);
  239. }
  240. } catch (Exception e2) {
  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(e -> dispose());
  266. }
  267. }
  268. }
  269. protected void BatteryPopUp(boolean edit, AbstractCpsObject obj, String cat, JFrame parentFrame) {
  270. //TODO: Click mich <3
  271. HolonBattery editBat = (HolonBattery) obj;
  272. //Window Settings
  273. this.setIconImage(Util.loadImage(this, "/Images/battery.png",30,30));
  274. this.setTitle("Edit Battery");
  275. setBounds(0, 0, 285, 290);
  276. setLocationRelativeTo(parentFrame);
  277. //Labels, TextFiels, Buttons
  278. JPanel myPanel = new JPanel();
  279. myPanel.setLayout(null);
  280. int beginBoxX = 140, beginLabelX = 30;
  281. int beginColumAtY = 20, newColumY = 40;
  282. JLabel batteryNameLabel = new JLabel("Name:");
  283. JTextField batteryNameBox = new JTextField(10);
  284. batteryNameBox.setText(editBat.getName());
  285. JLabel batteryInRateLabel = new JLabel("In ratio:");
  286. JTextField batteryInRateBox = new JTextField(10);
  287. batteryInRateBox.setText(Float.toString(editBat.getInRatio()));
  288. JLabel batteryOutRateLabel = new JLabel("Out ratio:");
  289. JTextField batteryOutRateBox = new JTextField(10);
  290. batteryOutRateBox.setText(Float.toString(editBat.getOutRatio()));
  291. JLabel batteryCapasityLabel = new JLabel("Capacity:");
  292. JTextField batteryCapasityBox = new JTextField(10);
  293. batteryCapasityBox.setText(Float.toString(editBat.getCapacity()));
  294. JLabel batterySOCLabel = new JLabel("State of charge:");
  295. JTextField batterySOCBox = new JTextField(10);
  296. batterySOCBox.setText(Float.toString(editBat.getInitialStateOfCharge()));
  297. batteryNameLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
  298. batteryNameBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
  299. myPanel.add(batteryNameLabel);
  300. myPanel.add(batteryNameBox);
  301. beginColumAtY += newColumY;
  302. batteryInRateLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
  303. batteryInRateBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
  304. myPanel.add(batteryInRateLabel);
  305. myPanel.add(batteryInRateBox);
  306. beginColumAtY += newColumY;
  307. batteryOutRateLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
  308. batteryOutRateBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
  309. myPanel.add(batteryOutRateLabel);
  310. myPanel.add(batteryOutRateBox);
  311. beginColumAtY += newColumY;
  312. batteryCapasityLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
  313. batteryCapasityBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
  314. myPanel.add(batteryCapasityLabel);
  315. myPanel.add(batteryCapasityBox);
  316. beginColumAtY += newColumY;
  317. batterySOCLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
  318. batterySOCBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
  319. myPanel.add(batterySOCLabel);
  320. myPanel.add(batterySOCBox);
  321. //Save , Cancel
  322. JPanel bottomPanel = new JPanel();
  323. bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  324. JButton saveButton = new JButton("Save");
  325. bottomPanel.add(saveButton);
  326. saveButton.addActionListener(e -> {
  327. //SaveButten Event:
  328. if(batteryNameBox.getText().isEmpty()){ //Check for valid name
  329. JOptionPane.showMessageDialog(getContentPane(), "Name is empty." , "Wrong input" , JOptionPane.WARNING_MESSAGE);
  330. }else{
  331. editBat.setName(batteryNameBox.getText());
  332. try{//Check for floats inputs
  333. float changedInRatio = Float.valueOf(batteryInRateBox.getText());
  334. float changedOutRatio = Float.valueOf(batteryOutRateBox.getText());
  335. float changedCapasity = Float.valueOf(batteryCapasityBox.getText());
  336. float changedSOC = Float.valueOf(batterySOCBox.getText());
  337. //Saving:
  338. editBat.setInRatio(changedInRatio);
  339. editBat.setOutRatio(changedOutRatio);
  340. editBat.setCapacity(changedCapasity);
  341. editBat.setInitialStateOfCharge(changedSOC);
  342. try {
  343. controller.saveCategory();
  344. controller.notifyAll();
  345. } catch (Exception e1) { //Controller Exceptions
  346. }
  347. dispose();
  348. }catch(NumberFormatException FloatException){
  349. String wrong= FloatException.getMessage();
  350. String message;
  351. if(wrong.startsWith("empty")){
  352. message = "A field is empty.";
  353. }else{
  354. message = wrong.substring(18) + " is not a valid Number.";
  355. }
  356. JOptionPane.showMessageDialog(getContentPane(), message, "Wrong input" , JOptionPane.WARNING_MESSAGE);
  357. }
  358. }
  359. });
  360. JButton cancelButton = new JButton("Cancel");
  361. bottomPanel.add(cancelButton);
  362. cancelButton.addActionListener(e -> dispose());
  363. getContentPane().setLayout(new BorderLayout());
  364. getContentPane().add(myPanel, BorderLayout.CENTER);
  365. getContentPane().add(bottomPanel, BorderLayout.SOUTH);
  366. }
  367. // /**
  368. // * Get Jar Containing Folder.
  369. // *
  370. // * @param aclass aClass
  371. // * @return String
  372. // * @throws Exception Exception
  373. // */
  374. // public static String getJarContainingFolder(Class aclass) throws Exception {
  375. // CodeSource codeSource = aclass.getProtectionDomain().getCodeSource();
  376. //
  377. // File jarFile;
  378. //
  379. // if (codeSource.getLocation() != null) {
  380. // jarFile = new File(codeSource.getLocation().toURI());
  381. // } else {
  382. // String path = aclass.getResource(aclass.getSimpleName() + ".class").getPath();
  383. // String jarFilePath = path.substring(path.indexOf(":") + 1, path.indexOf("!"));
  384. // jarFilePath = URLDecoder.decode(jarFilePath, "UTF-8");
  385. // jarFile = new File(jarFilePath);
  386. // }
  387. // return jarFile.getParentFile().getAbsolutePath();
  388. // }
  389. /**
  390. * adds a Holon Element.
  391. *
  392. * @param hl
  393. * the HolonElement
  394. */
  395. private void addElement(HolonElement hl) {
  396. hElements.add(hl);
  397. listModel.addElement(hl.getAmount() + "x: " + hl.getEleName() + " " + hl.getEnergyPerElement() + "U");
  398. }
  399. /**
  400. * Choose the file.
  401. */
  402. private void fileChooser() {
  403. JFileChooser fileChooser = new JFileChooser();
  404. FileNameExtensionFilter filter = new FileNameExtensionFilter("png, jpg or jpeg", "png", "jpg", "jpeg");
  405. fileChooser.setFileFilter(filter);
  406. int returnValue = fileChooser.showOpenDialog(null);
  407. if (returnValue == JFileChooser.APPROVE_OPTION) {
  408. selectedFile = fileChooser.getSelectedFile();
  409. filePath = selectedFile.getAbsolutePath();
  410. sourcePath.setText(filePath);
  411. ImageIcon icon = new ImageIcon(//TODO: ugly
  412. new ImageIcon(filePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
  413. lblImagePreview.setIcon(icon);
  414. imageChanged = true;
  415. } else {
  416. System.out.println("Failed to Load");
  417. }
  418. }
  419. /**
  420. * Copies the File.
  421. */
  422. private void copieFile() {
  423. InputStream inStream;
  424. OutputStream outStream;
  425. try {
  426. File source = new File(filePath);
  427. File dest = new File(System.getProperty("user.home") + "/.config/HolonGUI/Images/");
  428. dest.mkdirs();
  429. dest = new File(dest, selectedFile.getName());
  430. imagePath = "" + dest;
  431. inStream = new FileInputStream(source);
  432. outStream = new FileOutputStream(dest);
  433. byte[] buffer = new byte[1024];
  434. int length;
  435. while ((length = inStream.read(buffer)) > 0) {
  436. outStream.write(buffer, 0, length);
  437. }
  438. inStream.close();
  439. outStream.close();
  440. System.out.println("File Copied..");
  441. } catch (IOException eex) {
  442. eex.printStackTrace();
  443. }
  444. }
  445. // /**
  446. // * Edit the Information.
  447. // *
  448. // * @param obj
  449. // * the CpsObject
  450. // */
  451. // public void editInformation(HolonObject obj) {
  452. // objectName.setText(obj.getName());
  453. // }
  454. // /**
  455. // * Return the Object.
  456. // *
  457. // * @return the CpsObject
  458. // */
  459. // public HolonObject getObject() {
  460. // return theObject;
  461. // }
  462. /**
  463. * Sets the Controller.
  464. *
  465. * @param controller
  466. * the controller
  467. */
  468. public void setController(Control controller) {
  469. this.controller = controller;
  470. }
  471. /**
  472. * Set the Category.
  473. *
  474. * @param cat
  475. * the Category
  476. */
  477. public void setCategory(String cat) {
  478. givenCategory = cat;
  479. }
  480. }