www.pudn.com > lxc.rar > MovieModel.java
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MovieModel extends WmvcModel {
private int currentMovieIndex;
private Vector theList;
private final int FILE_ID=48879;
private boolean listChanged;
private boolean editsMade;
private File myFile;
public ListIterator getMovieListIterator(){
return theList.listIterator();
}
public boolean getListChanged(){
return listChanged;
}
public boolean getEditsMade(){
return editsMade;
}
public int getCurrentMovieIndex(){
return currentMovieIndex;
}
public int getNumberOfMovies(){
return theList.size();
}
public File getFile(){
return myFile;
}
public MovieModel(){
editsMade=false;
listChanged=false;
theList=new Vector();
myFile=null;
}
public void setCurrentMovieIndex(int index){
if(theList==null||theList.size()==0)
return;
if(index<0)
index=theList.size()-1;
if(index>=theList.size())
index=0;
currentMovieIndex=index;
notifyViews();
}
public void addMovie(Movie movie){
if(movie==null)
return;
editsMade=true;
listChanged=true;
ListIterator it=getMovieListIterator();
int nextI=0;
while(it.hasNext()){
nextI=it.nextIndex();
Movie m=(Movie)it.next();
String adding=movie.getTitle();
String curName=m.getTitle();
if(adding.compareToIgnoreCase(curName)<=0)
break;
}
if(!it.hasNext()){
theList.add(movie);
setCurrentMovieIndex(theList.size()-1);
}else{
theList.add(nextI,movie);
setCurrentMovieIndex(nextI);
}
}
public void deleteCurrentMovie(){
if(theList.size()<=0){
return;
}
editsMade=true;
listChanged=true;
theList.removeElementAt(currentMovieIndex);
setCurrentMovieIndex(currentMovieIndex-1);
}
public void replaceCurrentMovie(Movie movie){
if(movie==null)
return;
theList.setElementAt(movie,currentMovieIndex);
editsMade=true;
listChanged=true;
notifyViews();
}
public boolean saveMovies(){
return saveMoviesAs(myFile);
}
public boolean saveMoviesAs(File file){
if(file!=null){
try{
DataOutputStream out=new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(file)));
out.writeInt(FILE_ID);
ListIterator it=getMovieListIterator();
while(it.hasNext()){
Movie m=(Movie)it.next();
m.writeMovie(out);
}
out.flush();
out.close();
myFile=file;
}catch(IOException e){
JOptionPane.showMessageDialog(
WmvcApp.getFrame(),
"Error opening file:"+e,
"MovieCat Error",
JOptionPane.ERROR_MESSAGE);
return false;
}
}else{
return false;
}
editsMade=false;
return true;
}
public boolean openMovies(File file){
if(file!=null){
myFile=file;
try{
DataInputStream in=new DataInputStream(
new BufferedInputStream(
new FileInputStream(file)));
if(in.readInt()!=FILE_ID){
in.close();
myFile=null;
JOptionPane.showMessageDialog(
WmvcApp.getFrame(),
file.getName()+
"is not a valid MovieCat file.",
"Movie Error",
JOptionPane.ERROR_MESSAGE);
return false;
}
while(true){
Movie m=new Movie();
if(!m.readMovie(in))
break;
theList.add(m);
}
}catch(IOException e){
JOptionPane.showMessageDialog(
WmvcApp.getFrame(),
"Error reading file:"+e,
"MovieCat Error",
JOptionPane.ERROR_MESSAGE);
myFile=null;
return false;
}
editsMade=false;
listChanged=true;
notifyViews();
return true;
}else{
return false;
}
}
public boolean closeMovies(){
myFile=null;
theList.clear();
editsMade=false;
listChanged=true;
notifyViews();
return true;
}
public Movie getCurrentMovie(){
if(currentMovieIndex<0||currentMovieIndex>=theList.size())
return null;
else if(theList.size()==0)
return null;
else
return (Movie)theList.elementAt(currentMovieIndex);
}
public void notifyViews(){
super.notifyViews();
listChanged=false;
}
}