franta-hg@19: /** franta-hg@19: * Rozšířené atributy – program na správu rozšířených atributů souborů franta-hg@19: * Copyright © 2012 František Kučera (frantovo.cz) franta-hg@31: * franta-hg@19: * This program is free software: you can redistribute it and/or modify franta-hg@19: * it under the terms of the GNU General Public License as published by franta-hg@27: * the Free Software Foundation, either version 3 of the License. franta-hg@31: * franta-hg@19: * This program is distributed in the hope that it will be useful, franta-hg@19: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@19: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@19: * GNU General Public License for more details. franta-hg@31: * franta-hg@19: * You should have received a copy of the GNU General Public License franta-hg@19: * along with this program. If not, see . franta-hg@19: */ franta-hg@28: package cz.frantovo.rozsireneatributy.gui; franta-hg@6: franta-hg@28: import cz.frantovo.rozsireneatributy.Atribut; franta-hg@31: import cz.frantovo.rozsireneatributy.Konfigurace; franta-hg@6: import java.io.IOException; franta-hg@30: import java.io.RandomAccessFile; franta-hg@6: import java.nio.ByteBuffer; franta-hg@30: import java.nio.channels.FileChannel; franta-hg@30: import java.nio.channels.FileLock; 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@22: import java.util.Set; 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@30: * @author Ing. František Kučera (frantovo.cz) franta-hg@6: */ franta-hg@6: public class Model implements TableModel { franta-hg@6: franta-hg@29: private static final Logger log = Logger franta-hg@29: .getLogger(Model.class.getSimpleName()); franta-hg@29: private static final ResourceBundle překlady = ResourceBundle franta-hg@29: .getBundle(Atribut.class.getPackageName() + ".Překlady"); franta-hg@31: private final String[] sloupečky = { franta-hg@29: překlady.getString("tabulka.název"), franta-hg@29: překlady.getString("tabulka.hodnota") franta-hg@29: }; franta-hg@31: private final Set posluchače = new HashSet<>(); franta-hg@31: private final Konfigurace konfigurace; franta-hg@31: private final UserDefinedFileAttributeView souborovýSystém; franta-hg@31: private final List atributy = new ArrayList<>(); franta-hg@6: franta-hg@30: private RandomAccessFile zámekSoubor; franta-hg@30: private FileChannel zámekKanál; franta-hg@30: private FileLock zámek; franta-hg@30: franta-hg@31: public Model(Konfigurace konfigurace) throws IOException { franta-hg@31: this.konfigurace = konfigurace; franta-hg@31: Path cesta = konfigurace.getSoubor().toPath(); franta-hg@17: FileSystemProvider posyktovatelFS = cesta.getFileSystem().provider(); franta-hg@29: souborovýSystém = posyktovatelFS franta-hg@29: .getFileAttributeView(cesta, UserDefinedFileAttributeView.class); franta-hg@6: načtiAtributy(); franta-hg@6: } franta-hg@6: franta-hg@31: public Konfigurace getKonfigurace() { franta-hg@31: return konfigurace; franta-hg@31: } franta-hg@31: franta-hg@22: @Override franta-hg@6: public int getRowCount() { franta-hg@6: return atributy.size(); franta-hg@6: } franta-hg@6: franta-hg@22: @Override franta-hg@6: public int getColumnCount() { franta-hg@6: return sloupečky.length; franta-hg@6: } franta-hg@6: franta-hg@22: @Override franta-hg@6: public String getColumnName(int n) { franta-hg@6: return sloupečky[n]; franta-hg@6: } franta-hg@6: franta-hg@22: @Override franta-hg@6: public Class getColumnClass(int n) { franta-hg@6: return String.class; franta-hg@6: } franta-hg@6: franta-hg@22: @Override franta-hg@6: public boolean isCellEditable(int m, int n) { franta-hg@6: return true; franta-hg@6: } franta-hg@6: franta-hg@22: @Override franta-hg@6: public Object getValueAt(int m, int n) { franta-hg@31: switch (n) { franta-hg@31: case 0: franta-hg@31: return atributy.get(m).getKlíč(); franta-hg@31: case 1: franta-hg@31: return atributy.get(m).getHodnota(); franta-hg@31: default: franta-hg@31: return null; franta-hg@6: } franta-hg@6: } franta-hg@6: franta-hg@22: @Override 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@31: /** franta-hg@31: * Měníme klíč – název atributu franta-hg@31: */ 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@31: /** franta-hg@31: * Měníme hodnotu atributu franta-hg@31: */ 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@29: log.log(Level.SEVERE, "Selhalo ukládání atributu na FS", e); franta-hg@6: } franta-hg@6: } franta-hg@6: franta-hg@22: @Override franta-hg@6: public void addTableModelListener(TableModelListener l) { franta-hg@6: posluchače.add(l); franta-hg@6: } franta-hg@6: franta-hg@22: @Override 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@29: ByteBuffer hodnotaAtributu = ByteBuffer franta-hg@29: .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@30: franta-hg@30: public boolean isZámekPodporovaný() { franta-hg@31: return konfigurace.getSoubor().isFile(); franta-hg@30: } franta-hg@30: franta-hg@30: public void nastavZámek(boolean zamknout) throws IOException { franta-hg@30: if (!isZámekPodporovaný()) { franta-hg@30: throw new IOException(překlady franta-hg@30: .getString("chyba.lzeZamknoutJenSoubor")); franta-hg@30: } franta-hg@30: franta-hg@30: if (zamknout && zámekSoubor == null) { franta-hg@31: zámekSoubor = new RandomAccessFile(konfigurace.getSoubor(), "rw"); franta-hg@30: zámekKanál = zámekSoubor.getChannel(); franta-hg@30: zámek = zámekKanál.lock(); franta-hg@30: } else if (!zamknout && zámekSoubor != null) { franta-hg@30: zámek.release(); franta-hg@30: zámekKanál.close(); franta-hg@30: zámekSoubor.close(); franta-hg@30: zámek = null; franta-hg@30: zámekKanál = null; franta-hg@30: zámekSoubor = null; franta-hg@30: } franta-hg@30: franta-hg@30: } franta-hg@6: }