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;
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("cz.frantovo.rozsireneAtributy.Překlady");
45 private String[] sloupečky = {překlady.getString("tabulka.název"), překlady.getString("tabulka.hodnota")};
46 private HashSet<TableModelListener> posluchače = new HashSet<TableModelListener>();
47 private UserDefinedFileAttributeView souborovýSystém;
48 private ArrayList<Atribut> atributy = new ArrayList<Atribut>();
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);
57 public int getRowCount() {
58 return atributy.size();
61 public int getColumnCount() {
62 return sloupečky.length;
65 public String getColumnName(int n) {
69 public Class<?> getColumnClass(int n) {
73 public boolean isCellEditable(int m, int n) {
77 public Object getValueAt(int m, int n) {
79 return atributy.get(m).getKlíč();
81 return atributy.get(m).getHodnota();
87 public void setValueAt(Object hodnota, int m, int n) {
88 Atribut a = atributy.get(m);
91 /** Měníme klíč – název atributu */
92 String novýKlíč = String.valueOf(hodnota);
93 if (!novýKlíč.equals(a.getKlíč())) {
94 if (a.isPlatnýKlíč()) {
95 souborovýSystém.delete(a.getKlíč());
98 if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
99 souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
103 /** Měníme hodnotu atributu */
104 a.setHodnota(String.valueOf(hodnota));
105 if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
106 souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
109 } catch (IOException e) {
110 log.log(Level.SEVERE, "Selhalo ukládání atributu na souborový systém", e);
114 public void addTableModelListener(TableModelListener l) {
118 public void removeTableModelListener(TableModelListener l) {
119 posluchače.remove(l);
123 * @param m číslo řádku
124 * @return atribut, který se na něm nachází
126 public Atribut getAtribut(int m) {
127 return atributy.get(m);
130 public void přidejAtribut(Atribut a) {
132 upozorniPosluchače();
135 public void odeberAtribut(Atribut a) throws IOException {
137 if (a.isPlatnýKlíč()) {
138 souborovýSystém.delete(a.getKlíč());
140 upozorniPosluchače();
143 public final void načtiAtributy() throws IOException {
144 List<String> jménaAtributů = souborovýSystém.list();
146 for (String jménoAtributu : jménaAtributů) {
147 ByteBuffer hodnotaAtributu = ByteBuffer.allocate(souborovýSystém.size(jménoAtributu));
148 souborovýSystém.read(jménoAtributu, hodnotaAtributu);
149 atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
151 upozorniPosluchače();
154 private void upozorniPosluchače() {
155 for (TableModelListener p : posluchače) {
156 p.tableChanged(new TableModelEvent(this));