source: main/trunk/gli/src/org/greenstone/gatherer/util/DragGroup.java@ 22605

Last change on this file since 22605 was 8243, checked in by mdewsnip, 20 years ago

Removed all occurrences of classes explicitly importing other classes in the same package.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.util;
38
39import java.awt.Point;
40import java.awt.event.*;
41import java.awt.image.BufferedImage;
42import java.util.Vector;
43import javax.swing.JComponent;
44import javax.swing.tree.TreePath;
45import org.greenstone.gatherer.gui.tree.DragTree;
46
47
48/** This class acts as a linker between all the various drag and drop enabled DragComponent. It provides methods for ensuring only one component is the drop target, showing extra user feedback, and maintains a single point of focus. Moreover it provides a storage space for necessary shared variables such as mouse_offset of initial drag etc. */
49public class DragGroup
50 implements FocusListener {
51 /** The image used for the ghost icon when dragging. */
52 public BufferedImage image_ghost = null;
53 /** The initial offset of the mouse from the selection within a DragComponent. Then as you drag the mouse, the ghost appears in a visually consistant place, and remains at the same offset throughout the dragging process. */
54 public Point mouse_offset = null;
55 /** The component currently in charge of drawing the ghost icon. Note that this isn't necessarily the only component that needs to repaint 'spoilt' screen real-estate as a side effect of the ghost. */
56 private DragComponent ghost_owner = null;
57 /** The component where the drag began, so that extra information necessary for the drag can be garnered by the drop target, or so that focus can return to the source component if the drop is rejected. */
58 private DragTree drag_source = null;
59 /** The selected nodes, as determined when the drag begun. */
60 private TreePath[] selection = null;
61 /** A list of DragComponents registered as being part of this group. */
62 private Vector registered = new Vector();
63
64 /** Register a DragComponent as begin part of this group.
65 * @param component The DragComponent to add.
66 */
67 public void add(DragComponent component) {
68 if(!registered.contains(component)) {
69 registered.add(component);
70 component.addFocusListener(this);
71 component.setGroup(this);
72 }
73 }
74
75 /** Invoked when a component gains the keyboard focus. */
76 public void focusGained(FocusEvent event) {
77 ((DragComponent)event.getComponent()).gainFocus();
78 }
79
80 /** Invoked when a component loses the keyboard focus. */
81 public void focusLost(FocusEvent event) {
82 ((DragComponent)event.getComponent()).loseFocus();
83 }
84 /** Determines the current 'active' component, ie that component with focus.
85 * @return The DragComponent which is currently responsible for drawing the ghost, and thus is active.
86 */
87 public DragComponent getActive() {
88 return ghost_owner;
89 }
90 /** Retrieve the nodes selected at the beginning of this drag operation.
91 * @return A TreePath[].
92 */
93 public TreePath[] getSelection() {
94 return selection;
95 }
96 /** Retrieve the component which served as the source of this drag action.
97 * @return The DragComponent in question.
98 */
99 public DragTree getSource() {
100 return drag_source;
101 }
102 /** When called this method asserts that one of the registered GComponents just became the proud owner of focus, and is henceforth responsible for drawing the ghost, and acting like a component thats in focus.
103 * @param new_owner The DragComponent that has gained focus during a drag action.
104 */
105 public void grabFocus(DragComponent new_owner) {
106 // Tell the previous owner of ghost to clear itself.
107 if(ghost_owner != null) {
108 ghost_owner.clearGhost();
109 }
110 // Assign new owner
111 ghost_owner = new_owner;
112 // Then tell all other GComponents to indicate they aren't in focus in whatever way they do that (ie selected items of a GTree go gray). Of course the new owners told it is in focus.
113 for(int i = 0; i < registered.size(); i++) {
114 DragComponent comp = (DragComponent)registered.get(i);
115 if(comp == new_owner) {
116 comp.gainFocus();
117 }
118 else {
119 comp.loseFocus();
120 }
121 }
122 }
123 /** Sets the value of selected_nodes.
124 * @param selection The new value for selected_nodes as a TreePath[].
125 */
126 public void setSelection(TreePath[] selection) {
127 this.selection = selection;
128 }
129 /** Sets the value of drag_source.
130 * @param drag_source The new value for drag_source as a DragTree.
131 */
132 public void setSource(DragTree drag_source) {
133 this.drag_source = drag_source;
134 ///ystem.err.println("The drag source is now " + drag_source);
135 }
136}
Note: See TracBrowser for help on using the repository browser.