1.1 --- a/java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Model.java Thu Dec 16 00:51:22 2010 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,137 +0,0 @@
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.file.Path;
1.10 -import java.nio.file.attribute.UserDefinedFileAttributeView;
1.11 -import java.util.ArrayList;
1.12 -import java.util.HashSet;
1.13 -import java.util.List;
1.14 -import java.util.logging.Level;
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 souborovýSystém;
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 - souborovýSystém = 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).getKlíč();
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 - Atribut a = atributy.get(m);
1.70 - try {
1.71 - if (n == 0) {
1.72 - /** Měníme klíč – název atributu */
1.73 - String novýKlíč = String.valueOf(value.toString());
1.74 - if (!novýKlíč.equals(a.getKlíč())) {
1.75 - if (a.isPlatnýKlíč()) {
1.76 - souborovýSystém.delete(a.getKlíč());
1.77 - }
1.78 - a.setKlíč(novýKlíč);
1.79 - if (a.isPlatnáHodnota()) {
1.80 - souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
1.81 - }
1.82 - }
1.83 - } else if (n == 1) {
1.84 - /** Měníme hodnotu atributu */
1.85 - a.setHodnota(String.valueOf(value.toString()));
1.86 - if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
1.87 - souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
1.88 - }
1.89 - }
1.90 - } catch (IOException e) {
1.91 - log.log(Level.SEVERE, "Selhalo ukládání atributu na souborový systém", e);
1.92 - }
1.93 - }
1.94 -
1.95 - public void addTableModelListener(TableModelListener l) {
1.96 - posluchače.add(l);
1.97 - }
1.98 -
1.99 - public void removeTableModelListener(TableModelListener l) {
1.100 - posluchače.remove(l);
1.101 - }
1.102 -
1.103 - /**
1.104 - * @param m číslo řádku
1.105 - * @return atribut, který se na něm nachází
1.106 - */
1.107 - public Atribut getAtribut(int m) {
1.108 - return atributy.get(m);
1.109 - }
1.110 -
1.111 - public void přidejAtribut(Atribut a) {
1.112 - atributy.add(a);
1.113 - upozorniPosluchače();
1.114 - }
1.115 -
1.116 - public void odeberAtribut(Atribut a) throws IOException {
1.117 - atributy.remove(a);
1.118 - if (a.isPlatnýKlíč()) {
1.119 - souborovýSystém.delete(a.getKlíč());
1.120 - }
1.121 - upozorniPosluchače();
1.122 - }
1.123 -
1.124 - public final void načtiAtributy() throws IOException {
1.125 - List<String> jménaAtributů = souborovýSystém.list();
1.126 - atributy.clear();
1.127 - for (String jménoAtributu : jménaAtributů) {
1.128 - ByteBuffer hodnotaAtributu = ByteBuffer.allocate(souborovýSystém.size(jménoAtributu));
1.129 - souborovýSystém.read(jménoAtributu, hodnotaAtributu);
1.130 - atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
1.131 - }
1.132 - upozorniPosluchače();
1.133 - }
1.134 -
1.135 - private void upozorniPosluchače() {
1.136 - for (TableModelListener p : posluchače) {
1.137 - p.tableChanged(new TableModelEvent(this));
1.138 - }
1.139 - }
1.140 -}