java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Model.java
author František Kučera <franta-hg@frantovo.cz>
Sat Aug 18 13:27:00 2012 +0200 (2012-08-18)
changeset 19 c20edbed09c3
parent 17 75b7d41b7428
child 22 bf06eb899671
permissions -rw-r--r--
informace o licenci uvnitř .java souborů
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@19
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@19
     8
 * (at your option) any later version.
franta-hg@19
     9
 * 
franta-hg@19
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@19
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@19
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
franta-hg@19
    13
 * GNU General Public License for more details.
franta-hg@19
    14
 * 
franta-hg@19
    15
 * You should have received a copy of the GNU General Public License
franta-hg@19
    16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
franta-hg@19
    17
 */
franta-hg@13
    18
package cz.frantovo.rozsireneAtributy.gui;
franta-hg@6
    19
franta-hg@13
    20
import cz.frantovo.rozsireneAtributy.Atribut;
franta-hg@6
    21
import java.io.File;
franta-hg@6
    22
import java.io.IOException;
franta-hg@6
    23
import java.nio.ByteBuffer;
franta-hg@6
    24
import java.nio.file.Path;
franta-hg@6
    25
import java.nio.file.attribute.UserDefinedFileAttributeView;
franta-hg@17
    26
import java.nio.file.spi.FileSystemProvider;
franta-hg@6
    27
import java.util.ArrayList;
franta-hg@6
    28
import java.util.HashSet;
franta-hg@6
    29
import java.util.List;
franta-hg@15
    30
import java.util.ResourceBundle;
franta-hg@9
    31
import java.util.logging.Level;
franta-hg@6
    32
import java.util.logging.Logger;
franta-hg@6
    33
import javax.swing.event.TableModelEvent;
franta-hg@6
    34
import javax.swing.event.TableModelListener;
franta-hg@6
    35
import javax.swing.table.TableModel;
franta-hg@6
    36
franta-hg@6
    37
/**
franta-hg@6
    38
 *
franta-hg@6
    39
 * @author fiki
franta-hg@6
    40
 */
franta-hg@6
    41
public class Model implements TableModel {
franta-hg@6
    42
franta-hg@6
    43
	private static final Logger log = Logger.getLogger(Model.class.getSimpleName());
franta-hg@15
    44
	private static final ResourceBundle překlady = ResourceBundle.getBundle("cz.frantovo.rozsireneAtributy.Překlady");
franta-hg@15
    45
	private String[] sloupečky = {překlady.getString("tabulka.název"), překlady.getString("tabulka.hodnota")};
franta-hg@6
    46
	private HashSet<TableModelListener> posluchače = new HashSet<TableModelListener>();
franta-hg@11
    47
	private UserDefinedFileAttributeView souborovýSystém;
franta-hg@6
    48
	private ArrayList<Atribut> atributy = new ArrayList<Atribut>();
franta-hg@6
    49
franta-hg@6
    50
	public Model(File soubor) throws IOException {
franta-hg@6
    51
		Path cesta = soubor.toPath();
franta-hg@17
    52
		FileSystemProvider posyktovatelFS = cesta.getFileSystem().provider();
franta-hg@17
    53
		souborovýSystém = posyktovatelFS.getFileAttributeView(cesta, UserDefinedFileAttributeView.class);
franta-hg@6
    54
		načtiAtributy();
franta-hg@6
    55
	}
franta-hg@6
    56
franta-hg@6
    57
	public int getRowCount() {
franta-hg@6
    58
		return atributy.size();
franta-hg@6
    59
	}
franta-hg@6
    60
franta-hg@6
    61
	public int getColumnCount() {
franta-hg@6
    62
		return sloupečky.length;
franta-hg@6
    63
	}
franta-hg@6
    64
franta-hg@6
    65
	public String getColumnName(int n) {
franta-hg@6
    66
		return sloupečky[n];
franta-hg@6
    67
	}
franta-hg@6
    68
franta-hg@6
    69
	public Class<?> getColumnClass(int n) {
franta-hg@6
    70
		return String.class;
franta-hg@6
    71
	}
franta-hg@6
    72
franta-hg@6
    73
	public boolean isCellEditable(int m, int n) {
franta-hg@6
    74
		return true;
franta-hg@6
    75
	}
franta-hg@6
    76
franta-hg@6
    77
	public Object getValueAt(int m, int n) {
franta-hg@6
    78
		if (n == 0) {
franta-hg@11
    79
			return atributy.get(m).getKlíč();
franta-hg@6
    80
		} else if (n == 1) {
franta-hg@6
    81
			return atributy.get(m).getHodnota();
franta-hg@6
    82
		} else {
franta-hg@6
    83
			return null;
franta-hg@6
    84
		}
franta-hg@6
    85
	}
franta-hg@6
    86
franta-hg@14
    87
	public void setValueAt(Object hodnota, int m, int n) {
franta-hg@9
    88
		Atribut a = atributy.get(m);
franta-hg@9
    89
		try {
franta-hg@9
    90
			if (n == 0) {
franta-hg@9
    91
				/** Měníme klíč – název atributu */
franta-hg@14
    92
				String novýKlíč = String.valueOf(hodnota);
franta-hg@11
    93
				if (!novýKlíč.equals(a.getKlíč())) {
franta-hg@11
    94
					if (a.isPlatnýKlíč()) {
franta-hg@11
    95
						souborovýSystém.delete(a.getKlíč());
franta-hg@10
    96
					}
franta-hg@11
    97
					a.setKlíč(novýKlíč);
franta-hg@14
    98
					if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
franta-hg@11
    99
						souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
franta-hg@10
   100
					}
franta-hg@9
   101
				}
franta-hg@9
   102
			} else if (n == 1) {
franta-hg@9
   103
				/** Měníme hodnotu atributu */
franta-hg@14
   104
				a.setHodnota(String.valueOf(hodnota));
franta-hg@12
   105
				if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
franta-hg@12
   106
					souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
franta-hg@12
   107
				}
franta-hg@9
   108
			}
franta-hg@9
   109
		} catch (IOException e) {
franta-hg@9
   110
			log.log(Level.SEVERE, "Selhalo ukládání atributu na souborový systém", e);
franta-hg@6
   111
		}
