java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Model.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 11 Dec 2023 00:10:33 +0100
branchv_0
changeset 27 dc5786f3482b
parent 22 f3ba21170329
permissions -rw-r--r--
oprava verze licence / fix license version: GNU GPLv3
     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.
     8  * 
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  * 
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 package cz.frantovo.rozsireneAtributy.gui;
    18 
    19 import cz.frantovo.rozsireneAtributy.Atribut;
    20 import java.io.File;
    21 import java.io.IOException;
    22 import java.nio.ByteBuffer;
    23 import java.nio.file.Path;
    24 import java.nio.file.attribute.UserDefinedFileAttributeView;
    25 import java.nio.file.spi.FileSystemProvider;
    26 import java.util.ArrayList;
    27 import java.util.HashSet;
    28 import java.util.List;
    29 import java.util.ResourceBundle;
    30 import java.util.Set;
    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 Set<TableModelListener> posluchače = new HashSet<>();
    47 	private UserDefinedFileAttributeView souborovýSystém;
    48 	private List<Atribut> atributy = new ArrayList<>();
    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 	@Override
    58 	public int getRowCount() {
    59 		return atributy.size();
    60 	}
    61 
    62 	@Override
    63 	public int getColumnCount() {
    64 		return sloupečky.length;
    65 	}
    66 
    67 	@Override
    68 	public String getColumnName(int n) {
    69 		return sloupečky[n];
    70 	}
    71 
    72 	@Override
    73 	public Class<?> getColumnClass(int n) {
    74 		return String.class;
    75 	}
    76 
    77 	@Override
    78 	public boolean isCellEditable(int m, int n) {
    79 		return true;
    80 	}
    81 
    82 	@Override
    83 	public Object getValueAt(int m, int n) {
    84 		if (n == 0) {
    85 			return atributy.get(m).getKlíč();
    86 		} else if (n == 1) {
    87 			return atributy.get(m).getHodnota();
    88 		} else {
    89 			return null;
    90 		}
    91 	}
    92 
    93 	@Override
    94 	public void setValueAt(Object hodnota, int m, int n) {
    95 		Atribut a = atributy.get(m);
    96 		try {
    97 			if (n == 0) {
    98 				/** Měníme klíč – název atributu */
    99 				String novýKlíč = String.valueOf(hodnota);
   100 				if (!novýKlíč.equals(a.getKlíč())) {
   101 					if (a.isPlatnýKlíč()) {
   102 						souborovýSystém.delete(a.getKlíč());
   103 					}
   104 					a.setKlíč(novýKlíč);
   105 					if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
   106 						souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
   107 					}
   108 				}
   109 			} else if (n == 1) {
   110 				/** Měníme hodnotu atributu */
   111 				a.setHodnota(String.valueOf(hodnota));
   112 				if (a.isPlatnýKlíč() && a.isPlatnáHodnota()) {
   113 					souborovýSystém.write(a.getKlíč(), a.getHodnotaBajty());
   114 				}
   115 			}
   116 		} catch (IOException e) {
   117 			log.log(Level.SEVERE, "Selhalo ukládání atributu na souborový systém", e);
   118 		}
   119 	}
   120 
   121 	@Override
   122 	public void addTableModelListener(TableModelListener l) {
   123 		posluchače.add(l);
   124 	}
   125 
   126 	@Override
   127 	public void removeTableModelListener(TableModelListener l) {
   128 		posluchače.remove(l);
   129 	}
   130 
   131 	/**
   132 	 * @param m číslo řádku
   133 	 * @return atribut, který se na něm nachází
   134 	 */
   135 	public Atribut getAtribut(int m) {
   136 		return atributy.get(m);
   137 	}
   138 
   139 	public void přidejAtribut(Atribut a) {
   140 		atributy.add(a);
   141 		upozorniPosluchače();
   142 	}
   143 
   144 	public void odeberAtribut(Atribut a) throws IOException {
   145 		atributy.remove(a);
   146 		if (a.isPlatnýKlíč()) {
   147 			souborovýSystém.delete(a.getKlíč());
   148 		}
   149 		upozorniPosluchače();
   150 	}
   151 
   152 	public final void načtiAtributy() throws IOException {
   153 		List<String> jménaAtributů = souborovýSystém.list();
   154 		atributy.clear();
   155 		for (String jménoAtributu : jménaAtributů) {
   156 			ByteBuffer hodnotaAtributu = ByteBuffer.allocate(souborovýSystém.size(jménoAtributu));
   157 			souborovýSystém.read(jménoAtributu, hodnotaAtributu);
   158 			atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
   159 		}
   160 		upozorniPosluchače();
   161 	}
   162 
   163 	private void upozorniPosluchače() {
   164 		for (TableModelListener p : posluchače) {
   165 			p.tableChanged(new TableModelEvent(this));
   166 		}
   167 	}
   168 }