01: package desktop;
02:
03: import org.wings.*;
04: import org.wings.dnd.DropTarget;
05: import org.wings.event.SComponentDropListener;
06: import org.wings.session.SessionManager;
07: import org.wings.style.CSSProperty;
08:
09: import java.util.ArrayList;
10: import java.util.List;
11: import java.util.prefs.Preferences;
12:
13: public class DropLabel extends SLabel implements DropTarget {
14:
15: private ArrayList<SComponentDropListener> componentDropListeners = new ArrayList<SComponentDropListener>();
16: private Preferences pref;
17:
18: private static org.wings.util.SessionLocal<Integer> labelNo = new org.wings.util.SessionLocal<Integer>() {
19: @Override
20: protected Integer initialValue() {
21: return 0;
22: }
23: };
24:
25: public DropLabel() {
26: super ();
27: this .setName("droplabel" + labelNo.get().toString());
28: labelNo.set(labelNo.get() + 1);
29:
30: pref = Preferences.userRoot();
31:
32: this .setVerticalAlignment(SConstants.TOP_ALIGN);
33: this .setAttribute(CSSProperty.HEIGHT, "200px");
34: this .setAttribute(CSSProperty.WIDTH, "100%");
35: this .setEnabled(true);
36: this .setVisible(true);
37:
38: addComponentDropListener(new SComponentDropListener() {
39: public boolean handleDrop(SComponent dragSource) {
40: DesktopPane sourcePane = (DesktopPane) dragSource
41: .getParent();
42: DesktopPane targetPane = (DesktopPane) DropLabel.this
43: .getParent();
44: int sourceIndex = sourcePane.getIndexOf(dragSource);
45:
46: if (sourcePane == targetPane
47: && sourcePane.getIndexOf(dragSource) == sourcePane
48: .getComponentCount() - 2)
49: return false;
50:
51: DesktopItem item = ((InternalDesktopFrame) dragSource)
52: .getItem();
53:
54: sourcePane.remove(dragSource);
55: int inputPos = targetPane.getComponentCount() - 1;
56: targetPane.add(dragSource, inputPos);
57:
58: for (int i = sourceIndex; i < sourcePane
59: .getComponentCount() - 1; i++) {
60: DesktopItem it = ((InternalDesktopFrame) sourcePane
61: .getComponent(i)).getItem();
62: pref.node("desktopitems").node(
63: (String) it.getValue(DesktopItem.KEY))
64: .putInt(DesktopItem.POSITION_ON_PANE, i);
65: }
66:
67: pref.node("desktopitems").node(
68: (String) item.getValue(DesktopItem.KEY)).put(
69: DesktopItem.DESKTOPPANE, targetPane.getName());
70: pref.node("desktopitems").node(
71: (String) item.getValue(DesktopItem.KEY))
72: .putInt(DesktopItem.POSITION_ON_PANE, inputPos);
73: return true;
74: }
75: });
76:
77: }
78:
79: public void addComponentDropListener(SComponentDropListener listener) {
80: componentDropListeners.add(listener);
81: SessionManager.getSession().getDragAndDropManager()
82: .registerDropTarget(this );
83: }
84:
85: public List<SComponentDropListener> getComponentDropListeners() {
86: return componentDropListeners;
87: }
88:
89: }
|