Model.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import java.util.Observable;
  2. import java.util.*;
  3. import java.awt.*;
  4. import javax.swing.*;
  5. import java.io.*;
  6. import javax.imageio.ImageIO;
  7. public class Model extends Observable implements Serializable
  8. {
  9. /** The observers that are watching this model for changes. */
  10. private ArrayList<Observer> observers;
  11. private ArrayList<PicData> pics;
  12. private int filter;
  13. private boolean grid;
  14. // Constructor
  15. public Model()
  16. {
  17. this.observers = new ArrayList();
  18. this.pics = new ArrayList();
  19. this.filter = 0;
  20. this.grid = true;
  21. PicData.model = this;
  22. setChanged();
  23. }
  24. public void newPic(String filepath, String name, long size, long lastmod)
  25. {
  26. PicData temp = new PicData(filepath, name, size, lastmod);
  27. pics.add(temp);
  28. setChanged();
  29. notifyObservers();
  30. }
  31. public ArrayList<PicData> getPics()
  32. {
  33. return pics;
  34. }
  35. public boolean getGrid()
  36. {
  37. return grid;
  38. }
  39. public int getFilter()
  40. {
  41. return filter;
  42. }
  43. public void changeFilter(int x)
  44. {
  45. this.filter = x;
  46. setChanged();
  47. notifyObservers();
  48. }
  49. public void switchLayout(boolean flag)
  50. {
  51. if (grid != flag)
  52. {
  53. grid = flag;
  54. System.out.println("Layout Toggled!");
  55. setChanged();
  56. notifyObservers();
  57. }
  58. }
  59. public void update()
  60. {
  61. setChanged();
  62. notifyObservers();
  63. }
  64. // Add observer to be notified on change
  65. public void addObserver(Observer observer)
  66. {
  67. this.observers.add(observer);
  68. }
  69. // Remove an observer from opdate list
  70. public void removeObserver(Observer observer)
  71. {
  72. this.observers.remove(observer);
  73. }
  74. // Notify all observers shit went down
  75. public void notifyObservers()
  76. {
  77. for (Observer observer: this.observers)
  78. {
  79. observer.update(this);
  80. }
  81. }
  82. }
  83. class PicData implements Serializable
  84. {
  85. private int rating;
  86. private Image pic;
  87. private long size;
  88. private long lastmod;
  89. private String name;
  90. public static Model model;
  91. public PicData(String filepath, String name, long size, long lastmod)
  92. {
  93. try
  94. {
  95. this.pic = ImageIO.read(new File(filepath));
  96. } catch(IOException e) {}
  97. this.pic = pic.getScaledInstance(200, 200, java.awt.Image.SCALE_SMOOTH);
  98. this.size = size/1024;
  99. this.lastmod = lastmod;
  100. this.name = name;
  101. this.rating = 0;
  102. }
  103. public void setRating(int x)
  104. {
  105. this.rating = x;
  106. model.update();
  107. }
  108. public Image getPic()
  109. {
  110. return pic;
  111. }
  112. public int getRating()
  113. {
  114. return rating;
  115. }
  116. public long getSize()
  117. {
  118. return size;
  119. }
  120. public long getLastmod()
  121. {
  122. return lastmod;
  123. }
  124. public String getName()
  125. {
  126. return name;
  127. }
  128. }