44import com .intellij .openapi .application .ApplicationManager ;
55import com .intellij .openapi .components .ProjectComponent ;
66import com .intellij .openapi .project .ProjectManager ;
7+ import com .intellij .openapi .util .Computable ;
78import com .intellij .openapi .vfs .LocalFileSystem ;
89import com .intellij .util .messages .MessageBusConnection ;
910import com .intellij .openapi .vfs .VirtualFileManager ;
1415import com .intellij .openapi .editor .ScrollType ;
1516import com .intellij .openapi .fileEditor .FileEditorManager ;
1617import com .intellij .openapi .vfs .VirtualFile ;
17- import com .intellij .psi .search .FilenameIndex ;
18- import com .intellij .psi .search .GlobalSearchScope ;
1918import com .intellij .psi .PsiFile ;
2019import com .intellij .psi .PsiManager ;
2120import com .intellij .openapi .fileEditor .FileEditorManagerEvent ;
2221import com .intellij .openapi .fileEditor .FileEditorManagerListener ;
22+ import org .java_websocket .WebSocketImpl ;
2323import org .jetbrains .annotations .NotNull ;
2424
2525import core .model .SRCMLHandler ;
2626import core .model .SRCMLxml ;
27+ import org .jetbrains .annotations .Nullable ;
2728
2829
2930import java .util .*;
@@ -44,18 +45,20 @@ public class FileChangeManager implements ProjectComponent {
4445 private List <List <String >> ruleIndexText ; // index - text
4546 private List <List <String >> tagNameText ; // tagName - text
4647 private Project currentProject ;
47- public String projectPath ;
48+ String projectPath ;
4849
49- FileChangeManager (Project project , ChatServer server , SRCMLxml xmlP , List <List <String >> ruleList , List <List <String >> tagList ) {
50- connection = ApplicationManager .getApplication ().getMessageBus ().connect ();
51- s = server ;
52-
53- srcml = xmlP ;
54- ruleIndexText = ruleList ;
55- tagNameText = tagList ;
50+ FileChangeManager (Project project ) {
5651
52+ connection = ApplicationManager .getApplication ().getMessageBus ().connect ();
5753 currentProject = project ;
5854 projectPath = project .getBasePath ();
55+
56+ srcml = new SRCMLxml (FileChangeManager .getFilePaths (project ), project .getBasePath ());
57+ SRCMLHandler .createXMLForProject (srcml );
58+
59+ ruleIndexText = MessageProcessor .getInitialRulesAsList (project );
60+ tagNameText = MessageProcessor .getInitialTagsAsList (project );
61+
5962 }
6063
6164
@@ -140,31 +143,21 @@ SRCMLxml getSrcml() {
140143 //---------------------------
141144
142145 public void initComponent () {
143- s .start ();
144-
145- System .out .println ("(initComponent) ChatServer started on port: " + s .getPort ());
146-
147146 ignoredFiles = utilities .createIgnoredFileList (currentProject );
148-
149147 connection .subscribe (VirtualFileManager .VFS_CHANGES , new BulkFileListener .Adapter () {
150148 @ Override
151149 public void after (@ NotNull List <? extends VFileEvent > events ) {
152-
153150 // update rules in ChatServer
154-
155151 for (VFileEvent event : events ) {
156152 if (shouldIgnoreFile (event .getFile ()))
157153 continue ;
158-
159154 if (event instanceof VFileCreateEvent ) { // Create files
160155 handleVFileCreateEvent (event );
161-
162156 } else if (event instanceof VFileContentChangeEvent ) { // Text Change
163157 handleVFileChangeEvent (event );
164158
165159 } else if (event instanceof VFileDeleteEvent ) { // Delete files
166160 handleVFileDeleteEvent (event );
167-
168161 } else if (event instanceof VFilePropertyChangeEvent ) { // Property Change
169162 handleVFilePropertyChangeEvent (event );
170163 }
@@ -183,46 +176,55 @@ public void selectionChanged(@NotNull FileEditorManagerEvent event) {
183176
184177 /**
185178 * Overridden method
186- * Not working
187179 */
188180 public void disposeComponent () {
189- System .out .println ("(disposeComponent)" );
190- connection .disconnect ();
191- try {
192- s .stop ();
193- } catch (IOException e ) {
194- e .printStackTrace ();
195- } catch (InterruptedException e ) {
196- e .printStackTrace ();
181+ ruleIndexText = new ArrayList <>();
182+ tagNameText = new ArrayList <>();
183+
184+ Project [] AllProjects = ProjectManager .getInstance ().getOpenProjects ();
185+ if (AllProjects .length == 0 ) {
186+ try {
187+ s .stop ();
188+ } catch (IOException e ) {
189+ e .printStackTrace ();
190+ } catch (InterruptedException e ) {
191+ e .printStackTrace ();
192+ }
197193 }
198194 }
199195
200196 @ NotNull
201197 @ Override
202198 public String getComponentName () {
203- System .out .println ("(Component Name)" );
204199 return "" ;
205200 }
206201
207202 // ProjectComponent
208203
209204 /**
210205 * Overridden method
211- * Not working
212206 */
213207 @ Override
214208 public void projectOpened () {
215- System .out .println ("(project Opened)" );
209+ Project [] AllProjects = ProjectManager .getInstance ().getOpenProjects ();
210+ if (AllProjects .length == 1 ) {
211+ try {
212+ WebSocketImpl .DEBUG = false ;
213+ s = new ChatServer (8887 );
214+ s .setManager (this );
215+ s .start ();
216+ } catch (Exception e ) {
217+ System .out .println ("Error in creating a Chat server." );
218+ e .printStackTrace ();
219+ }
220+ }
216221 }
217222
218223 /**
219224 * Overridden method
220- * Not working
221225 */
222226 @ Override
223- public void projectClosed () {
224- System .out .println ("(project Closed)" );
225- }
227+ public void projectClosed () { }
226228
227229 //-------------------------------
228230
@@ -665,16 +667,21 @@ JsonObject generateProjectHierarchyAsJSON() {
665667 propertiesOfChild .addProperty ("isDirectory" , false );
666668 propertiesOfChild .addProperty ("fileType" , childOfItem .getFileType ().getName ());
667669 jsonChildOfItem .add ("properties" , propertiesOfChild );
668- PsiFile psiFile = PsiManager .getInstance (project ).findFile (childOfItem );
669- // propertiesOfChild.addProperty("text", psiFile.getText());
670- // propertiesOfChild.add("ast", generateASTAsJSON(psiFile));
671670
672- try {
673- propertiesOfChild .addProperty ("fileName" , psiFile .getName ());
674- }
675- catch (NullPointerException e ) {
676- // System.out.println("error! null pointer exception");
671+ if (!ApplicationManager .getApplication ().isReadAccessAllowed ()) {
672+ PsiFile psiFile = ApplicationManager .getApplication ().runReadAction (new Computable <PsiFile >() {
673+ @ Nullable
674+ @ Override
675+ public PsiFile compute () {
676+ return PsiManager .getInstance (project ).findFile (childOfItem );
677+ }
678+ });
679+ if (psiFile != null )
680+ propertiesOfChild .addProperty ("fileName" , psiFile .getName ());
677681 }
682+
683+ // propertiesOfChild.addProperty("text", psiFile.getText());
684+ // propertiesOfChild.add("ast", generateASTAsJSON(psiFile));
678685 }
679686 canonicalToJsonMap .get (item .getCanonicalPath ()).get ("children" ).getAsJsonArray ().add (jsonChildOfItem );
680687 canonicalToJsonMap .put (childOfItem .getCanonicalPath (), jsonChildOfItem );
@@ -688,9 +695,4 @@ JsonObject generateProjectHierarchyAsJSON() {
688695
689696 }
690697
691- // static Project getProject() {
692- // DataContext dataContext = DataManager.getInstance().getDataContextFromFocus().getResult();
693- // return DataKeys.PROJECT.getData(dataContext);
694- // }
695-
696698}
0 commit comments