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

Last change on this file since 4364 was 4364, checked in by mdewsnip, 21 years ago

Fixed tabbing.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 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/**
39 * Title: Gatherer<br>
40 * Description: The Gatherer: a tool for gathering and enriching a digital collection.<br>
41 * Copyright: Copyright (c) 2001<br>
42 * Company: The University of Waikato<br>
43 * Written: / /01<br>
44 * Revised: 21/06/02 - Moved into correct package and commented.
45 * 24/03/03 - Moved again, and rewritten for new Tree.
46 * @author John Thompson
47 * @version 2.3
48 */
49import java.awt.Point;
50import java.awt.event.FocusEvent;
51import java.awt.event.FocusListener;
52import java.awt.image.BufferedImage;
53import java.util.Vector;
54
55import javax.swing.JComponent;
56import javax.swing.tree.TreePath;
57
58import org.greenstone.gatherer.gui.tree.DragTree;
59import org.greenstone.gatherer.util.DragComponent;
60
61/** 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. */
62public class DragGroup
63 implements FocusListener {
64 /** The image used for the ghost icon when dragging. */
65 public BufferedImage image_ghost = null;
66 /** 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. */
67 public Point mouse_offset = null;
68 /** 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. */
69 private DragComponent ghost_owner = null;
70 /** 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.
71 * @see org.greenstone.gatherer.tree.DragComponent#gainFocus
72 * @see org.greenstone.gatherer.tree.DragComponent#loseFocus
73 */
74 private DragTree drag_source = null;
75 /** The selected nodes, as determined when the drag begun. */
76 private TreePath[] selection = null;
77 /** A list of DragComponents registered as being part of this group. */
78 private Vector registered = new Vector();
79
80 /** Register a DragComponent as begin part of this group.
81 * @param component The DragComponent to add.
82 */
83 public void add(DragComponent component) {
84 if(!registered.contains(component)) {
85 registered.add(component);
86 component.addFocusListener(this);
87 component.setGroup(this);
88 }
89 }
90
91 /** Invoked when a component gains the keyboard focus. */
92 public void focusGained(FocusEvent event) {
93 ((DragComponent)event.getComponent()).gainFocus();
94 }
95
96 /** Invoked when a component loses the keyboard focus. */
97 public void focusLost(FocusEvent event) {
98 ((DragComponent)event.getComponent()).loseFocus();
99 }
100 /** Determines the current 'active' component, ie that component with focus.
101 * @return The DragComponent which is currently responsible for drawing the ghost, and thus is active.
102 */
103 public DragComponent getActive() {
104 return ghost_owner;
105 }
106 /** Retrieve the nodes selected at the beginning of this drag operation.
107 * @return A TreePath[].
108 */
109 public TreePath[] getSelection() {
110 return selection;
111 }
112 /** Retrieve the component which served as the source of this drag action.
113 * @return The DragComponent in question.
114 */
115 public DragTree getSource() {
116 return drag_source;
117 }
118 /** 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.
119 * @param new_owner The DragComponent that has gained focus during a drag action.
120 */
121 public void grabFocus(DragComponent new_owner) {
122 // Tell the previous owner of ghost to clear itself.
123 if(ghost_owner != null) {
124 ghost_owner.clearGhost();
125 }
126 // Assign new owner
127 ghost_owner = new_owner;
128 // 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.
129 for(int i = 0; i < registered.size(); i++) {
130 DragComponent comp = (DragComponent)registered.get(i);
131 if(comp == new_owner) {
132 comp.gainFocus();
133 }
134 else {
135 comp.loseFocus();
136 }
137 }
138 }
139 /** Sets the value of selected_nodes.
140 * @param selection The new value for selected_nodes as a TreePath[].
141 */
142 public void setSelection(TreePath[] selection) {
143 this.selection = selection;
144 }
145 /** Sets the value of drag_source.
146 * @param drag_source The new value for drag_source as a DragTree.
147 */
148 public void setSource(DragTree drag_source) {
149 this.drag_source = drag_source;
150 System.err.println("The drag source is now " + drag_source);
151 }
152}
Note: See TracBrowser for help on using the repository browser.