První GUI. v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 15 Dec 2010 20:07:14 +0100
branchv_0
changeset 62b91eda684c9
parent 5 fc93dae7a366
child 7 d783f6fa1f25
První GUI.
java/rozsirene-atributy/nbproject/project.properties
java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Atribut.java
java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Model.java
java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/PokusnýVýpis.java
java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Startér.java
java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Panel.form
java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Panel.java
     1.1 --- a/java/rozsirene-atributy/nbproject/project.properties	Wed Dec 15 19:18:08 2010 +0100
     1.2 +++ b/java/rozsirene-atributy/nbproject/project.properties	Wed Dec 15 20:07:14 2010 +0100
     1.3 @@ -63,7 +63,7 @@
     1.4  jnlp.mixed.code=defaut
     1.5  jnlp.offline-allowed=false
     1.6  jnlp.signed=false
     1.7 -main.class=cz.frantovo.rozsireneAtributy.Pokusn\u00fdV\u00fdpis
     1.8 +main.class=cz.frantovo.rozsireneAtributy.Start\u00e9r
     1.9  manifest.file=manifest.mf
    1.10  meta.inf.dir=${src.dir}/META-INF
    1.11  platform.active=default_platform
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Atribut.java	Wed Dec 15 20:07:14 2010 +0100
     2.3 @@ -0,0 +1,28 @@
     2.4 +package cz.frantovo.rozsireneAtributy;
     2.5 +
     2.6 +public class Atribut {
     2.7 +
     2.8 +	private String klic;
     2.9 +	private String hodnota;
    2.10 +
    2.11 +	public Atribut(String klic, String hodnota) {
    2.12 +		this.klic = klic;
    2.13 +		this.hodnota = hodnota;
    2.14 +	}
    2.15 +
    2.16 +	public String getKlic() {
    2.17 +		return klic;
    2.18 +	}
    2.19 +
    2.20 +	public void setKlic(String klic) {
    2.21 +		this.klic = klic;
    2.22 +	}
    2.23 +
    2.24 +	public String getHodnota() {
    2.25 +		return hodnota;
    2.26 +	}
    2.27 +
    2.28 +	public void setHodnota(String hodnota) {
    2.29 +		this.hodnota = hodnota;
    2.30 +	}
    2.31 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Model.java	Wed Dec 15 20:07:14 2010 +0100
     3.3 @@ -0,0 +1,113 @@
     3.4 +package cz.frantovo.rozsireneAtributy;
     3.5 +
     3.6 +import java.io.File;
     3.7 +import java.io.IOException;
     3.8 +import java.nio.ByteBuffer;
     3.9 +import java.nio.charset.Charset;
    3.10 +import java.nio.file.Path;
    3.11 +import java.nio.file.attribute.UserDefinedFileAttributeView;
    3.12 +import java.util.ArrayList;
    3.13 +import java.util.HashSet;
    3.14 +import java.util.List;
    3.15 +import java.util.logging.Logger;
    3.16 +import javax.swing.event.TableModelEvent;
    3.17 +import javax.swing.event.TableModelListener;
    3.18 +import javax.swing.table.TableModel;
    3.19 +
    3.20 +/**
    3.21 + *
    3.22 + * @author fiki
    3.23 + */
    3.24 +public class Model implements TableModel {
    3.25 +
    3.26 +	private static final Logger log = Logger.getLogger(Model.class.getSimpleName());
    3.27 +	private String[] sloupečky = {"Název", "Hodnota"};
    3.28 +	private HashSet<TableModelListener> posluchače = new HashSet<TableModelListener>();
    3.29 +	private UserDefinedFileAttributeView pohled;
    3.30 +	private ArrayList<Atribut> atributy = new ArrayList<Atribut>();
    3.31 +
    3.32 +	public Model(File soubor) throws IOException {
    3.33 +		Path cesta = soubor.toPath();
    3.34 +		pohled = cesta.getFileAttributeView(UserDefinedFileAttributeView.class);
    3.35 +		načtiAtributy();
    3.36 +	}
    3.37 +
    3.38 +	public int getRowCount() {
    3.39 +		return atributy.size();
    3.40 +	}
    3.41 +
    3.42 +	public int getColumnCount() {
    3.43 +		return sloupečky.length;
    3.44 +	}
    3.45 +
    3.46 +	public String getColumnName(int n) {
    3.47 +		return sloupečky[n];
    3.48 +	}
    3.49 +
    3.50 +	public Class<?> getColumnClass(int n) {
    3.51 +		return String.class;
    3.52 +	}
    3.53 +
    3.54 +	public boolean isCellEditable(int m, int n) {
    3.55 +		return true;
    3.56 +	}
    3.57 +
    3.58 +	public Object getValueAt(int m, int n) {
    3.59 +		if (n == 0) {
    3.60 +			return atributy.get(m).getKlic();
    3.61 +		} else if (n == 1) {
    3.62 +			return atributy.get(m).getHodnota();
    3.63 +		} else {
    3.64 +			return null;
    3.65 +		}
    3.66 +	}
    3.67 +
    3.68 +	public void setValueAt(Object value, int m, int n) {
    3.69 +		if (n == 0) {
    3.70 +			atributy.get(m).setKlic(String.valueOf(value.toString()));
    3.71 +			/** TODO: uložit na souborový systém */
    3.72 +		} else if (n == 1) {
    3.73 +			atributy.get(m).setHodnota(String.valueOf(value.toString()));
    3.74 +			/** TODO: uložit na souborový systém */
    3.75 +		}
    3.76 +	}
    3.77 +
    3.78 +	public void addTableModelListener(TableModelListener l) {
    3.79 +		posluchače.add(l);
    3.80 +	}
    3.81 +
    3.82 +	public void removeTableModelListener(TableModelListener l) {
    3.83 +		posluchače.remove(l);
    3.84 +	}
    3.85 +
    3.86 +	public void přidejŘádek() {
    3.87 +		//atributy.add(new Atribut());
    3.88 +		//upozorniPosluchače();
    3.89 +	}
    3.90 +
    3.91 +	public void odeberŘádek(int m) {
    3.92 +		//atributy.remove(m);
    3.93 +		//upozorniPosluchače();
    3.94 +	}
    3.95 +
    3.96 +	private void načtiAtributy() throws IOException {
    3.97 +		List<String> jménaAtributů = pohled.list();
    3.98 +		for (String jménoAtributu : jménaAtributů) {
    3.99 +			ByteBuffer bajty = ByteBuffer.allocate(pohled.size(jménoAtributu));
   3.100 +			pohled.read(jménoAtributu, bajty);
   3.101 +			String hodnotaAtributu = dekóduj(bajty);
   3.102 +			atributy.add(new Atribut(jménoAtributu, hodnotaAtributu));
   3.103 +		}
   3.104 +	}
   3.105 +
   3.106 +	private static String dekóduj(ByteBuffer bajty) {
   3.107 +		bajty.flip();
   3.108 +		return Charset.defaultCharset().decode(bajty).toString();
   3.109 +	}
   3.110 +
   3.111 +	private void upozorniPosluchače() {
   3.112 +		for (TableModelListener p : posluchače) {
   3.113 +			p.tableChanged(new TableModelEvent(this));
   3.114 +		}
   3.115 +	}
   3.116 +}
     4.1 --- a/java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/PokusnýVýpis.java	Wed Dec 15 19:18:08 2010 +0100
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,44 +0,0 @@
     4.4 -package cz.frantovo.rozsireneAtributy;
     4.5 -
     4.6 -import java.io.File;
     4.7 -import java.io.IOException;
     4.8 -import java.nio.ByteBuffer;
     4.9 -import java.nio.charset.Charset;
    4.10 -import java.nio.file.Path;
    4.11 -import java.nio.file.attribute.UserDefinedFileAttributeView;
    4.12 -import java.util.List;
    4.13 -
    4.14 -/**
    4.15 - * http://freedesktop.org/wiki/CommonExtendedAttributes
    4.16 - * http://download.oracle.com/javase/tutorial/essential/io/fileAttr.html#user
    4.17 - * http://today.java.net/pub/a/today/2008/07/03/jsr-203-new-file-apis.html#so-what-is-a-path-really
    4.18 - *
    4.19 - * $ setfattr -n user.fiki.pozdrav -v 'Dobrý den!' pokus.txt
    4.20 - *
    4.21 - * @author fiki
    4.22 - */
    4.23 -public class PokusnýVýpis {
    4.24 -
    4.25 -	/**
    4.26 -	 * Vypíše rozšířené atributy souboru.
    4.27 -	 * @param args pole s jedním prvkem – názvem souboru
    4.28 -	 * @throws IOException
    4.29 -	 */
    4.30 -	public static void main(String[] args) throws IOException {
    4.31 -		File soubor = new File(args[0]);
    4.32 -		Path cesta = soubor.toPath();
    4.33 -		UserDefinedFileAttributeView pohled = cesta.getFileAttributeView(UserDefinedFileAttributeView.class);
    4.34 -		List<String> jménaAtributů = pohled.list();
    4.35 -		for (String jménoAtributu : jménaAtributů) {
    4.36 -			ByteBuffer bajty = ByteBuffer.allocate(pohled.size(jménoAtributu));
    4.37 -			pohled.read(jménoAtributu, bajty);
    4.38 -			String hodnotaAtributu = dekóduj(bajty);
    4.39 -			System.out.println(jménoAtributu + " = " + hodnotaAtributu);
    4.40 -		}
    4.41 -	}
    4.42 -
    4.43 -	private static String dekóduj(ByteBuffer bajty) {
    4.44 -		bajty.flip();
    4.45 -		return Charset.defaultCharset().decode(bajty).toString();
    4.46 -	}
    4.47 -}
     5.1 --- a/java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Startér.java	Wed Dec 15 19:18:08 2010 +0100
     5.2 +++ b/java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Startér.java	Wed Dec 15 20:07:14 2010 +0100
     5.3 @@ -1,13 +1,52 @@
     5.4  package cz.frantovo.rozsireneAtributy;
     5.5  
     5.6 +import cz.frantovo.rozsireneAtributy.gui.Panel;
     5.7 +import java.awt.BorderLayout;
     5.8 +import java.io.File;
     5.9 +import java.io.IOException;
    5.10 +import java.util.logging.Level;
    5.11 +import java.util.logging.Logger;
    5.12 +import javax.swing.JFrame;
    5.13 +
    5.14  /**
    5.15   * Spouštěč programu
    5.16 + *
    5.17 + * http://freedesktop.org/wiki/CommonExtendedAttributes
    5.18 + * http://download.oracle.com/javase/tutorial/essential/io/fileAttr.html#user
    5.19 + * http://today.java.net/pub/a/today/2008/07/03/jsr-203-new-file-apis.html#so-what-is-a-path-really
    5.20 + *
    5.21 + * $ setfattr -n "user.fiki.pozdrav" -v 'Dobrý den!' pokus.txt
    5.22 + * (v javě pak pracujeme s klíči bez předpony „user.“)
    5.23 + *
    5.24   * @author fiki
    5.25   */
    5.26  public class Startér {
    5.27  
    5.28 -	public static void main(String[] args) {
    5.29 -		System.err.println("ještě není implementované…");
    5.30 -		System.exit(1);
    5.31 +	private static final Logger log = Logger.getLogger(Startér.class.getSimpleName());
    5.32 +
    5.33 +	public static void main(String[] args) throws IOException {
    5.34 +
    5.35 +		if (args.length == 1) {
    5.36 +			File soubor = new File(args[0]);
    5.37 +
    5.38 +			log.log(Level.INFO, "Pracuji se souborem: {0}", soubor);
    5.39 +
    5.40 +			Model model = new Model(soubor);
    5.41 +
    5.42 +			JFrame f = new JFrame();
    5.43 +			Panel p = new Panel(model);
    5.44 +
    5.45 +			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    5.46 +			f.setTitle("Rozšířené stributy souboru: " + soubor);
    5.47 +			f.setLayout(new BorderLayout());
    5.48 +			f.add(p, BorderLayout.CENTER);
    5.49 +
    5.50 +			f.setSize(640, 240);
    5.51 +			f.setLocationRelativeTo(null);
    5.52 +			f.setVisible(true);
    5.53 +		} else {
    5.54 +			log.log(Level.SEVERE, "Chyba: Očekávám právě jeden parametr – název souboru.");
    5.55 +			System.exit(1);
    5.56 +		}
    5.57  	}
    5.58  }
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Panel.form	Wed Dec 15 20:07:14 2010 +0100
     6.3 @@ -0,0 +1,48 @@
     6.4 +<?xml version="1.1" encoding="UTF-8" ?>
     6.5 +
     6.6 +<Form version="1.3" maxVersion="1.7" type="org.netbeans.modules.form.forminfo.JPanelFormInfo">
     6.7 +  <AuxValues>
     6.8 +    <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
     6.9 +    <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
    6.10 +    <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
    6.11 +    <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
    6.12 +    <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
    6.13 +    <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
    6.14 +    <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
    6.15 +    <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
    6.16 +    <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
    6.17 +  </AuxValues>
    6.18 +
    6.19 +  <Layout>
    6.20 +    <DimensionLayout dim="0">
    6.21 +      <Group type="103" groupAlignment="0" attributes="0">
    6.22 +          <Component id="scroll" alignment="0" pref="447" max="32767" attributes="0"/>
    6.23 +      </Group>
    6.24 +    </DimensionLayout>
    6.25 +    <DimensionLayout dim="1">
    6.26 +      <Group type="103" groupAlignment="0" attributes="0">
    6.27 +          <Component id="scroll" alignment="0" pref="316" max="32767" attributes="0"/>
    6.28 +      </Group>
    6.29 +    </DimensionLayout>
    6.30 +  </Layout>
    6.31 +  <SubComponents>
    6.32 +    <Container class="javax.swing.JScrollPane" name="scroll">
    6.33 +
    6.34 +      <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
    6.35 +      <SubComponents>
    6.36 +        <Component class="javax.swing.JTable" name="tabulka">
    6.37 +          <Properties>
    6.38 +            <Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.editors2.TableModelEditor">
    6.39 +              <Table columnCount="4" rowCount="4">
    6.40 +                <Column editable="true" title="Title 1" type="java.lang.Object"/>
    6.41 +                <Column editable="true" title="Title 2" type="java.lang.Object"/>
    6.42 +                <Column editable="true" title="Title 3" type="java.lang.Object"/>
    6.43 +                <Column editable="true" title="Title 4" type="java.lang.Object"/>
    6.44 +              </Table>
    6.45 +            </Property>
    6.46 +          </Properties>
    6.47 +        </Component>
    6.48 +      </SubComponents>
    6.49 +    </Container>
    6.50 +  </SubComponents>
    6.51 +</Form>
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Panel.java	Wed Dec 15 20:07:14 2010 +0100
     7.3 @@ -0,0 +1,57 @@
     7.4 +package cz.frantovo.rozsireneAtributy.gui;
     7.5 +
     7.6 +import javax.swing.table.TableModel;
     7.7 +
     7.8 +/**
     7.9 + *
    7.10 + * @author fiki
    7.11 + */
    7.12 +public class Panel extends javax.swing.JPanel {
    7.13 +
    7.14 +	private TableModel model;
    7.15 +
    7.16 +    public Panel(TableModel model) {
    7.17 +		this.model = model;
    7.18 +        initComponents();
    7.19 +		tabulka.setModel(model);
    7.20 +    }
    7.21 +
    7.22 +    @SuppressWarnings("unchecked")
    7.23 +    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    7.24 +    private void initComponents() {
    7.25 +
    7.26 +        scroll = new javax.swing.JScrollPane();
    7.27 +        tabulka = new javax.swing.JTable();
    7.28 +
    7.29 +        tabulka.setModel(new javax.swing.table.DefaultTableModel(
    7.30 +            new Object [][] {
    7.31 +                {null, null, null, null},
    7.32 +                {null, null, null, null},
    7.33 +                {null, null, null, null},
    7.34 +                {null, null, null, null}
    7.35 +            },
    7.36 +            new String [] {
    7.37 +                "Title 1", "Title 2", "Title 3", "Title 4"
    7.38 +            }
    7.39 +        ));
    7.40 +        scroll.setViewportView(tabulka);
    7.41 +
    7.42 +        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    7.43 +        this.setLayout(layout);
    7.44 +        layout.setHorizontalGroup(
    7.45 +            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    7.46 +            .addComponent(scroll, javax.swing.GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
    7.47 +        );
    7.48 +        layout.setVerticalGroup(
    7.49 +            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    7.50 +            .addComponent(scroll, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
    7.51 +        );
    7.52 +    }// </editor-fold>//GEN-END:initComponents
    7.53 +
    7.54 +
    7.55 +    // Variables declaration - do not modify//GEN-BEGIN:variables
    7.56 +    private javax.swing.JScrollPane scroll;
    7.57 +    private javax.swing.JTable tabulka;
    7.58 +    // End of variables declaration//GEN-END:variables
    7.59 +
    7.60 +}