franta-hg@13: package cz.frantovo.rozsireneAtributy.gui; franta-hg@6: franta-hg@13: import cz.frantovo.rozsireneAtributy.Atribut; 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@17: import java.nio.file.spi.FileSystemProvider; franta-hg@6: import java.util.ArrayList; franta-hg@6: import java.util.HashSet; franta-hg@6: import java.util.List; franta-hg@15: import java.util.ResourceBundle; 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@15: private static final ResourceBundle překlady = ResourceBundle.getBundle("cz.frantovo.rozsireneAtributy.Překlady"); franta-hg@15: private String[] sloupečky = {překlady.getString("tabulka.název"), překlady.getString("tabulka.hodnota")}; franta-hg@6: private HashSet posluchače = new HashSet(); franta-hg@11: private UserDefinedFileAttributeView souborovýSystém; 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@17: FileSystemProvider posyktovatelFS = cesta.getFileSystem().provider(); franta-hg@17: souborovýSystém = posyktovatelFS.getFileAttributeView(cesta, 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@11: return atributy.get(m).getKlíč(); 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@14: public void setValueAt(Object hodnota, 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@14: String novýKlíč = String.valueOf(hodnota); franta-hg@11: if (!novýKlíč.equals(a.getKlíč())) { franta-hg@11: if (a.isPlatnýKlíč()) { franta-hg@11: souborovýSystém.delete(a.getKlíč()); franta-hg@10: } franta-hg@11: a.setKlíč(novýKlíč); franta-hg@14: if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) { franta-hg@11: souborovýSystém.write(a.getKlíč(), 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@14: a.setHodnota(String.valueOf(hodnota)); franta-hg@12: if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) { franta-hg@12: souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty()); franta-hg@12: } 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@11: if (a.isPlatnýKlíč()) { franta-hg@11: souborovýSystém.delete(a.getKlíč()); franta-hg@11: } franta-hg@9: upozorniPosluchače(); franta-hg@6: } franta-hg@6: franta-hg@10: public final void načtiAtributy() throws IOException { franta-hg@11: List jménaAtributů = souborovýSystém.list(); franta-hg@10: atributy.clear(); franta-hg@6: for (String jménoAtributu : jménaAtributů) { franta-hg@11: ByteBuffer hodnotaAtributu = ByteBuffer.allocate(souborovýSystém.size(jménoAtributu)); franta-hg@11: souborovýSystém.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: }