?? moviemodel.java
字號:
/* MovieModel - implements the movie model. Handles changes. * Copyright 2001, Bruce E. Wampler */ 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; // 0xBEEF // need two changed flags - one if a new entry has been // added that is true only until the views update, and a // global one that remains true if anything has changed // until the list is saved private boolean listChanged; // true until views updated private boolean editsMade; // true until saved 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 movieNumber) { if (theList == null || theList.size() == 0) // valid? return; // Validate number passed in, wrap appropriately if (movieNumber < 0) movieNumber = theList.size() - 1; if (movieNumber >= theList.size()) movieNumber = 0; currentMovieIndex = movieNumber; // change the movie notifyViews(); // update } public void addMovie(Movie movie) { if (movie == null) // some validation return; editsMade = true; // we've made some changes listChanged = true; ListIterator it = getMovieListIterator(); int nextI = 0; while (it.hasNext()) { // Assume list is sorted, so as soon as we find the // first entry that is > than this one, we insert // it before that one. nextI = it.nextIndex(); // index of next entry Movie m = (Movie) it.next(); String adding = movie.getTitle(); String curName = m.getTitle(); if (adding.compareToIgnoreCase(curName) <= 0) break; // curName > adding } if (!it.hasNext()) // add at end (also if 1st time) { theList.add(movie); // make it current movie setCurrentMovieIndex(theList.size() - 1); } else // add it before nextI { theList.add(nextI,movie); setCurrentMovieIndex(nextI); } } public void deleteCurrentMovie() { if (theList.size() <= 0) return; editsMade = true; // we've made some changes listChanged = true; theList.removeElementAt(currentMovieIndex); setCurrentMovieIndex(currentMovieIndex); } public void replaceCurrentMovie(Movie movie) { if (movie == null) return; theList.setElementAt(movie,currentMovieIndex); editsMade = true; // we've made some changes 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; // remember name } catch (IOException e) { JOptionPane.showMessageDialog( WmvcApp.getFrame(), "Error opening file: " + e, "MovieCat Error", JOptionPane.ERROR_MESSAGE); return false; } } else return false; editsMade = false; // no edits now! return true; } public boolean openMovies(File file) { if (file != null) { myFile = file; // remember the name try { DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream(file))); // check if file was made by us if (in.readInt() != FILE_ID) { in.close(); myFile = null; JOptionPane.showMessageDialog( WmvcApp.getFrame(), file.getName() + " is not a valid MovieCat file.", "MovieCat Error", JOptionPane.ERROR_MESSAGE); return false; } for ( ; ; ) // do until catch EOF Exception { 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; // no edits to start listChanged = true; notifyViews(); return true; } else return false; } public boolean closeMovies() { // Just close - Views responsible to save before closing myFile = null; // reset to empty values theList.clear(); editsMade = false; // no edits now! 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(); // updating views makes list correct listChanged = false; }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -