AddObjectPopUp.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. }
  156. sourcePath.setBounds(148, 77, 271, 20);
  157. contentPanel.add(sourcePath);
  158. sourcePath.setColumns(10);
  159. }
  160. {
  161. JButton btnAddDefaultElement = new JButton("Add Element");
  162. btnAddDefaultElement.addActionListener(new ActionListener() {
  163. public void actionPerformed(ActionEvent arg0) {
  164. addElement = new AddElementPopUp();
  165. addElement.setActualCps(obj);
  166. addElement.setVisible(true);
  167. HolonElement hl = addElement.getElement();
  168. addElement(hl);
  169. }
  170. });
  171. btnAddDefaultElement.setBounds(270, 144, 142, 23);
  172. contentPanel.add(btnAddDefaultElement);
  173. }
  174. {
  175. JScrollPane scrollPane = new JScrollPane();
  176. scrollPane.setBounds(10, 114, 236, 150);
  177. contentPanel.add(scrollPane);
  178. {
  179. listModel = new DefaultListModel();
  180. HolonElement hel = new HolonElement("Test", 100, 5);
  181. String name = hel.getEleName();
  182. /*
  183. * for (int i = 0; i < 11; i++) { hel.setEleName(name + i);
  184. * addElement(hel); }
  185. */
  186. list = new JList(listModel);
  187. scrollPane.setViewportView(list);
  188. }
  189. }
  190. if (edit) {
  191. for (HolonElement e : ((HolonObject) obj).getElements()) {
  192. addElement(e);
  193. }
  194. }
  195. {
  196. JButton btnNewButton = new JButton("Delete Element");
  197. btnNewButton.addActionListener(new ActionListener() {
  198. public void actionPerformed(ActionEvent e) {
  199. int selectedIndex = list.getSelectedIndex();
  200. if (selectedIndex != -1) {
  201. listModel.remove(selectedIndex);
  202. hElements.remove(selectedIndex);
  203. }
  204. }
  205. });
  206. btnNewButton.setBounds(270, 182, 142, 27);
  207. contentPanel.add(btnNewButton);
  208. }
  209. {
  210. JPanel buttonPane = new JPanel();
  211. buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
  212. getContentPane().add(buttonPane, BorderLayout.SOUTH);
  213. {
  214. JButton okButton = new JButton("OK");
  215. okButton.addMouseListener(new MouseAdapter() {
  216. public void mouseClicked(MouseEvent e) {
  217. Component frame = null;
  218. if (objectName.getText().length() > 0) {
  219. if (sourcePath.getText().equals(filePath)) {
  220. imagePath = filePath;
  221. if(imageChanged)
  222. copieFile();
  223. imageChanged = false;
  224. // theObject = new
  225. // HolonObject(objectName.getText());
  226. // theObject.setElements(hElements);
  227. // theObject.setImage(imagePath);
  228. if (edit) {
  229. controller.delObjectCategory(givenCategory, toEdit.getName());
  230. controller.addObject(controller.searchCategory(givenCategory), objectName.getText(),
  231. hElements, imagePath);
  232. } else {
  233. controller.addObject(controller.searchCategory(givenCategory), objectName.getText(),
  234. hElements, imagePath);
  235. }
  236. // controller.addObjectCategory(controller.searchCategory(givenCategory),
  237. // theObject);
  238. //
  239. // System.out.println(theObject.getImage());
  240. System.out.println(givenCategory);
  241. dispose();
  242. } else {
  243. sourcePath.setBackground(new Color(255, 50, 50));
  244. }
  245. } else {
  246. objectName.setBackground(new Color(255, 50, 50));
  247. if (!sourcePath.getText().equals(filePath))
  248. sourcePath.setBackground(new Color(255, 50, 50));
  249. }
  250. }
  251. });
  252. okButton.setActionCommand("OK");
  253. buttonPane.add(okButton);
  254. getRootPane().setDefaultButton(okButton);
  255. }
  256. {
  257. JButton cancelButton = new JButton("Cancel");
  258. cancelButton.setActionCommand("Cancel");
  259. buttonPane.add(cancelButton);
  260. cancelButton.addActionListener(new ActionListener() {
  261. public void actionPerformed(ActionEvent e) {
  262. dispose();
  263. }
  264. });
  265. }
  266. }
  267. }
  268. public void addElement(HolonElement hl) {
  269. hElements.add(hl);
  270. listModel.addElement(hl.getAmount() + "x: " + hl.getEleName() + " " + hl.getEnergy() + "U");
  271. }
  272. protected void fileChooser() {
  273. // TODO Auto-generated method stub
  274. JFileChooser FileChooser = new JFileChooser();
  275. FileNameExtensionFilter filter = new FileNameExtensionFilter("png, jpg or jpeg", "png", "jpg", "jpeg");
  276. FileChooser.setFileFilter(filter);
  277. int returnValue = FileChooser.showOpenDialog(null);
  278. if (returnValue == JFileChooser.APPROVE_OPTION) {
  279. selectedFile = FileChooser.getSelectedFile();
  280. filePath = selectedFile.getAbsolutePath();
  281. sourcePath.setText(filePath);
  282. ImageIcon icon = new ImageIcon(
  283. new ImageIcon(filePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
  284. lblImagePreview.setIcon(icon);
  285. imageChanged = true;
  286. } else {
  287. System.out.println("Failed to Load");
  288. }
  289. }
  290. protected void copieFile() {
  291. InputStream inStream = null;
  292. OutputStream outStream = null;
  293. try {
  294. File source = new File(filePath);
  295. File dest = new File(System.getProperty("user.home") + "/HolonGUI/Images/");
  296. dest.mkdirs();
  297. dest = new File(dest, selectedFile.getName());
  298. imagePath = "" + dest;
  299. inStream = new FileInputStream(source);
  300. outStream = new FileOutputStream(dest);
  301. byte[] buffer = new byte[1024];
  302. int length;
  303. while ((length = inStream.read(buffer)) > 0) {
  304. outStream.write(buffer, 0, length);
  305. }
  306. if (inStream != null)
  307. inStream.close();
  308. if (outStream != null)
  309. outStream.close();
  310. System.out.println("File Copied..");
  311. } catch (IOException e1) {
  312. e1.printStackTrace();
  313. }
  314. }
  315. public void editInformation(HolonObject obj) {
  316. objectName.setText(obj.getName());
  317. }
  318. public static String getJarContainingFolder(Class aclass) throws Exception {
  319. CodeSource codeSource = aclass.getProtectionDomain().getCodeSource();
  320. File jarFile;
  321. if (codeSource.getLocation() != null) {
  322. jarFile = new File(codeSource.getLocation().toURI());
  323. } else {
  324. String path = aclass.getResource(aclass.getSimpleName() + ".class").getPath();
  325. String jarFilePath = path.substring(path.indexOf(":") + 1, path.indexOf("!"));
  326. jarFilePath = URLDecoder.decode(jarFilePath, "UTF-8");
  327. jarFile = new File(jarFilePath);
  328. }
  329. return jarFile.getParentFile().getAbsolutePath();
  330. }
  331. public HolonObject getObject() {
  332. return theObject;
  333. }
  334. public void setController(Control controller) {
  335. this.controller = controller;
  336. }
  337. public void setCategory(String cat) {
  338. givenCategory = cat;
  339. }
  340. }