java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/EditorNázvůAtributů.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 07 Oct 2012 15:11:42 +0200
branchv_0
changeset 22 f3ba21170329
child 27 dc5786f3482b
permissions -rw-r--r--
nabídka standardizovaných atributů
http://www.freedesktop.org/wiki/CommonExtendedAttributes
     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, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.rozsireneAtributy.gui;
    19 
    20 import java.awt.Component;
    21 import java.awt.event.ActionEvent;
    22 import java.awt.event.ActionListener;
    23 import java.util.EventObject;
    24 import javax.swing.JComboBox;
    25 import javax.swing.JTable;
    26 import javax.swing.event.CellEditorListener;
    27 import javax.swing.event.ChangeEvent;
    28 import javax.swing.event.EventListenerList;
    29 import javax.swing.table.TableCellEditor;
    30 
    31 /**
    32  * Umožňuje výběr názvu atributu z předvoleného seznamu (standardizované atributy).
    33  *
    34  * @author Ing. František Kučera (frantovo.cz)
    35  */
    36 public class EditorNázvůAtributů extends JComboBox<String> implements TableCellEditor {
    37 
    38 	protected EventListenerList posluchače = new EventListenerList();
    39 	protected ChangeEvent událost = new ChangeEvent(this);
    40 
    41 	public EditorNázvůAtributů() {
    42 		super();
    43 		setEditable(true);
    44 		addActionListener(new ActionListener() {
    45 
    46 			@Override
    47 			public void actionPerformed(ActionEvent e) {
    48 				fireEditiaceSkončila();
    49 			}
    50 		});
    51 	}
    52 
    53 	protected void fireEditiaceSkončila() {
    54 		for (Object posluchač : posluchače.getListenerList()) {
    55 			if (posluchač instanceof CellEditorListener) {
    56 				((CellEditorListener) posluchač).editingStopped(událost);
    57 			}
    58 		}
    59 	}
    60 
    61 	protected void fireEditiaceZrušena() {
    62 		for (Object posluchač : posluchače.getListenerList()) {
    63 			if (posluchač instanceof CellEditorListener) {
    64 				((CellEditorListener) posluchač).editingCanceled(událost);
    65 			}
    66 		}
    67 	}
    68 
    69 	/**
    70 	 * TODO:
    71 	 * - další standardní atributy
    72 	 * - konfigurovatelnost
    73 	 *
    74 	 * @see http://www.freedesktop.org/wiki/CommonExtendedAttributes
    75 	 */
    76 	private void obnovHodnoty(Object názevAtributu) {
    77 		removeAllItems();
    78 
    79 		if (názevAtributu == null) {
    80 			názevAtributu = "";
    81 		} else if (!(názevAtributu instanceof String)) {
    82 			názevAtributu = String.valueOf(názevAtributu);
    83 		}
    84 		addItem((String) názevAtributu);
    85 		setSelectedItem(názevAtributu);
    86 
    87 
    88 		// General attributes in current use
    89 		addItem("mime_type");
    90 		addItem("charset");
    91 		addItem("creator");
    92 
    93 		// Proposed metadata attributes
    94 		addItem("xdg.comment");
    95 		addItem("xdg.origin.url");
    96 		addItem("xdg.origin.email.subject");
    97 		addItem("xdg.origin.email.from");
    98 		addItem("xdg.origin.email.message-id");
    99 		addItem("xdg.language");
   100 		addItem("xdg.creator");
   101 		addItem("xdg.publisher");
   102 
   103 		// Proposed control attributes
   104 		addItem("xdg.robots.index");
   105 		addItem("xdg.robots.backup");
   106 
   107 		// Dublin Core
   108 		addItem("dublincore.title");
   109 		addItem("dublincore.creator");
   110 		addItem("dublincore.subject");
   111 		addItem("dublincore.description");
   112 		addItem("dublincore.publisher");
   113 		addItem("dublincore.contributor");
   114 		addItem("dublincore.date");
   115 		addItem("dublincore.type");
   116 		addItem("dublincore.format");
   117 		addItem("dublincore.identifier");
   118 		addItem("dublincore.source");
   119 		addItem("dublincore.language");
   120 		addItem("dublincore.relation");
   121 		addItem("dublincore.coverage");
   122 		addItem("dublincore.rights");
   123 
   124 		// Application-specific attributes in current use
   125 		addItem("mime_encoding");
   126 		addItem("apache_handler");
   127 		addItem("Beagle.AttrTime");
   128 		addItem("Beagle.Fingerprint");
   129 		addItem("Beagle.MTime");
   130 		addItem("Beagle.Uid");
   131 	}
   132 
   133 	@Override
   134 	public Component getTableCellEditorComponent(JTable tabulka, Object hodnota, boolean vybraná, int řádek, int sloupec) {
   135 		obnovHodnoty(hodnota);
   136 		return this;
   137 	}
   138 
   139 	@Override
   140 	public Object getCellEditorValue() {
   141 		return getSelectedItem();
   142 	}
   143 
   144 	@Override
   145 	public boolean isCellEditable(EventObject anEvent) {
   146 		return true;
   147 	}
   148 
   149 	@Override
   150 	public boolean shouldSelectCell(EventObject anEvent) {
   151 		return true;
   152 	}
   153 
   154 	@Override
   155 	public boolean stopCellEditing() {
   156 		fireEditiaceSkončila();
   157 		return true;
   158 	}
   159 
   160 	@Override
   161 	public void cancelCellEditing() {
   162 		fireEditiaceZrušena();
   163 	}
   164 
   165 	@Override
   166 	public void addCellEditorListener(CellEditorListener l) {
   167 		posluchače.add(CellEditorListener.class, l);
   168 	}
   169 
   170 	@Override
   171 	public void removeCellEditorListener(CellEditorListener l) {
   172 		posluchače.remove(CellEditorListener.class, l);
   173 	}
   174 }