AddObjectPopUp.java 11 KB

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