forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReferencedFile.java
More file actions
239 lines (208 loc) · 7.26 KB
/
Copy pathReferencedFile.java
File metadata and controls
239 lines (208 loc) · 7.26 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.chat;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.e4.ui.css.swt.CSSSWTConstants;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import com.microsoft.copilot.eclipse.core.Constants;
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 two buttons.
*/
public class ReferencedFile extends Composite {
private Label lblfileIcon;
protected Label lblFileName;
protected Label lblClose;
private Image lblImage;
private IResource file;
private boolean isUnSupportedFile = false;
// make it static to avoid creating multiple instances of the same label provider
protected static WorkbenchLabelProvider labelProvider = new WorkbenchLabelProvider();
/**
* Creates a new TwinButton.
*/
public ReferencedFile(Composite parent, IResource file, boolean isUnSupportedFile) {
super(parent, SWT.BORDER);
this.isUnSupportedFile = isUnSupportedFile;
GridLayout layout = new GridLayout(3, false);
layout.marginWidth = 4;
layout.marginHeight = 2;
setLayout(layout);
lblfileIcon = new Label(this, SWT.NONE);
lblfileIcon.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
lblFileName = new Label(this, SWT.NONE);
lblFileName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
// Register for chat font updates via centralized service
registerControlForFontUpdates(lblFileName);
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
openOrRevealFile();
}
};
lblFileName.addMouseListener(mouseAdapter);
lblfileIcon.addMouseListener(mouseAdapter);
this.addMouseListener(mouseAdapter);
lblClose = new Label(this, SWT.NONE);
lblClose.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
setCloseClickAction();
lblImage = UiUtils.buildImageFromPngPath("/icons/close.png");
setFile(file);
this.addDisposeListener(e -> {
if (lblImage != null && !lblImage.isDisposed()) {
lblImage.dispose();
}
});
this.setCursor(getDisplay().getSystemCursor(SWT.CURSOR_HAND));
// Add keyboard support for Enter activation
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
openOrRevealFile();
}
}
});
AccessibilityUtils.addFocusBorderToComposite(this);
}
/**
* Set the mouse click adapter for the close button.
*/
protected void setCloseClickAction() {
lblClose.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
ReferencedFileService referencedFileService = CopilotUi.getPlugin().getChatServiceManager()
.getReferencedFileService();
referencedFileService.removeReferencedFile(file);
}
});
}
/**
* Set the icon for the close button.
*/
protected void setCloseClickBtnIcon(Image image) {
lblClose.setImage(image);
}
public IResource getFile() {
return file;
}
/**
* Returns whether this file is unsupported by the current model.
*/
public boolean isFileUnSupported() {
return isUnSupportedFile;
}
/**
* Set the file for this widget.
*/
protected void setFile(@Nullable IResource file) {
this.file = file;
RowData layoutData = getLayoutData() == null ? new RowData() : (RowData) getLayoutData();
if (file == null) {
// Hide the file name label
layoutData.exclude = true;
layoutData.width = 0;
layoutData.height = 0;
setVisible(false);
} else {
lblFileName.setText(file.getName());
lblClose.setImage(lblImage);
if (isUnSupportedFile) {
setupUnsupportedFileDisplay();
} else {
setupNormalFileDisplay();
}
// Show the file name label
layoutData.exclude = false;
layoutData.width = SWT.DEFAULT;
layoutData.height = SWT.DEFAULT;
setVisible(true);
}
setLayoutData(layoutData);
ChatView chatView = UiUtils.getView(Constants.CHAT_VIEW_ID, ChatView.class);
if (chatView != null) {
chatView.layout(true, true);
}
}
/**
* Setup display for unsupported files (e.g., images without vision support).
*/
private void setupUnsupportedFileDisplay() {
// Set warning icon
lblfileIcon.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_WARN_TSK));
// Set tooltip with model name
String modelName = CopilotUi.getPlugin().getChatServiceManager().getModelService().getActiveModel()
.getModelName();
String tooltipText = String.format(Messages.chat_referencedFile_noVision_tooltip, modelName);
lblfileIcon.setToolTipText(tooltipText);
lblFileName.setToolTipText(tooltipText);
lblClose.setToolTipText(tooltipText);
lblFileName.setData(CSSSWTConstants.CSS_ID_KEY, "not-supported-referenced-file-name");
lblFileName.addPaintListener(e -> {
String text = lblFileName.getText();
Point textSize = e.gc.textExtent(text);
int y = textSize.y / 2;
e.gc.setLineWidth(1);
e.gc.drawLine(0, y, textSize.x, y);
});
}
/**
* Setup display for normal supported files.
*/
private void setupNormalFileDisplay() {
Image image = labelProvider.getImage(file);
if (image != null) {
lblfileIcon.setImage(image);
}
lblFileName.setData(CSSSWTConstants.CSS_ID_KEY, "normal-referenced-file-name");
}
/**
* Opens the file in an editor or reveals the folder in the explorer.
*/
private void openOrRevealFile() {
if (file instanceof IFile) {
UiUtils.openInEditor((IFile) file);
} else if (file instanceof IFolder) {
UiUtils.revealInExplorer(file);
}
}
/**
* Registers a control for chat font updates via the centralized ChatFontService.
*
* @param control the control to register
*/
protected void registerControlForFontUpdates(org.eclipse.swt.widgets.Control control) {
var chatServiceManager = CopilotUi.getPlugin().getChatServiceManager();
if (chatServiceManager != null) {
chatServiceManager.getChatFontService().registerControl(control);
}
}
/**
* Dispose the label provider.
*/
static void disposeLabelProvider() {
labelProvider.dispose();
}
}