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.getLogger(Model.class.getSimpleName());
44 private static final ResourceBundle překlady = ResourceBundle.getBundle(Atribut.class.getPackageName() + ".Překlady");
45 private String[] sloupečky = {překlady.getString("tabulka.název"), překlady.getString("tabulka.hodnota")};
46 private Set<TableModelListener> posluchače = new HashSet<>();
47 private UserDefinedFileAttributeView souborovýSystém;
48 private List<Atribut> atributy = new ArrayList<>();
50 public Model(File soubor) throws IOException {
51 Path cesta = soubor.toPath();
52 FileSystemProvider posyktovatelFS = cesta.getFileSystem().provider();
53 souborovýSystém = posyktovatelFS.getFileAttributeView(cesta, UserDefinedFileAttributeView.class);
58 public int getRowCount() {
59 return atributy.size();
63 public int getColumnCount() {
64 return sloupečky.length;
68 public String getColumnName(int n) {
73 public Class<?> getColumnClass(int n) {
78 public boolean isCellEditable(int m, int n) {
83 public Object getValueAt(int m, int n) {
85 return atributy.get(m).getKlíč();
87 return atributy.get(m).getHodnota();
94 public void setValueAt(Object hodnota, int m, int n) {
95 Atribut a = atributy.get(m);
98 /** Měníme klíč – název atributu */
99 String novýKlíč = String.valueOf(hodnota);
100 if (!novýKlíč.equals(a.getKlíč())) {
101 if (a.isPlatnýKlíč()) {
102 souborovýSystém.delete(a.getKlíč());
105 if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
106 souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
110 /** Měníme hodnotu atributu */
111 a.setHodnota(String.valueOf(hodnota));
112 if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
113 souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
116 } catch (IOException e) {
117 log.log(Level.SEVERE, "Selhalo ukládání atributu na souborový systém", e);
122 public void addTableModelListener(TableModelListener l) {
127 public void removeTableModelListener(TableModelListener l) {
128 posluchače.remove(l);
132 * @param m číslo řádku
133 * @return atribut, který se na něm nachází
135 public Atribut getAtribut(int m) {
136 return atributy.get(m);
139 public void přidejAtribut(Atribut a) {
141 upozorniPosluchače();
144 public void odeberAtribut(Atribut a) throws IOException {
146 if (a.isPlatnýKlíč()) {
147 souborovýSystém.delete(a.getKlíč());
149 upozorniPosluchače();
152 public final void načtiAtributy() throws IOException {
153 List<String> jménaAtributů = souborovýSystém.list();
155 for (String jménoAtributu : jménaAtributů) {
156 ByteBuffer hodnotaAtributu = ByteBuffer.allocate(souborovýSystém.size(jménoAtributu));
157 souborovýSystém.read(jménoAtributu, hodnotaAtributu);
158 atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
160 upozorniPosluchače();
163 private void upozorniPosluchače() {
164 for (TableModelListener p : posluchače) {
165 p.tableChanged(new TableModelEvent(this));