001 /*
002 * Copyright (c) 1995-2010, The University of Sheffield. See the file
003 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004 *
005 * This file is part of GATE (see http://gate.ac.uk/), and is free
006 * software, licenced under the GNU Library General Public License,
007 * Version 2, June 1991 (in the distribution as file licence.html,
008 * and also available at http://gate.ac.uk/gate/licence.html).
009 *
010 * Ian Roberts 14/02/2009
011 *
012 * $Id: AnnotationSetNameComboEditor.java 12006 2009-12-01 17:24:28Z thomas_heitz $
013 *
014 */
015
016 package gate.gui.teamware;
017
018 import java.awt.Component;
019 import java.awt.Font;
020 import java.awt.event.ActionListener;
021 import java.awt.event.FocusEvent;
022 import java.awt.event.FocusListener;
023
024 import javax.swing.ComboBoxEditor;
025 import javax.swing.JTextField;
026
027 /**
028 * Combo box editor for annotation set names. When the text field is
029 * empty, it displays the text "<Default annotation set>" in
030 * italics. Other than that, it delegates to the underlying default
031 * editor.
032 */
033 class AnnotationSetNameComboEditor implements ComboBoxEditor, FocusListener {
034
035 public static final String DEFAULT_SET_TEXT = "<Default annotation set>";
036
037 private ComboBoxEditor realEditor;
038
039 private Font normalFont;
040
041 private Font italicFont;
042
043 private boolean isEmpty;
044
045 AnnotationSetNameComboEditor(ComboBoxEditor realEditor) {
046 this.realEditor = realEditor;
047 normalFont = realEditor.getEditorComponent().getFont();
048 italicFont = normalFont.deriveFont(Font.ITALIC);
049 setItem(null, false);
050 realEditor.getEditorComponent().addFocusListener(this);
051 }
052
053 public void addActionListener(ActionListener l) {
054 realEditor.addActionListener(l);
055 }
056
057 public Component getEditorComponent() {
058 return realEditor.getEditorComponent();
059 }
060
061 public Object getItem() {
062 Object realItem = realEditor.getItem();
063 if(isEmpty || DEFAULT_SET_TEXT.equals(realItem)) {
064 return null;
065 }
066 else {
067 return realItem;
068 }
069 }
070
071 public void removeActionListener(ActionListener l) {
072 realEditor.removeActionListener(l);
073 }
074
075 public void selectAll() {
076 if(!isEmpty) {
077 realEditor.selectAll();
078 }
079 }
080
081 public void setItem(Object item) {
082 setItem(item, true);
083 }
084
085 /**
086 * Set the current item. If updateFocus is true, call the relevant
087 * FocusListener method.
088 */
089 private void setItem(Object item, boolean updateFocus) {
090 if(item == null || "".equals(item)) {
091 isEmpty = true;
092 realEditor.getEditorComponent().setFont(italicFont);
093 realEditor.setItem(DEFAULT_SET_TEXT);
094 }
095 else {
096 isEmpty = false;
097 realEditor.getEditorComponent().setFont(normalFont);
098 realEditor.setItem(item);
099 }
100
101 // update the field properly
102 if(updateFocus) {
103 if(realEditor.getEditorComponent().isFocusOwner()) {
104 focusGained(null);
105 }
106 else {
107 focusLost(null);
108 }
109 }
110 }
111
112 // FocusListener methods
113
114 public void focusGained(FocusEvent e) {
115 if(isEmpty) {
116 JTextField field = (JTextField)realEditor.getEditorComponent();
117 field.setText("");
118 field.setFont(normalFont);
119 }
120 }
121
122 public void focusLost(FocusEvent e) {
123 JTextField field = (JTextField)realEditor.getEditorComponent();
124 if(field.getDocument().getLength() == 0
125 || DEFAULT_SET_TEXT.equals(field.getText())) {
126 setItem(null, false);
127 }
128 else {
129 isEmpty = false;
130 }
131 }
132
133 }
|