java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Model.java
author František Kučera <franta-hg@frantovo.cz>
Wed Dec 15 20:07:14 2010 +0100 (2010-12-15)
changeset 6 734f104f2869
child 9 a2e91b20198b
permissions -rw-r--r--
První GUI.
     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.charset.Charset;
     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.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 pohled;
    27 	private ArrayList<Atribut> atributy = new ArrayList<Atribut>();
    28 
    29 	public Model(File soubor) throws IOException {
    30 		Path cesta = soubor.toPath();
    31 		pohled = 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 		if (n == 0) {
    67 			atributy.get(m).setKlic(String.valueOf(value.toString()));
    68 			/** TODO: uložit na souborový systém */
    69 		} else if (n == 1) {
    70 			atributy.get(m).setHodnota(String.valueOf(value.toString()));
    71 			/** TODO: uložit na souborový systém */
    72 		}
    73 	}
    74 
    75 	public void addTableModelListener(TableModelListener l) {
    76 		posluchače.add(l);
    77 	}
    78 
    79 	public void removeTableModelListener(TableModelListener l) {
    80 		posluchače.remove(l);
    81 	}
    82 
    83 	public void přidejŘádek() {
    84 		//atributy.add(new Atribut());
    85 		//upozorniPosluchače();
    86 	}
    87 
    88 	public void odeberŘádek(int m) {
    89 		//atributy.remove(m);
    90 		//upozorniPosluchače();
    91 	}
    92 
    93 	private void načtiAtributy() throws IOException {
    94 		List<String> jménaAtributů = pohled.list();
    95 		for (String jménoAtributu : jménaAtributů) {
    96 			ByteBuffer bajty = ByteBuffer.allocate(pohled.size(jménoAtributu));
    97 			pohled.read(jménoAtributu, bajty);
    98 			String hodnotaAtributu = dekóduj(bajty);
    99 			atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
   100 		}
   101 	}
   102 
   103 	private static String dekóduj(ByteBuffer bajty) {
   104 		bajty.flip();
   105 		return Charset.defaultCharset().decode(bajty).toString();
   106 	}
   107 
   108 	private void upozorniPosluchače() {
   109 		for (TableModelListener p : posluchače) {
   110 			p.tableChanged(new TableModelEvent(this));
   111 		}
   112 	}
   113 }