Controls.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import java.io.*;
  2. import java.io.File;
  3. import java.util.*;
  4. import java.awt.*;
  5. import javax.swing.*;
  6. import javax.swing.event.*;
  7. import java.awt.event.*;
  8. import javax.swing.ImageIcon;
  9. import java.awt.image.*;
  10. import javax.imageio.ImageIO;
  11. import javax.swing.JFileChooser;
  12. public class Controls extends JPanel implements Observer
  13. {
  14. private Model model;
  15. private JSlider rating;
  16. private JRadioButton grid;
  17. private JRadioButton list;
  18. private ButtonGroup layouts;
  19. private Image starImg;
  20. private Image gridImg;
  21. private Image listImg;
  22. private Image folderImg;
  23. private ImageIcon starIcon;
  24. private ImageIcon gridIcon;
  25. private ImageIcon listIcon;
  26. private ImageIcon folderIcon;
  27. private JButton fileButton;
  28. private JFileChooser fileChooser;
  29. // Bob the Builder this shit
  30. public Controls(Model model)
  31. {
  32. // Hook up this observer so that it will be notified when the model
  33. // changes.
  34. this.model = model;
  35. this.setBackground(Color.cyan);
  36. this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  37. this.setPreferredSize(new Dimension(1280,75));
  38. this.setMinimumSize(new Dimension(320, 75));
  39. try
  40. {
  41. this.starImg = ImageIO.read(new File("src/main/resources/star.png"));
  42. } catch(IOException e) {}
  43. try
  44. {
  45. this.gridImg = ImageIO.read(new File("src/main/resources/grid.png"));
  46. } catch(IOException e) {}
  47. try
  48. {
  49. this.listImg = ImageIO.read(new File("src/main/resources/list.png"));
  50. } catch(IOException e) {}
  51. try
  52. {
  53. this.folderImg = ImageIO.read(new File("src/main/resources/folder.jpg"));
  54. } catch(IOException e) {}
  55. this.starImg = starImg.getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
  56. this.gridImg = gridImg.getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
  57. this.listImg = listImg.getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
  58. this.folderImg = folderImg.getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
  59. this.starIcon = new ImageIcon(starImg);
  60. this.gridIcon = new ImageIcon(gridImg);
  61. this.listIcon = new ImageIcon(listImg);
  62. this.folderIcon = new ImageIcon(folderImg);
  63. this.grid = new JRadioButton(gridIcon, true);
  64. grid.addActionListener(new ActionListener()
  65. {
  66. public void actionPerformed(ActionEvent e)
  67. {
  68. if (!(grid.isSelected(layouts)))
  69. {
  70. grid.setSelected(layouts, true);
  71. list.setSelected(layouts, false);
  72. model.switchLayout();
  73. }
  74. }
  75. });
  76. this.list = new JRadioButton(listIcon, false);
  77. list.addActionListener(new ActionListener()
  78. {
  79. public void actionPerformed(ActionEvent e)
  80. {
  81. if (!(list.isSelected(layouts)))
  82. {
  83. list.setSelected(layouts, true);
  84. grid.setSelected(layouts, false);
  85. model.switchLayout();
  86. }
  87. }
  88. });
  89. this.layouts = new ButtonGroup();
  90. layouts.add(grid);
  91. layouts.add(list);
  92. grid.setSelected(layouts, true);
  93. list.setSelected(layouts, false);
  94. this.fileChooser = new JFileChooser();
  95. this.fileButton = new JButton(folderIcon);
  96. fileButton.addActionListener(new ActionListener()
  97. {
  98. public void actionPerformed(ActionEvent e)
  99. {
  100. int retval = fileChooser.showOpenDialog(null);
  101. if (retval == JFileChooser.APPROVE_OPTION)
  102. {
  103. File file = fileChooser.getSelectedFile();
  104. if (file.isFile())
  105. {
  106. model.newPic(file.getAbsolutePath(), file.getName(), file.length(), file.lastModified());
  107. }
  108. }
  109. }
  110. });
  111. this.rating = new JSlider(0, 5, 0);
  112. rating.addChangeListener(new ChangeListener()
  113. {
  114. public void stateChanged(ChangeEvent e)
  115. {
  116. model.changeFilter(rating.getValue());
  117. }
  118. });
  119. this.rating.setPreferredSize(new Dimension(280, 75));
  120. this.add(grid);
  121. this.add(list);
  122. this.add(rating);
  123. this.add(fileButton);
  124. model.addObserver(this);
  125. }
  126. public void update(Object observable)
  127. {
  128. revalidate();
  129. repaint();
  130. }
  131. }