diff -r a2b41c8ee039 -r 6c633be53dd6 java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Model.java --- a/java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Model.java Thu Dec 16 00:51:22 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,137 +0,0 @@ -package cz.frantovo.rozsireneAtributy; - -import java.io.File; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.file.Path; -import java.nio.file.attribute.UserDefinedFileAttributeView; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; -import javax.swing.event.TableModelEvent; -import javax.swing.event.TableModelListener; -import javax.swing.table.TableModel; - -/** - * - * @author fiki - */ -public class Model implements TableModel { - - private static final Logger log = Logger.getLogger(Model.class.getSimpleName()); - private String[] sloupečky = {"Název", "Hodnota"}; - private HashSet posluchače = new HashSet(); - private UserDefinedFileAttributeView souborovýSystém; - private ArrayList atributy = new ArrayList(); - - public Model(File soubor) throws IOException { - Path cesta = soubor.toPath(); - souborovýSystém = cesta.getFileAttributeView(UserDefinedFileAttributeView.class); - načtiAtributy(); - } - - public int getRowCount() { - return atributy.size(); - } - - public int getColumnCount() { - return sloupečky.length; - } - - public String getColumnName(int n) { - return sloupečky[n]; - } - - public Class getColumnClass(int n) { - return String.class; - } - - public boolean isCellEditable(int m, int n) { - return true; - } - - public Object getValueAt(int m, int n) { - if (n == 0) { - return atributy.get(m).getKlíč(); - } else if (n == 1) { - return atributy.get(m).getHodnota(); - } else { - return null; - } - } - - public void setValueAt(Object value, int m, int n) { - Atribut a = atributy.get(m); - try { - if (n == 0) { - /** Měníme klíč – název atributu */ - String novýKlíč = String.valueOf(value.toString()); - if (!novýKlíč.equals(a.getKlíč())) { - if (a.isPlatnýKlíč()) { - souborovýSystém.delete(a.getKlíč()); - } - a.setKlíč(novýKlíč); - if (a.isPlatnáHodnota()) { - souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty()); - } - } - } else if (n == 1) { - /** Měníme hodnotu atributu */ - a.setHodnota(String.valueOf(value.toString())); - if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) { - souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty()); - } - } - } catch (IOException e) { - log.log(Level.SEVERE, "Selhalo ukládání atributu na souborový systém", e); - } - } - - public void addTableModelListener(TableModelListener l) { - posluchače.add(l); - } - - public void removeTableModelListener(TableModelListener l) { - posluchače.remove(l); - } - - /** - * @param m číslo řádku - * @return atribut, který se na něm nachází - */ - public Atribut getAtribut(int m) { - return atributy.get(m); - } - - public void přidejAtribut(Atribut a) { - atributy.add(a); - upozorniPosluchače(); - } - - public void odeberAtribut(Atribut a) throws IOException { - atributy.remove(a); - if (a.isPlatnýKlíč()) { - souborovýSystém.delete(a.getKlíč()); - } - upozorniPosluchače(); - } - - public final void načtiAtributy() throws IOException { - List jménaAtributů = souborovýSystém.list(); - atributy.clear(); - for (String jménoAtributu : jménaAtributů) { - ByteBuffer hodnotaAtributu = ByteBuffer.allocate(souborovýSystém.size(jménoAtributu)); - souborovýSystém.read(jménoAtributu, hodnotaAtributu); - atributy.add(new Atribut(jménoAtributu, hodnotaAtributu)); - } - upozorniPosluchače(); - } - - private void upozorniPosluchače() { - for (TableModelListener p : posluchače) { - p.tableChanged(new TableModelEvent(this)); - } - } -}