BackgroundPopUp.java 9.3 KB

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