AddObjectPopUp.java 16 KB

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