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