java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Model.java
author František Kučera <franta-hg@frantovo.cz>
Sat Aug 18 13:27:00 2012 +0200 (2012-08-18)
changeset 19 c20edbed09c3
parent 17 75b7d41b7428
child 22 bf06eb899671
permissions -rw-r--r--
informace o licenci uvnitř .java souborů
     1 /**
     2  * Rozšířené atributy – program na správu rozšířených atributů souborů
     3  * Copyright © 2012 František Kučera (frantovo.cz)
     4  * 
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  * 
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  * GNU General Public License for more details.
    14  * 
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.rozsireneAtributy.gui;
    19 
    20 import cz.frantovo.rozsireneAtributy.Atribut;
    21 import java.io.File;
    22 import java.io.IOException;
    23 import java.nio.ByteBuffer;
    24 import java.nio.file.Path;
    25 import java.nio.file.attribute.UserDefinedFileAttributeView;
    26 import java.nio.file.spi.FileSystemProvider;
    27 import java.util.ArrayList;
    28 import java.util.HashSet;
    29 import java.util.List;
    30 import java.util.ResourceBundle;
    31 import java.util.logging.Level;
    32 import java.util.logging.Logger;
    33 import javax.swing.event.TableModelEvent;
    34 import javax.swing.event.TableModelListener;
    35 import javax.swing.table.TableModel;
    36 
    37 /**
    38  *
    39  * @author fiki
    40  */
    41 public class Model implements TableModel {
    42 
    43 	private static final Logger log = Logger.getLogger(Model.class.getSimpleName());
    44 	private static final ResourceBundle překlady = ResourceBundle.getBundle("cz.frantovo.rozsireneAtributy.Překlady");
    45 	private String[] sloupečky = {překlady.getString("tabulka.název"), překlady.getString("tabulka.hodnota")};
    46 	private HashSet<TableModelListener> posluchače = new HashSet<TableModelListener>();
    47 	private UserDefinedFileAttributeView souborovýSystém;
    48 	private ArrayList<Atribut> atributy = new ArrayList<Atribut>();
    49 
    50 	public Model(File soubor) throws IOException {
    51 		Path cesta = soubor.toPath();
    52 		FileSystemProvider posyktovatelFS = cesta.getFileSystem().provider();
    53 		souborovýSystém = posyktovatelFS.getFileAttributeView(cesta, UserDefinedFileAttributeView.class);
    54 		načtiAtributy();
    55 	}
    56 
    57 	public int getRowCount() {
    58 		return atributy.size();
    59 	}
    60 
    61 	public int getColumnCount() {
    62 		return sloupečky.length;
    63 	}
    64 
    65 	public String getColumnName(int n) {
    66 		return sloupečky[n];
    67 	}
    68 
    69 	public Class<?> getColumnClass(int n) {
    70 		return String.class;
    71 	}
    72 
    73 	public boolean isCellEditable(int m, int n) {
    74 		return true;
    75 	}
    76 
    77 	public Object getValueAt(int m, int n) {
    78 		if (n == 0) {
    79 			return atributy.get(m).getKlíč();
    80 		} else if (n == 1) {
    81 			return atributy.get(m).getHodnota();
    82 		} else {
    83 			return null;
    84 		}
    85 	}
    86 
    87 	public void setValueAt(Object hodnota, int m, int n) {
    88 		Atribut a = atributy.get(m);
    89 		try {
    90 			if (n == 0) {
    91 				/** Měníme klíč – název atributu */
    92 				String novýKlíč = String.valueOf(hodnota);
    93 				if (!novýKlíč.equals(a.getKlíč())) {
    94 					if (a.isPlatnýKlíč()) {
    95 						souborovýSystém.delete(a.getKlíč());
    96 					}
    97 					a.setKlíč(novýKlíč);
    98 					if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
    99 						souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
   100 					}
   101 				}
   102 			} else if (n == 1) {
   103 				/** Měníme hodnotu atributu */
   104 				a.setHodnota(String.valueOf(hodnota));
   105 				if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
   106 					souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
   107 				}
   108 			}
   109 		} catch (IOException e) {
   110 			log.log(Level.SEVERE, "Selhalo ukládání atributu na souborový systém", e);
   111 		}
   112 	}
   113 
   114 	public void addTableModelListener(TableModelListener l) {
   115 		posluchače.add(l);
   116 	}
   117 
   118 	public void removeTableModelListener(TableModelListener l) {
   119 		posluchače.remove(l);
   120 	}
   121 
   122 	/**
   123 	 * @param m číslo řádku
   124 	 * @return atribut, který se na něm nachází
   125 	 */
   126 	public Atribut getAtribut(int m) {
   127 		return atributy.get(m);
   128 	}
   129 
   130 	public void přidejAtribut(Atribut a) {
   131 		atributy.add(a);
   132 		upozorniPosluchače();
   133 	}
   134 
   135 	public void odeberAtribut(Atribut a) throws IOException {
   136 		atributy.remove(a);
   137 		if (a.isPlatnýKlíč()) {
   138 			souborovýSystém.delete(a.getKlíč());
   139 		}
   140 		upozorniPosluchače();
   141 	}
   142 
   143 	public final void načtiAtributy() throws IOException {
   144 		List<String> jménaAtributů = souborovýSystém.list();
   145 		atributy.clear();
   146 		for (String jménoAtributu : jménaAtributů) {
   147 			ByteBuffer hodnotaAtributu = ByteBuffer.allocate(souborovýSystém.size(jménoAtributu));
   148 			souborovýSystém.read(jménoAtributu, hodnotaAtributu);
   149 			atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
   150 		}
   151 		upozorniPosluchače();
   152 	}
   153 
   154 	private void upozorniPosluchače() {
   155 		for (TableModelListener p : posluchače) {
   156 			p.tableChanged(new TableModelEvent(this));
   157 		}
   158 	}
   159 }