AddObjectPopUp.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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.ImageImport;
  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(ImageImport.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. lblImagePreview.setIcon(new ImageIcon(ImageImport.loadImage(obj.getImage(), 50, 50)));
  147. }
  148. sourcePath.setBounds(148, 77, 271, 20);
  149. if(edit) {
  150. this.filePath = obj.getImage();
  151. sourcePath.setText(filePath);
  152. }
  153. contentPanel.add(sourcePath);
  154. sourcePath.setColumns(10);
  155. }
  156. {
  157. JButton btnAddDefaultElement = new JButton(Languages.getLanguage()[61]);
  158. btnAddDefaultElement.addActionListener(actionEvent -> {
  159. addElement = new AddElementPopUp(parentFrame, model);
  160. addElement.setActualCps(toEdit);
  161. addElement.setVisible(true);
  162. HolonElement hl = addElement.getElement();
  163. hl.setSaving(new Pair<>(givenCategory, objectName.getText()));
  164. // if (hl != null) {
  165. // hl.setSav(givenCategory);
  166. // }
  167. // hl.setObj(objectName.getText());
  168. addElement(hl);
  169. });
  170. btnAddDefaultElement.setBounds(270, 144, 142, 23);
  171. contentPanel.add(btnAddDefaultElement);
  172. }
  173. {
  174. JScrollPane scrollPane = new JScrollPane();
  175. scrollPane.setBounds(10, 114, 236, 150);
  176. contentPanel.add(scrollPane);
  177. {
  178. listModel = new DefaultListModel();
  179. /*
  180. * HolonElement hel = new HolonElement("Test", 100, 5); String
  181. * name = hel.getEleName(); for (int i = 0; i < 11; i++) {
  182. * hel.setEleName(name + i); addElement(hel); }
  183. */
  184. list = new JList(listModel);
  185. scrollPane.setViewportView(list);
  186. }
  187. }
  188. if (edit) {
  189. for (HolonElement e : ((HolonObject) obj).getElements()) {
  190. addElement(e);
  191. }
  192. }
  193. {
  194. JButton btnNewButton = new JButton(Languages.getLanguage()[62]);
  195. btnNewButton.addActionListener(actionEvent -> {
  196. int selectedIndex = list.getSelectedIndex();
  197. if (selectedIndex != -1) {
  198. listModel.remove(selectedIndex);
  199. hElements.remove(selectedIndex);
  200. }
  201. });
  202. btnNewButton.setBounds(270, 182, 142, 27);
  203. contentPanel.add(btnNewButton);
  204. }
  205. {
  206. JPanel buttonPane = new JPanel();
  207. buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
  208. getContentPane().add(buttonPane, BorderLayout.SOUTH);
  209. {
  210. JButton okButton = new JButton("OK");
  211. okButton.addMouseListener(new MouseAdapter() {
  212. public void mouseClicked(MouseEvent e) {
  213. // Component frame = null;
  214. if (objectName.getText().length() > 0) {
  215. if (sourcePath.getText().equals(filePath)) {
  216. imagePath = filePath;
  217. if (imageChanged)
  218. copieFile();
  219. imageChanged = false;
  220. // theObject = new
  221. // HolonObject(objectName.getText());
  222. // theObject.setElements(hElements);
  223. // theObject.setImage(imagePath);// TODO Auto-generated catch block
  224. try {
  225. if (editState) {
  226. controller.delObjectCategory(givenCategory, toEdit.getName());
  227. hElements.forEach(ele -> ele
  228. .setSaving(new Pair<>(ele.getSaving().getKey(), objectName.getText())));
  229. controller.addObject(controller.searchCategory(givenCategory),
  230. objectName.getText(), hElements, imagePath);
  231. } else {
  232. controller.addObject(controller.searchCategory(givenCategory),
  233. objectName.getText(), hElements, imagePath);
  234. }
  235. } catch (Exception e2) {
  236. }
  237. // controller.addObjectCategory(controller.searchCategory(givenCategory),
  238. // theObject);
  239. //
  240. // System.out.println(theObject.getImage());
  241. dispose();
  242. } else {
  243. sourcePath.setBackground(new Color(255, 50, 50));
  244. }
  245. } else {
  246. objectName.setBackground(new Color(255, 50, 50));
  247. if (!sourcePath.getText().equals(filePath))
  248. sourcePath.setBackground(new Color(255, 50, 50));
  249. }
  250. }
  251. });
  252. okButton.setActionCommand("OK");
  253. buttonPane.add(okButton);
  254. getRootPane().setDefaultButton(okButton);
  255. }
  256. {
  257. JButton cancelButton = new JButton(Languages.getLanguage()[63]);
  258. cancelButton.setActionCommand("Cancel");
  259. buttonPane.add(cancelButton);
  260. cancelButton.addActionListener(e -> dispose());
  261. }
  262. }
  263. }
  264. protected void BatteryPopUp(boolean edit, AbstractCanvasObject obj, String cat, JFrame parentFrame) {
  265. //TODO: Click mich <3
  266. HolonBattery editBat = (HolonBattery) obj;
  267. //Window Settings
  268. this.setIconImage(ImageImport.loadImage("/Images/battery.png",30,30));
  269. this.setTitle("Edit Battery");
  270. setBounds(0, 0, 285, 290);
  271. setLocationRelativeTo(parentFrame);
  272. //Labels, TextFiels, Buttons
  273. JPanel myPanel = new JPanel();
  274. myPanel.setLayout(null);
  275. int beginBoxX = 140, beginLabelX = 30;
  276. int beginColumAtY = 20, newColumY = 40;
  277. JLabel batteryNameLabel = new JLabel("Name:");
  278. JTextField batteryNameBox = new JTextField(10);
  279. batteryNameBox.setText(editBat.getName());
  280. JLabel batteryInRateLabel = new JLabel("In ratio:");
  281. JTextField batteryInRateBox = new JTextField(10);
  282. batteryInRateBox.setText(Float.toString(editBat.getInRatio()));
  283. JLabel batteryOutRateLabel = new JLabel("Out ratio:");
  284. JTextField batteryOutRateBox = new JTextField(10);
  285. batteryOutRateBox.setText(Float.toString(editBat.getOutRatio()));
  286. JLabel batteryCapasityLabel = new JLabel("Capacity:");
  287. JTextField batteryCapasityBox = new JTextField(10);
  288. batteryCapasityBox.setText(Float.toString(editBat.getCapacity()));
  289. JLabel batterySOCLabel = new JLabel("State of charge:");
  290. JTextField batterySOCBox = new JTextField(10);
  291. batterySOCBox.setText(Float.toString(editBat.getInitialStateOfCharge()));
  292. batteryNameLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
  293. batteryNameBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
  294. myPanel.add(batteryNameLabel);
  295. myPanel.add(batteryNameBox);
  296. beginColumAtY += newColumY;
  297. batteryInRateLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
  298. batteryInRateBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
  299. myPanel.add(batteryInRateLabel);
  300. myPanel.add(batteryInRateBox);
  301. beginColumAtY += newColumY;
  302. batteryOutRateLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
  303. batteryOutRateBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
  304. myPanel.add(batteryOutRateLabel);
  305. myPanel.add(batteryOutRateBox);
  306. beginColumAtY += newColumY;
  307. batteryCapasityLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
  308. batteryCapasityBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
  309. myPanel.add(batteryCapasityLabel);
  310. myPanel.add(batteryCapasityBox);
  311. beginColumAtY += newColumY;
  312. batterySOCLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
  313. batterySOCBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
  314. myPanel.add(batterySOCLabel);
  315. myPanel.add(batterySOCBox);
  316. //Save , Cancel
  317. JPanel bottomPanel = new JPanel();
  318. bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  319. JButton saveButton = new JButton("Save");
  320. bottomPanel.add(saveButton);
  321. saveButton.addActionListener(e -> {
  322. //SaveButten Event:
  323. if(batteryNameBox.getText().isEmpty()){ //Check for valid name
  324. JOptionPane.showMessageDialog(getContentPane(), "Name is empty." , "Wrong input" , JOptionPane.WARNING_MESSAGE);
  325. }else{
  326. editBat.setName(batteryNameBox.getText());
  327. try{//Check for floats inputs
  328. float changedInRatio = Float.valueOf(batteryInRateBox.getText());
  329. float changedOutRatio = Float.valueOf(batteryOutRateBox.getText());
  330. float changedCapasity = Float.valueOf(batteryCapasityBox.getText());
  331. float changedSOC = Float.valueOf(batterySOCBox.getText());
  332. //Saving:
  333. editBat.setInRatio(changedInRatio);
  334. editBat.setOutRatio(changedOutRatio);
  335. editBat.setCapacity(changedCapasity);
  336. editBat.setInitialStateOfCharge(changedSOC);
  337. try {
  338. controller.saveCategory();
  339. controller.notifyAll();
  340. } catch (Exception e1) { //Controller Exceptions
  341. }
  342. dispose();
  343. }catch(NumberFormatException FloatException){
  344. String wrong= FloatException.getMessage();
  345. String message;
  346. if(wrong.startsWith("empty")){
  347. message = "A field is empty.";
  348. }else{
  349. message = wrong.substring(18) + " is not a valid Number.";
  350. }
  351. JOptionPane.showMessageDialog(getContentPane(), message, "Wrong input" , JOptionPane.WARNING_MESSAGE);
  352. }
  353. }
  354. });
  355. JButton cancelButton = new JButton("Cancel");
  356. bottomPanel.add(cancelButton);
  357. cancelButton.addActionListener(e -> dispose());
  358. getContentPane().setLayout(new BorderLayout());
  359. getContentPane().add(myPanel, BorderLayout.CENTER);
  360. getContentPane().add(bottomPanel, BorderLayout.SOUTH);
  361. }
  362. // /**
  363. // * Get Jar Containing Folder.
  364. // *
  365. // * @param aclass aClass
  366. // * @return String
  367. // * @throws Exception Exception
  368. // */
  369. // public static String getJarContainingFolder(Class aclass) throws Exception {
  370. // CodeSource codeSource = aclass.getProtectionDomain().getCodeSource();
  371. //
  372. // File jarFile;
  373. //
  374. // if (codeSource.getLocation() != null) {
  375. // jarFile = new File(codeSource.getLocation().toURI());
  376. // } else {
  377. // String path = aclass.getResource(aclass.getSimpleName() + ".class").getPath();
  378. // String jarFilePath = path.substring(path.indexOf(":") + 1, path.indexOf("!"));
  379. // jarFilePath = URLDecoder.decode(jarFilePath, "UTF-8");
  380. // jarFile = new File(jarFilePath);
  381. // }
  382. // return jarFile.getParentFile().getAbsolutePath();
  383. // }
  384. /**
  385. * adds a Holon Element.
  386. *
  387. * @param hl
  388. * the HolonElement
  389. */
  390. private void addElement(HolonElement hl) {
  391. hElements.add(hl);
  392. listModel.addElement(hl.getAmount() + "x: " + hl.getEleName() + " " + hl.getEnergyPerElement() + "U");
  393. }
  394. /**
  395. * Choose the file.
  396. */
  397. private void fileChooser() {
  398. JFileChooser fileChooser = new JFileChooser();
  399. FileNameExtensionFilter filter = new FileNameExtensionFilter("png, jpg or jpeg", "png", "jpg", "jpeg");
  400. fileChooser.setFileFilter(filter);
  401. int returnValue = fileChooser.showOpenDialog(null);
  402. if (returnValue == JFileChooser.APPROVE_OPTION) {
  403. selectedFile = fileChooser.getSelectedFile();
  404. filePath = selectedFile.getAbsolutePath();
  405. sourcePath.setText(filePath);
  406. ImageIcon icon = new ImageIcon(//TODO: ugly
  407. new ImageIcon(filePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
  408. lblImagePreview.setIcon(icon);
  409. imageChanged = true;
  410. } else {
  411. System.out.println("Failed to Load");
  412. }
  413. }
  414. /**
  415. * Copies the File.
  416. */
  417. private void copieFile() {
  418. InputStream inStream;
  419. OutputStream outStream;
  420. try {
  421. File source = new File(filePath);
  422. File dest = new File(System.getProperty("user.home") + "/.config/HolonGUI/Images/");
  423. dest.mkdirs();
  424. dest = new File(dest, selectedFile.getName());
  425. imagePath = "" + dest;
  426. inStream = new FileInputStream(source);
  427. outStream = new FileOutputStream(dest);
  428. byte[] buffer = new byte[1024];
  429. int length;
  430. while ((length = inStream.read(buffer)) > 0) {
  431. outStream.write(buffer, 0, length);
  432. }
  433. inStream.close();
  434. outStream.close();
  435. System.out.println("File Copied..");
  436. } catch (IOException eex) {
  437. eex.printStackTrace();
  438. }
  439. }
  440. // /**
  441. // * Edit the Information.
  442. // *
  443. // * @param obj
  444. // * the CpsObject
  445. // */
  446. // public void editInformation(HolonObject obj) {
  447. // objectName.setText(obj.getName());
  448. // }
  449. // /**
  450. // * Return the Object.
  451. // *
  452. // * @return the CpsObject
  453. // */
  454. // public HolonObject getObject() {
  455. // return theObject;
  456. // }
  457. /**
  458. * Sets the Controller.
  459. *
  460. * @param controller
  461. * the controller
  462. */
  463. public void setController(Control controller) {
  464. this.controller = controller;
  465. }
  466. /**
  467. * Set the Category.
  468. *
  469. * @param cat
  470. * the Category
  471. */
  472. public void setCategory(String cat) {
  473. givenCategory = cat;
  474. }
  475. }