AddObjectPopUp.java 17 KB

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