BackgroundPopUp.java 9.2 KB

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