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