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