src/main/java/cz/frantovo/rozsireneatributy/gui/EditorHodnotAtributů.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 16 Dec 2023 20:13:13 +0100
branchv_0
changeset 39 ec0e970e0830
parent 32 java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/gui/EditorHodnotAtributů.java@ca064ecf3ca3
permissions -rw-r--r--
Make, Ant, Maven a Netbeans - různé způsoby sestavení aplikace
franta-hg@22
     1
/**
franta-hg@22
     2
 * Rozšířené atributy – program na správu rozšířených atributů souborů
franta-hg@22
     3
 * Copyright © 2012 František Kučera (frantovo.cz)
franta-hg@22
     4
 *
franta-hg@22
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@22
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@27
     7
 * the Free Software Foundation, either version 3 of the License.
franta-hg@22
     8
 *
franta-hg@22
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@22
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@22
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@22
    12
 * GNU General Public License for more details.
franta-hg@22
    13
 *
franta-hg@22
    14
 * You should have received a copy of the GNU General Public License
franta-hg@22
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@22
    16
 */
franta-hg@28
    17
package cz.frantovo.rozsireneatributy.gui;
franta-hg@22
    18
franta-hg@31
    19
import cz.frantovo.rozsireneatributy.Konfigurace;
franta-hg@31
    20
import cz.frantovo.rozsireneatributy.Konfigurace.DefiniceAtributu;
franta-hg@32
    21
import cz.frantovo.rozsireneatributy.Konfigurace.DefiniceHodnoty;
franta-hg@22
    22
import java.awt.Component;
franta-hg@22
    23
import java.awt.event.ActionEvent;
franta-hg@22
    24
import java.awt.event.ActionListener;
franta-hg@22
    25
import java.util.EventObject;
franta-hg@32
    26
import java.util.Objects;
franta-hg@22
    27
import javax.swing.JComboBox;
franta-hg@22
    28
import javax.swing.JTable;
franta-hg@22
    29
import javax.swing.event.CellEditorListener;
franta-hg@22
    30
import javax.swing.event.ChangeEvent;
franta-hg@22
    31
import javax.swing.event.EventListenerList;
franta-hg@22
    32
import javax.swing.table.TableCellEditor;
franta-hg@32
    33
import javax.swing.table.TableModel;
franta-hg@22
    34
franta-hg@22
    35
/**
franta-hg@32
    36
 * Umožňuje výběr hodnoty atributu z předvoleného seznamu.
franta-hg@22
    37
 *
franta-hg@22
    38
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@22
    39
 */
franta-hg@32
    40
public class EditorHodnotAtributů
franta-hg@29
    41
	extends JComboBox<String>
