2 * Rozšířené atributy – program na správu rozšířených atributů souborů
3 * Copyright © 2012 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package cz.frantovo.rozsireneAtributy.gui;
20 import cz.frantovo.rozsireneAtributy.Atribut;
22 import java.io.IOException;
23 import java.nio.ByteBuffer;
24 import java.nio.file.Path;
25 import java.nio.file.attribute.UserDefinedFileAttributeView;
26 import java.nio.file.spi.FileSystemProvider;
27 import java.util.ArrayList;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.ResourceBundle;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34 import javax.swing.event.TableModelEvent;
35 import javax.swing.event.TableModelListener;
36 import javax.swing.table.TableModel;
42 public class Model implements TableModel {
44 private static final Logger log = Logger.getLogger(Model.class.getSimpleName());
45 private static final ResourceBundle překlady = ResourceBundle.getBundle("cz.frantovo.rozsireneAtributy.Překlady");
46 private String[] sloupečky = {překlady.getString("tabulka.název"), překlady.getString("tabulka.hodnota")};
47 private Set<TableModelListener> posluchače = new HashSet<>();
48 private UserDefinedFileAttributeView souborovýSystém;
49 private List<Atribut> atributy = new ArrayList<>();
51 public Model(File soubor) throws IOException {
52 Path cesta = soubor.toPath();
53 FileSystemProvider posyktovatelFS = cesta.getFileSystem().provider();
54 souborovýSystém = posyktovatelFS.getFileAttributeView(cesta, UserDefinedFileAttributeView.class);
59 public int getRowCount() {
60 return atributy.size();
64 public int getColumnCount() {
65 return sloupečky.length;
69 public String getColumnName(int n) {
74 public Class<?> getColumnClass(int n) {
79 public boolean isCellEditable(int m, int n) {
84 public Object getValueAt(int m, int n) {
86 return atributy.get(m).getKlíč();
88 return atributy.get(m).getHodnota();
95 public void setValueAt(Object hodnota, int m, int n) {
96 Atribut a = atributy.get(m);
99 /** Měníme klíč – název atributu */
100 String novýKlíč = String.valueOf(hodnota);
101 if (!novýKlíč.equals(a.getKlíč())) {
102 if (a.isPlatnýKlíč()) {
103 souborovýSystém.delete(a.getKlíč());
106 if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
107 souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
111 /** Měníme hodnotu atributu */
112 a.setHodnota(String.valueOf(hodnota));
113 if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
114 souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
117 } catch (IOException e) {
118 log.log(Level.SEVERE, "Selhalo ukládání atributu na souborový systém", e);
123 public void addTableModelListener(TableModelListener l) {
128 public void removeTableModelListener(TableModelListener l) {
129 posluchače.remove(l);
133 * @param m číslo řádku
134 * @return atribut, který se na něm nachází
136 public Atribut getAtribut(int m) {
137 return atributy.get(m);
140 public void přidejAtribut(Atribut a) {
142 upozorniPosluchače();
145 public void odeberAtribut(Atribut a) throws IOException {
147 if (a.isPlatnýKlíč()) {
148 souborovýSystém.delete(a.getKlíč());
150 upozorniPosluchače();
153 public final void načtiAtributy() throws IOException {
154 List<String> jménaAtributů = souborovýSystém.list();
156 for (String jménoAtributu : jménaAtributů) {
157 ByteBuffer hodnotaAtributu = ByteBuffer.allocate(souborovýSystém.size(jménoAtributu));
158 souborovýSystém.read(jménoAtributu, hodnotaAtributu);
159 atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
161 upozorniPosluchače();
164 private void upozorniPosluchače() {
165 for (TableModelListener p : posluchače) {
166 p.tableChanged(new TableModelEvent(this));