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.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package cz.frantovo.rozsireneatributy.gui;
19 import cz.frantovo.rozsireneatributy.Atribut;
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23 import java.nio.file.Path;
24 import java.nio.file.attribute.UserDefinedFileAttributeView;
25 import java.nio.file.spi.FileSystemProvider;
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.ResourceBundle;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
33 import javax.swing.event.TableModelEvent;
34 import javax.swing.event.TableModelListener;
35 import javax.swing.table.TableModel;
41 public class Model implements TableModel {
43 private static final Logger log = Logger
44 .getLogger(Model.class.getSimpleName());
45 private static final ResourceBundle překlady = ResourceBundle
46 .getBundle(Atribut.class.getPackageName() + ".Překlady");
47 private String[] sloupečky = {
48 překlady.getString("tabulka.název"),
49 překlady.getString("tabulka.hodnota")
51 private Set<TableModelListener> posluchače = new HashSet<>();
52 private UserDefinedFileAttributeView souborovýSystém;
53 private List<Atribut> atributy = new ArrayList<>();
55 public Model(File soubor) throws IOException {
56 Path cesta = soubor.toPath();
57 FileSystemProvider posyktovatelFS = cesta.getFileSystem().provider();
58 souborovýSystém = posyktovatelFS
59 .getFileAttributeView(cesta, UserDefinedFileAttributeView.class);
64 public int getRowCount() {
65 return atributy.size();
69 public int getColumnCount() {
70 return sloupečky.length;
74 public String getColumnName(int n) {
79 public Class<?> getColumnClass(int n) {
84 public boolean isCellEditable(int m, int n) {
89 public Object getValueAt(int m, int n) {
91 return atributy.get(m).getKlíč();
93 return atributy.get(m).getHodnota();
100 public void setValueAt(Object hodnota, int m, int n) {
101 Atribut a = atributy.get(m);
104 /** Měníme klíč – název atributu */
105 String novýKlíč = String.valueOf(hodnota);
106 if (!novýKlíč.equals(a.getKlíč())) {
107 if (a.isPlatnýKlíč()) {
108 souborovýSystém.delete(a.getKlíč());
111 if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
112 souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
116 /** Měníme hodnotu atributu */
117 a.setHodnota(String.valueOf(hodnota));
118 if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
119 souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
122 } catch (IOException e) {
123 log.log(Level.SEVERE, "Selhalo ukládání atributu na FS", e);
128 public void addTableModelListener(TableModelListener l) {
133 public void removeTableModelListener(TableModelListener l) {
134 posluchače.remove(l);
138 * @param m číslo řádku
139 * @return atribut, který se na něm nachází
141 public Atribut getAtribut(int m) {
142 return atributy.get(m);
145 public void přidejAtribut(Atribut a) {
147 upozorniPosluchače();
150 public void odeberAtribut(Atribut a) throws IOException {
152 if (a.isPlatnýKlíč()) {
153 souborovýSystém.delete(a.getKlíč());
155 upozorniPosluchače();
158 public final void načtiAtributy() throws IOException {
159 List<String> jménaAtributů = souborovýSystém.list();
161 for (String jménoAtributu : jménaAtributů) {
162 ByteBuffer hodnotaAtributu = ByteBuffer
163 .allocate(souborovýSystém.size(jménoAtributu));
164 souborovýSystém.read(jménoAtributu, hodnotaAtributu);
165 atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
167 upozorniPosluchače();
170 private void upozorniPosluchače() {
171 for (TableModelListener p : posluchače) {
172 p.tableChanged(new TableModelEvent(this));