src/main/java/cz/frantovo/rozsireneatributy/gui/EditorNázvůAtributů.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 25 Dec 2023 23:45:13 +0100
branchv_0
changeset 44 c43c96b0ab1b
parent 39 ec0e970e0830
permissions -rw-r--r--
back to the snapshot: 0.2-SNAPSHOT
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@22
    21
import java.awt.Component;
franta-hg@22
    22
import java.awt.event.ActionEvent;
franta-hg@22
    23
import java.awt.event.ActionListener;
franta-hg@22
    24
import java.util.EventObject;
franta-hg@22
    25
import javax.swing.JComboBox;
franta-hg@22
    26
import javax.swing.JTable;
franta-hg@22
    27
import javax.swing.event.CellEditorListener;
franta-hg@22
    28
import javax.swing.event.ChangeEvent;
franta-hg@22
    29
import javax.swing.event.EventListenerList;
franta-hg@22
    30
import javax.swing.table.TableCellEditor;
franta-hg@22
    31
franta-hg@22
    32
/**
franta-hg@29
    33
 * Umožňuje výběr názvu atributu z předvoleného seznamu
franta-hg@29
    34
 * (standardizované atributy).
franta-hg@22
    35
 *
franta-hg@22
    36
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@22
    37
 */
franta-hg@29
    38
public class EditorNázvůAtributů
franta-hg@29
    39
	extends JComboBox<String>
