AddObjectPopUp.java 16 KB

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