Skip to content

Commit c8b54be

Browse files
author
saharmehrpour
committed
removed main functionality from tool window, the tool is functional immediately without the need to launch tool window
1 parent 72b7487 commit c8b54be

File tree

4 files changed

+62
-119
lines changed

4 files changed

+62
-119
lines changed

resources/META-INF/plugin.xml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<idea-plugin>
22
<id>active_doc_plugin</id>
33
<name>ActiveDoc Plugin</name>
4-
<version>2.0</version>
4+
<version>3.0</version>
55
<vendor email="[email protected]" url="http://mason.gmu.edu/~smehrpou/">GMU DevUXD</vendor>
66

77
<description><![CDATA[
88
ActiveDocumentation Tool
99
]]></description>
1010

1111
<change-notes><![CDATA[
12-
initial release
12+
The tool window in the plugin is not functional. Instead the plugin runs in background and is accessible in the browser immediately.
1313
]]>
1414
</change-notes>
1515

@@ -22,9 +22,14 @@
2222
<depends>com.intellij.modules.lang</depends>
2323
-->
2424

25+
<project-components>
26+
<component>
27+
<implementation-class>FileChangeManager</implementation-class>
28+
</component>
29+
</project-components>
30+
2531
<extensions defaultExtensionNs="com.intellij">
26-
<!-- Add your extensions here -->
27-
<toolWindow id="ActiveDocs" anchor="right" factoryClass="GrepServerToolWindowFactory"/>
32+
<toolWindow id="ActiveDocs" anchor="right" factoryClass="CustomToolWindowFactory"/>
2833
</extensions>
2934

3035
<actions>

src/ChatServer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.util.LinkedList;
1313
import java.util.List;
1414

15-
/**
15+
/*
1616
* edited by saharmehrpour
1717
*/
1818

@@ -28,7 +28,6 @@ public class ChatServer extends WebSocketServer {
2828
private FileChangeManager manager; // to process received messages
2929

3030
/**
31-
* only used in GrepServerToolWindowFactory.run()
3231
*
3332
* @param port = 8887
3433
* @throws UnknownHostException
Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
* This module creates the GUI for the plugin
44
*/
55

6-
import core.model.SRCMLxml;
7-
import core.model.SRCMLHandler;
8-
96
import javafx.application.Platform;
107
import javafx.embed.swing.JFXPanel;
118
import javafx.scene.Scene;
129
import javafx.scene.layout.StackPane;
1310
import javafx.scene.web.WebEngine;
1411
import javafx.scene.web.WebView;
15-
import org.java_websocket.WebSocketImpl;
1612
import org.jetbrains.annotations.NotNull;
1713

1814
import com.intellij.openapi.project.Project;
@@ -21,13 +17,8 @@
2117

2218
import javax.swing.*;
2319

24-
import java.io.File;
25-
import java.io.IOException;
26-
import java.net.ServerSocket;
27-
28-
public class GrepServerToolWindowFactory implements ToolWindowFactory {
20+
public class CustomToolWindowFactory implements ToolWindowFactory {
2921

30-
private ChatServer chatServer;
3122
private WebEngine webEngine;
3223
private WebView browser;
3324

@@ -37,34 +28,6 @@ public class GrepServerToolWindowFactory implements ToolWindowFactory {
3728
@Override
3829
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
3930

40-
// start the connection when the project is loaded and indexing is done
41-
try {
42-
WebSocketImpl.DEBUG = false;
43-
// int port = findAvailablePort(new int[]{8887, 8888, 8889, 8885, 8886}); // 843 flash policy port
44-
chatServer = new ChatServer(8887);
45-
// System.out.println("-> started on port: " + port);
46-
47-
} catch (Exception e) {
48-
System.out.println("Error in creating a Chat server.");
49-
e.printStackTrace();
50-
}
51-
52-
// This will allow file changes to be sent to the web client
53-
54-
System.out.println("-> project path: " + project.getBasePath());
55-
56-
SRCMLxml srcml = new SRCMLxml(FileChangeManager.getFilePaths(project), project.getBasePath());
57-
SRCMLHandler.createXMLForProject(srcml);
58-
System.out.println("XML data is created.");
59-
60-
FileChangeManager fcm = new FileChangeManager(project, chatServer, srcml/*, MessageProcessor.getInitialRules().toString()*/
61-
, MessageProcessor.getInitialRulesAsList(project)
62-
, MessageProcessor.getInitialTagsAsList(project));
63-
chatServer.setManager(fcm);
64-
65-
fcm.initComponent();
66-
67-
6831
// generate the GUI
6932

7033
System.out.println("(createToolWindowContent)");
@@ -126,31 +89,5 @@ private synchronized void publishServices() {
12689
}
12790

12891

129-
private int findAvailablePort(int[] ports) throws IOException {
130-
ServerSocket tempSocket = null;
131-
boolean found = false;
132-
int foundPort = 0;
133-
for (int port : ports) {
134-
135-
try {
136-
tempSocket = new ServerSocket(port);
137-
found = true;
138-
foundPort = port;
139-
break;
140-
} catch (IOException e) {
141-
//e.printStackTrace();
142-
}
143-
}
144-
145-
if (!found)
146-
throw new IOException("no free port found");
147-
148-
else {
149-
tempSocket.close();
150-
return foundPort;
151-
}
152-
}
153-
154-
15592
}
15693

src/FileChangeManager.java

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.intellij.openapi.application.ApplicationManager;
55
import com.intellij.openapi.components.ProjectComponent;
66
import com.intellij.openapi.project.ProjectManager;
7+
import com.intellij.openapi.util.Computable;
78
import com.intellij.openapi.vfs.LocalFileSystem;
89
import com.intellij.util.messages.MessageBusConnection;
910
import com.intellij.openapi.vfs.VirtualFileManager;
@@ -14,16 +15,16 @@
1415
import com.intellij.openapi.editor.ScrollType;
1516
import com.intellij.openapi.fileEditor.FileEditorManager;
1617
import com.intellij.openapi.vfs.VirtualFile;
17-
import com.intellij.psi.search.FilenameIndex;
18-
import com.intellij.psi.search.GlobalSearchScope;
1918
import com.intellij.psi.PsiFile;
2019
import com.intellij.psi.PsiManager;
2120
import com.intellij.openapi.fileEditor.FileEditorManagerEvent;
2221
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
22+
import org.java_websocket.WebSocketImpl;
2323
import org.jetbrains.annotations.NotNull;
2424

2525
import core.model.SRCMLHandler;
2626
import core.model.SRCMLxml;
27+
import org.jetbrains.annotations.Nullable;
2728

2829

2930
import 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

Comments
 (0)