Package de.innovationgate.eclipse.utils

Source Code of de.innovationgate.eclipse.utils.BeanListStore

/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.utils;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.Status;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class BeanListStore<T> {
  List<T> _beans = new ArrayList<T>();
  private XStream _xstream = new XStream(new DomDriver());
  private Set<BeanListStoreListener> _listeners = new HashSet<BeanListStoreListener>();

  public void removeListener(BeanListStoreListener listener) {
    _listeners.remove(listener);
  }

  public void addListener(BeanListStoreListener listener) {
    _listeners.add(listener);
  }

  private File _file;

  public BeanListStore(File folder, String key, ClassLoader classloader) throws IOException {
    _file = new File(folder, key);
    _xstream.setClassLoader(classloader);
    load();
  }

  @SuppressWarnings("unchecked")
  public void load() throws IOException {
    if (_file.exists()) {
      Reader reader = null;
      try {
        reader = new FileReader(_file);
        _beans = (List<T>) _xstream.fromXML(reader);
      } finally {
        if (reader != null) {
          try {
            reader.close();
          } catch (IOException e) {
          }
        }
      }
    }
  }

  public void flush() throws IOException {
    Writer writer = null;
    try {
      writer = new FileWriter(_file);
      _xstream.toXML(_beans, writer);
    } finally {
      if (writer != null) {
        try {
          writer.close();
        } catch (IOException e) {
        }
      }
    }
   
    Iterator<BeanListStoreListener> it = _listeners.iterator();
    while (it.hasNext()) {
      BeanListStoreListener listener = it.next();
      try {
        listener.handelEvent(new BeanListStoreEvent(BeanListStoreEvent.TYPE_STORE_FLUSHED));
      } catch (Exception e) {
        Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, "handelEvent() failed on listener '" + listener.getClass().getName() + "' .", e));
      }
    }

  }

  public List<T> getBeans() {
    return _beans;
  }

}
TOP

Related Classes of de.innovationgate.eclipse.utils.BeanListStore

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.