Ignore:
Timestamp:
2006-04-06T15:05:44+12:00 (18 years ago)
Author:
mdewsnip
Message:

Moved the KeyListener from GatherPane into DragTree and removed the KeyListener from EnrichPane so now there is only one (consistent) key listener between the two panes (ie. you can now press "Enter" and "Delete" in the collection tree of the Enrich pane).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/gui/tree/DragTree.java

    r11367 r11601  
    44import java.awt.datatransfer.*;
    55import java.awt.dnd.*;
     6import java.awt.event.*;
    67import java.awt.geom.AffineTransform;
    78import java.awt.image.BufferedImage;
     
    99100
    100101    // Connection
     102    addKeyListener(new DragTreeKeyListener());
    101103    addTreeSelectionListener(this);
    102104
     
    699701    return valid;
    700702    }
     703
     704
     705    /** This class listens for certain key presses, such as [Enter] or [Delete], and responds appropriately. */
     706    private class DragTreeKeyListener
     707    extends KeyAdapter
     708    {
     709    private boolean vk_left_pressed = false;
     710    /** Called whenever a key that was pressed is released, it is this action that will cause the desired effects (this allows for the key event itself to be processed prior to this listener dealing with it). */
     711    public void keyReleased(KeyEvent event) {
     712        ///ystem.err.println("Key Release detected. " + event.getKeyCode());
     713        if(event.getKeyCode() == KeyEvent.VK_DELETE) {
     714        // Get the selected files from the tree and removal them using the default dnd removal method.
     715        // Find the active tree (you've made selections in).
     716        DragTree tree = (DragTree) group.getActive();
     717        // Fudge things a bit
     718        group.setSource(tree);
     719        // Determine the selection.
     720        TreePath paths[] = tree.getSelectionPaths();
     721        if(paths != null) {
     722            FileNode[] source_nodes = new FileNode[paths.length];
     723            for(int i = 0; i < source_nodes.length; i++) {
     724            source_nodes[i] = (FileNode) paths[i].getLastPathComponent();
     725            }
     726            Gatherer.f_man.action(tree, source_nodes, Gatherer.recycle_bin, null);
     727            source_nodes = null;
     728        }
     729        }
     730        else if(event.getKeyCode() == KeyEvent.VK_ENTER) {
     731        // Get the first selected file.
     732        DragTree tree = (DragTree)event.getSource();
     733        TreePath path = tree.getSelectionPath();
     734        if(path != null) {
     735            File file = ((FileNode)path.getLastPathComponent()).getFile();
     736            if (file != null && file.isFile()) {
     737            Gatherer.f_man.openFileInExternalApplication(file);
     738            }
     739            else {
     740            if(!tree.isExpanded(path)) {
     741                tree.expandPath(path);
     742            }
     743            else {
     744                tree.collapsePath(path);
     745            }
     746            }
     747        }
     748        } else if (event.getKeyCode() == KeyEvent.VK_UP || event.getKeyCode() == KeyEvent.VK_DOWN) {
     749        DragTree tree = (DragTree)event.getSource();
     750        // we need to manually do the up and down selections
     751        boolean up = (event.getKeyCode() == KeyEvent.VK_UP);
     752        int current_row = tree.getLeadSelectionRow();
     753        tree.setImmediate(true);
     754        if (up) {
     755            if (current_row > 0) {
     756            tree.clearSelection();
     757            tree.setSelectionRow(current_row-1);
     758            }
     759        } else {
     760            if (current_row < tree.getRowCount()-1){
     761            tree.clearSelection();
     762            tree.setSelectionRow(current_row+1);
     763            }
     764        }
     765        tree.setImmediate(false);
     766        } else if (event.getKeyCode() == KeyEvent.VK_LEFT) {
     767        // left key on a file shifts the selection to the parent folder
     768        DragTree tree = (DragTree)event.getSource();
     769        TreePath path = tree.getLeadSelectionPath();
     770        if(path != null) {
     771            File file = ((FileNode)path.getLastPathComponent()).getFile();
     772            if(file != null) {
     773            if (file.isFile() || vk_left_pressed) {
     774                vk_left_pressed = false;
     775                TreePath parent_path = path.getParentPath();
     776                if (parent_path != null && parent_path.getParentPath() != null) {
     777                // if this file is in the top level folder, don't move the focus
     778                tree.setImmediate(true);
     779                tree.clearSelection();
     780                tree.setSelectionPath(parent_path);
     781                tree.setImmediate(false);
     782                }
     783            }
     784            }
     785        }
     786        }
     787    }
     788   
     789    // we need to watch for left clicks on an unopened folder - should shift the focus to teh parent folder. But because there is some other mysterious key listener that does opening and closing folders on right and left clicks, we must detect the situation before the other handler has done its job, and process it after.
     790    public void keyPressed(KeyEvent event) {
     791        if (event.getKeyCode() == KeyEvent.VK_LEFT) {
     792        // left key on  closed directory shifts the selection to the parent folder
     793        DragTree tree = (DragTree)event.getSource();
     794        TreePath path = tree.getLeadSelectionPath();
     795        if(path == null) return;
     796        File file = ((FileNode)path.getLastPathComponent()).getFile();
     797        if(file == null) return;
     798       
     799        if (file.isDirectory() && tree.isCollapsed(path)) {
     800            vk_left_pressed = true;
     801        }
     802        }
     803    }
     804    }
    701805}
Note: See TracChangeset for help on using the changeset viewer.