diff -r 41e91ea94acb -r ec0e970e0830 java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/gui/Model.java --- a/java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/gui/Model.java Sat Dec 16 19:09:35 2023 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,218 +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 cz.frantovo.rozsireneatributy.Konfigurace; -import java.io.File; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.nio.ByteBuffer; -import java.nio.channels.FileChannel; -import java.nio.channels.FileLock; -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 Ing. František Kučera (frantovo.cz) - */ -public class Model implements TableModel { - - private static final Logger log = Logger - .getLogger(Model.class.getSimpleName()); - private static final ResourceBundle překlady = ResourceBundle - .getBundle(Atribut.class.getPackageName() + ".Překlady"); - private final String[] sloupečky = { - překlady.getString("tabulka.název"), - překlady.getString("tabulka.hodnota") - }; - private final Set posluchače = new HashSet<>(); - private final Konfigurace konfigurace; - private final UserDefinedFileAttributeView souborovýSystém; - private final List atributy = new ArrayList<>(); - - private RandomAccessFile zámekSoubor; - private FileChannel zámekKanál; - private FileLock zámek; - - public Model(Konfigurace konfigurace) throws IOException { - this.konfigurace = konfigurace; - Path cesta = konfigurace.getSoubor().toPath(); - FileSystemProvider posyktovatelFS = cesta.getFileSystem().provider(); - souborovýSystém = posyktovatelFS - .getFileAttributeView(cesta, UserDefinedFileAttributeView.class); - načtiAtributy(); - } - - public Konfigurace getKonfigurace() { - return konfigurace; - } - - @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) { - switch (n) { - case 0: - return atributy.get(m).getKlíč(); - case 1: - return atributy.get(m).getHodnota(); - default: - 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 FS", 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)); - } - } - - public boolean isZámekPodporovaný() { - return konfigurace.getSoubor().isFile(); - } - - public void nastavZámek(boolean zamknout) throws IOException { - if (!isZámekPodporovaný()) { - throw new IOException(překlady - .getString("chyba.lzeZamknoutJenSoubor")); - } - - if (zamknout && zámekSoubor == null) { - zámekSoubor = new RandomAccessFile(konfigurace.getSoubor(), "rw"); - zámekKanál = zámekSoubor.getChannel(); - zámek = zámekKanál.lock(); - } else if (!zamknout && zámekSoubor != null) { - zámek.release(); - zámekKanál.close(); - zámekSoubor.close(); - zámek = null; - zámekKanál = null; - zámekSoubor = null; - } - - } -}