java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/gui/Model.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 12 Dec 2023 21:19:11 +0100
branchv_0
changeset 30 d511e4bf7d8f
parent 29 8d42303538ed
child 31 1ab5ce94a146
permissions -rw-r--r--
volitelné zamykání souborů (POSIX) a zaslání notifikace ostatním aplikacím (inotify/CLOSE_WRITE)
franta-hg@19
     1
/**
franta-hg@19
     2
 * Rozšířené atributy – program na správu rozšířených atributů souborů
franta-hg@19
     3
 * Copyright © 2012 František Kučera (frantovo.cz)
franta-hg@19
     4
 * 
franta-hg@19
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@19
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@27
     7
 * the Free Software Foundation, either version 3 of the License.
franta-hg@19
     8
 * 
franta-hg@19
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@19
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@19
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
franta-hg@19
    12
 * GNU General Public License for more details.
franta-hg@19
    13
 * 
franta-hg@19
    14
 * You should have received a copy of the GNU General Public License
franta-hg@19
    15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
franta-hg@19
    16
 */
franta-hg@28
    17
package cz.frantovo.rozsireneatributy.gui;
franta-hg@6
    18
franta-hg@28
    19
import cz.frantovo.rozsireneatributy.Atribut;
franta-hg@6
    20
import java.io.File;
franta-hg@6
    21
import java.io.IOException;
franta-hg@30
    22
import java.io.RandomAccessFile;
franta-hg@6
    23
import java.nio.ByteBuffer;
franta-hg@30
    24
import java.nio.channels.FileChannel;
franta-hg@30
    25
import java.nio.channels.FileLock;
franta-hg@6
    26
import java.nio.file.Path;
franta-hg@6
    27
import java.nio.file.attribute.UserDefinedFileAttributeView;
franta-hg@17
    28
import java.nio.file.spi.FileSystemProvider;
franta-hg@6
    29
import java.util.ArrayList;
franta-hg@6
    30
import java.util.HashSet;
franta-hg@6
    31
import java.util.List;
franta-hg@15
    32
import java.util.ResourceBundle;
franta-hg@22
    33
import java.util.Set;
franta-hg@9
    34
import java.util.logging.Level;
franta-hg@6
    35
import java.util.logging.Logger;
franta-hg@6
    36
import javax.swing.event.TableModelEvent;
franta-hg@6
    37
import javax.swing.event.TableModelListener;
franta-hg@6
    38
import javax.swing.table.TableModel;
franta-hg@6
    39
franta-hg@6
    40
/**
franta-hg@30
    41
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@6
    42
 */
franta-hg@6
    43
