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