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@19: *
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@19: * the Free Software Foundation, either version 3 of the License, or
franta-hg@19: * (at your option) any later version.
franta-hg@19: *
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@19: *
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@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@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@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@22: private Set posluchače = new HashSet<>();
franta-hg@11: private UserDefinedFileAttributeView souborovýSystém;
franta-hg@22: private List 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@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@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@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@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@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@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: }