franta-hg@6
   112
	}
franta-hg@6
   113
franta-hg@6
   114
	public void addTableModelListener(TableModelListener l) {
franta-hg@6
   115
		posluchače.add(l);
franta-hg@6
   116
	}
franta-hg@6
   117
franta-hg@6
   118
	public void removeTableModelListener(TableModelListener l) {
franta-hg@6
   119
		posluchače.remove(l);
franta-hg@6
   120
	}
franta-hg@6
   121
franta-hg@10
   122
	/**
franta-hg@10
   123
	 * @param m číslo řádku
franta-hg@10
   124
	 * @return atribut, který se na něm nachází
franta-hg@10
   125
	 */
franta-hg@10
   126
	public Atribut getAtribut(int m) {
franta-hg@10
   127
		return atributy.get(m);
franta-hg@10
   128
	}
franta-hg@10
   129
franta-hg@9
   130
	public void přidejAtribut(Atribut a) {
franta-hg@9
   131
		atributy.add(a);
franta-hg@9
   132
		upozorniPosluchače();
franta-hg@6
   133
	}
franta-hg@6
   134
franta-hg@10
   135
	public void odeberAtribut(Atribut a) throws IOException {
franta-hg@9
   136
		atributy.remove(a);
franta-hg@11
   137
		if (a.isPlatnýKlíč()) {
franta-hg@11
   138
			souborovýSystém.delete(a.getKlíč());
franta-hg@11
   139
		}
franta-hg@9
   140
		upozorniPosluchače();
franta-hg@6
   141
	}
franta-hg@6
   142
franta-hg@10
   143
	public final void načtiAtributy() throws IOException {
franta-hg@11
   144
		List<String> jménaAtributů = souborovýSystém.list();
franta-hg@10
   145
		atributy.clear();
franta-hg@6
   146
		for (String jménoAtributu : jménaAtributů) {
franta-hg@11
   147
			ByteBuffer hodnotaAtributu = ByteBuffer.allocate(souborovýSystém.size(jménoAtributu));
franta-hg@11
   148
			souborovýSystém.read(jménoAtributu, hodnotaAtributu);
franta-hg@6
   149
			atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
franta-hg@6
   150
		}
franta-hg@9
   151
		upozorniPosluchače();
franta-hg@6
   152
	}
franta-hg@6
   153
franta-hg@6
   154
	private void upozorniPosluchače() {
franta-hg@6
   155
		for (TableModelListener p : posluchače) {
franta-hg@6
   156
			p.tableChanged(new TableModelEvent(this));
franta-hg@6
   157
		}
franta-hg@6
   158
	}
franta-hg@6
   159
}