BackgroundPopUp.java 9.1 KB

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