AddObjectPopUp.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. package ui.view;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.FlowLayout;
  6. import java.awt.Image;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.KeyEvent;
  10. import java.awt.event.KeyListener;
  11. import java.awt.event.MouseAdapter;
  12. import java.awt.event.MouseEvent;
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19. import java.net.URLDecoder;
  20. import java.security.CodeSource;
  21. import java.util.ArrayList;
  22. import javax.swing.DefaultListModel;
  23. import javax.swing.ImageIcon;
  24. import javax.swing.JButton;
  25. import javax.swing.JDialog;
  26. import javax.swing.JFileChooser;
  27. import javax.swing.JLabel;
  28. import javax.swing.JList;
  29. import javax.swing.JPanel;
  30. import javax.swing.JScrollPane;
  31. import javax.swing.JTextField;
  32. import javax.swing.SwingConstants;
  33. import javax.swing.border.EmptyBorder;
  34. import javax.swing.filechooser.FileNameExtensionFilter;
  35. import classes.AbstractCpsObject;
  36. import classes.HolonElement;
  37. import classes.HolonObject;
  38. import ui.controller.Control;
  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 final JPanel contentPanel = new JPanel();
  47. private AddElementPopUp addElement;
  48. private JTextField objectName;
  49. private JTextField sourcePath;
  50. private ArrayList<HolonElement> hElements;
  51. private DefaultListModel listModel;
  52. private JList list;
  53. private String imagePath;
  54. private HolonObject theObject;
  55. private Control controller;
  56. private File selectedFile = null;
  57. private String filePath = " ";
  58. private String givenCategory;
  59. private JLabel lblImagePreview;
  60. private AbstractCpsObject toEdit;
  61. private boolean editState;
  62. private boolean imageChanged = false;
  63. /**
  64. * Launch the application.
  65. *
  66. * @param args
  67. * standard
  68. */
  69. public static void main(String[] args) {
  70. try {
  71. AddObjectPopUp dialog = new AddObjectPopUp(false, null, null);
  72. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  73. dialog.setVisible(true);
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. /**
  79. * Create the dialog.
  80. *
  81. * @param edit
  82. * true if edit
  83. * @param obj
  84. * the object
  85. * @param cat
  86. * the categorie
  87. */
  88. public AddObjectPopUp(boolean edit, AbstractCpsObject obj, String cat) {
  89. toEdit = obj;
  90. editState = edit;
  91. this.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
  92. .getScaledInstance(30, 30, Image.SCALE_SMOOTH));
  93. setBounds(100, 100, 450, 342);
  94. getContentPane().setLayout(new BorderLayout());
  95. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  96. getContentPane().add(contentPanel, BorderLayout.CENTER);
  97. contentPanel.setLayout(null);
  98. hElements = new ArrayList<HolonElement>();
  99. this.setTitle(Languages.getLanguage()[58]);
  100. {
  101. JLabel lblName = new JLabel(Languages.getLanguage()[59]);
  102. lblName.setHorizontalAlignment(SwingConstants.CENTER);
  103. lblName.setBounds(28, 21, 76, 14);
  104. contentPanel.add(lblName);
  105. }
  106. {
  107. objectName = new JTextField();
  108. objectName.addKeyListener(new KeyListener() {
  109. @Override
  110. public void keyPressed(KeyEvent arg0) {
  111. // TODO Auto-generated method stub
  112. }
  113. @Override
  114. public void keyReleased(KeyEvent e) {
  115. // TODO Auto-generated method stub
  116. }
  117. @Override
  118. public void keyTyped(KeyEvent e) {
  119. objectName.setBackground(Color.WHITE);
  120. }
  121. });
  122. if (edit) {
  123. objectName.setText(obj.getName());
  124. }
  125. objectName.setBounds(98, 18, 172, 20);
  126. contentPanel.add(objectName);
  127. objectName.setColumns(10);
  128. }
  129. {
  130. JButton btnBrowseImage = new JButton(Languages.getLanguage()[60]);
  131. btnBrowseImage.setBounds(10, 75, 134, 23);
  132. contentPanel.add(btnBrowseImage);
  133. btnBrowseImage.addMouseListener(new MouseAdapter() {
  134. public void mouseClicked(MouseEvent e) {
  135. fileChooser();
  136. }
  137. });
  138. }
  139. {
  140. lblImagePreview = new JLabel("");
  141. lblImagePreview.setBounds(295, 3, 50, 50);
  142. contentPanel.add(lblImagePreview);
  143. }
  144. {
  145. sourcePath = new JTextField();
  146. sourcePath.addKeyListener(new KeyListener() {
  147. @Override
  148. public void keyPressed(KeyEvent arg0) {
  149. // TODO Auto-generated method stub
  150. }
  151. @Override
  152. public void keyReleased(KeyEvent e) {
  153. // TODO Auto-generated method stub
  154. }
  155. @Override
  156. public void keyTyped(KeyEvent e) {
  157. sourcePath.setBackground(Color.WHITE);
  158. }
  159. });
  160. if (edit) {
  161. File checkPath = new File(obj.getImage());
  162. if (checkPath.exists()) {
  163. selectedFile = new File(obj.getImage());
  164. } else {
  165. selectedFile = new File(this.getClass().getResource(obj.getImage()).getFile());
  166. }
  167. filePath = selectedFile.getAbsolutePath();
  168. sourcePath.setText(filePath);
  169. ImageIcon icon = new ImageIcon(
  170. new ImageIcon(filePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
  171. if (icon != null)
  172. lblImagePreview.setIcon(icon);
  173. }
  174. sourcePath.setBounds(148, 77, 271, 20);
  175. contentPanel.add(sourcePath);
  176. sourcePath.setColumns(10);
  177. }
  178. {
  179. JButton btnAddDefaultElement = new JButton(Languages.getLanguage()[61]);
  180. btnAddDefaultElement.addActionListener(new ActionListener() {
  181. public void actionPerformed(ActionEvent arg0) {
  182. addElement = new AddElementPopUp();
  183. addElement.setActualCps(toEdit);
  184. addElement.setVisible(true);
  185. HolonElement hl = addElement.getElement();
  186. if (hl != null) {
  187. hl.setSav(givenCategory);
  188. }
  189. hl.setObj(objectName.getText());
  190. addElement(hl);
  191. }
  192. });
  193. btnAddDefaultElement.setBounds(270, 144, 142, 23);
  194. contentPanel.add(btnAddDefaultElement);
  195. }
  196. {
  197. JScrollPane scrollPane = new JScrollPane();
  198. scrollPane.setBounds(10, 114, 236, 150);
  199. contentPanel.add(scrollPane);
  200. {
  201. listModel = new DefaultListModel();
  202. /*
  203. * HolonElement hel = new HolonElement("Test", 100, 5); String
  204. * name = hel.getEleName(); for (int i = 0; i < 11; i++) {
  205. * hel.setEleName(name + i); addElement(hel); }
  206. */
  207. list = new JList(listModel);
  208. scrollPane.setViewportView(list);
  209. }
  210. }
  211. if (edit) {
  212. for (HolonElement e : ((HolonObject) obj).getElements()) {
  213. addElement(e);
  214. }
  215. }
  216. {
  217. JButton btnNewButton = new JButton(Languages.getLanguage()[62]);
  218. btnNewButton.addActionListener(new ActionListener() {
  219. public void actionPerformed(ActionEvent e) {
  220. int selectedIndex = list.getSelectedIndex();
  221. if (selectedIndex != -1) {
  222. listModel.remove(selectedIndex);
  223. hElements.remove(selectedIndex);
  224. }
  225. }
  226. });
  227. btnNewButton.setBounds(270, 182, 142, 27);
  228. contentPanel.add(btnNewButton);
  229. }
  230. {
  231. JPanel buttonPane = new JPanel();
  232. buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
  233. getContentPane().add(buttonPane, BorderLayout.SOUTH);
  234. {
  235. JButton okButton = new JButton("OK");
  236. okButton.addMouseListener(new MouseAdapter() {
  237. public void mouseClicked(MouseEvent e) {
  238. Component frame = null;
  239. if (objectName.getText().length() > 0) {
  240. if (sourcePath.getText().equals(filePath)) {
  241. imagePath = filePath;
  242. if (imageChanged)
  243. copieFile();
  244. imageChanged = false;
  245. // theObject = new
  246. // HolonObject(objectName.getText());
  247. // theObject.setElements(hElements);
  248. // theObject.setImage(imagePath);
  249. if (editState) {
  250. controller.delObjectCategory(givenCategory, toEdit.getName());
  251. controller.addObject(controller.searchCategory(givenCategory), objectName.getText(),
  252. hElements, imagePath);
  253. } else {
  254. controller.addObject(controller.searchCategory(givenCategory), objectName.getText(),
  255. hElements, imagePath);
  256. }
  257. // controller.addObjectCategory(controller.searchCategory(givenCategory),
  258. // theObject);
  259. //
  260. // System.out.println(theObject.getImage());
  261. System.out.println(givenCategory);
  262. dispose();
  263. } else {
  264. sourcePath.setBackground(new Color(255, 50, 50));
  265. }
  266. } else {
  267. objectName.setBackground(new Color(255, 50, 50));
  268. if (!sourcePath.getText().equals(filePath))
  269. sourcePath.setBackground(new Color(255, 50, 50));
  270. }
  271. }
  272. });
  273. okButton.setActionCommand("OK");
  274. buttonPane.add(okButton);
  275. getRootPane().setDefaultButton(okButton);
  276. }
  277. {
  278. JButton cancelButton = new JButton(Languages.getLanguage()[63]);
  279. cancelButton.setActionCommand("Cancel");
  280. buttonPane.add(cancelButton);
  281. cancelButton.addActionListener(new ActionListener() {
  282. public void actionPerformed(ActionEvent e) {
  283. dispose();
  284. }
  285. });
  286. }
  287. }
  288. }
  289. /**
  290. * adds a Holon Element.
  291. *
  292. * @param hl
  293. * the HolonElement
  294. */
  295. public void addElement(HolonElement hl) {
  296. hElements.add(hl);
  297. listModel.addElement(hl.getAmount() + "x: " + hl.getEleName() + " " + hl.getEnergy() + "U");
  298. }
  299. /**
  300. * Choose the file.
  301. */
  302. protected void fileChooser() {
  303. // TODO Auto-generated method stub
  304. JFileChooser fileChooser = new JFileChooser();
  305. FileNameExtensionFilter filter = new FileNameExtensionFilter("png, jpg or jpeg", "png", "jpg", "jpeg");
  306. fileChooser.setFileFilter(filter);
  307. int returnValue = fileChooser.showOpenDialog(null);
  308. if (returnValue == JFileChooser.APPROVE_OPTION) {
  309. selectedFile = fileChooser.getSelectedFile();
  310. filePath = selectedFile.getAbsolutePath();
  311. sourcePath.setText(filePath);
  312. ImageIcon icon = new ImageIcon(
  313. new ImageIcon(filePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
  314. lblImagePreview.setIcon(icon);
  315. imageChanged = true;
  316. } else {
  317. System.out.println("Failed to Load");
  318. }
  319. }
  320. /**
  321. * Copies the File.
  322. */
  323. protected void copieFile() {
  324. InputStream inStream = null;
  325. OutputStream outStream = null;
  326. try {
  327. File source = new File(filePath);
  328. File dest = new File(System.getProperty("user.home") + "/HolonGUI/Images/");
  329. dest.mkdirs();
  330. dest = new File(dest, selectedFile.getName());
  331. imagePath = "" + dest;
  332. inStream = new FileInputStream(source);
  333. outStream = new FileOutputStream(dest);
  334. byte[] buffer = new byte[1024];
  335. int length;
  336. while ((length = inStream.read(buffer)) > 0) {
  337. outStream.write(buffer, 0, length);
  338. }
  339. if (inStream != null)
  340. inStream.close();
  341. if (outStream != null)
  342. outStream.close();
  343. System.out.println("File Copied..");
  344. } catch (IOException eex) {
  345. eex.printStackTrace();
  346. }
  347. }
  348. /**
  349. * Edit the Information.
  350. *
  351. * @param obj
  352. * the CpsObject
  353. */
  354. public void editInformation(HolonObject obj) {
  355. objectName.setText(obj.getName());
  356. }
  357. /**
  358. * Get Jar Containing Folder.
  359. *
  360. * @param aclass
  361. * aClass
  362. * @return String
  363. * @throws Exception
  364. * Exception
  365. */
  366. public static String getJarContainingFolder(Class aclass) throws Exception {
  367. CodeSource codeSource = aclass.getProtectionDomain().getCodeSource();
  368. File jarFile;
  369. if (codeSource.getLocation() != null) {
  370. jarFile = new File(codeSource.getLocation().toURI());
  371. } else {
  372. String path = aclass.getResource(aclass.getSimpleName() + ".class").getPath();
  373. String jarFilePath = path.substring(path.indexOf(":") + 1, path.indexOf("!"));
  374. jarFilePath = URLDecoder.decode(jarFilePath, "UTF-8");
  375. jarFile = new File(jarFilePath);
  376. }
  377. return jarFile.getParentFile().getAbsolutePath();
  378. }
  379. /**
  380. * Return the Object.
  381. *
  382. * @return the CpsObject
  383. */
  384. public HolonObject getObject() {
  385. return theObject;
  386. }
  387. /**
  388. * Sets the Controller.
  389. *
  390. * @param controller
  391. * the controller
  392. */
  393. public void setController(Control controller) {
  394. this.controller = controller;
  395. }
  396. /**
  397. * Set the Category.
  398. *
  399. * @param cat
  400. * the Category
  401. */
  402. public void setCategory(String cat) {
  403. givenCategory = cat;
  404. }
  405. }