BackgroundPopUp.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package ui.view;
  2. import javax.swing.JDialog;
  3. import javax.swing.JFileChooser;
  4. import javax.swing.JPanel;
  5. import javax.swing.JTextField;
  6. import javax.swing.filechooser.FileNameExtensionFilter;
  7. import javax.swing.ButtonGroup;
  8. import javax.swing.ImageIcon;
  9. import javax.swing.JButton;
  10. import classes.CpsUpperNode;
  11. import ui.controller.Control;
  12. import ui.model.Model;
  13. import java.awt.event.ActionEvent;
  14. import java.awt.event.ActionListener;
  15. import java.awt.event.ComponentAdapter;
  16. import java.awt.event.ComponentEvent;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import java.awt.BorderLayout;
  24. import javax.swing.JLabel;
  25. import javax.swing.JOptionPane;
  26. import javax.swing.JRadioButton;
  27. import javax.swing.SwingConstants;
  28. import java.awt.GridLayout;
  29. import java.awt.Image;
  30. import java.awt.Color;
  31. import java.awt.FlowLayout;
  32. /**
  33. * Popup for setting the Background Image for the current View.
  34. **/
  35. public class BackgroundPopUp extends JDialog {
  36. /**
  37. *
  38. */
  39. private static final long serialVersionUID = 1L;
  40. private final JTextField textPath = new JTextField("");
  41. private final JButton btnBrowse = new JButton("Browse");
  42. private JPanel panelImageRadio = new JPanel();
  43. private String path = "";
  44. private final JPanel panelBrowse = new JPanel();
  45. private final JPanel panelOK = new JPanel();
  46. private final JButton btnOK = new JButton("OK");
  47. private final JLabel lblImage = new JLabel();
  48. private boolean imageChanged = false;
  49. private ImageIcon icon = null;
  50. private double imgScale = 1;
  51. private final JButton btnCancel = new JButton("Cancel");
  52. private final JPanel panelRadio = new JPanel();
  53. private final JRadioButton rdbtnImagePixel = new JRadioButton("Use Image Size");
  54. private final JRadioButton rdbtnStretched = new JRadioButton("Strech Image");
  55. private final JRadioButton rdbtnCustom = new JRadioButton("Custom Size");
  56. private final JButton removeImageBtn = new JButton("Remove Background Image");
  57. private boolean imageBtnClearedPressed = false;
  58. // Modes
  59. public static final int IMAGE_PIXELS = 0, STRETCHED = 1, CUSTOM = 2;
  60. private int mode = 0;
  61. private final JPanel panel = new JPanel();
  62. private final JTextField imageWidth = new JTextField();
  63. private final JTextField imageHeight = new JTextField();
  64. JFileChooser fileChooser;
  65. public BackgroundPopUp(Model model, Control controller, MyCanvas canvas, CpsUpperNode uNode) {
  66. super((java.awt.Frame) null, true);
  67. getContentPane().setBackground(Color.WHITE);
  68. this.setTitle("Set View Background");
  69. // Show background Image
  70. if (canvas != null) {
  71. path = model.getCanvasImagePath();
  72. } else if (uNode != null) {
  73. path = uNode.getImagePath();
  74. }
  75. if (!path.isEmpty()) {
  76. textPath.setText(path);
  77. icon = new ImageIcon(path);
  78. imageWidth.setText("" + icon.getIconWidth());
  79. imageHeight.setText("" + icon.getIconHeight());
  80. }
  81. this.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
  82. .getScaledInstance(30, 30, Image.SCALE_SMOOTH));
  83. setBounds(100, 100, 600, 340);
  84. setLocationRelativeTo(this.getParent());
  85. panelBrowse.setBorder(null);
  86. panelImageRadio.setBorder(null);
  87. panelOK.setBorder(null);
  88. panelRadio.setBorder(null);
  89. getContentPane().add(panelImageRadio, BorderLayout.CENTER);
  90. panelImageRadio.setLayout(new BorderLayout(0, 0));
  91. lblImage.setBackground(Color.WHITE);
  92. lblImage.setHorizontalAlignment(SwingConstants.LEFT);
  93. panelImageRadio.add(lblImage, BorderLayout.CENTER);
  94. panelImageRadio.add(panelRadio, BorderLayout.EAST);
  95. // Radio Buttons
  96. panelRadio.setLayout(new GridLayout(0, 1, 0, 0));
  97. rdbtnImagePixel.setBackground(Color.WHITE);
  98. rdbtnImagePixel.setSelected(true);
  99. panelRadio.add(rdbtnImagePixel);
  100. rdbtnStretched.setBackground(Color.WHITE);
  101. panelRadio.add(rdbtnStretched);
  102. panelBrowse.setBackground(Color.WHITE);
  103. ButtonGroup bgroup = new ButtonGroup();
  104. bgroup.add(rdbtnImagePixel);
  105. bgroup.add(rdbtnStretched);
  106. panelRadio.add(rdbtnCustom);
  107. rdbtnCustom.setBackground(new Color(255, 255, 255));
  108. bgroup.add(rdbtnCustom);
  109. panel.setBackground(Color.WHITE);
  110. panelRadio.add(panel);
  111. panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
  112. panel.add(imageWidth);
  113. imageWidth.setColumns(10);
  114. panel.add(imageHeight);
  115. imageHeight.setColumns(10);
  116. // remove BackgroundImage Button
  117. removeImageBtn.setBackground(Color.WHITE);
  118. removeImageBtn.addActionListener(new ActionListener() {
  119. @Override
  120. public void actionPerformed(ActionEvent e) {
  121. path = "";
  122. textPath.setText("");
  123. imageBtnClearedPressed = true;
  124. lblImage.setIcon(null);
  125. }
  126. });
  127. JPanel removePanel = new JPanel();
  128. removePanel.add(removeImageBtn);
  129. removePanel.setBackground(Color.WHITE);
  130. panelRadio.add(removePanel);
  131. // Browse Panel
  132. getContentPane().add(panelBrowse, BorderLayout.NORTH);
  133. panelBrowse.setLayout(new BorderLayout(0, 0));
  134. panelBrowse.add(btnBrowse, BorderLayout.WEST);
  135. // Browse Row Functions
  136. btnBrowse.addActionListener(new ActionListener() {
  137. @Override
  138. public void actionPerformed(ActionEvent e) {
  139. fileChooser = new JFileChooser();
  140. FileNameExtensionFilter filter = new FileNameExtensionFilter("png, jpg or jpeg", "png", "jpg", "jpeg");
  141. fileChooser.setFileFilter(filter);
  142. if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
  143. path = fileChooser.getSelectedFile().getPath();
  144. textPath.setText(path);
  145. icon = new ImageIcon(textPath.getText());
  146. imageWidth.setText("" + icon.getIconWidth());
  147. imageHeight.setText("" + icon.getIconHeight());
  148. imageChanged = true;
  149. // Calculate the Image scale to fit the Panel
  150. if (Math.min(panelImageRadio.getWidth() - panelRadio.getWidth(),
  151. panelImageRadio.getHeight()) == (panelImageRadio.getWidth() - panelRadio.getWidth())) {
  152. imgScale = (double) Math.min(icon.getIconWidth(), icon.getIconHeight())
  153. / panelImageRadio.getWidth();
  154. } else {
  155. imgScale = (double) Math.min(icon.getIconWidth(), icon.getIconHeight())
  156. / panelImageRadio.getHeight();
  157. }
  158. lblImage.setIcon(new ImageIcon(
  159. new ImageIcon(path).getImage().getScaledInstance((int) (icon.getIconWidth() / imgScale),
  160. (int) (icon.getIconHeight() / imgScale), java.awt.Image.SCALE_FAST)));
  161. }
  162. }
  163. });
  164. textPath.setEditable(false);
  165. panelBrowse.add(textPath, BorderLayout.CENTER);
  166. textPath.setColumns(20);
  167. panelOK.setBackground(Color.WHITE);
  168. getContentPane().add(panelOK, BorderLayout.SOUTH);
  169. panelOK.add(btnOK);
  170. btnOK.addActionListener(new ActionListener() {
  171. @Override
  172. public void actionPerformed(ActionEvent e) {
  173. // picture selected?
  174. if (!path.isEmpty()) {
  175. if (rdbtnImagePixel.isSelected()) {
  176. mode = 0;
  177. } else if (rdbtnStretched.isSelected()) {
  178. mode = 1;
  179. } else if (rdbtnCustom.isSelected()) {
  180. mode = 2;
  181. }
  182. if (canvas != null) {
  183. if (imageChanged) {
  184. copieImage();
  185. }
  186. controller.setBackgroundImage(path, mode, Integer.parseInt(imageWidth.getText()),
  187. Integer.parseInt(imageHeight.getText()));
  188. canvas.repaint();
  189. } else if (uNode != null) {
  190. if (imageChanged) {
  191. copieImage();
  192. }
  193. uNode.setBackgroundImage(path, mode, Integer.parseInt(imageWidth.getText()),
  194. Integer.parseInt(imageHeight.getText()));
  195. }
  196. dispose();
  197. } else if (imageBtnClearedPressed) {
  198. if (canvas != null) {
  199. controller.setBackgroundImage(path, mode, Integer.parseInt(imageWidth.getText()),
  200. Integer.parseInt(imageHeight.getText()));
  201. canvas.repaint();
  202. } else if (uNode != null) {
  203. uNode.setBackgroundImage(path, mode, Integer.parseInt(imageWidth.getText()),
  204. Integer.parseInt(imageHeight.getText()));
  205. }
  206. dispose();
  207. } else {
  208. JOptionPane.showMessageDialog(null, "No image selected!", "Warning!", JOptionPane.WARNING_MESSAGE);
  209. }
  210. }
  211. });
  212. panelOK.add(btnCancel);
  213. btnCancel.addActionListener(new ActionListener() {
  214. @Override
  215. public void actionPerformed(ActionEvent e) {
  216. dispose();
  217. }
  218. });
  219. this.addComponentListener(new ComponentAdapter() {
  220. @Override
  221. public void componentResized(ComponentEvent e) {
  222. if (icon != null) {
  223. // Calculate the Image scale to fit the Panel
  224. if (Math.min(panelImageRadio.getWidth() - panelRadio.getWidth(),
  225. panelImageRadio.getHeight()) == (panelImageRadio.getWidth() - panelRadio.getWidth())) {
  226. imgScale = (double) Math.min(icon.getIconWidth(), icon.getIconHeight())
  227. / panelImageRadio.getWidth();
  228. } else {
  229. imgScale = (double) Math.min(icon.getIconWidth(), icon.getIconHeight())
  230. / panelImageRadio.getHeight();
  231. }
  232. lblImage.setIcon(new ImageIcon(
  233. new ImageIcon(path).getImage().getScaledInstance((int) (icon.getIconWidth() / imgScale),
  234. (int) (icon.getIconHeight() / imgScale), java.awt.Image.SCALE_SMOOTH)));
  235. }
  236. }
  237. });
  238. }
  239. protected void copieImage() {
  240. InputStream inStream = null;
  241. OutputStream outStream = null;
  242. try {
  243. File source = new File(path);
  244. File dest = new File(System.getProperty("user.home") + "/HolonGUI/BackgroundImages/");
  245. dest.mkdirs();
  246. dest = new File(dest, source.getName());
  247. path = "" + dest;
  248. inStream = new FileInputStream(source);
  249. outStream = new FileOutputStream(dest);
  250. byte[] buffer = new byte[1024];
  251. int length;
  252. while ((length = inStream.read(buffer)) > 0) {
  253. outStream.write(buffer, 0, length);
  254. }
  255. if (inStream != null)
  256. inStream.close();
  257. if (outStream != null)
  258. outStream.close();
  259. } catch (IOException eex) {
  260. eex.printStackTrace();
  261. }
  262. }
  263. }