java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Model.java
author František Kučera <franta-hg@frantovo.cz>
Thu Dec 16 00:51:22 2010 +0100 (2010-12-16)
changeset 12 a2b41c8ee039
parent 11 9b399cde6a3b
permissions -rw-r--r--
Budeme ukládat jen platné atributy
     1 package cz.frantovo.rozsireneAtributy;
     2 
     3 import java.io.File;
     4 import java.io.IOException;
     5 import java.nio.ByteBuffer;
     6 import java.nio.file.Path;
     7 import java.nio.file.attribute.UserDefinedFileAttributeView;
     8 import java.util.ArrayList;
     9 import java.util.HashSet;
    10 import java.util.List;
    11 import java.util.logging.Level;
    12 import java.util.logging.Logger;
    13 import javax.swing.event.TableModelEvent;
    14 import javax.swing.event.TableModelListener;
    15 import javax.swing.table.TableModel;
    16 
    17 /**
    18  *
    19  * @author fiki
    20  */
    21 public class Model implements TableModel {
    22 
    23 	private static final Logger log = Logger.getLogger(Model.class.getSimpleName());
    24 	private String[] sloupečky = {"Název", "Hodnota"};
    25 	private HashSet<TableModelListener> posluchače = new HashSet<TableModelListener>();
    26 	private UserDefinedFileAttributeView souborovýSystém;
    27 	private ArrayList<Atribut> atributy = new ArrayList<Atribut>();
    28 
    29 	public Model(File soubor) throws IOException {
    30 		Path cesta = soubor.toPath();
    31 		souborovýSystém = cesta.getFileAttributeView(UserDefinedFileAttributeView.class);
    32 		načtiAtributy();
    33 	}
    34 
    35 	public int getRowCount() {
    36 		return atributy.size();
    37 	}
    38 
    39 	public int getColumnCount() {
    40 		return sloupečky.length;
    41 	}
    42 
    43 	public String getColumnName(int n) {
    44 		return sloupečky[n];
    45 	}
    46 
    47 	public Class<?> getColumnClass(int n) {
    48 		return String.class;
    49 	}
    50 
    51 	public boolean isCellEditable(int m, int n) {
    52 		return true;
    53 	}
    54 
    55 	public Object getValueAt(int m, int n) {
    56 		if (n == 0) {
    57 			return atributy.get(m).getKlíč();
    58 		} else if (n == 1) {
    59 			return atributy.get(m).getHodnota();
    60 		} else {
    61 			return null;
    62 		}
    63 	}
    64 
    65 	public void setValueAt(Object value, int m, int n) {
    66 		Atribut a = atributy.get(m);
    67 		try {
    68 			if (n == 0) {
    69 				/** Měníme klíč – název atributu */
    70 				String novýKlíč = String.valueOf(value.toString());
    71 				if (!novýKlíč.equals(a.getKlíč())) {
    72 					if (a.isPlatnýKlíč()) {
    73 						souborovýSystém.delete(a.getKlíč());
    74 					}
    75 					a.setKlíč(novýKlíč);
    76 					if (a.isPlatnáHodnota()) {
    77 						souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
    78 					}
    79 				}
    80 			} else if (n == 1) {
    81 				/** Měníme hodnotu atributu */
    82 				a.setHodnota(String.valueOf(value.toString()));
    83 				if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
    84 					souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
    85 				}
    86 			}
    87 		} catch (IOException e) {
    88 			log.log(Level.SEVERE, "Selhalo ukládání atributu na souborový systém", e);
    89 		}
    90 	}
    91 
    92 	public void addTableModelListener(TableModelListener l) {
    93 		posluchače.add(l);
    94 	}
    95 
    96 	public void removeTableModelListener(TableModelListener l) {
    97 		posluchače.remove(l);
    98 	}
    99 
   100 	/**
   101 	 * @param m číslo řádku
   102 	 * @return atribut, který se na něm nachází
   103 	 */
   104 	public Atribut getAtribut(int m) {
   105 		return atributy.get(m);
   106 	}
   107 
   108 	public void přidejAtribut(Atribut a) {
   109 		atributy.add(a);
   110 		upozorniPosluchače();
   111 	}
   112 
   113 	public void odeberAtribut(Atribut a) throws IOException {
   114 		atributy.remove(a);
   115 		if (a.isPlatnýKlíč()) {
   116 			souborovýSystém.delete(a.getKlíč());
   117 		}
   118 		upozorniPosluchače();
   119 	}
   120 
   121 	public final void načtiAtributy() throws IOException {
   122 		List<String> jménaAtributů = souborovýSystém.list();
   123 		atributy.clear();
   124 		for (String jménoAtributu : jménaAtributů) {
   125 			ByteBuffer hodnotaAtributu = ByteBuffer.allocate(souborovýSystém.size(jménoAtributu));
   126 			souborovýSystém.read(jménoAtributu, hodnotaAtributu);
   127 			atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
   128 		}
   129 		upozorniPosluchače();
   130 	}
   131 
   132 	private void upozorniPosluchače() {
   133 		for (TableModelListener p : posluchače) {
   134 			p.tableChanged(new TableModelEvent(this));
   135 		}
   136 	}
   137 }