forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddContextButton.java
More file actions
112 lines (102 loc) · 4.02 KB
/
Copy pathAddContextButton.java
File metadata and controls
112 lines (102 loc) · 4.02 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.chat;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import com.microsoft.copilot.eclipse.ui.CopilotUi;
import com.microsoft.copilot.eclipse.ui.chat.services.ReferencedFileService;
import com.microsoft.copilot.eclipse.ui.i18n.Messages;
import com.microsoft.copilot.eclipse.ui.utils.AccessibilityUtils;
import com.microsoft.copilot.eclipse.ui.utils.UiUtils;
/**
* A widget that displays a button to attach context.
*/
public class AddContextButton extends Composite {
private Button btnAttachIcon;
/**
* Creates a new AddContextButton.
*/
public AddContextButton(Composite parent) {
super(parent, SWT.NONE);
GridLayout layout = new GridLayout(1, false);
layout.marginWidth = 0;
layout.marginHeight = 0;
setLayout(layout);
btnAttachIcon = UiUtils.createIconButton(this, SWT.PUSH | SWT.FLAT);
Image attachImage = UiUtils.buildImageFromPngPath("/icons/chat/attach_context.png");
btnAttachIcon.setImage(attachImage);
String attachTooltip = Messages.chat_addContext_tooltip;
btnAttachIcon.setToolTipText(attachTooltip);
AccessibilityUtils.addAccessibilityNameForUiComponent(btnAttachIcon, attachTooltip);
btnAttachIcon.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
openFilePickerAndAddFiles();
}
});
btnAttachIcon.addDisposeListener(e -> {
if (attachImage != null && !attachImage.isDisposed()) {
attachImage.dispose();
}
});
}
/**
* Opens the file picker dialog and adds the selected files to the referenced files.
*/
private void openFilePickerAndAddFiles() {
List<IResource> files = selectFiles();
ReferencedFileService fileService = CopilotUi.getPlugin().getChatServiceManager().getReferencedFileService();
fileService.addReferencedFiles(files);
}
/**
* Popup a file picker dialog to select files. It's guaranteed that the selected files are unique.
*/
@NonNull
private List<IResource> selectFiles() {
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IContainer container = root.getContainerForLocation(root.getLocation());
AttachFileSelectionDialog dialog = new AttachFileSelectionDialog(shell, true, container);
dialog.setTitle(Messages.chat_filePicker_title);
dialog.setMessage(Messages.chat_filePicker_message);
List<IResource> result = new ArrayList<>();
if (dialog.open() == Window.OK) {
Object[] selectedFiles = dialog.getResult();
Set<String> selectedFileUris = new HashSet<>();
for (Object selectedFile : selectedFiles) {
if (selectedFile instanceof IFile file) {
URI fileUri = file.getLocationURI();
if (fileUri != null && selectedFileUris.add(fileUri.toASCIIString())) {
result.add(file);
}
} else if (selectedFile instanceof IFolder folder) {
URI folderUri = folder.getLocationURI();
if (folderUri != null && selectedFileUris.add(folderUri.toASCIIString())) {
result.add(folder);
}
}
}
}
return result;
}
}