1.1 --- a/java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Model.java Mon Dec 11 00:10:33 2023 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,168 +0,0 @@
1.4 -/**
1.5 - * Rozšířené atributy – program na správu rozšířených atributů souborů
1.6 - * Copyright © 2012 František Kučera (frantovo.cz)
1.7 - *
1.8 - * This program is free software: you can redistribute it and/or modify
1.9 - * it under the terms of the GNU General Public License as published by
1.10 - * the Free Software Foundation, either version 3 of the License.
1.11 - *
1.12 - * This program is distributed in the hope that it will be useful,
1.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 - * GNU General Public License for more details.
1.16 - *
1.17 - * You should have received a copy of the GNU General Public License
1.18 - * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.19 - */
1.20 -package cz.frantovo.rozsireneAtributy.gui;
1.21 -
1.22 -import cz.frantovo.rozsireneAtributy.Atribut;
1.23 -import java.io.File;
1.24 -import java.io.IOException;
1.25 -import java.nio.ByteBuffer;
1.26 -import java.nio.file.Path;
1.27 -import java.nio.file.attribute.UserDefinedFileAttributeView;
1.28 -import java.nio.file.spi.FileSystemProvider;
1.29 -import java.util.ArrayList;
1.30 -import java.util.HashSet;
1.31 -import java.util.List;
1.32 -import java.util.ResourceBundle;
1.33 -import java.util.Set;
1.34 -import java.util.logging.Level;
1.35 -import java.util.logging.Logger;
1.36 -import javax.swing.event.TableModelEvent;
1.37 -import javax.swing.event.TableModelListener;
1.38 -import javax.swing.table.TableModel;
1.39 -
1.40 -/**
1.41 - *
1.42 - * @author fiki
1.43 - */
1.44 -public class Model implements TableModel {
1.45 -
1.46 - private static final Logger log = Logger.getLogger(Model.class.getSimpleName());
1.47 - private static final ResourceBundle překlady = ResourceBundle.getBundle("cz.frantovo.rozsireneAtributy.Překlady");
1.48 - private String[] sloupečky = {překlady.getString("tabulka.název"), překlady.getString("tabulka.hodnota")};
1.49 - private Set<TableModelListener> posluchače = new HashSet<>();
1.50 - private UserDefinedFileAttributeView souborovýSystém;
1.51 - private List<Atribut> atributy = new ArrayList<>();
1.52 -
1.53 - public Model(File soubor) throws IOException {
1.54 - Path cesta = soubor.toPath();
1.55 - FileSystemProvider posyktovatelFS = cesta.getFileSystem().provider();
1.56 - souborovýSystém = posyktovatelFS.getFileAttributeView(cesta, UserDefinedFileAttributeView.class);
1.57 - načtiAtributy();
1.58 - }
1.59 -
1.60 - @Override
1.61 - public int getRowCount() {
1.62 - return atributy.size();
1.63 - }
1.64 -
1.65 - @Override
1.66 - public int getColumnCount() {
1.67 - return sloupečky.length;
1.68 - }
1.69 -
1.70 - @Override
1.71 - public String getColumnName(int n) {
1.72 - return sloupečky[n];
1.73 - }
1.74 -
1.75 - @Override
1.76 - public Class<?> getColumnClass(int n) {
1.77 - return String.class;
1.78 - }
1.79 -
1.80 - @Override
1.81 - public boolean isCellEditable(int m, int n) {
1.82 - return true;
1.83 - }
1.84 -
1.85 - @Override
1.86 - public Object getValueAt(int m, int n) {
1.87 - if (n == 0) {
1.88 - return atributy.get(m).getKlíč();
1.89 - } else if (n == 1) {
1.90 - return atributy.get(m).getHodnota();
1.91 - } else {
1.92 - return null;
1.93 - }
1.94 - }
1.95 -
1.96 - @Override
1.97 - public void setValueAt(Object hodnota, int m, int n) {
1.98 - Atribut a = atributy.get(m);
1.99 - try {
1.100 - if (n == 0) {
1.101 - /** Měníme klíč – název atributu */
1.102 - String novýKlíč = String.valueOf(hodnota);
1.103 - if (!novýKlíč.equals(a.getKlíč())) {
1.104 - if (a.isPlatnýKlíč()) {
1.105 - souborovýSystém.delete(a.getKlíč());
1.106 - }
1.107 - a.setKlíč(novýKlíč);
1.108 - if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
1.109 - souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
1.110 - }
1.111 - }
1.112 - } else if (n == 1) {
1.113 - /** Měníme hodnotu atributu */
1.114 - a.setHodnota(String.valueOf(hodnota));
1.115 - if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
1.116 - souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
1.117 - }
1.118 - }
1.119 - } catch (IOException e) {
1.120 - log.log(Level.SEVERE, "Selhalo ukládání atributu na souborový systém", e);
1.121 - }
1.122 - }
1.123 -
1.124 - @Override
1.125 - public void addTableModelListener(TableModelListener l) {
1.126 - posluchače.add(l);
1.127 - }
1.128 -
1.129 - @Override
1.130 - public void removeTableModelListener(TableModelListener l) {
1.131 - posluchače.remove(l);
1.132 - }
1.133 -
1.134 - /**
1.135 - * @param m číslo řádku
1.136 - * @return atribut, který se na něm nachází
1.137 - */
1.138 - public Atribut getAtribut(int m) {
1.139 - return atributy.get(m);
1.140 - }
1.141 -
1.142 - public void přidejAtribut(Atribut a) {
1.143 - atributy.add(a);
1.144 - upozorniPosluchače();
1.145 - }
1.146 -
1.147 - public void odeberAtribut(Atribut a) throws IOException {
1.148 - atributy.remove(a);
1.149 - if (a.isPlatnýKlíč()) {
1.150 - souborovýSystém.delete(a.getKlíč());
1.151 - }
1.152 - upozorniPosluchače();
1.153 - }
1.154 -
1.155 - public final void načtiAtributy() throws IOException {
1.156 - List<String> jménaAtributů = souborovýSystém.list();
1.157 - atributy.clear();
1.158 - for (String jménoAtributu : jménaAtributů) {
1.159 - ByteBuffer hodnotaAtributu = ByteBuffer.allocate(souborovýSystém.size(jménoAtributu));
1.160 - souborovýSystém.read(jménoAtributu, hodnotaAtributu);
1.161 - atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
1.162 - }
1.163 - upozorniPosluchače();
1.164 - }
1.165 -
1.166 - private void upozorniPosluchače() {
1.167 - for (TableModelListener p : posluchače) {
1.168 - p.tableChanged(new TableModelEvent(this));
1.169 - }
1.170 - }
1.171 -}