123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- import java.util.Observable;
- import java.util.*;
- import java.awt.*;
- import javax.swing.*;
- import java.io.*;
- import javax.imageio.ImageIO;
- public class Model extends Observable implements Serializable
- {
- /** The observers that are watching this model for changes. */
- private ArrayList<Observer> observers;
- private ArrayList<PicData> pics;
- private int filter;
- private boolean grid;
- // Constructor
- public Model()
- {
- this.observers = new ArrayList();
- this.pics = new ArrayList();
- this.filter = 0;
- this.grid = true;
- PicData.model = this;
- setChanged();
- }
-
- public void newPic(String filepath, String name, long size, long lastmod)
- {
- PicData temp = new PicData(filepath, name, size, lastmod);
- pics.add(temp);
- setChanged();
- notifyObservers();
- }
-
- public ArrayList<PicData> getPics()
- {
- return pics;
- }
-
- public boolean getGrid()
- {
- return grid;
- }
-
- public int getFilter()
- {
- return filter;
- }
-
- public void changeFilter(int x)
- {
- this.filter = x;
- setChanged();
- notifyObservers();
- }
-
- public void switchLayout(boolean flag)
- {
- if (grid != flag)
- {
- grid = flag;
- System.out.println("Layout Toggled!");
- setChanged();
- notifyObservers();
- }
- }
-
- public void update()
- {
- setChanged();
- notifyObservers();
- }
- // Add observer to be notified on change
- public void addObserver(Observer observer)
- {
- this.observers.add(observer);
- }
- // Remove an observer from opdate list
- public void removeObserver(Observer observer)
- {
- this.observers.remove(observer);
- }
- // Notify all observers shit went down
- public void notifyObservers()
- {
- for (Observer observer: this.observers)
- {
- observer.update(this);
- }
- }
- }
- class PicData implements Serializable
- {
- private int rating;
- private Image pic;
- private long size;
- private long lastmod;
- private String name;
- public static Model model;
- public PicData(String filepath, String name, long size, long lastmod)
- {
- try
- {
- this.pic = ImageIO.read(new File(filepath));
- } catch(IOException e) {}
-
- this.pic = pic.getScaledInstance(200, 200, java.awt.Image.SCALE_SMOOTH);
- this.size = size/1024;
- this.lastmod = lastmod;
- this.name = name;
- this.rating = 0;
- }
-
- public void setRating(int x)
- {
- this.rating = x;
- model.update();
- }
-
- public Image getPic()
- {
- return pic;
- }
-
- public int getRating()
- {
- return rating;
- }
-
- public long getSize()
- {
- return size;
- }
-
- public long getLastmod()
- {
- return lastmod;
- }
-
- public String getName()
- {
- return name;
- }
- }
|