franta-hg@22: /**
franta-hg@22: * Rozšířené atributy – program na správu rozšířených atributů souborů
franta-hg@22: * Copyright © 2012 František Kučera (frantovo.cz)
franta-hg@22: *
franta-hg@22: * This program is free software: you can redistribute it and/or modify
franta-hg@22: * it under the terms of the GNU General Public License as published by
franta-hg@27: * the Free Software Foundation, either version 3 of the License.
franta-hg@22: *
franta-hg@22: * This program is distributed in the hope that it will be useful,
franta-hg@22: * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@22: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@22: * GNU General Public License for more details.
franta-hg@22: *
franta-hg@22: * You should have received a copy of the GNU General Public License
franta-hg@22: * along with this program. If not, see .
franta-hg@22: */
franta-hg@28: package cz.frantovo.rozsireneatributy.gui;
franta-hg@22:
franta-hg@31: import cz.frantovo.rozsireneatributy.Konfigurace;
franta-hg@31: import cz.frantovo.rozsireneatributy.Konfigurace.DefiniceAtributu;
franta-hg@32: import cz.frantovo.rozsireneatributy.Konfigurace.DefiniceHodnoty;
franta-hg@22: import java.awt.Component;
franta-hg@22: import java.awt.event.ActionEvent;
franta-hg@22: import java.awt.event.ActionListener;
franta-hg@22: import java.util.EventObject;
franta-hg@32: import java.util.Objects;
franta-hg@22: import javax.swing.JComboBox;
franta-hg@22: import javax.swing.JTable;
franta-hg@22: import javax.swing.event.CellEditorListener;
franta-hg@22: import javax.swing.event.ChangeEvent;
franta-hg@22: import javax.swing.event.EventListenerList;
franta-hg@22: import javax.swing.table.TableCellEditor;
franta-hg@32: import javax.swing.table.TableModel;
franta-hg@22:
franta-hg@22: /**
franta-hg@32: * Umožňuje výběr hodnoty atributu z předvoleného seznamu.
franta-hg@22: *
franta-hg@22: * @author Ing. František Kučera (frantovo.cz)
franta-hg@22: */
franta-hg@32: public class EditorHodnotAtributů
franta-hg@29: extends JComboBox
franta-hg@29: implements TableCellEditor {
franta-hg@22:
franta-hg@31: private final Konfigurace konfigurace;
franta-hg@32: private JTable tabulka;
franta-hg@32: private TableModel model;
franta-hg@32: private int řádek;
franta-hg@32: private int sloupec;
franta-hg@22: protected EventListenerList posluchače = new EventListenerList();
franta-hg@22: protected ChangeEvent událost = new ChangeEvent(this);
franta-hg@22:
franta-hg@32: public EditorHodnotAtributů(Konfigurace konfigurace) {
franta-hg@22: super();
franta-hg@31: this.konfigurace = konfigurace;
franta-hg@22: setEditable(true);
franta-hg@22: addActionListener(new ActionListener() {
franta-hg@22:
franta-hg@22: @Override
franta-hg@22: public void actionPerformed(ActionEvent e) {
franta-hg@22: fireEditiaceSkončila();
franta-hg@22: }
franta-hg@22: });
franta-hg@22: }
franta-hg@22:
franta-hg@22: protected void fireEditiaceSkončila() {
franta-hg@22: for (Object posluchač : posluchače.getListenerList()) {
franta-hg@22: if (posluchač instanceof CellEditorListener) {
franta-hg@22: ((CellEditorListener) posluchač).editingStopped(událost);
franta-hg@22: }
franta-hg@22: }
franta-hg@22: }
franta-hg@22:
franta-hg@22: protected void fireEditiaceZrušena() {
franta-hg@22: for (Object posluchač : posluchače.getListenerList()) {
franta-hg@22: if (posluchač instanceof CellEditorListener) {
franta-hg@22: ((CellEditorListener) posluchač).editingCanceled(událost);
franta-hg@22: }
franta-hg@22: }
franta-hg@22: }
franta-hg@22:
franta-hg@32: private void obnovHodnoty(Object hodnotaAtributu) {
franta-hg@22: removeAllItems();
franta-hg@22:
franta-hg@32: if (hodnotaAtributu == null) {
franta-hg@32: hodnotaAtributu = "";
franta-hg@32: } else if (!(hodnotaAtributu instanceof String)) {
franta-hg@32: hodnotaAtributu = String.valueOf(hodnotaAtributu);
franta-hg@22: }
franta-hg@32: addItem((String) hodnotaAtributu);
franta-hg@32: setSelectedItem(hodnotaAtributu);
franta-hg@32:
franta-hg@32: Object názevAtributu = model.getValueAt(řádek, Panel.SLOUPEC_NÁZVU);
franta-hg@31: for (DefiniceAtributu da : konfigurace.getAtributy()) {
franta-hg@32: if (Objects.equals(názevAtributu, da.getNázev())) {
franta-hg@32: for (DefiniceHodnoty dh : da.getHodnoty()) {
franta-hg@32: addItem(dh.getNázev());
franta-hg@32: }
franta-hg@32: }
franta-hg@31: }
franta-hg@22:
franta-hg@22: }
franta-hg@22:
franta-hg@22: @Override
franta-hg@29: public Component getTableCellEditorComponent(
franta-hg@29: JTable tabulka,
franta-hg@29: Object hodnota,
franta-hg@29: boolean vybraná,
franta-hg@29: int řádek,
franta-hg@32: int sloupec) //
franta-hg@32: {
franta-hg@32: this.řádek = řádek;
franta-hg@32: this.sloupec = sloupec;
franta-hg@32: this.tabulka = tabulka;
franta-hg@32: this.model = tabulka.getModel();
franta-hg@22: obnovHodnoty(hodnota);
franta-hg@32: // TODO: více různých instancí?
franta-hg@22: return this;
franta-hg@22: }
franta-hg@22:
franta-hg@22: @Override
franta-hg@22: public Object getCellEditorValue() {
franta-hg@22: return getSelectedItem();
franta-hg@22: }
franta-hg@22:
franta-hg@22: @Override
franta-hg@22: public boolean isCellEditable(EventObject anEvent) {
franta-hg@22: return true;
franta-hg@22: }
franta-hg@22:
franta-hg@22: @Override
franta-hg@22: public boolean shouldSelectCell(EventObject anEvent) {
franta-hg@22: return true;
franta-hg@22: }
franta-hg@22:
franta-hg@22: @Override
franta-hg@22: public boolean stopCellEditing() {
franta-hg@22: fireEditiaceSkončila();
franta-hg@22: return true;
franta-hg@22: }
franta-hg@22:
franta-hg@22: @Override
franta-hg@22: public void cancelCellEditing() {
franta-hg@22: fireEditiaceZrušena();
franta-hg@22: }
franta-hg@22:
franta-hg@22: @Override
franta-hg@22: public void addCellEditorListener(CellEditorListener l) {
franta-hg@22: posluchače.add(CellEditorListener.class, l);
franta-hg@22: }
franta-hg@22:
franta-hg@22: @Override
franta-hg@22: public void removeCellEditorListener(CellEditorListener l) {
franta-hg@22: posluchače.remove(CellEditorListener.class, l);
franta-hg@22: }
franta-hg@22: }