diff -r dc5786f3482b -r c2ffda907125 java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Model.java --- a/java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Model.java Mon Dec 11 00:10:33 2023 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,168 +0,0 @@ -/** - * Rozšířené atributy – program na správu rozšířených atributů souborů - * Copyright © 2012 František Kučera (frantovo.cz) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package cz.frantovo.rozsireneAtributy.gui; - -import cz.frantovo.rozsireneAtributy.Atribut; -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.nio.file.spi.FileSystemProvider; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.ResourceBundle; -import java.util.Set; -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 static final ResourceBundle překlady = ResourceBundle.getBundle("cz.frantovo.rozsireneAtributy.Překlady"); - private String[] sloupečky = {překlady.getString("tabulka.název"), překlady.getString("tabulka.hodnota")}; - private Set posluchače = new HashSet<>(); - private UserDefinedFileAttributeView souborovýSystém; - private List atributy = new ArrayList<>(); - - public Model(File soubor) throws IOException { - Path cesta = soubor.toPath(); - FileSystemProvider posyktovatelFS = cesta.getFileSystem().provider(); - souborovýSystém = posyktovatelFS.getFileAttributeView(cesta, UserDefinedFileAttributeView.class); - načtiAtributy(); - } - - @Override - public int getRowCount() { - return atributy.size(); - } - - @Override - public int getColumnCount() { - return sloupečky.length; - } - - @Override - public String getColumnName(int n) { - return sloupečky[n]; - } - - @Override - public Class getColumnClass(int n) { - return String.class; - } - - @Override - public boolean isCellEditable(int m, int n) { - return true; - } - - @Override - 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; - } - } - - @Override - public void setValueAt(Object hodnota, 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(hodnota); - if (!novýKlíč.equals(a.getKlíč())) { - if (a.isPlatnýKlíč()) { - souborovýSystém.delete(a.getKlíč()); - } - a.setKlíč(novýKlíč); - if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) { - souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty()); - } - } - } else if (n == 1) { - /** Měníme hodnotu atributu */ - a.setHodnota(String.valueOf(hodnota)); - 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); - } - } - - @Override - public void addTableModelListener(TableModelListener l) { - posluchače.add(l); - } - - @Override - 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)); - } - } -}