java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/gui/Model.java
branchv_0
changeset 39 ec0e970e0830
parent 38 41e91ea94acb
child 40 1978eaf429de
     1.1 --- a/java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/gui/Model.java	Sat Dec 16 19:09:35 2023 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,218 +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 cz.frantovo.rozsireneatributy.Konfigurace;
    1.24 -import java.io.File;
    1.25 -import java.io.IOException;
    1.26 -import java.io.RandomAccessFile;
    1.27 -import java.nio.ByteBuffer;
    1.28 -import java.nio.channels.FileChannel;
    1.29 -import java.nio.channels.FileLock;
    1.30 -import java.nio.file.Path;
    1.31 -import java.nio.file.attribute.UserDefinedFileAttributeView;
    1.32 -import java.nio.file.spi.FileSystemProvider;
    1.33 -import java.util.ArrayList;
    1.34 -import java.util.HashSet;
    1.35 -import java.util.List;
    1.36 -import java.util.ResourceBundle;
    1.37 -import java.util.Set;
    1.38 -import java.util.logging.Level;
    1.39 -import java.util.logging.Logger;
    1.40 -import javax.swing.event.TableModelEvent;
    1.41 -import javax.swing.event.TableModelListener;
    1.42 -import javax.swing.table.TableModel;
    1.43 -
    1.44 -/**
    1.45 - * @author Ing. František Kučera (frantovo.cz)
    1.46 - */
    1.47 -public class Model implements TableModel {
    1.48 -
    1.49 -	private static final Logger log = Logger
    1.50 -		.getLogger(Model.class.getSimpleName());
    1.51 -	private static final ResourceBundle překlady = ResourceBundle
    1.52 -		.getBundle(Atribut.class.getPackageName() + ".Překlady");
    1.53 -	private final String[] sloupečky = {
    1.54 -		překlady.getString("tabulka.název"),
    1.55 -		překlady.getString("tabulka.hodnota")
    1.56 -	};
    1.57 -	private final Set<TableModelListener> posluchače = new HashSet<>();
    1.58 -	private final Konfigurace konfigurace;
    1.59 -	private final UserDefinedFileAttributeView souborovýSystém;
    1.60 -	private final List<Atribut> atributy = new ArrayList<>();
    1.61 -
    1.62 -	private RandomAccessFile zámekSoubor;
    1.63 -	private FileChannel zámekKanál;
    1.64 -	private FileLock zámek;
    1.65 -
    1.66 -	public Model(Konfigurace konfigurace) throws IOException {
    1.67 -		this.konfigurace = konfigurace;
    1.68 -		Path cesta = konfigurace.getSoubor().toPath();
    1.69 -		FileSystemProvider posyktovatelFS = cesta.getFileSystem().provider();
    1.70 -		souborovýSystém = posyktovatelFS
    1.71 -			.getFileAttributeView(cesta, UserDefinedFileAttributeView.class);
    1.72 -		načtiAtributy();
    1.73 -	}
    1.74 -
    1.75 -	public Konfigurace getKonfigurace() {
    1.76 -		return konfigurace;
    1.77 -	}
    1.78 -
    1.79 -	@Override
    1.80 -	public int getRowCount() {
    1.81 -		return atributy.size();
    1.82 -	}
    1.83 -
    1.84 -	@Override
    1.85 -	public int getColumnCount() {
    1.86 -		return sloupečky.length;
    1.87 -	}
    1.88 -
    1.89 -	@Override
    1.90 -	public String getColumnName(int n) {
    1.91 -		return sloupečky[n];
    1.92 -	}
    1.93 -
    1.94 -	@Override
    1.95 -	public Class<?> getColumnClass(int n) {
    1.96 -		return String.class;
    1.97 -	}
    1.98 -
    1.99 -	@Override
   1.100 -	public boolean isCellEditable(int m, int n) {
   1.101 -		return true;
   1.102 -	}
   1.103 -
   1.104 -	@Override
   1.105 -	public Object getValueAt(int m, int n) {
   1.106 -		switch (n) {
   1.107 -			case 0:
   1.108 -				return atributy.get(m).getKlíč();
   1.109 -			case 1:
   1.110 -				return atributy.get(m).getHodnota();
   1.111 -			default:
   1.112 -				return null;
   1.113 -		}
   1.114 -	}
   1.115 -
   1.116 -	@Override
   1.117 -	public void setValueAt(Object hodnota, int m, int n) {
   1.118 -		Atribut a = atributy.get(m);
   1.119 -		try {
   1.120 -			if (n == 0) {
   1.121 -				/**
   1.122 -				 * Měníme klíč – název atributu
   1.123 -				 */
   1.124 -				String novýKlíč = String.valueOf(hodnota);
   1.125 -				if (!novýKlíč.equals(a.getKlíč())) {
   1.126 -					if (a.isPlatnýKlíč()) {
   1.127 -						souborovýSystém.delete(a.getKlíč());
   1.128 -					}
   1.129 -					a.setKlíč(novýKlíč);
   1.130 -					if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
   1.131 -						souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
   1.132 -					}
   1.133 -				}
   1.134 -			} else if (n == 1) {
   1.135 -				/**
   1.136 -				 * Měníme hodnotu atributu
   1.137 -				 */
   1.138 -				a.setHodnota(String.valueOf(hodnota));
   1.139 -				if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
   1.140 -					souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
   1.141 -				}
   1.142 -			}
   1.143 -		} catch (IOException e) {
   1.144 -			log.log(Level.SEVERE, "Selhalo ukládání atributu na FS", e);
   1.145 -		}
   1.146 -	}
   1.147 -
   1.148 -	@Override
   1.149 -	public void addTableModelListener(TableModelListener l) {
   1.150 -		posluchače.add(l);
   1.151 -	}
   1.152 -
   1.153 -	@Override
   1.154 -	public void removeTableModelListener(TableModelListener l) {
   1.155 -		posluchače.remove(l);
   1.156 -	}
   1.157 -
   1.158 -	/**
   1.159 -	 * @param m číslo řádku
   1.160 -	 * @return atribut, který se na něm nachází
   1.161 -	 */
   1.162 -	public Atribut getAtribut(int m) {
   1.163 -		return atributy.get(m);
   1.164 -	}
   1.165 -
   1.166 -	public void přidejAtribut(Atribut a) {
   1.167 -		atributy.add(a);
   1.168 -		upozorniPosluchače();
   1.169 -	}
   1.170 -
   1.171 -	public void odeberAtribut(Atribut a) throws IOException {
   1.172 -		atributy.remove(a);
   1.173 -		if (a.isPlatnýKlíč()) {
   1.174 -			souborovýSystém.delete(a.getKlíč());
   1.175 -		}
   1.176 -		upozorniPosluchače();
   1.177 -	}
   1.178 -
   1.179 -	public final void načtiAtributy() throws IOException {
   1.180 -		List<String> jménaAtributů = souborovýSystém.list();
   1.181 -		atributy.clear();
   1.182 -		for (String jménoAtributu : jménaAtributů) {
   1.183 -			ByteBuffer hodnotaAtributu = ByteBuffer
   1.184 -				.allocate(souborovýSystém.size(jménoAtributu));
   1.185 -			souborovýSystém.read(jménoAtributu, hodnotaAtributu);
   1.186 -			atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
   1.187 -		}
   1.188 -		upozorniPosluchače();
   1.189 -	}
   1.190 -
   1.191 -	private void upozorniPosluchače() {
   1.192 -		for (TableModelListener p : posluchače) {
   1.193 -			p.tableChanged(new TableModelEvent(this));
   1.194 -		}
   1.195 -	}
   1.196 -
   1.197 -	public boolean isZámekPodporovaný() {
   1.198 -		return konfigurace.getSoubor().isFile();
   1.199 -	}
   1.200 -
   1.201 -	public void nastavZámek(boolean zamknout) throws IOException {
   1.202 -		if (!isZámekPodporovaný()) {
   1.203 -			throw new IOException(překlady
   1.204 -				.getString("chyba.lzeZamknoutJenSoubor"));
   1.205 -		}
   1.206 -
   1.207 -		if (zamknout && zámekSoubor == null) {
   1.208 -			zámekSoubor = new RandomAccessFile(konfigurace.getSoubor(), "rw");
   1.209 -			zámekKanál = zámekSoubor.getChannel();
   1.210 -			zámek = zámekKanál.lock();
   1.211 -		} else if (!zamknout && zámekSoubor != null) {
   1.212 -			zámek.release();
   1.213 -			zámekKanál.close();
   1.214 -			zámekSoubor.close();
   1.215 -			zámek = null;
   1.216 -			zámekKanál = null;
   1.217 -			zámekSoubor = null;
   1.218 -		}
   1.219 -
   1.220 -	}
   1.221 -}