AddObjectPopUp.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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.CpsObject;
  36. import classes.HolonElement;
  37. import classes.HolonObject;
  38. import ui.controller.Control;
  39. public class AddObjectPopUp extends JDialog {
  40. private final JPanel contentPanel = new JPanel();
  41. private AddElementPopUp addElement;
  42. private JTextField objectName;
  43. private JTextField sourcePath;
  44. private ArrayList<HolonElement> hElements;
  45. private DefaultListModel listModel;
  46. private JList list;
  47. private String imagePath;
  48. private HolonObject theObject;
  49. private Control controller;
  50. private File selectedFile = null;
  51. private String filePath = " ";
  52. private String givenCategory;
  53. private JLabel lblImagePreview;
  54. private CpsObject toEdit;
  55. private boolean imageChanged = false;
  56. /**
  57. * Launch the application.
  58. */
  59. public static void main(String[] args) {
  60. try {
  61. AddObjectPopUp dialog = new AddObjectPopUp(false, null, null);
  62. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  63. dialog.setVisible(true);
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. /**
  69. * Create the dialog.
  70. */
  71. public AddObjectPopUp(boolean edit, CpsObject obj, String cat) {
  72. toEdit = obj;
  73. this.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
  74. .getScaledInstance(30, 30, Image.SCALE_SMOOTH));
  75. setBounds(100, 100, 450, 342);
  76. getContentPane().setLayout(new BorderLayout());
  77. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  78. getContentPane().add(contentPanel, BorderLayout.CENTER);
  79. contentPanel.setLayout(null);
  80. hElements = new ArrayList<HolonElement>();
  81. this.setTitle("Add Object Menu");
  82. {
  83. JLabel lblName = new JLabel("Name:");
  84. lblName.setHorizontalAlignment(SwingConstants.CENTER);
  85. lblName.setBounds(28, 21, 76, 14);
  86. contentPanel.add(lblName);
  87. }
  88. {
  89. objectName = new JTextField();
  90. objectName.addKeyListener(new KeyListener() {
  91. @Override
  92. public void keyPressed(KeyEvent arg0) {
  93. // TODO Auto-generated method stub
  94. }
  95. @Override
  96. public void keyReleased(KeyEvent e) {
  97. // TODO Auto-generated method stub
  98. }
  99. @Override
  100. public void keyTyped(KeyEvent e) {
  101. objectName.setBackground(Color.WHITE);
  102. }
  103. });
  104. if (edit) {
  105. objectName.setText(obj.getName());
  106. }
  107. objectName.setBounds(98, 18, 172, 20);
  108. contentPanel.add(objectName);
  109. objectName.setColumns(10);
  110. }
  111. {
  112. JButton btnBrowseImage = new JButton("Browse Image");
  113. btnBrowseImage.setBounds(10, 75, 134, 23);
  114. contentPanel.add(btnBrowseImage);
  115. btnBrowseImage.addMouseListener(new MouseAdapter() {
  116. public void mouseClicked(MouseEvent e) {
  117. fileChooser();
  118. }
  119. });
  120. }
  121. {
  122. lblImagePreview = new JLabel("");
  123. lblImagePreview.setBounds(295, 3, 50, 50);
  124. contentPanel.add(lblImagePreview);
  125. }
  126. {
  127. sourcePath = new JTextField();
  128. sourcePath.addKeyListener(new KeyListener() {
  129. @Override
  130. public void keyPressed(KeyEvent arg0) {
  131. // TODO Auto-generated method stub
  132. }
  133. @Override
  134. public void keyReleased(KeyEvent e) {
  135. // TODO Auto-generated method stub
  136. }
  137. @Override
  138. public void keyTyped(KeyEvent e) {
  139. sourcePath.setBackground(Color.WHITE);
  140. }
  141. });
  142. if (edit) {
  143. File checkPath = new File(obj.getImage());
  144. if (checkPath.exists()) {
  145. selectedFile = new File(obj.getImage());
  146. } else {
  147. selectedFile = new File(this.getClass().getResource(obj.getImage()).getFile());
  148. }
  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. if(icon != null)
  154. lblImagePreview.setIcon(icon);
  155. // sourcePath.setText(obj.getImage());
  156. // if (sourcePath.getText().startsWith("/Images/")) {
  157. // sourcePath
  158. // .setText(System.getProperty("user.home") + "/praktikum-holons/res/" + sourcePath.getText());
  159. // }
  160. // filePath = sourcePath.getText();
  161. // selectedFile = new File(sourcePath.getText());
  162. }
  163. sourcePath.setBounds(148, 77, 271, 20);
  164. contentPanel.add(sourcePath);
  165. sourcePath.setColumns(10);
  166. }
  167. {
  168. JButton btnAddDefaultElement = new JButton("Add Element");
  169. btnAddDefaultElement.addActionListener(new ActionListener() {
  170. public void actionPerformed(ActionEvent arg0) {
  171. addElement = new AddElementPopUp();
  172. addElement.setVisible(true);
  173. HolonElement hl = addElement.getElement();
  174. addElement(hl);
  175. }
  176. });
  177. btnAddDefaultElement.setBounds(270, 144, 142, 23);
  178. contentPanel.add(btnAddDefaultElement);
  179. }
  180. {
  181. JScrollPane scrollPane = new JScrollPane();
  182. scrollPane.setBounds(10, 114, 236, 150);
  183. contentPanel.add(scrollPane);
  184. {
  185. listModel = new DefaultListModel();
  186. HolonElement hel = new HolonElement("Test", 100, 5);
  187. String name = hel.getEleName();
  188. /*
  189. * for (int i = 0; i < 11; i++) { hel.setEleName(name + i);
  190. * addElement(hel); }
  191. */
  192. list = new JList(listModel);
  193. scrollPane.setViewportView(list);
  194. }
  195. }
  196. if (edit) {
  197. for (HolonElement e : ((HolonObject) obj).getElements()) {
  198. addElement(e);
  199. }
  200. }
  201. {
  202. JButton btnNewButton = new JButton("Delete Element");
  203. btnNewButton.addActionListener(new ActionListener() {
  204. public void actionPerformed(ActionEvent e) {
  205. int selectedIndex = list.getSelectedIndex();
  206. if (selectedIndex != -1) {
  207. listModel.remove(selectedIndex);
  208. hElements.remove(selectedIndex);
  209. }
  210. }
  211. });
  212. btnNewButton.setBounds(270, 182, 142, 27);
  213. contentPanel.add(btnNewButton);
  214. }
  215. {
  216. JPanel buttonPane = new JPanel();
  217. buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
  218. getContentPane().add(buttonPane, BorderLayout.SOUTH);
  219. {
  220. JButton okButton = new JButton("OK");
  221. okButton.addMouseListener(new MouseAdapter() {
  222. public void mouseClicked(MouseEvent e) {
  223. Component frame = null;
  224. if (objectName.getText().length() > 0) {
  225. if (sourcePath.getText().equals(filePath)) {
  226. imagePath = filePath;
  227. if(imageChanged)
  228. copieFile();
  229. imageChanged = false;
  230. // theObject = new
  231. // HolonObject(objectName.getText());
  232. // theObject.setElements(hElements);
  233. // theObject.setImage(imagePath);
  234. if (edit) {
  235. controller.delObjectCategory(givenCategory, toEdit.getName());
  236. controller.addObject(controller.searchCategory(givenCategory), objectName.getText(),
  237. hElements, imagePath);
  238. } else {
  239. controller.addObject(controller.searchCategory(givenCategory), objectName.getText(),
  240. hElements, imagePath);
  241. }
  242. // controller.addObjectCategory(controller.searchCategory(givenCategory),
  243. // theObject);
  244. //
  245. // System.out.println(theObject.getImage());
  246. System.out.println(givenCategory);
  247. dispose();
  248. } else {
  249. sourcePath.setBackground(new Color(255, 50, 50));
  250. }
  251. } else {
  252. objectName.setBackground(new Color(255, 50, 50));
  253. if (!sourcePath.getText().equals(filePath))
  254. sourcePath.setBackground(new Color(255, 50, 50));
  255. }
  256. }
  257. });
  258. okButton.setActionCommand("OK");
  259. buttonPane.add(okButton);
  260. getRootPane().setDefaultButton(okButton);
  261. }
  262. {
  263. JButton cancelButton = new JButton("Cancel");
  264. cancelButton.setActionCommand("Cancel");
  265. buttonPane.add(cancelButton);
  266. cancelButton.addActionListener(new ActionListener() {
  267. public void actionPerformed(ActionEvent e) {
  268. dispose();
  269. }
  270. });
  271. }
  272. }
  273. }
  274. public void addElement(HolonElement hl) {
  275. hElements.add(hl);
  276. listModel.addElement(hl.getAmount() + "x: " + hl.getEleName() + " " + hl.getEnergy() + "U");
  277. }
  278. protected void fileChooser() {
  279. // TODO Auto-generated method stub
  280. JFileChooser FileChooser = new JFileChooser();
  281. FileNameExtensionFilter filter = new FileNameExtensionFilter("png, jpg or jpeg", "png", "jpg", "jpeg");
  282. FileChooser.setFileFilter(filter);
  283. int returnValue = FileChooser.showOpenDialog(null);
  284. if (returnValue == JFileChooser.APPROVE_OPTION) {
  285. selectedFile = FileChooser.getSelectedFile();
  286. filePath = selectedFile.getAbsolutePath();
  287. sourcePath.setText(filePath);
  288. ImageIcon icon = new ImageIcon(
  289. new ImageIcon(filePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
  290. lblImagePreview.setIcon(icon);
  291. imageChanged = true;
  292. } else {
  293. System.out.println("Failed to Load");
  294. }
  295. }
  296. protected void copieFile() {
  297. InputStream inStream = null;
  298. OutputStream outStream = null;
  299. try {
  300. File source = new File(filePath);
  301. File dest = new File(System.getProperty("user.home") + "/HolonGUI/Images/");
  302. dest.mkdirs();
  303. dest = new File(dest, selectedFile.getName());
  304. imagePath = "" + dest;
  305. inStream = new FileInputStream(source);
  306. outStream = new FileOutputStream(dest);
  307. byte[] buffer = new byte[1024];
  308. int length;
  309. while ((length = inStream.read(buffer)) > 0) {
  310. outStream.write(buffer, 0, length);
  311. }
  312. if (inStream != null)
  313. inStream.close();
  314. if (outStream != null)
  315. outStream.close();
  316. System.out.println("File Copied..");
  317. } catch (IOException e1) {
  318. e1.printStackTrace();
  319. }
  320. }
  321. public void editInformation(HolonObject obj) {
  322. objectName.setText(obj.getName());
  323. }
  324. public static String getJarContainingFolder(Class aclass) throws Exception {
  325. CodeSource codeSource = aclass.getProtectionDomain().getCodeSource();
  326. File jarFile;
  327. if (codeSource.getLocation() != null) {
  328. jarFile = new File(codeSource.getLocation().toURI());
  329. } else {
  330. String path = aclass.getResource(aclass.getSimpleName() + ".class").getPath();
  331. String jarFilePath = path.substring(path.indexOf(":") + 1, path.indexOf("!"));
  332. jarFilePath = URLDecoder.decode(jarFilePath, "UTF-8");
  333. jarFile = new File(jarFilePath);
  334. }
  335. return jarFile.getParentFile().getAbsolutePath();
  336. }
  337. public HolonObject getObject() {
  338. return theObject;
  339. }
  340. public void setController(Control controller) {
  341. this.controller = controller;
  342. }
  343. public void setCategory(String cat) {
  344. givenCategory = cat;
  345. }
  346. }