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