public class Model implements TableModel {
franta-hg@6
    44
franta-hg@29
    45
	private static final Logger log = Logger
franta-hg@29
    46
		.getLogger(Model.class.getSimpleName());
franta-hg@29
    47
	private static final ResourceBundle překlady = ResourceBundle
franta-hg@29
    48
		.getBundle(Atribut.class.getPackageName() + ".Překlady");
franta-hg@29
    49
	private String[] sloupečky = {
franta-hg@29
    50
		překlady.getString("tabulka.název"),
franta-hg@29
    51
		překlady.getString("tabulka.hodnota")
franta-hg@29
    52
	};
franta-hg@22
    53
	private Set<TableModelListener> posluchače = new HashSet<>();
franta-hg@30
    54
	private File soubor;
franta-hg@11
    55
	private UserDefinedFileAttributeView souborovýSystém;
franta-hg@22
    56
	private List<Atribut> atributy = new ArrayList<>();
franta-hg@6
    57
franta-hg@30
    58
	private RandomAccessFile zámekSoubor;
franta-hg@30
    59
	private FileChannel zámekKanál;
franta-hg@30
    60
	private FileLock zámek;
franta-hg@30
    61
franta-hg@6
    62
	public Model(File soubor) throws IOException {
franta-hg@30
    63
		this.soubor = soubor;
franta-hg@6
    64
		Path cesta = soubor.toPath();
franta-hg@17
    65
		FileSystemProvider posyktovatelFS = cesta.getFileSystem().provider();
franta-hg@29
    66
		souborovýSystém = posyktovatelFS
franta-hg@29
    67
			.getFileAttributeView(cesta, UserDefinedFileAttributeView.class);
franta-hg@6
    68
		načtiAtributy();
franta-hg@6
    69
	}
franta-hg@6
    70
franta-hg@22
    71
	@Override
franta-hg@6
    72
	public int getRowCount() {
franta-hg@6
    73
		return atributy.size();
franta-hg@6
    74
	}
franta-hg@6
    75
franta-hg@22
    76
	@Override
franta-hg@6
    77
	public int getColumnCount() {
franta-hg@6
    78
		return sloupečky.length;
franta-hg@6
    79
	}
franta-hg@6
    80
franta-hg@22
    81
	@Override
franta-hg@6
    82
	public String getColumnName(int n) {
franta-hg@6
    83
		return sloupečky[n];
franta-hg@6
    84
	}
franta-hg@6
    85
franta-hg@22
    86
	@Override
franta-hg@6
    87
	public Class<?> getColumnClass(int n) {
franta-hg@6
    88
		return String.class;
franta-hg@6
    89
	}
franta-hg@6
    90
franta-hg@22
    91
	@Override
franta-hg@6
    92
	public boolean isCellEditable(int m, int n) {
franta-hg@6
    93
		return true;
franta-hg@6
    94
	}
franta-hg@6
    95
franta-hg@22
    96
	@Override
franta-hg@6
    97
	public Object getValueAt(int m, int n) {
franta-hg@6
    98
		if (n == 0) {
franta-hg@11
    99
			return atributy.get(m).getKlíč();
franta-hg@6
   100
		} else if (n == 1) {
franta-hg@6
   101
			return atributy.get(m).getHodnota();
franta-hg@6
   102
		} else {
franta-hg@6
   103
			return null;
franta-hg@6
   104
		}
franta-hg@6
   105
	}
franta-hg@6
   106
franta-hg@22
   107
	@Override
franta-hg@14
   108
	public void setValueAt(Object hodnota, int m, int n) {
franta-hg@9
   109
		Atribut a = atributy.get(m);
franta-hg@9
   110
		try {
franta-hg@9
   111
			if (n == 0) {
franta-hg@9
   112
				/** Měníme klíč – název atributu */
franta-hg@14
   113
				String novýKlíč = String.valueOf(hodnota);
franta-hg@11
   114
				if (!novýKlíč.equals(a.getKlíč())) {
franta-hg@11
   115
					if (a.isPlatnýKlíč()) {
franta-hg@11
   116
						souborovýSystém.delete(a.getKlíč());
franta-hg@10
   117
					}
franta-hg@11
   118
					a.setKlíč(novýKlíč);
franta-hg@14
   119
					if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
franta-hg@11
   120
						souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
franta-hg@10
   121
					}
franta-hg@9
   122
				}
franta-hg@9
   123
			} else if (n == 1) {
franta-hg@9
   124
				/** Měníme hodnotu atributu */
franta-hg@14
   125
				a.setHodnota(String.valueOf(hodnota));
franta-hg@12
   126
				if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
franta-hg@12
   127
					souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
franta-hg@12
   128
				}
franta-hg@9
   129
			}
franta-hg@9
   130
		} catch (IOException e) {
franta-hg@29
   131
			log.log(Level.SEVERE, "Selhalo ukládání atributu na FS", e);
franta-hg@6
   132
		}
franta-hg@6
   133
	}
franta-hg@6
   134
franta-hg@22
   135
	@Override
franta-hg@6
   136
	public void addTableModelListener(TableModelListener l) {
franta-hg@6
   137
		posluchače.add(l);
franta-hg@6
   138
	}
