java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Model.java
author František Kučera <franta-hg@frantovo.cz>
Wed Dec 15 23:58:34 2010 +0100 (2010-12-15)
changeset 11 9b399cde6a3b
parent 10 ed2b6ebf138d
child 12 a2b41c8ee039
permissions -rw-r--r--
Klávesové zkratky, kontroly, čeština.
     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 				souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
    84 			}
    85 		} catch (IOException e) {
    86 			log.log(Level.SEVERE, "Selhalo ukládání atributu na souborový systém", e);
    87 		}
    88 	}
    89 
    90 	public void addTableModelListener(TableModelListener l) {
    91 		posluchače.add(l);
    92 	}
    93 
    94 	public void removeTableModelListener(TableModelListener l) {
    95 		posluchače.remove(l);
    96 	}
    97 
    98 	/**
    99 	 * @param m číslo řádku
   100 	 * @return atribut, který se na něm nachází
   101 	 */
   102 	public Atribut getAtribut(int m) {
   103 		return atributy.get(m);
   104 	}
   105 
   106 	public void přidejAtribut(Atribut a) {
   107 		atributy.add(a);
   108 		upozorniPosluchače();
   109 	}
   110 
   111 	public void odeberAtribut(Atribut a) throws IOException {
   112 		atributy.remove(a);
   113 		if (a.isPlatnýKlíč()) {
   114 			souborovýSystém.delete(a.getKlíč());
   115 		}
   116 		upozorniPosluchače();
   117 	}
   118 
   119 	public final void načtiAtributy() throws IOException {
   120 		List<String> jménaAtributů = souborovýSystém.list();
   121 		atributy.clear();
   122 		for (String jménoAtributu : jménaAtributů) {
   123 			ByteBuffer hodnotaAtributu = ByteBuffer.allocate(souborovýSystém.size(jménoAtributu));
   124 			souborovýSystém.read(jménoAtributu, hodnotaAtributu);
   125 			atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
   126 		}
   127 		upozorniPosluchače();
   128 	}
   129 
   130 	private void upozorniPosluchače() {
   131 		for (TableModelListener p : posluchače) {
   132 			p.tableChanged(new TableModelEvent(this));
   133 		}
   134 	}
   135 }