První GUI.
1 package cz.frantovo.rozsireneAtributy;
4 import java.io.IOException;
5 import java.nio.ByteBuffer;
6 import java.nio.charset.Charset;
7 import java.nio.file.Path;
8 import java.nio.file.attribute.UserDefinedFileAttributeView;
9 import java.util.ArrayList;
10 import java.util.HashSet;
11 import java.util.List;
12 import java.util.logging.Logger;
13 import javax.swing.event.TableModelEvent;
14 import javax.swing.event.TableModelListener;
15 import javax.swing.table.TableModel;
21 public class Model implements TableModel {
23 private static final Logger log = Logger.getLogger(Model.class.getSimpleName());
24 private String[] sloupečky = {"Název", "Hodnota"};
25 private HashSet<TableModelListener> posluchače = new HashSet<TableModelListener>();
26 private UserDefinedFileAttributeView pohled;
27 private ArrayList<Atribut> atributy = new ArrayList<Atribut>();
29 public Model(File soubor) throws IOException {
30 Path cesta = soubor.toPath();
31 pohled = cesta.getFileAttributeView(UserDefinedFileAttributeView.class);
35 public int getRowCount() {
36 return atributy.size();
39 public int getColumnCount() {
40 return sloupečky.length;
43 public String getColumnName(int n) {
47 public Class<?> getColumnClass(int n) {
51 public boolean isCellEditable(int m, int n) {
55 public Object getValueAt(int m, int n) {
57 return atributy.get(m).getKlic();
59 return atributy.get(m).getHodnota();
65 public void setValueAt(Object value, int m, int n) {
67 atributy.get(m).setKlic(String.valueOf(value.toString()));
68 /** TODO: uložit na souborový systém */
70 atributy.get(m).setHodnota(String.valueOf(value.toString()));
71 /** TODO: uložit na souborový systém */
75 public void addTableModelListener(TableModelListener l) {
79 public void removeTableModelListener(TableModelListener l) {
83 public void přidejŘádek() {
84 //atributy.add(new Atribut());
85 //upozorniPosluchače();
88 public void odeberŘádek(int m) {
90 //upozorniPosluchače();
93 private void načtiAtributy() throws IOException {
94 List<String> jménaAtributů = pohled.list();
95 for (String jménoAtributu : jménaAtributů) {
96 ByteBuffer bajty = ByteBuffer.allocate(pohled.size(jménoAtributu));
97 pohled.read(jménoAtributu, bajty);
98 String hodnotaAtributu = dekóduj(bajty);
99 atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
103 private static String dekóduj(ByteBuffer bajty) {
105 return Charset.defaultCharset().decode(bajty).toString();
108 private void upozorniPosluchače() {
109 for (TableModelListener p : posluchače) {
110 p.tableChanged(new TableModelEvent(this));