franta-hg@29
    40
	implements TableCellEditor {
franta-hg@22
    41
franta-hg@31
    42
	private final Konfigurace konfigurace;
franta-hg@22
    43
	protected EventListenerList posluchače = new EventListenerList();
franta-hg@22
    44
	protected ChangeEvent událost = new ChangeEvent(this);
franta-hg@22
    45
franta-hg@31
    46
	public EditorNázvůAtributů(Konfigurace konfigurace) {
franta-hg@22
    47
		super();
franta-hg@31
    48
		this.konfigurace = konfigurace;
franta-hg@22
    49
		setEditable(true);
franta-hg@22
    50
		addActionListener(new ActionListener() {
franta-hg@22
    51
franta-hg@22
    52
			@Override
franta-hg@22
    53
			public void actionPerformed(ActionEvent e) {
franta-hg@22
    54
				fireEditiaceSkončila();
franta-hg@22
    55
			}
franta-hg@22
    56
		});
franta-hg@22
    57
	}
franta-hg@22
    58
franta-hg@22
    59
	protected void fireEditiaceSkončila() {
franta-hg@22
    60
		for (Object posluchač : posluchače.getListenerList()) {
franta-hg@22
    61
			if (posluchač instanceof CellEditorListener) {
franta-hg@22
    62
				((CellEditorListener) posluchač).editingStopped(událost);
franta-hg@22
    63
			}
franta-hg@22
    64
		}
franta-hg@22
    65
	}
franta-hg@22
    66
franta-hg@22
    67
	protected void fireEditiaceZrušena() {
franta-hg@22
    68
		for (Object posluchač : posluchače.getListenerList()) {
franta-hg@22
    69
			if (posluchač instanceof CellEditorListener) {
franta-hg@22
    70
				((CellEditorListener) posluchač).editingCanceled(událost);
franta-hg@22
    71
			}
franta-hg@22
    72
		}
franta-hg@22
    73
	}
franta-hg@22
    74
franta-hg@22
    75
	/**
franta-hg@22
    76
	 * TODO:
franta-hg@22
    77
	 * - další standardní atributy
franta-hg@31
    78
	 * - načítat z XML souboru
franta-hg@22
    79
	 *
franta-hg@22
    80
	 * @see http://www.freedesktop.org/wiki/CommonExtendedAttributes
franta-hg@22
    81
	 */
franta-hg@22
    82
	private void obnovHodnoty(Object názevAtributu) {
franta-hg@22
    83
		removeAllItems();
franta-hg@22
    84
franta-hg@22
    85
		if (názevAtributu == null) {
franta-hg@22
    86
			názevAtributu = "";
franta-hg@22
    87
		} else if (!(názevAtributu instanceof String)) {
franta-hg@22
    88
			názevAtributu = String.valueOf(názevAtributu);
franta-hg@22
    89
		}
franta-hg@22
    90
		addItem((String) názevAtributu);
franta-hg@22
    91
		setSelectedItem(názevAtributu);
franta-hg@31
    92
		
franta-hg@31
    93
		for (DefiniceAtributu da : konfigurace.getAtributy()) {
franta-hg@31
    94
			addItem(da.getNázev());
franta-hg@31
    95
		}
franta-hg@31
    96
		
franta-hg@31
    97
		if (!konfigurace.getAtributy().isEmpty()) return;
franta-hg@22
    98
franta-hg@22
    99
		// General attributes in current use
franta-hg@22
   100
		addItem("mime_type");
franta-hg@22
   101
		addItem("charset");
franta-hg@22
   102
		addItem("creator");
franta-hg@22
   103
franta-hg@22
   104
		// Proposed metadata attributes
franta-hg@22
   105
		addItem("xdg.comment");
franta-hg@22
   106
		addItem("xdg.origin.url");
franta-hg@22
   107
		addItem("xdg.origin.email.subject");
franta-hg@22
   108
		addItem("xdg.origin.email.from");
franta-hg@22
   109
		addItem("xdg.origin.email.message-id");
franta-hg@22
   110
		addItem("xdg.language");
franta-hg@22
   111
		addItem("xdg.creator");
franta-hg@22
   112
		addItem("xdg.publisher");
franta-hg@22
   113
franta-hg@22
   114
		// Proposed control attributes
franta-hg@22
   115
		addItem("xdg.robots.index");
franta-hg@22
   116
		addItem("xdg.robots.backup");
franta-hg@22
   117
franta-hg@22
   118
		// Dublin Core
franta-hg@22
   119
		addItem("dublincore.title");
franta-hg@22
   120
		addItem("dublincore.creator");
franta-hg@22
   121
		addItem("dublincore.subject");
franta-hg@22
   122
		addItem("dublincore.description");
franta-hg@22
   123
		addItem("dublincore.publisher");
franta-hg@22
   124
		addItem("dublincore.contributor");
franta-hg@22
   125
		addItem("dublincore.date");
franta-hg@22
   126
		addItem("dublincore.type");
franta-hg@22
   127
		addItem("dublincore.format");
franta-hg@22
   128
		addItem("dublincore.identifier");
franta-hg@22
   129
		addItem("dublincore.source");
franta-hg@22
   130
		addItem("dublincore.language");
franta-hg@22
   131
		addItem("dublincore.relation");
franta-hg@22
   132
		addItem("dublincore.coverage");
franta-hg@22
   133
		addItem("dublincore.rights");
franta-hg@22
   134
franta-hg@22
   135
		// Application-specific attributes in current use
franta-hg@22
   136
		addItem("mime_encoding");
franta-hg@22
   137
		addItem("apache_handler");
franta-hg@22
   138
		addItem("Beagle.AttrTime");
franta-hg@22
   139
		addItem("Beagle.Fingerprint");
franta-hg@22
   140
		addItem("Beagle.MTime");
franta-hg@22
   141
		addItem("Beagle.Uid");
franta-hg@22
   142
	}
franta-hg@22
   143
franta-hg@22
   144
	@Override
franta-hg@29
   145
	public Component getTableCellEditorComponent(
franta-hg@29
   146
		JTable tabulka,
franta-hg@29
   147
		Object hodnota,
franta-hg@29
   148
		boolean vybraná,
franta-hg@29
   149
		int řádek,
franta-hg@29
   150
		int sloupec) {
franta-hg@22
   151
		obnovHodnoty(hodnota);
franta-hg@22
   152
		return this;
franta-hg@22
   153
	}
franta-hg@22
   154
franta-hg@22
   155
	@Override
franta-hg@22
   156
	public Object getCellEditorValue() {
franta-hg@22
   157
		return getSelectedItem();
franta-hg@22
   158
	}
franta-hg@22
   159
franta-hg@22
   160
	@Override
franta-hg@22
   161
	public boolean isCellEditable(EventObject anEvent) {
franta-hg@22
   162
		return true;
franta-hg@22
   163
	}
franta-hg@22
   164
franta-hg@22
   165
	@Override
franta-hg@22
   166
	public boolean shouldSelectCell(EventObject anEvent) {
franta-hg@22
   167
		return true;
franta-hg@22
   168
	}
franta-hg@22
   169
franta-hg@22
   170
	@Override
franta-hg@22
   171
	public boolean stopCellEditing() {
franta-hg@22
   172
		fireEditiaceSkončila();
franta-hg@22
   173
		return true;
franta-hg@22
   174
	}
franta-hg@22
   175
franta-hg@22
   176
	@Override
franta-hg@22
   177
	public void cancelCellEditing() {
franta-hg@22
   178
		fireEditiaceZrušena();
franta-hg@22
   179
	}
franta-hg@22
   180
franta-hg@22
   181
	@Override
franta-hg@22
   182
	public void addCellEditorListener(CellEditorListener l) {
franta-hg@22
   183
		posluchače.add(CellEditorListener.class, l);
franta-hg@22
   184
	}
franta-hg@22
   185
franta-hg@22
   186
	@Override
franta-hg@22
   187
	public void removeCellEditorListener(CellEditorListener l) {
franta-hg@22
   188
		posluchače.remove(CellEditorListener.class, l);
franta-hg@22
   189
	}
franta-hg@22
   190
}