diff -r 000000000000 -r 734f104f2869 java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Model.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Model.java Wed Dec 15 20:07:14 2010 +0100 @@ -0,0 +1,113 @@ +package cz.frantovo.rozsireneAtributy; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +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.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 pohled; + private ArrayList atributy = new ArrayList(); + + public Model(File soubor) throws IOException { + Path cesta = soubor.toPath(); + pohled = 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).getKlic(); + } else if (n == 1) { + return atributy.get(m).getHodnota(); + } else { + return null; + } + } + + public void setValueAt(Object value, int m, int n) { + if (n == 0) { + atributy.get(m).setKlic(String.valueOf(value.toString())); + /** TODO: uložit na souborový systém */ + } else if (n == 1) { + atributy.get(m).setHodnota(String.valueOf(value.toString())); + /** TODO: uložit na souborový systém */ + } + } + + public void addTableModelListener(TableModelListener l) { + posluchače.add(l); + } + + public void removeTableModelListener(TableModelListener l) { + posluchače.remove(l); + } + + public void přidejŘádek() { + //atributy.add(new Atribut()); + //upozorniPosluchače(); + } + + public void odeberŘádek(int m) { + //atributy.remove(m); + //upozorniPosluchače(); + } + + private void načtiAtributy() throws IOException { + List jménaAtributů = pohled.list(); + for (String jménoAtributu : jménaAtributů) { + ByteBuffer bajty = ByteBuffer.allocate(pohled.size(jménoAtributu)); + pohled.read(jménoAtributu, bajty); + String hodnotaAtributu = dekóduj(bajty); + atributy.add(new Atribut(jménoAtributu, hodnotaAtributu)); + } + } + + private static String dekóduj(ByteBuffer bajty) { + bajty.flip(); + return Charset.defaultCharset().decode(bajty).toString(); + } + + private void upozorniPosluchače() { + for (TableModelListener p : posluchače) { + p.tableChanged(new TableModelEvent(this)); + } + } +}