001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.dnd;
014:
015: import java.util.ArrayList;
016: import java.util.HashMap;
017: import java.util.Iterator;
018: import java.util.List;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.wings.*;
023: import org.wings.event.SComponentDropListener;
024:
025: /**
026: * The Drag and Drop Manager. It receives DnD events and dispatches them to the
027: * registered components. You must register your component with this class,
028: * which is accessible via the wings session. you should do this in the
029: * @link org.wings.dnd.DragSource#setDragEnabled or
030: * @link org.wings.dnd.DropTarget#addComponentDropListener methods.
031: * @author ole
032: *
033: */
034: public class DragAndDropManager extends SComponent implements
035: LowLevelEventListener {
036: private final static Log log = LogFactory
037: .getLog(DragAndDropManager.class);
038: private ArrayList<DragSource> dragSources;
039: private ArrayList<DropTarget> dropTargets;
040: private HashMap<String, SComponent> namesToComponentsMap;
041: private String sourceName;
042: private String targetName;
043:
044: /**
045: * The constructor. It initializes all fields
046: */
047: public DragAndDropManager() {
048: super ();
049: dragSources = new ArrayList<DragSource>();
050: dropTargets = new ArrayList<DropTarget>();
051: namesToComponentsMap = new HashMap<String, SComponent>();
052: setParentFrame(getSession().getRootFrame());
053: getSession().getDispatcher().register(this );
054: }
055:
056: /**
057: * register a dragSource with the Manager. After registering it will be
058: * receivable by dropTargets.
059: * @param dragSource the SComponent which is the DragSource
060: */
061: public void registerDragSource(DragSource dragSource) {
062: // don't add a component more than once
063: if (!dragSources.contains(dragSource)) {
064: dragSources.add(dragSource);
065: namesToComponentsMap.put(((SComponent) dragSource)
066: .getName(), (SComponent) dragSource);
067: }
068: }
069:
070: /**
071: * deregister a dragSource.
072: * @param dragSource the SComponent which is the DragSource to deregister
073: */
074: public void deregisterDragSource(DragSource dragSource) {
075: dragSources.remove(dragSource);
076: namesToComponentsMap
077: .remove(((SComponent) dragSource).getName());
078: }
079:
080: /**
081: * register a dropTarget with the Manager. After registering it can receive
082: * drop events.
083: * @param dropTarget the SComponent which is the DropTarget
084: */
085: public void registerDropTarget(DropTarget dropTarget) {
086: // don't add a component more than once
087: if (!dropTargets.contains(dropTarget)) {
088: dropTargets.add(dropTarget);
089: namesToComponentsMap.put(((SComponent) dropTarget)
090: .getName(), (SComponent) dropTarget);
091: }
092: }
093:
094: /**
095: * deregister a dropTarget.
096: * @param dropTarget the SComponent which is the DropTarget to deregister
097: */
098: public void deregisterDropTarget(DropTarget dropTarget) {
099: dropTargets.remove(dropTarget);
100: namesToComponentsMap
101: .remove(((SComponent) dropTarget).getName());
102: }
103:
104: public SComponent getComponentByName(String name) {
105: return namesToComponentsMap.get(name);
106: }
107:
108: /**
109: * getter for the list of drag sources. Used for initializing them in the
110: * client code.
111: * @return a List of all drag sources
112: */
113: public List<DragSource> getDragSources() {
114: return dragSources;
115: }
116:
117: /**
118: * getter for the list of drop targets. Used for initializing them in the
119: * client code.
120: * @return a List of all drop targets
121: */
122: public List<DropTarget> getDropTargets() {
123: return dropTargets;
124: }
125:
126: /* (non-Javadoc)
127: * @see org.wings.SComponent#processLowLevelEvent(java.lang.String, java.lang.String[])
128: */
129: public void processLowLevelEvent(String name, String[] values) {
130: log.debug("processLowLevelEvent processing");
131: // handle the somehow coded drag to drop connection, look for the components and dispatch
132: sourceName = null;
133: targetName = null;
134: String value = values[0];
135: int index = value.indexOf(':');
136: sourceName = value.substring(0, index);
137: targetName = value.substring(index + 1);
138: log.info("sourcename: " + sourceName);
139: log.info("targetname: " + targetName);
140: SForm.addArmedComponent(this );
141: }
142:
143: /* (non-Javadoc)
144: * @see org.wings.LowLevelEventListener#fireIntermediateEvents()
145: */
146: public void fireIntermediateEvents() {
147: }
148:
149: /* (non-Javadoc)
150: * @see org.wings.SComponent#fireFinalEvents()
151: */
152: public void fireFinalEvents() {
153: log.debug("fireFinalEvents processing");
154: if (sourceName != null && targetName != null) {
155: log.debug("fireFinalEvents processing");
156: DropTarget target = (DropTarget) namesToComponentsMap
157: .get(targetName);
158: DragSource source = (DragSource) namesToComponentsMap
159: .get(sourceName);
160: if (target != null && source != null) {
161: Iterator<SComponentDropListener> listIter = target
162: .getComponentDropListeners().iterator();
163: SComponentDropListener listener;
164: while (listIter.hasNext()) {
165: log.debug("fireFinalEvents listener");
166: listener = listIter.next();
167: // TODO: evaluate return value of handleDrop and visualize it in the client
168: listener.handleDrop((SComponent) source);
169: }
170: ((SComponent) source).reload();
171: ((SComponent) target).reload();
172: }
173: }
174: }
175:
176: /* (non-Javadoc)
177: * @see org.wings.SComponent#isEnabled()
178: */
179: public boolean isEnabled() {
180: return true;
181: }
182:
183: /* (non-Javadoc)
184: * @see org.wings.LowLevelEventListener#isEpochCheckEnabled()
185: */
186: public boolean isEpochCheckEnabled() {
187: return true;
188: }
189: }
|