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