1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Model.java Thu Dec 16 01:16:55 2010 +0100
1.3 @@ -0,0 +1,138 @@
1.4 +package cz.frantovo.rozsireneAtributy.gui;
1.5 +
1.6 +import cz.frantovo.rozsireneAtributy.Atribut;
1.7 +import java.io.File;
1.8 +import java.io.IOException;
1.9 +import java.nio.ByteBuffer;
1.10 +import java.nio.file.Path;
1.11 +import java.nio.file.attribute.UserDefinedFileAttributeView;
1.12 +import java.util.ArrayList;
1.13 +import java.util.HashSet;
1.14 +import java.util.List;
1.15 +import java.util.logging.Level;
1.16 +import java.util.logging.Logger;
1.17 +import javax.swing.event.TableModelEvent;
1.18 +import javax.swing.event.TableModelListener;
1.19 +import javax.swing.table.TableModel;
1.20 +
1.21 +/**
1.22 + *
1.23 + * @author fiki
1.24 + */
1.25 +public class Model implements TableModel {
1.26 +
1.27 + private static final Logger log = Logger.getLogger(Model.class.getSimpleName());
1.28 + private String[] sloupečky = {"Název", "Hodnota"};
1.29 + private HashSet<TableModelListener> posluchače = new HashSet<TableModelListener>();
1.30 + private UserDefinedFileAttributeView souborovýSystém;
1.31 + private ArrayList<Atribut> atributy = new ArrayList<Atribut>();
1.32 +
1.33 + public Model(File soubor) throws IOException {
1.34 + Path cesta = soubor.toPath();
1.35 + souborovýSystém = cesta.getFileAttributeView(UserDefinedFileAttributeView.class);
1.36 + načtiAtributy();
1.37 + }
1.38 +
1.39 + public int getRowCount() {
1.40 + return atributy.size();
1.41 + }
1.42 +
1.43 + public int getColumnCount() {
1.44 + return sloupečky.length;
1.45 + }
1.46 +
1.47 + public String getColumnName(int n) {
1.48 + return sloupečky[n];
1.49 + }
1.50 +
1.51 + public Class<?> getColumnClass(int n) {
1.52 + return String.class;
1.53 + }
1.54 +
1.55 + public boolean isCellEditable(int m, int n) {
1.56 + return true;
1.57 + }
1.58 +
1.59 + public Object getValueAt(int m, int n) {
1.60 + if (n == 0) {
1.61 + return atributy.get(m).getKlíč();
1.62 + } else if (n == 1) {
1.63 + return atributy.get(m).getHodnota();
1.64 + } else {
1.65 + return null;
1.66 + }
1.67 + }
1.68 +
1.69 + public void setValueAt(Object value, int m, int n) {
1.70 + Atribut a = atributy.get(m);
1.71 + try {
1.72 + if (n == 0) {
1.73 + /** Měníme klíč – název atributu */
1.74 + String novýKlíč = String.valueOf(value.toString());
1.75 + if (!novýKlíč.equals(a.getKlíč())) {
1.76 + if (a.isPlatnýKlíč()) {
1.77 + souborovýSystém.delete(a.getKlíč());
1.78 + }
1.79 + a.setKlíč(novýKlíč);
1.80 + if (a.isPlatnáHodnota()) {
1.81 + souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
1.82 + }
1.83 + }
1.84 + } else if (n == 1) {
1.85 + /** Měníme hodnotu atributu */
1.86 + a.setHodnota(String.valueOf(value.toString()));
1.87 + if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
1.88 + souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
1.89 + }
1.90 + }
1.91 + } catch (IOException e) {
1.92 + log.log(Level.SEVERE, "Selhalo ukládání atributu na souborový systém", e);
1.93 + }
1.94 + }
1.95 +
1.96 + public void addTableModelListener(TableModelListener l) {
1.97 + posluchače.add(l);
1.98 + }
1.99 +
1.100 + public void removeTableModelListener(TableModelListener l) {
1.101 + posluchače.remove(l);
1.102 + }
1.103 +
1.104 + /**
1.105 + * @param m číslo řádku
1.106 + * @return atribut, který se na něm nachází
1.107 + */
1.108 + public Atribut getAtribut(int m) {
1.109 + return atributy.get(m);
1.110 + }
1.111 +
1.112 + public void přidejAtribut(Atribut a) {
1.113 + atributy.add(a);
1.114 + upozorniPosluchače();
1.115 + }
1.116 +
1.117 + public void odeberAtribut(Atribut a) throws IOException {
1.118 + atributy.remove(a);
1.119 + if (a.isPlatnýKlíč()) {
1.120 + souborovýSystém.delete(a.getKlíč());
1.121 + }
1.122 + upozorniPosluchače();
1.123 + }
1.124 +
1.125 + public final void načtiAtributy() throws IOException {
1.126 + List<String> jménaAtributů = souborovýSystém.list();
1.127 + atributy.clear();
1.128 + for (String jménoAtributu : jménaAtributů) {
1.129 + ByteBuffer hodnotaAtributu = ByteBuffer.allocate(souborovýSystém.size(jménoAtributu));
1.130 + souborovýSystém.read(jménoAtributu, hodnotaAtributu);
1.131 + atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
1.132 + }
1.133 + upozorniPosluchače();
1.134 + }
1.135 +
1.136 + private void upozorniPosluchače() {
1.137 + for (TableModelListener p : posluchače) {
1.138 + p.tableChanged(new TableModelEvent(this));
1.139 + }
1.140 + }
1.141 +}