Skip to content

Commit 64681b0

Browse files
committed
Refactor actions and bug fixes
1 parent 0abf83b commit 64681b0

19 files changed

+565
-259
lines changed

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/ExportAction.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@
77

88
package de.fraunhofer.iem.swan.assist.actions;
99

10+
import com.intellij.notification.Notification;
11+
import com.intellij.notification.NotificationType;
12+
import com.intellij.notification.Notifications;
1013
import com.intellij.openapi.actionSystem.AnAction;
1114
import com.intellij.openapi.actionSystem.AnActionEvent;
15+
import com.intellij.openapi.actionSystem.CommonDataKeys;
16+
import com.intellij.openapi.project.Project;
1217
import de.fraunhofer.iem.swan.assist.data.JSONFileLoader;
1318
import de.fraunhofer.iem.swan.assist.data.JSONWriter;
19+
import de.fraunhofer.iem.swan.assist.util.Constants;
20+
import org.jetbrains.annotations.NotNull;
1421

1522
import javax.swing.*;
1623
import javax.swing.filechooser.FileNameExtensionFilter;
24+
import javax.swing.filechooser.FileSystemView;
1725
import java.io.File;
1826
import java.io.IOException;
1927

@@ -28,14 +36,23 @@ public class ExportAction extends AnAction {
2836
* @param anActionEvent source event
2937
*/
3038
@Override
31-
public void actionPerformed(AnActionEvent anActionEvent) {
39+
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
3240

3341
final String FILE_EXTENSION = ".json";
3442
String filePath = "";
3543

36-
JFileChooser fileChooser = new JFileChooser();
44+
final Project project = anActionEvent.getRequiredData(CommonDataKeys.PROJECT);
45+
46+
File projectPath;
47+
48+
if (project.getBasePath() == null)
49+
projectPath = FileSystemView.getFileSystemView().getDefaultDirectory();
50+
else
51+
projectPath = new File(project.getBasePath());
52+
53+
JFileChooser fileChooser = new JFileChooser(projectPath);
3754
fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
38-
fileChooser.setSelectedFile(new File("projectmethods.json"));
55+
fileChooser.setSelectedFile(new File(project.getName() + "-methods.json"));
3956
fileChooser.setFileFilter(new FileNameExtensionFilter("JSON Files", "json"));
4057

4158
int returnValue = fileChooser.showSaveDialog(null);
@@ -53,6 +70,10 @@ public void actionPerformed(AnActionEvent anActionEvent) {
5370
//TODO deal with exception
5471
try {
5572
exportFile.writeToJsonFile(JSONFileLoader.getMethods(), filePath);
73+
74+
Notifications.Bus.notify(
75+
new Notification(Constants.PLUGIN_GROUP_DISPLAY_ID, "", JSONFileLoader.getMethods().size()+ " methods exported to: "+filePath, NotificationType.INFORMATION));
76+
5677
} catch (IOException e) {
5778
e.printStackTrace();
5879
}
@@ -64,7 +85,7 @@ public void actionPerformed(AnActionEvent anActionEvent) {
6485
* @param event source event
6586
*/
6687
@Override
67-
public void update(AnActionEvent event) {
88+
public void update(@NotNull AnActionEvent event) {
6889

6990
//Disable/Enable action button
7091
if (JSONFileLoader.isFileSelected())

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/ImportAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
public class ImportAction extends AnAction {
2626

2727
/**
28-
* Allows user to select configuration file that is then loaded.
28+
* Allows user to select configuration file that is then loaded.
2929
* @param e source event
3030
*/
3131
@Override

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/LaunchSwanAction.java renamed to swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/RunSwanAnalysisAction.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
/**
3232
* Action opens dialog for user to set parameters for running SWAN. After which thread is created to run SWAN.
3333
*/
34-
public class LaunchSwanAction extends AnAction {
34+
public class RunSwanAnalysisAction extends AnAction {
3535

3636
protected Set<Method> methods = new HashSet<Method>();
3737

@@ -72,21 +72,22 @@ public void actionPerformed(AnActionEvent anActionEvent) {
7272

7373
HashMap<String, String> swanParameters = dialog.getParameters();
7474

75-
String outputPath = swanParameters.get(Constants.OUTPUT_DIRECTORY) + File.separator + config.getProperty("input_json_suffix");
76-
TrainingFileManager trainingFileManager = new TrainingFileManager(project);
75+
if(!swanParameters.get(Constants.CONFIGURATION_FILE).contentEquals(config.getProperty("swan_default_param_value"))){
76+
String outputPath = swanParameters.get(Constants.OUTPUT_DIRECTORY) + File.separator + config.getProperty("input_json_suffix");
77+
TrainingFileManager trainingFileManager = new TrainingFileManager(project);
7778

78-
if (trainingFileManager.mergeExport(JSONFileLoader.getAllMethods(), outputPath))
79-
swanParameters.put(Constants.CONFIGURATION_FILE, outputPath);
80-
else
81-
swanParameters.put(Constants.CONFIGURATION_FILE, config.getProperty("swan_default_param_value"));
79+
if (trainingFileManager.mergeExport(JSONFileLoader.getAllMethods(), outputPath))
80+
swanParameters.put(Constants.CONFIGURATION_FILE, outputPath);
81+
}
8282

83-
SwanProcessBuilder processBuilder = new SwanProcessBuilder(project, dialog.getParameters());
84-
processBuilder.start();
83+
RunSwanAnalysisImpl processBuilder = new RunSwanAnalysisImpl(project, dialog.getParameters());
84+
processBuilder.run();
8585

8686
SwanNotifier publisher = messageBus.syncPublisher(SwanNotifier.START_SWAN_PROCESS_TOPIC);
8787
publisher.launchSwan(null);
8888
}
89-
}
89+
90+
}
9091

9192
/**
9293
* Controls whether the action is enabled or disabled
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 Fraunhofer IEM, Paderborn, Germany.
3+
*
4+
* Contributors:
5+
* Oshando Johnson (oshando.johnson@iem.fraunhofer.de ) - initial implementation
6+
******************************************************************************/
7+
8+
package de.fraunhofer.iem.swan.assist.actions;
9+
10+
import com.intellij.openapi.progress.ProgressIndicator;
11+
import com.intellij.openapi.progress.ProgressManager;
12+
import com.intellij.openapi.progress.Task;
13+
import com.intellij.openapi.project.Project;
14+
import com.intellij.util.messages.MessageBus;
15+
import de.fraunhofer.iem.swan.Main;
16+
import de.fraunhofer.iem.swan.assist.comm.SwanNotifier;
17+
import de.fraunhofer.iem.swan.assist.data.JSONFileParser;
18+
import de.fraunhofer.iem.swan.assist.data.MethodWrapper;
19+
import de.fraunhofer.iem.swan.assist.util.Constants;
20+
import org.apache.commons.io.FileUtils;
21+
import org.jetbrains.annotations.NotNull;
22+
23+
import java.io.File;
24+
import java.io.IOException;
25+
import java.time.LocalDateTime;
26+
import java.time.format.DateTimeFormatter;
27+
import java.util.HashMap;
28+
import java.util.ResourceBundle;
29+
30+
/**
31+
* Creates process to run SWAN on a separate thread.
32+
*/
33+
public class RunSwanAnalysisImpl {
34+
35+
private static HashMap<String, String> parameters;
36+
private Project project;
37+
private long duration;
38+
39+
/**
40+
* Initializes builder.
41+
* @param project Project on which the plugin is being used with
42+
* @param param Parameters that will be used as program arguments
43+
*/
44+
RunSwanAnalysisImpl(Project project, HashMap<String, String> param) {
45+
46+
this.project = project;
47+
parameters = param;
48+
}
49+
50+
/**
51+
* Sets up process to run the application and also send notification to subscribers when finished.
52+
*/
53+
public void run() {
54+
55+
ResourceBundle resource = ResourceBundle.getBundle("dialog_messages");
56+
57+
File outputFolder = new File(parameters.get(Constants.OUTPUT_DIRECTORY));
58+
59+
if(!outputFolder.exists())
60+
outputFolder.mkdir();
61+
62+
/*File logFile = new File(outputFolder, currentTimestamp + parameters.get(Constants.OUTPUT_LOG));
63+
try {
64+
logFile.createNewFile();
65+
parameters.replace(Constants.OUTPUT_LOG, logFile.getPath());
66+
67+
FileOutputStream fileOutputStream = new FileOutputStream(logFile.getAbsolutePath());
68+
69+
System.setOut(new PrintStream(fileOutputStream));
70+
71+
} catch (IOException e) {
72+
e.printStackTrace();
73+
}*/
74+
75+
ProgressManager.getInstance().run(new Task.Backgroundable(project, resource.getString("Messages.Title.Progress")) {
76+
@Override
77+
public void run(@NotNull ProgressIndicator indicator) {
78+
79+
long start = System.currentTimeMillis();
80+
81+
Main.main(new String[]{parameters.get(Constants.SOURCE_DIRECTORY),
82+
parameters.get(Constants.TRAIN_DIRECTORY),
83+
parameters.get(Constants.CONFIGURATION_FILE),
84+
parameters.get(Constants.OUTPUT_DIRECTORY)});
85+
86+
duration = System.currentTimeMillis() - start;
87+
}
88+
89+
@Override
90+
public void onCancel() {
91+
super.onCancel();
92+
}
93+
94+
@Override
95+
public void onSuccess() {
96+
super.onSuccess();
97+
98+
System.out.println("FILE: "+parameters.get(Constants.OUTPUT_FILE));
99+
//Create copy of file
100+
copyFile(new File(parameters.get(Constants.OUTPUT_FILE)));
101+
102+
JSONFileParser parser = new JSONFileParser(parameters.get(Constants.OUTPUT_FILE));
103+
HashMap<String, MethodWrapper> exportedMethods = parser.parseJSONFileMap();
104+
105+
HashMap<String, String> results = new HashMap<String, String>();
106+
results.put(Constants.OUTPUT_FILE, parameters.get(Constants.OUTPUT_FILE));
107+
results.put(Constants.OUTPUT_LOG, parameters.get(Constants.OUTPUT_LOG));
108+
109+
int m = (int) (((duration / 1000) / 60) % 60);
110+
int s = (int) ((duration / 1000) % 60);
111+
112+
results.put(Constants.ANALYSIS_RESULT, exportedMethods.size() + " methods found in "+m+" mins "+s+ " secs");
113+
114+
MessageBus messageBus = project.getMessageBus();
115+
SwanNotifier publisher = messageBus.syncPublisher(SwanNotifier.END_SWAN_PROCESS_TOPIC);
116+
publisher.launchSwan(results);
117+
}
118+
});
119+
}
120+
121+
private void copyFile(File original ){
122+
123+
File copied = new File(
124+
parameters.get(Constants.OUTPUT_DIRECTORY) + File.separator +getCurrentTimestamp()+ "-config.json");
125+
try {
126+
FileUtils.copyFile(original, copied);
127+
} catch (IOException e) {
128+
e.printStackTrace();
129+
}
130+
}
131+
132+
/**
133+
* Get the timestamp in a specified format.
134+
* @return Formatted date
135+
*/
136+
private String getCurrentTimestamp() {
137+
138+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HHmmss");
139+
return LocalDateTime.now().format(formatter);
140+
}
141+
}

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/SwanProcessBuilder.java

Lines changed: 0 additions & 94 deletions
This file was deleted.

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/method/RestoreMethodAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import de.fraunhofer.iem.swan.assist.data.MethodWrapper;
1818
import de.fraunhofer.iem.swan.assist.ui.MethodListTree;
1919
import de.fraunhofer.iem.swan.assist.util.PsiTraversal;
20+
import org.jetbrains.annotations.NotNull;
2021

2122
import javax.swing.*;
2223
import java.util.ResourceBundle;
@@ -49,7 +50,7 @@ public RestoreMethodAction(MethodWrapper method) {
4950
* @param anActionEvent source event
5051
*/
5152
@Override
52-
public void actionPerformed(AnActionEvent anActionEvent) {
53+
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
5354

5455
final Project project = anActionEvent.getRequiredData(CommonDataKeys.PROJECT);
5556
ResourceBundle resource = ResourceBundle.getBundle("dialog_messages");

0 commit comments

Comments
 (0)