forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDragReferenceManager.java
More file actions
160 lines (141 loc) · 4.82 KB
/
Copy pathDragReferenceManager.java
File metadata and controls
160 lines (141 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.chat;
import java.util.List;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.swt.widgets.Composite;
import com.microsoft.copilot.eclipse.ui.chat.services.ReferencedFileService;
import com.microsoft.copilot.eclipse.ui.utils.ResourceUtils;
import com.microsoft.copilot.eclipse.ui.utils.ResourceUtils.SelectionStats;
/**
* Manages drag & drop of workspace resources into the ChatView to become referenced files.
*/
public class DragReferenceManager {
private final ChatView chatView;
private final ReferencedFileService referencedFileService;
private DropTarget dropTarget;
private Transfer[] transfers = new Transfer[] { LocalSelectionTransfer.getTransfer()};
/**
* Create a new DragReferenceManager.
*
* @param chatView the chat view
*/
public DragReferenceManager(ChatView chatView, ReferencedFileService referencedFileService) {
this.chatView = chatView;
this.referencedFileService = referencedFileService;
}
/** Attach DnD to a composite. Re-attach will dispose the previous target. */
public void attach(Composite parent) {
if (parent == null || parent.isDisposed()) {
return;
}
if (dropTarget != null && !dropTarget.isDisposed() && dropTarget.getControl() == parent) {
return;
}
disposeDropTarget();
dropTarget = new DropTarget(parent, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT);
dropTarget.setTransfer(transfers);
dropTarget.addDropListener(new DropTargetListener() {
@Override
public void dragEnter(DropTargetEvent e) {
if (!canAcceptDrop()) {
e.detail = DND.DROP_NONE;
return;
}
if (e.detail == DND.DROP_DEFAULT) {
e.detail = DND.DROP_COPY;
}
}
@Override
public void dragOver(DropTargetEvent e) {
if (!canAcceptDrop()) {
e.detail = DND.DROP_NONE;
return;
}
ISelection sel = LocalSelectionTransfer.getTransfer().getSelection();
if (sel instanceof IStructuredSelection selection) {
SelectionStats stats = ResourceUtils.analyzeSelection(selection);
e.detail = stats.hasOnlyValidResources() ? DND.DROP_COPY : DND.DROP_NONE;
e.feedback = stats.hasOnlyValidResources() ? (DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL) : DND.FEEDBACK_NONE;
return;
}
e.detail = DND.DROP_NONE;
e.feedback = DND.FEEDBACK_NONE;
}
@Override
public void drop(DropTargetEvent event) {
if (!canAcceptDrop()) {
event.detail = DND.DROP_NONE;
return;
}
if (!isSupportedType(event.currentDataType)) {
event.detail = DND.DROP_NONE;
return;
}
List<IResource> resources = extractValidWorkspaceResources(event);
if (resources.isEmpty()) {
return;
}
referencedFileService.addReferencedFiles(resources);
}
@Override
public void dragLeave(DropTargetEvent event) {
}
@Override
public void dragOperationChanged(DropTargetEvent event) {
if (!canAcceptDrop()) {
event.detail = DND.DROP_NONE;
}
}
@Override
public void dropAccept(DropTargetEvent event) {
if (!canAcceptDrop()) {
event.detail = DND.DROP_NONE;
}
}
});
}
/**
* Check if the chat view is in a state where it can accept file drops.
*
* @return true if the chat view can accept drops, false otherwise
*/
private boolean canAcceptDrop() {
Composite actionBar = chatView.getActionBar();
return actionBar != null && !actionBar.isDisposed();
}
private List<IResource> extractValidWorkspaceResources(DropTargetEvent e) {
ISelection sel = LocalSelectionTransfer.getTransfer().getSelection();
if (sel instanceof IStructuredSelection selection) {
return ResourceUtils.collectValidResources(selection);
}
return List.of();
}
private boolean isSupportedType(TransferData data) {
return LocalSelectionTransfer.getTransfer().isSupportedType(data);
}
/**
* Detach and dispose current DropTarget if any.
*/
private void disposeDropTarget() {
if (dropTarget != null && !dropTarget.isDisposed()) {
dropTarget.dispose();
}
dropTarget = null;
}
/**
* Dispose the drag reference manager.
*/
public void dispose() {
disposeDropTarget();
}
}