Coverage Report - de.glossmaker.gui.gloss.main.tree.TreeTextFieldCellEditor
 
Classes in this File Line Coverage Branch Coverage Complexity
TreeTextFieldCellEditor
0%
0/22
0%
0/8
2,2
TreeTextFieldCellEditor$1
0%
0/4
0%
0/2
2,2
TreeTextFieldCellEditor$2
0%
0/6
0%
0/2
2,2
 
 1  
 package de.glossmaker.gui.gloss.main.tree;
 2  
 
 3  
 import java.awt.Component;
 4  
 import java.awt.event.ActionEvent;
 5  
 import java.awt.event.ActionListener;
 6  
 import java.awt.event.KeyAdapter;
 7  
 import java.awt.event.KeyEvent;
 8  
 import java.util.EventObject;
 9  
 
 10  
 import javax.swing.JTextField;
 11  
 import javax.swing.JTree;
 12  
 import javax.swing.tree.DefaultTreeCellEditor;
 13  
 import javax.swing.tree.DefaultTreeCellRenderer;
 14  
 import javax.swing.tree.TreePath;
 15  
 
 16  
 import de.glossmaker.gloss.datastructure.GlossItem;
 17  
 import de.glossmaker.gloss.datastructure.GlossItems;
 18  
 import de.glossmaker.gloss.datastructure.gui.Config;
 19  
 import de.glossmaker.gloss.observer.GlossItemsChangePublisher;
 20  
 
 21  
 /**
 22  
  * 
 23  
  * @author Markus Flingelli
 24  
  *
 25  
  */
 26  0
 public class TreeTextFieldCellEditor extends DefaultTreeCellEditor {
 27  
         private static final long serialVersionUID = -7607822048622345897L;
 28  
         private JTextField textfield;
 29  0
         private Object value = null;
 30  
         
 31  0
         private GlossItemsChangePublisher mItemsPropertyChangeSupport = null;
 32  
                 
 33  
         
 34  
         public TreeTextFieldCellEditor(JTree tree, DefaultTreeCellRenderer renderer, GlossItems items) {
 35  0
                 super(tree, renderer);
 36  0
                 mItemsPropertyChangeSupport = GlossItemsChangePublisher.getInstance(items);
 37  0
         }
 38  
 
 39  
         @Override
 40  
     public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
 41  0
         Component component = super.getTreeCellEditorComponent(tree, value, isSelected, expanded, leaf, row);
 42  0
         this.value = value;
 43  0
         if(textfield == null) {                        
 44  0
             textfield = (JTextField)editingComponent;
 45  0
             textfield.addKeyListener(new KeyAdapter() {
 46  
                 @Override
 47  
                 public void keyPressed(KeyEvent e) {
 48  0
                     if(e.getKeyCode() == KeyEvent.VK_ESCAPE){
 49  0
                         stopCellEditing();
 50  
                     }
 51  0
                 }
 52  
             });
 53  0
             textfield.addActionListener(new ActionListener() {
 54  
                                 
 55  
                                 public void actionPerformed(ActionEvent event) {                                                
 56  0
                                         if (TreeTextFieldCellEditor.this.value instanceof GlossItem) {
 57  0
                                                 String word = event.getActionCommand();
 58  0
                                                 GlossItem item = (GlossItem) TreeTextFieldCellEditor.this.value;
 59  0
                                                 mItemsPropertyChangeSupport.setWordInTree(item,
 60  
                                                                 word, Config.getInstance().isAutoUpdateEnabled());
 61  
                                         }
 62  0
                                 }
 63  
                         });
 64  
         }                
 65  
 
 66  0
         return component;
 67  
     }
 68  
         
 69  
         public boolean isCellEditable(EventObject event) {
 70  0
             boolean returnValue = super.isCellEditable(event);
 71  0
             if (returnValue) {
 72  0
               returnValue = false;
 73  0
               TreePath path = tree.getSelectionPath();
 74  0
               if (path.getParentPath() != null) {
 75  0
                         path = path.getParentPath();
 76  0
                         if (path.getParentPath() != null) {
 77  0
                                 returnValue = true;
 78  
                         }
 79  
                 }
 80  
             }
 81  0
             return returnValue;
 82  
           }
 83  
 }