BackgroundPopUp.java 9.4 KB

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