franta-hg@6
   139
franta-hg@22
   140
	@Override
franta-hg@6
   141
	public void removeTableModelListener(TableModelListener l) {
franta-hg@6
   142
		posluchače.remove(l);
franta-hg@6
   143
	}
franta-hg@6
   144
franta-hg@10
   145
	/**
franta-hg@10
   146
	 * @param m číslo řádku
franta-hg@10
   147
	 * @return atribut, který se na něm nachází
franta-hg@10
   148
	 */
franta-hg@10
   149
	public Atribut getAtribut(int m) {
franta-hg@10
   150
		return atributy.get(m);
franta-hg@10
   151
	}
franta-hg@10
   152
franta-hg@9
   153
	public void přidejAtribut(Atribut a) {
franta-hg@9
   154
		atributy.add(a);
franta-hg@9
   155
		upozorniPosluchače();
franta-hg@6
   156
	}
franta-hg@6
   157
franta-hg@10
   158
	public void odeberAtribut(Atribut a) throws IOException {
franta-hg@9
   159
		atributy.remove(a);
franta-hg@11
   160
		if (a.isPlatnýKlíč()) {
franta-hg@11
   161
			souborovýSystém.delete(a.getKlíč());
franta-hg@11
   162
		}
franta-hg@9
   163
		upozorniPosluchače();
franta-hg@6
   164
	}
franta-hg@6
   165
franta-hg@10
   166
	public final void načtiAtributy() throws IOException {
franta-hg@11
   167
		List<String> jménaAtributů = souborovýSystém.list();
franta-hg@10
   168
		atributy.clear();
franta-hg@6
   169
		for (String jménoAtributu : jménaAtributů) {
franta-hg@29
   170
			ByteBuffer hodnotaAtributu = ByteBuffer
franta-hg@29
   171
				.allocate(souborovýSystém.size(jménoAtributu));
franta-hg@11
   172
			souborovýSystém.read(jménoAtributu, hodnotaAtributu);
franta-hg@6
   173
			atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
franta-hg@6
   174
		}
franta-hg@9
   175
		upozorniPosluchače();
franta-hg@6
   176
	}
franta-hg@6
   177
franta-hg@6
   178
	private void upozorniPosluchače() {
franta-hg@6
   179
		for (TableModelListener p : posluchače) {
franta-hg@6
   180
			p.tableChanged(new TableModelEvent(this));
franta-hg@6
   181
		}
franta-hg@6
   182
	}
franta-hg@30
   183
franta-hg@30
   184
	public boolean isZámekPodporovaný() {
franta-hg@30
   185
		return soubor.isFile();
franta-hg@30
   186
	}
franta-hg@30
   187
franta-hg@30
   188
	public void nastavZámek(boolean zamknout) throws IOException {
franta-hg@30
   189
		if (!isZámekPodporovaný()) {
franta-hg@30
   190
			throw new IOException(překlady
franta-hg@30
   191
				.getString("chyba.lzeZamknoutJenSoubor"));
franta-hg@30
   192
		}
franta-hg@30
   193
franta-hg@30
   194
		if (zamknout && zámekSoubor == null) {
franta-hg@30
   195
			zámekSoubor = new RandomAccessFile(soubor, "rw");
franta-hg@30
   196
			zámekKanál = zámekSoubor.getChannel();
franta-hg@30
   197
			zámek = zámekKanál.lock();
franta-hg@30
   198
		} else if (!zamknout && zámekSoubor != null) {
franta-hg@30
   199
			zámek.release();
franta-hg@30
   200
			zámekKanál.close();
franta-hg@30
   201
			zámekSoubor.close();
franta-hg@30
   202
			zámek = null;
franta-hg@30
   203
			zámekKanál = null;
franta-hg@30
   204
			zámekSoubor = null;
franta-hg@30
   205
		}
franta-hg@30
   206
franta-hg@30
   207
	}
franta-hg@6
   208
}