BackgroundPopUp.java 9.9 KB

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