franta-hg@29
    42
	implements TableCellEditor {
franta-hg@22
    43
franta-hg@31
    44
	private final Konfigurace konfigurace;
franta-hg@32
    45
	private JTable tabulka;
franta-hg@32
    46
	private TableModel model;
franta-hg@32
    47
	private int řádek;
franta-hg@32
    48
	private int sloupec;
franta-hg@22
    49
	protected EventListenerList posluchače = new EventListenerList();
franta-hg@22
    50
	protected ChangeEvent událost = new ChangeEvent(this);
franta-hg@22
    51
franta-hg@32
    52
	public EditorHodnotAtributů(Konfigurace konfigurace) {
franta-hg@22
    53
		super();
franta-hg@31
    54
		this.konfigurace = konfigurace;
franta-hg@22
    55
		setEditable(true);
franta-hg@22
    56
		addActionListener(new ActionListener() {
franta-hg@22
    57
franta-hg@22
    58
			@Override
franta-hg@22
    59
			public void actionPerformed(ActionEvent e) {
franta-hg@22
    60
				fireEditiaceSkončila();
franta-hg@22
    61
			}
franta-hg@22
    62
		});
franta-hg@22
    63
	}
franta-hg@22
    64
franta-hg@22
    65
	protected void fireEditiaceSkončila() {
franta-hg@22
    66
		for (Object posluchač : posluchače.getListenerList()) {
franta-hg@22
    67
			if (posluchač instanceof CellEditorListener) {
franta-hg@22
    68
				((CellEditorListener) posluchač).editingStopped(událost);
franta-hg@22
    69
			}
franta-hg@22
    70
		}
franta-hg@22
    71
	}
franta-hg@22
    72
franta-hg@22
    73
	protected void fireEditiaceZrušena() {
franta-hg@22
    74
		for (Object posluchač : posluchače.getListenerList()) {
franta-hg@22
    75
			if (posluchač instanceof CellEditorListener) {
franta-hg@22
    76
				((CellEditorListener) posluchač).editingCanceled(událost);
franta-hg@22
    77
			}
franta-hg@22
    78
		}
franta-hg@22
    79
	}
franta-hg@22
    80
franta-hg@32
    81
	private void obnovHodnoty(Object hodnotaAtributu) {
franta-hg@22
    82
		removeAllItems();
franta-hg@22
    83
franta-hg@32
    84
		if (hodnotaAtributu == null) {
franta-hg@32
    85
			hodnotaAtributu = "";
franta-hg@32
    86
		} else if (!(hodnotaAtributu instanceof String)) {
franta-hg@32
    87
			hodnotaAtributu = String.valueOf(hodnotaAtributu);
franta-hg@22
    88
		}
franta-hg@32
    89
		addItem((String) hodnotaAtributu);
franta-hg@32
    90
		setSelectedItem(hodnotaAtributu);
franta-hg@32
    91
franta-hg@32
    92
		Object názevAtributu = model.getValueAt(řádek, Panel.SLOUPEC_NÁZVU);
franta-hg@31
    93
		for (DefiniceAtributu da : konfigurace.getAtributy()) {
franta-hg@32
    94
			if (Objects.equals(názevAtributu, da.getNázev())) {
franta-hg@32
    95
				for (DefiniceHodnoty dh : da.getHodnoty()) {
franta-hg@32
    96
					addItem(dh.getNázev());
franta-hg@32
    97
				}
franta-hg@32
    98
			}
franta-hg@31
    99
		}
franta-hg@22
   100
franta-hg@22
   101
	}
franta-hg@22
   102
franta-hg@22
   103
	@Override
franta-hg@29
   104
	public Component getTableCellEditorComponent(
franta-hg@29
   105
		JTable tabulka,
franta-hg@29
   106
		Object hodnota,
franta-hg@29
   107
		boolean vybraná,
franta-hg@29
   108
		int řádek,
franta-hg@32
   109
		int sloupec) //
franta-hg@32
   110
	{
franta-hg@32
   111
		this.řádek = řádek;
franta-hg@32
   112
		this.sloupec = sloupec;
franta-hg@32
   113
		this.tabulka = tabulka;
franta-hg@32
   114
		this.model = tabulka.getModel();
franta-hg@22
   115
		obnovHodnoty(hodnota);
franta-hg@32
   116
		// TODO: více různých instancí?
franta-hg@22
   117
		return this;
franta-hg@22
   118
	}
franta-hg@22
   119
franta-hg@22
   120
	@Override
franta-hg@22
   121
	public Object getCellEditorValue() {
franta-hg@22
   122
		return getSelectedItem();
franta-hg@22
   123
	}
franta-hg@22
   124
franta-hg@22
   125
	@Override
franta-hg@22
   126
	public boolean isCellEditable(EventObject anEvent) {
franta-hg@22
   127
		return true;
franta-hg@22
   128
	}
franta-hg@22
   129
franta-hg@22
   130
	@Override
franta-hg@22
   131
	public boolean shouldSelectCell(EventObject anEvent) {
franta-hg@22
   132
		return true;
franta-hg@22
   133
	}
franta-hg@22
   134
franta-hg@22
   135
	@Override
franta-hg@22
   136
	public boolean stopCellEditing() {
franta-hg@22
   137
		fireEditiaceSkončila();
franta-hg@22
   138
		return true;
franta-hg@22
   139
	}
franta-hg@22
   140
franta-hg@22
   141
	@Override
franta-hg@22
   142
	public void cancelCellEditing() {
franta-hg@22
   143
		fireEditiaceZrušena();
franta-hg@22
   144
	}
franta-hg@22
   145
franta-hg@22
   146
	@Override
franta-hg@22
   147
	public void addCellEditorListener(CellEditorListener l) {
franta-hg@22
   148
		posluchače.add(CellEditorListener.class, l);
franta-hg@22
   149
	}
franta-hg@22
   150
franta-hg@22
   151
	@Override
franta-hg@22
   152
	public void removeCellEditorListener(CellEditorListener l) {
franta-hg@22
   153
		posluchače.remove(CellEditorListener.class, l);
franta-hg@22
   154
	}
franta-hg@22
   155
}