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