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