# HG changeset patch # User František Kučera # Date 1702603797 -3600 # Node ID ca064ecf3ca325b8132cfca49965cb4f820a3985 # Parent 1ab5ce94a146093520a26bfed29d5bd18a58800a nabízení hodnot atributů dle konfigurace diff -r 1ab5ce94a146 -r ca064ecf3ca3 java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/gui/EditorHodnotAtributů.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/gui/EditorHodnotAtributů.java Fri Dec 15 02:29:57 2023 +0100 @@ -0,0 +1,155 @@ +/** + * Rozšířené atributy – program na správu rozšířených atributů souborů + * Copyright © 2012 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.rozsireneatributy.gui; + +import cz.frantovo.rozsireneatributy.Konfigurace; +import cz.frantovo.rozsireneatributy.Konfigurace.DefiniceAtributu; +import cz.frantovo.rozsireneatributy.Konfigurace.DefiniceHodnoty; +import java.awt.Component; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.EventObject; +import java.util.Objects; +import javax.swing.JComboBox; +import javax.swing.JTable; +import javax.swing.event.CellEditorListener; +import javax.swing.event.ChangeEvent; +import javax.swing.event.EventListenerList; +import javax.swing.table.TableCellEditor; +import javax.swing.table.TableModel; + +/** + * Umožňuje výběr hodnoty atributu z předvoleného seznamu. + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class EditorHodnotAtributů + extends JComboBox + implements TableCellEditor { + + private final Konfigurace konfigurace; + private JTable tabulka; + private TableModel model; + private int řádek; + private int sloupec; + protected EventListenerList posluchače = new EventListenerList(); + protected ChangeEvent událost = new ChangeEvent(this); + + public EditorHodnotAtributů(Konfigurace konfigurace) { + super(); + this.konfigurace = konfigurace; + setEditable(true); + addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + fireEditiaceSkončila(); + } + }); + } + + protected void fireEditiaceSkončila() { + for (Object posluchač : posluchače.getListenerList()) { + if (posluchač instanceof CellEditorListener) { + ((CellEditorListener) posluchač).editingStopped(událost); + } + } + } + + protected void fireEditiaceZrušena() { + for (Object posluchač : posluchače.getListenerList()) { + if (posluchač instanceof CellEditorListener) { + ((CellEditorListener) posluchač).editingCanceled(událost); + } + } + } + + private void obnovHodnoty(Object hodnotaAtributu) { + removeAllItems(); + + if (hodnotaAtributu == null) { + hodnotaAtributu = ""; + } else if (!(hodnotaAtributu instanceof String)) { + hodnotaAtributu = String.valueOf(hodnotaAtributu); + } + addItem((String) hodnotaAtributu); + setSelectedItem(hodnotaAtributu); + + Object názevAtributu = model.getValueAt(řádek, Panel.SLOUPEC_NÁZVU); + for (DefiniceAtributu da : konfigurace.getAtributy()) { + if (Objects.equals(názevAtributu, da.getNázev())) { + for (DefiniceHodnoty dh : da.getHodnoty()) { + addItem(dh.getNázev()); + } + } + } + + } + + @Override + public Component getTableCellEditorComponent( + JTable tabulka, + Object hodnota, + boolean vybraná, + int řádek, + int sloupec) // + { + this.řádek = řádek; + this.sloupec = sloupec; + this.tabulka = tabulka; + this.model = tabulka.getModel(); + obnovHodnoty(hodnota); + // TODO: více různých instancí? + return this; + } + + @Override + public Object getCellEditorValue() { + return getSelectedItem(); + } + + @Override + public boolean isCellEditable(EventObject anEvent) { + return true; + } + + @Override + public boolean shouldSelectCell(EventObject anEvent) { + return true; + } + + @Override + public boolean stopCellEditing() { + fireEditiaceSkončila(); + return true; + } + + @Override + public void cancelCellEditing() { + fireEditiaceZrušena(); + } + + @Override + public void addCellEditorListener(CellEditorListener l) { + posluchače.add(CellEditorListener.class, l); + } + + @Override + public void removeCellEditorListener(CellEditorListener l) { + posluchače.remove(CellEditorListener.class, l); + } +} diff -r 1ab5ce94a146 -r ca064ecf3ca3 java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/gui/Panel.java --- a/java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/gui/Panel.java Fri Dec 15 01:36:17 2023 +0100 +++ b/java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/gui/Panel.java Fri Dec 15 02:29:57 2023 +0100 @@ -32,7 +32,8 @@ */ public class Panel extends javax.swing.JPanel { - private static final int SLOUPEC_NÁZVU = 0; + public static final int SLOUPEC_NÁZVU = 0; + public static final int SLOUPEC_HODNOTY = 1; private static final Logger log = Logger .getLogger(Panel.class.getSimpleName()); private static final ResourceBundle překlady = ResourceBundle @@ -54,7 +55,7 @@ nastavEditor(); tabulka.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); posuvnýPanel.setViewportView(tabulka); - + /** Výběr aktuálního atributu v tabulce */ tabulka.getSelectionModel().addListSelectionListener( new ListSelectionListener() { @@ -76,6 +77,8 @@ private void nastavEditor() { tabulka.getColumnModel().getColumn(SLOUPEC_NÁZVU) .setCellEditor(new EditorNázvůAtributů(model.getKonfigurace())); + tabulka.getColumnModel().getColumn(SLOUPEC_HODNOTY) + .setCellEditor(new EditorHodnotAtributů(model.getKonfigurace())); } private Model getModel() {