Started to implement DnD

This commit is contained in:
ejentzsx 2015-03-16 11:12:18 +01:00
parent af1f21cac9
commit eb726a366f
2 changed files with 69 additions and 25 deletions

View File

@ -12,6 +12,8 @@ package com.minres.scviewer.database.swt;
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -19,6 +21,7 @@ import java.util.TreeMap;
import java.util.Vector; import java.util.Vector;
import org.eclipse.core.runtime.ListenerList; import org.eclipse.core.runtime.ListenerList;
import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.jface.viewers.ISelectionProvider;
@ -100,13 +103,15 @@ public class TxDisplay implements PropertyChangeListener, ISelectionProvider, Mo
private Composite trackPane; private Composite trackPane;
private Ruler ruler; private Ruler ruler;
TreeMap<Integer, IWaveform<? extends IWaveformEvent>> trackVerticalOffset; private int trackVerticalHeight;
HashMap<IWaveform<? extends IWaveformEvent>, String> actualValues; private TreeMap<Integer, IWaveform<? extends IWaveformEvent>> trackVerticalOffset;
private HashMap<IWaveform<? extends IWaveformEvent>, String> actualValues;
// private long maxTime=0; // private long maxTime=0;
private Font nameFont, nameFontB; private Font nameFont, nameFontB;
public TxDisplay(Composite parent) { public TxDisplay(Composite parent) {
trackVerticalOffset = new TreeMap<Integer, IWaveform<? extends IWaveformEvent>>(); trackVerticalOffset = new TreeMap<Integer, IWaveform<? extends IWaveformEvent>>();
trackVerticalHeight=0;
actualValues = new HashMap<IWaveform<? extends IWaveformEvent>, String>(); actualValues = new HashMap<IWaveform<? extends IWaveformEvent>, String>();
cursorPainters = new Vector<CursorPainter>(); cursorPainters = new Vector<CursorPainter>();
@ -267,14 +272,14 @@ public class TxDisplay implements PropertyChangeListener, ISelectionProvider, Mo
@Override @Override
public void propertyChange(PropertyChangeEvent pce) { public void propertyChange(PropertyChangeEvent pce) {
if ("size".equals(pce.getPropertyName())) { if ("size".equals(pce.getPropertyName()) || "content".equals(pce.getPropertyName())) {
updateTracklist(); updateTracklist();
} }
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected void updateTracklist() { protected void updateTracklist() {
int yoffs = 0; int trackVerticalHeight = 0;
int nameMaxWidth = 0; int nameMaxWidth = 0;
int previousHeight = trackVerticalOffset.size() == 0 ? 0 : trackVerticalOffset.lastKey(); int previousHeight = trackVerticalOffset.size() == 0 ? 0 : trackVerticalOffset.lastKey();
IWaveformPainter painter = null; IWaveformPainter painter = null;
@ -294,17 +299,17 @@ public class TxDisplay implements PropertyChangeListener, ISelectionProvider, Mo
painter = new SignalPainter(trackList, even, height, (ISignal<?>) waveform); painter = new SignalPainter(trackList, even, height, (ISignal<?>) waveform);
actualValues.put(waveform, "---"); actualValues.put(waveform, "---");
} }
trackList.addWavefromPainter(yoffs, painter); trackList.addWavefromPainter(trackVerticalHeight, painter);
trackVerticalOffset.put(yoffs, waveform); trackVerticalOffset.put(trackVerticalHeight, waveform);
tl.setText(waveform.getFullName()); tl.setText(waveform.getFullName());
nameMaxWidth = Math.max(nameMaxWidth, tl.getBounds().width); nameMaxWidth = Math.max(nameMaxWidth, tl.getBounds().width);
yoffs += height; trackVerticalHeight += height;
even = !even; even = !even;
} }
nameList.setSize(nameMaxWidth + 15, yoffs); nameList.setSize(nameMaxWidth + 15, trackVerticalHeight);
nameListScrolled.setMinSize(nameMaxWidth + 15, yoffs); nameListScrolled.setMinSize(nameMaxWidth + 15, trackVerticalHeight);
valueList.setSize(calculateValueWidth(), yoffs); valueList.setSize(calculateValueWidth(), trackVerticalHeight);
valueListScrolled.setMinSize(calculateValueWidth(), yoffs); valueListScrolled.setMinSize(calculateValueWidth(), trackVerticalHeight);
nameList.redraw(); nameList.redraw();
valueList.redraw(); valueList.redraw();
trackList.redraw(); trackList.redraw();
@ -572,23 +577,24 @@ public class TxDisplay implements PropertyChangeListener, ISelectionProvider, Mo
} }
private void createDragSource(final Canvas sourceText) { private void createDragSource(final Canvas sourceText) {
Transfer[] types = new Transfer[] { TextTransfer.getInstance() }; Transfer[] types = new Transfer[] { LocalSelectionTransfer.getTransfer() };
DragSource dragSource = new DragSource(sourceText, DND.DROP_MOVE); DragSource dragSource = new DragSource(sourceText, DND.DROP_MOVE);
dragSource.setTransfer(types); dragSource.setTransfer(types);
dragSource.addDragListener(new DragSourceListener() { dragSource.addDragListener(new DragSourceListener() {
public void dragStart(DragSourceEvent event) { public void dragStart(DragSourceEvent event) {
if (event.y < trackVerticalOffset.lastKey() + trackList.getTrackHeight()) { if (event.y < trackVerticalHeight) {
// event.data = // event.data =
// trackVerticalOffset.floorEntry(event.y).getValue().getFullName(); // trackVerticalOffset.floorEntry(event.y).getValue().getFullName();
event.doit = true; event.doit = true;
LocalSelectionTransfer.getTransfer().setSelection(getSelection());
// System.out.println("dragStart at location "+new // System.out.println("dragStart at location "+new
// Point(event.x, event.y)); // Point(event.x, event.y));
} }
} }
public void dragSetData(DragSourceEvent event) { public void dragSetData(DragSourceEvent event) {
event.data = trackVerticalOffset.floorEntry(event.y).getValue().getFullName(); event.data =getSelection();
// System.out.println("dragSetData with data " + event.data); // System.out.println("dragSetData with data " + event.data);
} }
@ -599,7 +605,7 @@ public class TxDisplay implements PropertyChangeListener, ISelectionProvider, Mo
} }
private void createDropTarget(final Canvas targetText) { private void createDropTarget(final Canvas targetText) {
Transfer[] types = new Transfer[] { TextTransfer.getInstance() }; Transfer[] types = new Transfer[] { LocalSelectionTransfer.getTransfer() };
DropTarget dropTarget = new DropTarget(targetText, DND.DROP_MOVE); DropTarget dropTarget = new DropTarget(targetText, DND.DROP_MOVE);
dropTarget.setTransfer(types); dropTarget.setTransfer(types);
@ -618,14 +624,27 @@ public class TxDisplay implements PropertyChangeListener, ISelectionProvider, Mo
} }
public void drop(DropTargetEvent event) { public void drop(DropTargetEvent event) {
Object data = event.data; if (LocalSelectionTransfer.getTransfer().isSupportedType(event.currentDataType)){
ISelection sel = LocalSelectionTransfer.getTransfer().getSelection();
if(sel!=null && sel instanceof IStructuredSelection){
Object source = ((IStructuredSelection)sel).getFirstElement();
DropTarget tgt = (DropTarget) event.widget; DropTarget tgt = (DropTarget) event.widget;
Point dropPoint = ((Canvas) tgt.getControl()).toControl(event.x, event.y); Point dropPoint = ((Canvas) tgt.getControl()).toControl(event.x, event.y);
String fullname1 = trackVerticalOffset.floorEntry(dropPoint.y).getValue().getFullName(); Object target = trackVerticalOffset.floorEntry(dropPoint.y).getValue();
// System.out.println("drop with data '" + event.data if(source instanceof IWaveform<?> && target instanceof IWaveform<?>){
// +"' at location "+dropPoint + " and origin " + // int srcIdx=streams.indexOf(source);
// nameListScrolled.getOrigin()); // int tgtIdx=streams.indexOf(target);
// System.out.println("drop on " + fullname1); // if(srcIdx<tgtIdx){
// streams.rotate(srcIdx, tgtIdx, -1);
// } else {
// streams.rotate(tgtIdx, srcIdx+1, 1);
// }
streams.remove(source);
int tgtIdx=streams.indexOf(target);
streams.add(tgtIdx, (IWaveform<? extends IWaveformEvent>) source);
}
}
}
} }
public void dropAccept(DropTargetEvent event) { public void dropAccept(DropTargetEvent event) {

View File

@ -280,6 +280,11 @@ public class ObservableList<E> implements List<E> {
return this.delegate.subList(fromIndex, toIndex); return this.delegate.subList(fromIndex, toIndex);
} }
public void rotate(int fromIndex, int toIndex, int distance){
Collections.rotate(this.delegate.subList(fromIndex, toIndex), distance);
fireElementEvent(new MultiElementUpdatedEvent(this, this.delegate.subList(fromIndex, toIndex)));
}
public Object[] toArray() { public Object[] toArray() {
return this.delegate.toArray(); return this.delegate.toArray();
} }
@ -316,6 +321,26 @@ public class ObservableList<E> implements List<E> {
return this.pcs.hasListeners(propertyName); return this.pcs.hasListeners(propertyName);
} }
public static class MultiElementUpdatedEvent extends ObservableList.ElementEvent {
/**
*
*/
private static final long serialVersionUID = 7819626246672640599L;
private List<Object> values = new ArrayList<Object>();
public MultiElementUpdatedEvent(Object source, List<?> values) {
super(source, ObservableList.ChangeType.oldValue, ObservableList.ChangeType.newValue, 0,
ObservableList.ChangeType.MULTI_UPDATED);
if (values != null)
this.values.addAll(values);
}
public List<?> getValues() {
return Collections.unmodifiableList(this.values);
}
}
public static class MultiElementRemovedEvent extends ObservableList.ElementEvent { public static class MultiElementRemovedEvent extends ObservableList.ElementEvent {
/** /**
* *
@ -439,7 +464,7 @@ public class ObservableList<E> implements List<E> {
} }
public static enum ChangeType { public static enum ChangeType {
ADDED, UPDATED, REMOVED, CLEARED, MULTI_ADD, MULTI_REMOVE, NONE; ADDED, UPDATED, REMOVED, CLEARED, MULTI_ADD, MULTI_UPDATED, MULTI_REMOVE, NONE;
public static final Object oldValue; public static final Object oldValue;
public static final Object newValue; public static final Object newValue;