1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Model.java Wed Dec 15 20:07:14 2010 +0100
1.3 @@ -0,0 +1,113 @@
1.4 +package cz.frantovo.rozsireneAtributy;
1.5 +
1.6 +import java.io.File;
1.7 +import java.io.IOException;
1.8 +import java.nio.ByteBuffer;
1.9 +import java.nio.charset.Charset;
1.10 +import java.nio.file.Path;
1.11 +import java.nio.file.attribute.UserDefinedFileAttributeView;
1.12 +import java.util.ArrayList;
1.13 +import java.util.HashSet;
1.14 +import java.util.List;
1.15 +import java.util.logging.Logger;
1.16 +import javax.swing.event.TableModelEvent;
1.17 +import javax.swing.event.TableModelListener;
1.18 +import javax.swing.table.TableModel;
1.19 +
1.20 +/**
1.21 + *
1.22 + * @author fiki
1.23 + */
1.24 +public class Model implements TableModel {
1.25 +
1.26 + private static final Logger log = Logger.getLogger(Model.class.getSimpleName());
1.27 + private String[] sloupečky = {"Název", "Hodnota"};
1.28 + private HashSet<TableModelListener> posluchače = new HashSet<TableModelListener>();
1.29 + private UserDefinedFileAttributeView pohled;
1.30 + private ArrayList<Atribut> atributy = new ArrayList<Atribut>();
1.31 +
1.32 + public Model(File soubor) throws IOException {
1.33 + Path cesta = soubor.toPath();
1.34 + pohled = cesta.getFileAttributeView(UserDefinedFileAttributeView.class);
1.35 + načtiAtributy();
1.36 + }
1.37 +
1.38 + public int getRowCount() {
1.39 + return atributy.size();
1.40 + }
1.41 +
1.42 + public int getColumnCount() {
1.43 + return sloupečky.length;
1.44 + }
1.45 +
1.46 + public String getColumnName(int n) {
1.47 + return sloupečky[n];
1.48 + }
1.49 +
1.50 + public Class<?> getColumnClass(int n) {
1.51 + return String.class;
1.52 + }
1.53 +
1.54 + public boolean isCellEditable(int m, int n) {
1.55 + return true;
1.56 + }
1.57 +
1.58 + public Object getValueAt(int m, int n) {
1.59 + if (n == 0) {
1.60 + return atributy.get(m).getKlic();
1.61 + } else if (n == 1) {
1.62 + return atributy.get(m).getHodnota();
1.63 + } else {
1.64 + return null;
1.65 + }
1.66 + }
1.67 +
1.68 + public void setValueAt(Object value, int m, int n) {
1.69 + if (n == 0) {
1.70 + atributy.get(m).setKlic(String.valueOf(value.toString()));
1.71 + /** TODO: uložit na souborový systém */
1.72 + } else if (n == 1) {
1.73 + atributy.get(m).setHodnota(String.valueOf(value.toString()));
1.74 + /** TODO: uložit na souborový systém */
1.75 + }
1.76 + }
1.77 +
1.78 + public void addTableModelListener(TableModelListener l) {
1.79 + posluchače.add(l);
1.80 + }
1.81 +
1.82 + public void removeTableModelListener(TableModelListener l) {
1.83 + posluchače.remove(l);
1.84 + }
1.85 +
1.86 + public void přidejŘádek() {
1.87 + //atributy.add(new Atribut());
1.88 + //upozorniPosluchače();
1.89 + }
1.90 +
1.91 + public void odeberŘádek(int m) {
1.92 + //atributy.remove(m);
1.93 + //upozorniPosluchače();
1.94 + }
1.95 +
1.96 + private void načtiAtributy() throws IOException {
1.97 + List<String> jménaAtributů = pohled.list();
1.98 + for (String jménoAtributu : jménaAtributů) {
1.99 + ByteBuffer bajty = ByteBuffer.allocate(pohled.size(jménoAtributu));
1.100 + pohled.read(jménoAtributu, bajty);
1.101 + String hodnotaAtributu = dekóduj(bajty);
1.102 + atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
1.103 + }
1.104 + }
1.105 +
1.106 + private static String dekóduj(ByteBuffer bajty) {
1.107 + bajty.flip();
1.108 + return Charset.defaultCharset().decode(bajty).toString();
1.109 + }
1.110 +
1.111 + private void upozorniPosluchače() {
1.112 + for (TableModelListener p : posluchače) {
1.113 + p.tableChanged(new TableModelEvent(this));
1.114 + }
1.115 + }
1.116 +}