java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Model.java
author František Kučera <franta-hg@frantovo.cz>
Sun Oct 07 15:11:42 2012 +0200 (2012-10-07)
changeset 22 bf06eb899671
parent 19 c20edbed09c3
child 27 36cee2c8f5f8
permissions -rw-r--r--
nabídka standardizovaných atributů
http://www.freedesktop.org/wiki/CommonExtendedAttributes
     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.Set;
    32 import java.util.logging.Level;
    33 import java.util.logging.Logger;
    34 import javax.swing.event.TableModelEvent;
    35 import javax.swing.event.TableModelListener;
    36 import javax.swing.table.TableModel;
    37 
    38 /**
    39  *
    40  * @author fiki
    41  */
    42 public class Model implements TableModel {
    43 
    44 	private static final Logger log = Logger.getLogger(Model.class.getSimpleName());
    45 	private static final ResourceBundle překlady = ResourceBundle.getBundle("cz.frantovo.rozsireneAtributy.Překlady");
    46 	private String[] sloupečky = {překlady.getString("tabulka.název"), překlady.getString("tabulka.hodnota")};
    47 	private Set<TableModelListener> posluchače = new HashSet<>();
    48 	private UserDefinedFileAttributeView souborovýSystém;
    49 	private List<Atribut> atributy = new ArrayList<>();
    50 
    51 	public Model(File soubor) throws IOException {
    52 		Path cesta = soubor.toPath();
    53 		FileSystemProvider posyktovatelFS = cesta.getFileSystem().provider();
    54 		souborovýSystém = posyktovatelFS.getFileAttributeView(cesta, UserDefinedFileAttributeView.class);
    55 		načtiAtributy();
    56 	}
    57 
    58 	@Override
    59 	public int getRowCount() {
    60 		return atributy.size();
    61 	}
    62 
    63 	@Override
    64 	public int getColumnCount() {
    65 		return sloupečky.length;
    66 	}
    67 
    68 	@Override
    69 	public String getColumnName(int n) {
    70 		return sloupečky[n];
    71 	}
    72 
    73 	@Override
    74 	public Class<?> getColumnClass(int n) {
    75 		return String.class;
    76 	}
    77 
    78 	@Override
    79 	public boolean isCellEditable(int m, int n) {
    80 		return true;
    81 	}
    82 
    83 	@Override
    84 	public Object getValueAt(int m, int n) {
    85 		if (n == 0) {
    86 			return atributy.get(m).getKlíč();
    87 		} else if (n == 1) {
    88 			return atributy.get(m).getHodnota();
    89 		} else {
    90 			return null;
    91 		}
    92 	}
    93 
    94 	@Override
    95 	public void setValueAt(Object hodnota, int m, int n) {
    96 		Atribut a = atributy.get(m);
    97 		try {
    98 			if (n == 0) {
    99 				/** Měníme klíč – název atributu */
   100 				String novýKlíč = String.valueOf(hodnota);
   101 				if (!novýKlíč.equals(a.getKlíč())) {
   102 					if (a.isPlatnýKlíč()) {
   103 						souborovýSystém.delete(a.getKlíč());
   104 					}
   105 					a.setKlíč(novýKlíč);
   106 					if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
   107 						souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
   108 					}
   109 				}
   110 			} else if (n == 1) {
   111 				/** Měníme hodnotu atributu */
   112 				a.setHodnota(String.valueOf(hodnota));
   113 				if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
   114 					souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
   115 				}
   116 			}
   117 		} catch (IOException e) {
   118 			log.log(Level.SEVERE, "Selhalo ukládání atributu na souborový systém", e);
   119 		}
   120 	}
   121 
   122 	@Override
   123 	public void addTableModelListener(TableModelListener l) {
   124 		posluchače.add(l);
   125 	}
   126 
   127 	@Override
   128 	public void removeTableModelListener(TableModelListener l) {
   129 		posluchače.remove(l);
   130 	}
   131 
   132 	/**
   133 	 * @param m číslo řádku
   134 	 * @return atribut, který se na něm nachází
   135 	 */
   136 	public Atribut getAtribut(int m) {
   137 		return atributy.get(m);
   138 	}
   139 
   140 	public void přidejAtribut(Atribut a) {
   141 		atributy.add(a);
   142 		upozorniPosluchače();
   143 	}
   144 
   145 	public void odeberAtribut(Atribut a) throws IOException {
   146 		atributy.remove(a);
   147 		if (a.isPlatnýKlíč()) {
   148 			souborovýSystém.delete(a.getKlíč());
   149 		}
   150 		upozorniPosluchače();
   151 	}
   152 
   153 	public final void načtiAtributy() throws IOException {
   154 		List<String> jménaAtributů = souborovýSystém.list();
   155 		atributy.clear();
   156 		for (String jménoAtributu : jménaAtributů) {
   157 			ByteBuffer hodnotaAtributu = ByteBuffer.allocate(souborovýSystém.size(jménoAtributu));
   158 			souborovýSystém.read(jménoAtributu, hodnotaAtributu);
   159 			atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
   160 		}
   161 		upozorniPosluchače();
   162 	}
   163 
   164 	private void upozorniPosluchače() {
   165 		for (TableModelListener p : posluchače) {
   166 			p.tableChanged(new TableModelEvent(this));
   167 		}
   168 	}
   169 }