5858import static io .github .jeddict .ai .util .EditorUtil .getHTMLContent ;
5959import static io .github .jeddict .ai .util .Icons .ICON_ATTACH ;
6060import static io .github .jeddict .ai .util .Icons .ICON_CONTEXT ;
61- import static io .github .jeddict .ai .util .Icons .ICON_COPY ;
6261import static io .github .jeddict .ai .util .Icons .ICON_NEW_CHAT ;
6362import static io .github .jeddict .ai .util .Icons .ICON_NEXT ;
6463import static io .github .jeddict .ai .util .Icons .ICON_PREV ;
65- import static io .github .jeddict .ai .util .Icons .ICON_SAVE ;
6664import static io .github .jeddict .ai .util .Icons .ICON_SEND ;
6765import static io .github .jeddict .ai .util .Icons .ICON_SETTINGS ;
6866import static io .github .jeddict .ai .util .Icons .ICON_STATS ;
69- import static io .github .jeddict .ai .util .Icons .ICON_UPDATE ;
7067import static io .github .jeddict .ai .util .Icons .ICON_WEB ;
7168import io .github .jeddict .ai .util .Labels ;
7269import static io .github .jeddict .ai .util .MimeUtil .MIME_PLAIN_TEXT ;
7976import java .awt .Dimension ;
8077import java .awt .EventQueue ;
8178import java .awt .FlowLayout ;
82- import java .awt .Toolkit ;
83- import java .awt .datatransfer .Clipboard ;
84- import java .awt .datatransfer .StringSelection ;
8579import java .awt .event .ComponentAdapter ;
8680import java .awt .event .ComponentEvent ;
8781import java .io .File ;
8882import java .io .FileWriter ;
8983import java .io .IOException ;
84+ import java .lang .ref .WeakReference ;
9085import java .util .ArrayList ;
9186import java .util .HashSet ;
9287import java .util .List ;
138133 * @author Shiwani Gupta
139134 */
140135public class AssistantChatManager extends JavaFix {
141-
142136
143137 private static final ExecutorService executorService = Executors .newSingleThreadExecutor ();
138+ public static final String ASSISTANT_CHAT_MANAGER_KEY = "ASSISTANT_CHAT_MANAGER_KEY" ;
144139
145140 private TreePath treePath ;
146141 private final Action action ;
@@ -149,7 +144,7 @@ public class AssistantChatManager extends JavaFix {
149144 private JComboBox <String > models ;
150145 private JComboBox <AssistantAction > actionComboBox ;
151146 private Timer timer ;
152- private JButton prevButton , nextButton , openInBrowserButton , submitButton ;//copyButton, saveButton,
147+ private JButton prevButton , nextButton , openInBrowserButton , submitButton ;//copyButton, saveButton,
153148 private AssistantChat topComponent ;
154149 private JEditorPane questionPane ;
155150 private JScrollPane questionScrollPane ;
@@ -258,7 +253,7 @@ protected void performRewrite(JavaFix.TransformationContext tc) throws Exception
258253 @ Override
259254 public void onComplete (String textResponse ) {
260255 Response response = new Response (null , textResponse , messageContextCopy );
261- sourceCode = EditorUtil .updateEditors (null , getProject (),topComponent , response , getContextFiles ());
256+ sourceCode = EditorUtil .updateEditors (null , getProject (), topComponent , response , getContextFiles ());
262257 responseHistory .add (response );
263258 currentResponseIndex = responseHistory .size () - 1 ;
264259 }
@@ -298,7 +293,7 @@ public void askQueryForProjectCommit(Project project, String commitChanges, Stri
298293 this .commitMessage = true ;
299294 handleQuestion (intitalCommitMessage , messageContext , true );
300295 }
301-
296+
302297 private boolean commitMessage , codeReview ;
303298
304299 public void askQueryForCodeReview (Project project , String commitChanges , String contextMessage ) {
@@ -314,6 +309,7 @@ public void displayHtmlContent(String filename, String title) {
314309 Preferences prefs = Preferences .userNodeForPackage (AssistantChat .class );
315310 prefs .putBoolean (AssistantChat .PREFERENCE_KEY , true );
316311 topComponent = new AssistantChat (title , null , getProject ());
312+ topComponent .putClientProperty (ASSISTANT_CHAT_MANAGER_KEY , new WeakReference <>(AssistantChatManager .this ));
317313 JScrollPane scrollPane = new JScrollPane (topComponent .getParentPanel ());
318314 topComponent .add (scrollPane , BorderLayout .CENTER );
319315 topComponent .add (createBottomPanel (null , filename , null ), BorderLayout .SOUTH );
@@ -335,6 +331,7 @@ public void openChat(String type, final String query, String fileName, String ti
335331 prefs .putBoolean (AssistantChat .PREFERENCE_KEY , true );
336332 topComponent = new AssistantChat (title , type , getProject ());
337333 topComponent .setLayout (new BorderLayout ());
334+ topComponent .putClientProperty (ASSISTANT_CHAT_MANAGER_KEY , new WeakReference <>(AssistantChatManager .this ));
338335 JScrollPane scrollPane = new JScrollPane (topComponent .getParentPanel ());
339336 Color bgColor = getBackgroundColorFromMimeType (MIME_PLAIN_TEXT );
340337 boolean isDark = ColorUtil .isDarkColor (bgColor );
@@ -391,7 +388,7 @@ private void initialMessage() {
391388 } catch (Exception ex ) {
392389 Exceptions .printStackTrace (ex );
393390 }
394- }else if ("tweet" .equals (link )) {
391+ } else if ("tweet" .equals (link )) {
395392 try {
396393 java .awt .Desktop .getDesktop ().browse (java .net .URI .create (RandomTweetSelector .getRandomTweet ()));
397394 } catch (Exception ex ) {
@@ -467,7 +464,7 @@ private JPanel createBottomPanel(String type, String fileName, Consumer<String>
467464 openInBrowserButton .setVisible (topComponent .getAllEditorCount () > 0 );
468465 leftButtonPanel .add (openInBrowserButton );
469466
470- Set <String > modelsByProvider = getModelsByProvider (pm .getProvider ()) ;
467+ Set <String > modelsByProvider = getModelsByProvider (pm .getProvider ());
471468 modelsByProvider .add (pm .getModelName ());
472469 models = createStyledComboBox (modelsByProvider .toArray (new String [0 ]));
473470 models .setSelectedItem (pm .getChatModel () != null ? pm .getChatModel () : pm .getModel ());
@@ -479,7 +476,7 @@ private JPanel createBottomPanel(String type, String fileName, Consumer<String>
479476 }
480477 });
481478 leftButtonPanel .add (models );
482-
479+
483480 AssistantAction [] options = AssistantAction .values ();
484481 actionComboBox = createStyledComboBox (options );
485482 actionComboBox .setSelectedItem (AssistantAction .ASK );
@@ -490,7 +487,7 @@ private JPanel createBottomPanel(String type, String fileName, Consumer<String>
490487 if (getProject () == null ) {
491488 Project [] openProjects = org .netbeans .api .project .ui .OpenProjects .getDefault ().getOpenProjects ();
492489 if (openProjects .length == 1 ) {
493- project = openProjects [0 ];
490+ project = openProjects [0 ];
494491 DialogDisplayer .getDefault ().notify (
495492 new NotifyDescriptor .Message (
496493 "Connected chat to project: " + ProjectUtils .getInformation (project ).getDisplayName (),
@@ -557,12 +554,10 @@ public java.awt.Component getListCellRendererComponent(javax.swing.JList<? exten
557554// saveButton.setToolTipText("Save as");
558555// saveButton.setVisible(javaEditorCount == 1);
559556// leftButtonPanel.add(saveButton);
560-
561557// JButton saveToEditorButton = createIconButton(Labels.UPDATE + " " + fileName, ICON_UPDATE);
562558// saveToEditorButton.setToolTipText("Update " + fileName);
563559// saveToEditorButton.setVisible(fileName != null);
564560// leftButtonPanel.add(saveToEditorButton);
565-
566561 JButton showChartsButton = createIconButton (Labels .STATS , ICON_STATS );
567562 showChartsButton .setToolTipText ("Show Token Usage Charts" );
568563 rightButtonPanel .add (showChartsButton );
@@ -618,15 +613,16 @@ public void componentResized(ComponentEvent e) {
618613 updateButton (messageContextButton , showOnlyIcons , ICON_ATTACH , Labels .MESSAGE_CONTEXT + " " + ICON_ATTACH );
619614 updateButton (sessionContextButton , showOnlyIcons , ICON_CONTEXT , Labels .SESSION_CONTEXT + " " + ICON_CONTEXT );
620615 updateButton (submitButton , showOnlyIcons , ICON_SEND , Labels .SEND + " " + ICON_SEND );
621- updateCombobox (models , showOnlyIcons );
622- updateCombobox (actionComboBox , showOnlyIcons );
616+ updateCombobox (models , showOnlyIcons );
617+ updateCombobox (actionComboBox , showOnlyIcons );
623618 topComponent .updateUserPaneButtons (showOnlyIcons );
624619
625620 }
626621
627622 private void updateButton (JButton button , boolean iconOnly , String iconText , String fullText ) {
628623 button .setText (iconOnly ? iconText : fullText );
629624 }
625+
630626 private <T > void updateCombobox (JComboBox <T > comboBox , boolean iconOnly ) {
631627 comboBox .putClientProperty ("minimal" , iconOnly );
632628 }
@@ -694,7 +690,7 @@ public void changedUpdate(DocumentEvent e) {
694690 try {
695691 File latestTempFile = File .createTempFile ("gen-ai" , ".html" );
696692 latestTempFile .deleteOnExit ();
697- try (FileWriter writer = new FileWriter (latestTempFile )) {
693+ try ( FileWriter writer = new FileWriter (latestTempFile )) {
698694 writer .write (topComponent .getAllEditorText ());
699695 }
700696 if (Desktop .isDesktopSupported () && Desktop .getDesktop ().isSupported (Desktop .Action .BROWSE )) {
@@ -749,7 +745,7 @@ public void changedUpdate(DocumentEvent e) {
749745 if (currentResponseIndex > 0 ) {
750746 currentResponseIndex --;
751747 Response historyResponse = responseHistory .get (currentResponseIndex );
752- sourceCode = EditorUtil .updateEditors (queryUpdate , getProject (),topComponent , historyResponse , getContextFiles ());
748+ sourceCode = EditorUtil .updateEditors (queryUpdate , getProject (), topComponent , historyResponse , getContextFiles ());
753749 updateButtons (prevButton , nextButton );
754750 }
755751 });
@@ -758,7 +754,7 @@ public void changedUpdate(DocumentEvent e) {
758754 if (currentResponseIndex < responseHistory .size () - 1 ) {
759755 currentResponseIndex ++;
760756 Response historyResponse = responseHistory .get (currentResponseIndex );
761- sourceCode = EditorUtil .updateEditors (queryUpdate , getProject (),topComponent , historyResponse , getContextFiles ());
757+ sourceCode = EditorUtil .updateEditors (queryUpdate , getProject (), topComponent , historyResponse , getContextFiles ());
762758 updateButtons (prevButton , nextButton );
763759 }
764760 });
@@ -801,7 +797,7 @@ private void startLoading() {
801797 private void stopLoading () {
802798 timer .stop ();
803799 }
804-
800+
805801 private void updateHeight () {
806802 int lineHeight = questionPane .getFontMetrics (questionPane .getFont ()).getHeight ();
807803 String text = questionPane .getText ().trim ();
@@ -860,6 +856,7 @@ private void showFilePathPopup() {
860856
861857 private Future result ;
862858 private JeddictStreamHandler handler ;
859+
863860 private void handleQuestion (String question , Set <FileObject > messageContext , boolean newQuery ) {
864861 result = executorService .submit (() -> {
865862 try {
@@ -903,7 +900,7 @@ public void onComplete(String textResponse) {
903900 topComponent .setReviews (reviews );
904901 response .getBlocks ().clear ();
905902 response .getBlocks ().add (new Block ("web" , web ));
906- }
903+ }
907904 sourceCode = EditorUtil .updateEditors (queryUpdate , getProject (), topComponent , response , getContextFiles ());
908905
909906 stopLoading ();
@@ -988,10 +985,10 @@ public void onComplete(String textResponse) {
988985 }
989986 });
990987 }
991-
988+
992989 private String getModelName () {
993- String modelName = (String )models .getSelectedItem ();
994- if (modelName == null || modelName .isEmpty ()) {
990+ String modelName = (String ) models .getSelectedItem ();
991+ if (modelName == null || modelName .isEmpty ()) {
995992 return pm .getModel ();
996993 }
997994 return modelName ;
@@ -1008,4 +1005,8 @@ private void updateButtons(JButton prevButton, JButton nextButton) {
10081005 openInBrowserButton .setVisible (topComponent .getAllEditorCount () > 0 );
10091006 }
10101007
1011- }
1008+ public void addToSessionContext (List <FileObject > files ) {
1009+ threadContext .addAll (files );
1010+ }
1011+
1012+ }
0 commit comments