IgnoreFileModel.java :  » Source-Control » gruntspud » gruntspud » ui » ignore » Java Open Source

Java Open Source » Source Control » gruntspud 
gruntspud » gruntspud » ui » ignore » IgnoreFileModel.java
/*
 *  Gruntspud
 *
 *  Copyright (C) 2002 Brett Smith.
 *
 *  Written by: Brett Smith <t_magicthize@users.sourceforge.net>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package gruntspud.ui.ignore;

import gruntspud.Constants;
import gruntspud.GruntspudContext;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Vector;

import javax.swing.AbstractListModel;

/**
 * DOCUMENT ME!
 *
 * @author $author$
 */
public class IgnoreFileModel
    extends AbstractListModel {
    private Vector patterns;
    private File file;
    private GruntspudContext context;

    /**
     * Creates a new IgnoreFileModel object.
     *
     * @param context DOCUMENT ME!
     * @param file DOCUMENT ME!
     */
    public IgnoreFileModel(GruntspudContext context, File file) {
        this.file = file;
        this.context = context;

        patterns = new Vector();

        FileInputStream in = null;

        if (file.exists() && file.isFile() && file.canRead()) {
            try {
                in = new FileInputStream(file);

                BufferedReader reader = new BufferedReader(new
                    InputStreamReader(
                    in));
                String pattern = null;

                while ( (pattern = reader.readLine()) != null) {
                    patterns.addElement(pattern.trim());
                }
            }
            catch (IOException ioe) {
                Constants.IO_LOG.error("Failed to .cvsignore file " +
                                       file.getAbsolutePath(), ioe);
            }
            finally {
                if (in != null) {
                    try {
                        in.close();
                    }
                    catch (IOException ioe) {
                    }
                }
            }
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getSize() {
        return patterns.size();
    }

    /**
     * DOCUMENT ME!
     */
    public void save() {
        if (patterns.size() == 0) {
            if (file.exists()) {
                if (!file.delete()) {
                    Constants.IO_LOG.error("Failed to remove " +
                                           file.getAbsolutePath());
                }
            }
        }
        else {
            FileOutputStream out = null;

            try {
                out = new FileOutputStream(file);

                PrintWriter w = new PrintWriter(out, true);

                for (int i = 0; i < getSize(); i++) {
                    w.println(getElementAt(i).toString());

                }
                out.flush();
            }
            catch (IOException ioe) {
                Constants.IO_LOG.error("Failed to save .cvsignore file " +
                                       file.getAbsolutePath());
            }
            finally {
                if (out != null) {
                    try {
                        out.close();
                    }
                    catch (IOException ioe) {
                    }
                }
            }
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @param pattern DOCUMENT ME!
     */
    public void addPattern(String pattern) {
        int s = getSize();
        patterns.addElement(pattern);
        fireIntervalAdded(this, s, s);
    }

    /**
     * DOCUMENT ME!
     *
     * @param idx DOCUMENT ME!
     */
    public void removePatternAt(int idx) {
        patterns.removeElementAt(idx);
        fireIntervalRemoved(this, idx, idx);
    }

    /**
     * DOCUMENT ME!
     *
     * @param r DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public Object getElementAt(int r) {
        return patterns.elementAt(r);
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.