java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Model.java
author František Kučera <franta-hg@frantovo.cz>
Wed Dec 15 22:38:03 2010 +0100 (2010-12-15)
changeset 9 a2e91b20198b
parent 6 734f104f2869
child 10 ed2b6ebf138d
permissions -rw-r--r--
Ukládání atributů (možnost změny stávajících).
     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 souborovySystem;
    27 	private ArrayList<Atribut> atributy = new ArrayList<Atribut>();
    28 
    29 	public Model(File soubor) throws IOException {
    30 		Path cesta = soubor.toPath();
    31 		souborovySystem = 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).getKlic();
    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.getKlic())) {
    72 					souborovySystem.delete(a.getKlic());
    73 					a.setKlic(novýKlíč);
    74 					souborovySystem.write(a.getKlic(), a.getHodnotaBajty());
    75 				}
    76 			} else if (n == 1) {
    77 				/** Měníme hodnotu atributu */
    78 				a.setHodnota(String.valueOf(value.toString()));
    79 				souborovySystem.write(a.getKlic(), a.getHodnotaBajty());
    80 			}
    81 		} catch (IOException e) {
    82 			log.log(Level.SEVERE, "Selhalo ukládání atributu na souborový systém", e);
    83 		}
    84 	}
    85 
    86 	public void addTableModelListener(TableModelListener l) {
    87 		posluchače.add(l);
    88 	}
    89 
    90 	public void removeTableModelListener(TableModelListener l) {
    91 		posluchače.remove(l);
    92 	}
    93 
    94 	public void přidejAtribut(Atribut a) {
    95 		atributy.add(a);
    96 		upozorniPosluchače();
    97 	}
    98 
    99 	public void odeberAtribut(Atribut a) {
   100 		atributy.remove(a);
   101 		upozorniPosluchače();
   102 	}
   103 
   104 	private void načtiAtributy() throws IOException {
   105 		List<String> jménaAtributů = souborovySystem.list();
   106 		for (String jménoAtributu : jménaAtributů) {
   107 			ByteBuffer hodnotaAtributu = ByteBuffer.allocate(souborovySystem.size(jménoAtributu));
   108 			souborovySystem.read(jménoAtributu, hodnotaAtributu);
   109 			atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
   110 		}
   111 		upozorniPosluchače();
   112 	}
   113 
   114 	private void upozorniPosluchače() {
   115 		for (TableModelListener p : posluchače) {
   116 			p.tableChanged(new TableModelEvent(this));
   117 		}
   118 	}
   119 }