BackgroundPopUp.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. panelBrowse.setBorder(null);
  85. panelImageRadio.setBorder(null);
  86. panelOK.setBorder(null);
  87. panelRadio.setBorder(null);
  88. getContentPane().add(panelImageRadio, BorderLayout.CENTER);
  89. panelImageRadio.setLayout(new BorderLayout(0, 0));
  90. lblImage.setBackground(Color.WHITE);
  91. lblImage.setHorizontalAlignment(SwingConstants.LEFT);
  92. panelImageRadio.add(lblImage, BorderLayout.CENTER);
  93. panelImageRadio.add(panelRadio, BorderLayout.EAST);
  94. // Radio Buttons
  95. panelRadio.setLayout(new GridLayout(0, 1, 0, 0));
  96. rdbtnImagePixel.setBackground(Color.WHITE);
  97. rdbtnImagePixel.setSelected(true);
  98. panelRadio.add(rdbtnImagePixel);
  99. rdbtnStretched.setBackground(Color.WHITE);
  100. panelRadio.add(rdbtnStretched);
  101. panelBrowse.setBackground(Color.WHITE);
  102. ButtonGroup bgroup = new ButtonGroup();
  103. bgroup.add(rdbtnImagePixel);
  104. bgroup.add(rdbtnStretched);
  105. panelRadio.add(rdbtnCustom);
  106. rdbtnCustom.setBackground(new Color(255, 255, 255));
  107. bgroup.add(rdbtnCustom);
  108. panel.setBackground(Color.WHITE);
  109. panelRadio.add(panel);
  110. panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
  111. panel.add(imageWidth);
  112. imageWidth.setColumns(10);
  113. panel.add(imageHeight);
  114. imageHeight.setColumns(10);
  115. // remove BackgroundImage Button
  116. removeImageBtn.setBackground(Color.WHITE);
  117. removeImageBtn.addActionListener(new ActionListener() {
  118. @Override
  119. public void actionPerformed(ActionEvent e) {
  120. path = "";
  121. textPath.setText("");
  122. imageBtnClearedPressed = true;
  123. lblImage.setIcon(null);
  124. }
  125. });
  126. JPanel removePanel = new JPanel();
  127. removePanel.add(removeImageBtn);
  128. removePanel.setBackground(Color.WHITE);
  129. panelRadio.add(removePanel);
  130. // Browse Panel
  131. getContentPane().add(panelBrowse, BorderLayout.NORTH);
  132. panelBrowse.setLayout(new BorderLayout(0, 0));
  133. panelBrowse.add(btnBrowse, BorderLayout.WEST);
  134. // Browse Row Functions
  135. btnBrowse.addActionListener(new ActionListener() {
  136. @Override
  137. public void actionPerformed(ActionEvent e) {
  138. fileChooser = new JFileChooser();
  139. FileNameExtensionFilter filter = new FileNameExtensionFilter("png, jpg or jpeg", "png", "jpg", "jpeg");
  140. fileChooser.setFileFilter(filter);
  141. if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
  142. path = fileChooser.getSelectedFile().getPath();
  143. textPath.setText(path);
  144. icon = new ImageIcon(textPath.getText());
  145. imageWidth.setText("" + icon.getIconWidth());
  146. imageHeight.setText("" + icon.getIconHeight());
  147. imageChanged = true;
  148. // Calculate the Image scale to fit the Panel
  149. if (Math.min(panelImageRadio.getWidth() - panelRadio.getWidth(),
  150. panelImageRadio.getHeight()) == (panelImageRadio.getWidth() - panelRadio.getWidth())) {
  151. imgScale = (double) Math.min(icon.getIconWidth(), icon.getIconHeight())
  152. / panelImageRadio.getWidth();
  153. } else {
  154. imgScale = (double) Math.min(icon.getIconWidth(), icon.getIconHeight())
  155. / panelImageRadio.getHeight();
  156. }
  157. lblImage.setIcon(new ImageIcon(
  158. new ImageIcon(path).getImage().getScaledInstance((int) (icon.getIconWidth() / imgScale),
  159. (int) (icon.getIconHeight() / imgScale), java.awt.Image.SCALE_FAST)));
  160. }
  161. }
  162. });
  163. textPath.setEditable(false);
  164. panelBrowse.add(textPath, BorderLayout.CENTER);
  165. textPath.setColumns(20);
  166. panelOK.setBackground(Color.WHITE);
  167. getContentPane().add(panelOK, BorderLayout.SOUTH);
  168. panelOK.add(btnOK);
  169. btnOK.addActionListener(new ActionListener() {
  170. @Override
  171. public void actionPerformed(ActionEvent e) {
  172. // picture selected?
  173. if (!path.isEmpty()) {
  174. if (rdbtnImagePixel.isSelected()) {
  175. mode = 0;
  176. } else if (rdbtnStretched.isSelected()) {
  177. mode = 1;
  178. } else if (rdbtnCustom.isSelected()) {
  179. mode = 2;
  180. }
  181. if (canvas != null) {
  182. if (imageChanged) {
  183. copieImage();
  184. }
  185. controller.setBackgroundImage(path, mode, Integer.parseInt(imageWidth.getText()),
  186. Integer.parseInt(imageHeight.getText()));
  187. canvas.repaint();
  188. } else if (uNode != null) {
  189. if (imageChanged) {
  190. copieImage();
  191. }
  192. uNode.setBackgroundImage(path, mode, Integer.parseInt(imageWidth.getText()),
  193. Integer.parseInt(imageHeight.getText()));
  194. }
  195. dispose();
  196. } else if (imageBtnClearedPressed) {
  197. if (canvas != null) {
  198. controller.setBackgroundImage(path, mode, Integer.parseInt(imageWidth.getText()),
  199. Integer.parseInt(imageHeight.getText()));
  200. canvas.repaint();
  201. } else if (uNode != null) {
  202. uNode.setBackgroundImage(path, mode, Integer.parseInt(imageWidth.getText()),
  203. Integer.parseInt(imageHeight.getText()));
  204. }
  205. dispose();
  206. } else {
  207. JOptionPane.showMessageDialog(null, "No image selected!", "Warning!", JOptionPane.WARNING_MESSAGE);
  208. }
  209. }
  210. });
  211. panelOK.add(btnCancel);
  212. btnCancel.addActionListener(new ActionListener() {
  213. @Override
  214. public void actionPerformed(ActionEvent e) {
  215. dispose();
  216. }
  217. });
  218. this.addComponentListener(new ComponentAdapter() {
  219. @Override
  220. public void componentResized(ComponentEvent e) {
  221. if (icon != null) {
  222. // Calculate the Image scale to fit the Panel
  223. if (Math.min(panelImageRadio.getWidth() - panelRadio.getWidth(),
  224. panelImageRadio.getHeight()) == (panelImageRadio.getWidth() - panelRadio.getWidth())) {
  225. imgScale = (double) Math.min(icon.getIconWidth(), icon.getIconHeight())
  226. / panelImageRadio.getWidth();
  227. } else {
  228. imgScale = (double) Math.min(icon.getIconWidth(), icon.getIconHeight())
  229. / panelImageRadio.getHeight();
  230. }
  231. lblImage.setIcon(new ImageIcon(
  232. new ImageIcon(path).getImage().getScaledInstance((int) (icon.getIconWidth() / imgScale),
  233. (int) (icon.getIconHeight() / imgScale), java.awt.Image.SCALE_SMOOTH)));
  234. }
  235. }
  236. });
  237. }
  238. protected void copieImage() {
  239. InputStream inStream = null;
  240. OutputStream outStream = null;
  241. try {
  242. File source = new File(path);
  243. File dest = new File(System.getProperty("user.home") + "/HolonGUI/BackgroundImages/");
  244. dest.mkdirs();
  245. dest = new File(dest, source.getName());
  246. path = "" + dest;
  247. inStream = new FileInputStream(source);
  248. outStream = new FileOutputStream(dest);
  249. byte[] buffer = new byte[1024];
  250. int length;
  251. while ((length = inStream.read(buffer)) > 0) {
  252. outStream.write(buffer, 0, length);
  253. }
  254. if (inStream != null)
  255. inStream.close();
  256. if (outStream != null)
  257. outStream.close();
  258. } catch (IOException eex) {
  259. eex.printStackTrace();
  260. }
  261. }
  262. }