forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentStatusLabel.java
More file actions
150 lines (133 loc) · 4.62 KB
/
Copy pathAgentStatusLabel.java
File metadata and controls
150 lines (133 loc) · 4.62 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.chat;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.osgi.service.event.EventHandler;
import com.microsoft.copilot.eclipse.core.events.CopilotEventConstants;
import com.microsoft.copilot.eclipse.ui.swt.CssConstants;
import com.microsoft.copilot.eclipse.ui.swt.SpinnerAnimator;
import com.microsoft.copilot.eclipse.ui.utils.AccessibilityUtils;
import com.microsoft.copilot.eclipse.ui.utils.UiUtils;
/**
* A label with icon that displays the running status of the agent.
*/
public class AgentStatusLabel extends Composite {
private Image completedIcon;
private Image cancelledIcon;
private Label iconLabel;
private ChatMarkupViewer textLabel;
private SpinnerAnimator spinner;
private Status status;
private EventHandler cancelStatusHandler;
private IEventBroker eventBroker;
/**
* Create the composite.
*
* @param parent the parent composite
* @param style the style
*/
public AgentStatusLabel(Composite parent, int style) {
super(parent, style);
GridLayout layout = new GridLayout(2, false);
layout.marginWidth = 0;
layout.marginHeight = 0;
layout.horizontalSpacing = 0;
setLayout(layout);
setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
this.addDisposeListener(e -> {
if (this.completedIcon != null && !this.completedIcon.isDisposed()) {
this.completedIcon.dispose();
}
if (this.cancelledIcon != null && !this.cancelledIcon.isDisposed()) {
this.cancelledIcon.dispose();
}
if (this.eventBroker != null) {
this.eventBroker.unsubscribe(cancelStatusHandler);
}
});
iconLabel = new Label(this, SWT.LEFT);
spinner = new SpinnerAnimator(iconLabel);
this.status = Status.RUNNING;
this.cancelStatusHandler = new EventHandler() {
@Override
public void handleEvent(org.osgi.service.event.Event event) {
setCancelledStatus();
}
};
this.eventBroker = PlatformUI.getWorkbench().getService(IEventBroker.class);
this.eventBroker.subscribe(CopilotEventConstants.TOPIC_CHAT_MESSAGE_CANCELLED, cancelStatusHandler);
}
/**
* Set the status as completed for the agent with a status message.
*
* @param statusText the text to display when the agent is completed
*/
public void setCompletedStatus(String statusText) {
spinner.stop();
if (this.completedIcon == null) {
this.completedIcon = UiUtils.buildImageFromPngPath("/icons/complete_status.png");
}
iconLabel.setImage(completedIcon);
setText(statusText);
this.status = Status.COMPLETED;
}
/**
* Set the status as running for the agent with a rotating spinner and a status message.
*
* @param statusText the text to display when the agent is running
*/
public void setRunningStatus(String statusText) {
spinner.start();
setText(statusText);
this.status = Status.RUNNING;
}
/**
* Set the error status for the agent with a status message.
*/
public void setErrorStatus() {
if (this.status == Status.RUNNING) {
spinner.stop();
}
iconLabel.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_ERROR_TSK));
this.status = Status.ERROR;
}
/**
* Cancel the current running status of the agent status label.
*/
public void setCancelledStatus() {
if (this.status == Status.RUNNING) {
spinner.stop();
if (this.cancelledIcon == null) {
this.cancelledIcon = UiUtils.buildImageFromPngPath("/icons/cancel_status.png");
}
iconLabel.setImage(cancelledIcon);
this.status = Status.CANCELLED;
}
}
/**
* Set the text to display next to the icon.
*/
public void setText(String text) {
if (this.textLabel == null) {
textLabel = new ChatMarkupViewer(this, SWT.LEFT | SWT.WRAP);
StyledText styledText = textLabel.getTextWidget();
styledText.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true));
styledText.setEditable(false);
styledText.setData(CssConstants.CSS_CLASS_NAME_KEY, "text-secondary");
AccessibilityUtils.addFocusBorderToComposite(styledText);
}
textLabel.setMarkup(text);
}
private enum Status {
RUNNING, COMPLETED, ERROR, CANCELLED
}
}