AddObjectPopUp.java 9.6 KB

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