BackgroundPopUp.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. // Modes
  59. public static final int IMAGE_PIXELS = 0, STRETCHED = 1, CUSTOM = 2;
  60. private int mode = 0;
  61. private final JPanel panel = new JPanel();
  62. private final JTextField imageWidth = new JTextField();
  63. private final JTextField imageHeight = new JTextField();
  64. JFileChooser fileChooser;
  65. public BackgroundPopUp(Model model, Control controller, MyCanvas canvas, CpsUpperNode uNode) {
  66. super((java.awt.Frame) null, true);
  67. getContentPane().setBackground(Color.WHITE);
  68. this.setTitle("Set View Background");
  69. // Show background Image
  70. if (canvas != null) {
  71. path = model.getCanvasImagePath();
  72. } else if (uNode != null) {
  73. path = uNode.getImagePath();
  74. }
  75. if (!path.isEmpty()) {
  76. textPath.setText(path);
  77. icon = new ImageIcon(path);
  78. imageWidth.setText("" + icon.getIconWidth());
  79. imageHeight.setText("" + icon.getIconHeight());
  80. }
  81. this.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
  82. .getScaledInstance(30, 30, Image.SCALE_SMOOTH));
  83. setBounds(100, 100, 600, 340);
  84. panelBrowse.setBorder(null);
  85. panelImageRadio.setBorder(null);
  86. panelOK.setBorder(null);
  87. panelRadio.setBorder(null);
  88. getContentPane().add(panelImageRadio, BorderLayout.CENTER);
  89. panelImageRadio.setLayout(new BorderLayout(0, 0));
  90. lblImage.setBackground(Color.WHITE);
  91. lblImage.setHorizontalAlignment(SwingConstants.LEFT);
  92. panelImageRadio.add(lblImage, BorderLayout.CENTER);
  93. panelImageRadio.add(panelRadio, BorderLayout.EAST);
  94. // Radio Buttons
  95. panelRadio.setLayout(new GridLayout(0, 1, 0, 0));
  96. rdbtnImagePixel.setBackground(Color.WHITE);
  97. rdbtnImagePixel.setSelected(true);
  98. panelRadio.add(rdbtnImagePixel);
  99. rdbtnStretched.setBackground(Color.WHITE);
  100. panelRadio.add(rdbtnStretched);
  101. panelBrowse.setBackground(Color.WHITE);
  102. ButtonGroup bgroup = new ButtonGroup();
  103. bgroup.add(rdbtnImagePixel);
  104. bgroup.add(rdbtnStretched);
  105. panelRadio.add(rdbtnCustom);
  106. rdbtnCustom.setBackground(new Color(255, 255, 255));
  107. bgroup.add(rdbtnCustom);
  108. panel.setBackground(Color.WHITE);
  109. panelRadio.add(panel);
  110. panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
  111. panel.add(imageWidth);
  112. imageWidth.setColumns(10);
  113. panel.add(imageHeight);
  114. imageHeight.setColumns(10);
  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. panelImageRadio.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[] { btnBrowse, textPath }));
  152. panelOK.setBackground(Color.WHITE);
  153. getContentPane().add(panelOK, BorderLayout.SOUTH);
  154. panelOK.add(btnOK);
  155. btnOK.addActionListener(new ActionListener() {
  156. @Override
  157. public void actionPerformed(ActionEvent e) {
  158. // Bild ausgewählt?
  159. if (!path.isEmpty()) {
  160. if (rdbtnImagePixel.isSelected()) {
  161. mode = 0;
  162. } else if (rdbtnStretched.isSelected()) {
  163. mode = 1;
  164. } else if (rdbtnCustom.isSelected()) {
  165. mode = 2;
  166. }
  167. if (canvas != null) {
  168. if (imageChanged) {
  169. copieImage();
  170. }
  171. controller.setBackgroundImage(path, mode, Integer.parseInt(imageWidth.getText()),
  172. Integer.parseInt(imageHeight.getText()));
  173. canvas.repaint();
  174. } else if (uNode != null) {
  175. if (imageChanged) {
  176. copieImage();
  177. }
  178. uNode.setBackgroundImage(path, mode, Integer.parseInt(imageWidth.getText()),
  179. Integer.parseInt(imageHeight.getText()));
  180. }
  181. dispose();
  182. } else {
  183. JOptionPane.showMessageDialog(null, "No image selected!", "Warning!", JOptionPane.WARNING_MESSAGE);
  184. }
  185. }
  186. });
  187. panelOK.add(btnCancel);
  188. btnCancel.addActionListener(new ActionListener() {
  189. @Override
  190. public void actionPerformed(ActionEvent e) {
  191. dispose();
  192. }
  193. });
  194. this.addComponentListener(new ComponentAdapter() {
  195. @Override
  196. public void componentResized(ComponentEvent e) {
  197. if (icon != null) {
  198. // Calculate the Image scale to fit the Panel
  199. if (Math.min(panelImageRadio.getWidth() - panelRadio.getWidth(),
  200. panelImageRadio.getHeight()) == (panelImageRadio.getWidth() - panelRadio.getWidth())) {
  201. imgScale = (double) Math.min(icon.getIconWidth(), icon.getIconHeight())
  202. / panelImageRadio.getWidth();
  203. } else {
  204. imgScale = (double) Math.min(icon.getIconWidth(), icon.getIconHeight())
  205. / panelImageRadio.getHeight();
  206. }
  207. lblImage.setIcon(new ImageIcon(
  208. new ImageIcon(path).getImage().getScaledInstance((int) (icon.getIconWidth() / imgScale),
  209. (int) (icon.getIconHeight() / imgScale), java.awt.Image.SCALE_SMOOTH)));
  210. }
  211. }
  212. });
  213. }
  214. protected void copieImage() {
  215. InputStream inStream = null;
  216. OutputStream outStream = null;
  217. try {
  218. File source = new File(path);
  219. File dest = new File(System.getProperty("user.home") + "/HolonGUI/Images/");
  220. dest.mkdirs();
  221. dest = new File(dest, source.getName());
  222. path = "" + dest;
  223. inStream = new FileInputStream(source);
  224. outStream = new FileOutputStream(dest);
  225. byte[] buffer = new byte[1024];
  226. int length;
  227. while ((length = inStream.read(buffer)) > 0) {
  228. outStream.write(buffer, 0, length);
  229. }
  230. if (inStream != null)
  231. inStream.close();
  232. if (outStream != null)
  233. outStream.close();
  234. } catch (IOException eex) {
  235. eex.printStackTrace();
  236. }
  237. }
  238. }