Skip to content

Commit c05d9af

Browse files
committed
Refactor action communication
1 parent b878fcc commit c05d9af

File tree

11 files changed

+206
-82
lines changed

11 files changed

+206
-82
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import com.intellij.openapi.actionSystem.CommonDataKeys;
1313
import com.intellij.openapi.project.Project;
1414
import com.intellij.util.messages.MessageBus;
15-
import de.fraunhofer.iem.swan.assist.comm.FileSelectedNotifier;
15+
import de.fraunhofer.iem.swan.assist.comm.ConfigurationFileNotifier;
1616

1717
import javax.swing.*;
1818
import javax.swing.filechooser.FileNameExtensionFilter;
@@ -51,8 +51,8 @@ public void actionPerformed(AnActionEvent e) {
5151
File selectedFile = fileChooser.getSelectedFile();
5252

5353
MessageBus messageBus = project.getMessageBus();
54-
FileSelectedNotifier publisher = messageBus.syncPublisher(FileSelectedNotifier.INITIAL_FILE_NOTIFIER_TOPIC);
55-
publisher.notifyFileChange(selectedFile.getAbsoluteFile().toString());
54+
ConfigurationFileNotifier publisher = messageBus.syncPublisher(ConfigurationFileNotifier.FILE_NOTIFIER_TOPIC);
55+
publisher.loadInitialFile(selectedFile.getAbsoluteFile().toString());
5656
}
5757
}
5858
}

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

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,29 @@
1515
import com.intellij.util.messages.MessageBus;
1616
import de.fraunhofer.iem.swan.assist.comm.SwanNotifier;
1717
import de.fraunhofer.iem.swan.assist.data.JSONFileLoader;
18-
import de.fraunhofer.iem.swan.assist.data.JSONFileParser;
19-
import de.fraunhofer.iem.swan.assist.data.JSONWriter;
20-
import de.fraunhofer.iem.swan.assist.data.MethodWrapper;
18+
import de.fraunhofer.iem.swan.assist.data.TrainingFileManager;
2119
import de.fraunhofer.iem.swan.assist.ui.dialog.SwanLauncherDialog;
2220
import de.fraunhofer.iem.swan.assist.util.Constants;
2321
import de.fraunhofer.iem.swan.data.Method;
2422

2523
import java.io.File;
2624
import java.io.IOException;
2725
import java.io.InputStream;
28-
import java.io.InputStreamReader;
29-
import java.util.*;
26+
import java.util.HashMap;
27+
import java.util.HashSet;
28+
import java.util.Properties;
29+
import java.util.Set;
3030

3131
/**
3232
* Action opens dialog for user to set parameters for running SWAN. After which thread is created to run SWAN.
3333
*/
3434
public class LaunchSwanAction extends AnAction {
3535

3636
protected Set<Method> methods = new HashSet<Method>();
37+
3738
/**
3839
* Obtains application parameters from user, exports updated JSON file and starts thread to run SWAN.
40+
*
3941
* @param anActionEvent source event
4042
*/
4143
@Override
@@ -70,35 +72,13 @@ public void actionPerformed(AnActionEvent anActionEvent) {
7072

7173
HashMap<String, String> swanParameters = dialog.getParameters();
7274

73-
if(JSONFileLoader.isFileSelected()) {
74-
//Merge current list with training methods
75-
HashMap<String, MethodWrapper> methods = JSONFileLoader.getAllMethods();
76-
77-
InputStream stream = getClass().getClassLoader().getResourceAsStream(config.getProperty("train_config_file"));
78-
HashMap<String, MethodWrapper> trainingMethods = new HashMap<>();
79-
80-
if (stream != null) {
81-
82-
JSONFileParser fileParser = new JSONFileParser();
83-
trainingMethods = fileParser.parseJSONFileStream(new InputStreamReader(stream));
84-
}
85-
86-
HashMap<String, MethodWrapper> mergedMethods = new HashMap<>(methods);
87-
mergedMethods.putAll(trainingMethods);
75+
String outputPath = swanParameters.get(Constants.SWAN_OUTPUT_DIR) + File.separator + config.getProperty("input_json_suffix");
76+
TrainingFileManager trainingFileManager = new TrainingFileManager(project);
8877

89-
//Export changes to configuration files
90-
JSONWriter exportFile = new JSONWriter();
91-
String newConfigFile = swanParameters.get(Constants.SWAN_OUTPUT_DIR) + File.separator + config.getProperty("input_json_suffix");
92-
try {
93-
exportFile.writeToJsonFile(new ArrayList<>(mergedMethods.values()), newConfigFile);
94-
} catch (IOException e) {
95-
e.printStackTrace();
96-
}
97-
swanParameters.put(Constants.SWAN_CONFIG_FILE, newConfigFile);
98-
}
99-
else{
78+
if (trainingFileManager.mergeExport(JSONFileLoader.getAllMethods(), outputPath))
79+
swanParameters.put(Constants.SWAN_CONFIG_FILE, outputPath);
80+
else
10081
swanParameters.put(Constants.SWAN_CONFIG_FILE, config.getProperty("swan_default_param_value"));
101-
}
10282

10383
SwanProcessBuilder processBuilder = new SwanProcessBuilder(project, dialog.getParameters());
10484
processBuilder.start();
@@ -110,6 +90,7 @@ public void actionPerformed(AnActionEvent anActionEvent) {
11090

11191
/**
11292
* Controls whether the action is enabled or disabled
93+
*
11394
* @param event source event
11495
*/
11596
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public void actionPerformed(AnActionEvent anActionEvent) {
6868
if (confirmation == JOptionPane.YES_OPTION) {
6969

7070
MessageBus messageBus = project.getMessageBus();
71-
MethodNotifier publisher = messageBus.syncPublisher(MethodNotifier.METHOD_REMOVED_TOPIC);
72-
publisher.afterAction(deleteMethod);
71+
MethodNotifier publisher = messageBus.syncPublisher(MethodNotifier.ADD_UPDATE_DELETE_METHOD);
72+
publisher.removeMethod(deleteMethod);
7373
}
7474
} else {
7575
final Editor editor = anActionEvent.getRequiredData(CommonDataKeys.EDITOR);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public void actionPerformed(AnActionEvent anActionEvent) {
6666
//Notify Summary Tool window that new method was restored
6767
method.setUpdateOperation(null);
6868
MessageBus messageBus = project.getMessageBus();
69-
MethodNotifier publisher = messageBus.syncPublisher(MethodNotifier.METHOD_UPDATED_ADDED_TOPIC);
70-
publisher.afterAction(method);
69+
MethodNotifier publisher = messageBus.syncPublisher(MethodNotifier.ADD_UPDATE_DELETE_METHOD);
70+
publisher.restoreMethod(method);
7171
}
7272
}
7373
}

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/comm/FileSelectedNotifier.java renamed to swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/comm/ConfigurationFileNotifier.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
/**
1313
* Notifier for file operation events.
1414
*/
15-
public interface FileSelectedNotifier {
15+
public interface ConfigurationFileNotifier {
1616

17-
Topic<FileSelectedNotifier> INITIAL_FILE_NOTIFIER_TOPIC = Topic.create("Configuration file selected",FileSelectedNotifier.class);
18-
Topic<FileSelectedNotifier> UPDATED_FILE_NOTIFIER_TOPIC = Topic.create("Updated configuration file selected",FileSelectedNotifier.class);
17+
Topic<ConfigurationFileNotifier> FILE_NOTIFIER_TOPIC = Topic.create("Configuration file selected", ConfigurationFileNotifier.class);
1918

2019
/**
2120
* This method will be executed after a file as been selected
2221
* @param fileName Name of configuration file
2322
*/
24-
void notifyFileChange(String fileName);
23+
void loadUpdatedFile(String fileName);
24+
25+
void loadInitialFile(String fileName);
2526

2627
}

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/comm/MethodNotifier.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,27 @@
1010
import com.intellij.util.messages.Topic;
1111
import de.fraunhofer.iem.swan.assist.data.MethodWrapper;
1212

13+
import java.util.ArrayList;
14+
1315
/**
1416
* Notification events for methods.
1517
*/
1618

1719
public interface MethodNotifier {
1820

19-
Topic<MethodNotifier> METHOD_UPDATED_ADDED_TOPIC = Topic.create("Method Updated",MethodNotifier.class);
20-
Topic<MethodNotifier> METHOD_REMOVED_TOPIC = Topic.create("Method Removed",MethodNotifier.class);
21+
Topic<MethodNotifier> ADD_UPDATE_DELETE_METHOD = Topic.create("Method Updated", MethodNotifier.class);
2122

2223
/**
2324
* Sends notification whenever methods are updated, added or removed.
25+
*
2426
* @param method Method that is being modified, added or deleted.
2527
*/
26-
void afterAction(MethodWrapper method);
28+
29+
void removeMethod(MethodWrapper method);
30+
31+
void restoreMethod(MethodWrapper method);
32+
33+
void addNewExistingMethod(MethodWrapper method);
34+
35+
void afterSuggestAction(ArrayList<MethodWrapper> methods);
2736
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package de.fraunhofer.iem.swan.assist.data;
2+
3+
import com.intellij.ide.util.PropertiesComponent;
4+
import com.intellij.openapi.project.Project;
5+
import de.fraunhofer.iem.swan.assist.util.Constants;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.InputStreamReader;
11+
import java.util.ArrayList;
12+
import java.util.HashMap;
13+
import java.util.Properties;
14+
15+
/**
16+
* @author Oshando Johnson on 2019-07-04
17+
*/
18+
public class TrainingFileManager {
19+
20+
private Properties config;
21+
private String trainingFile;
22+
private Project currentProject;
23+
24+
public TrainingFileManager(Project project) {
25+
26+
currentProject = project;
27+
28+
config = new Properties();
29+
InputStream input = null;
30+
31+
try {
32+
input = getClass().getClassLoader().getResourceAsStream("config.properties");
33+
config.load(input);
34+
} catch (IOException e) {
35+
e.printStackTrace();
36+
} finally {
37+
if (input != null) {
38+
try {
39+
input.close();
40+
} catch (IOException e) {
41+
e.printStackTrace();
42+
}
43+
}
44+
}
45+
}
46+
47+
48+
public HashMap<String, MethodWrapper> getTrainingMethods() {
49+
50+
HashMap<String, MethodWrapper> methods = new HashMap<>();
51+
//Check if training file was set and if it exists
52+
if (PropertiesComponent.getInstance(currentProject).isValueSet(Constants.TRAIN_FILE_SUGGESTED)) {
53+
54+
File trainFile = new File(PropertiesComponent.getInstance(currentProject).getValue(Constants.SWAN_OUTPUT_DIR));
55+
56+
if (!trainFile.exists())
57+
PropertiesComponent.getInstance(currentProject).unsetValue(Constants.SWAN_OUTPUT_DIR);
58+
else {
59+
JSONFileParser fileParser = new JSONFileParser(PropertiesComponent.getInstance(currentProject).getValue(Constants.TRAIN_FILE_SUGGESTED));
60+
methods = fileParser.parseJSONFileMap();
61+
}
62+
}
63+
64+
//If training file was not found, thedefault training file will be used
65+
if (methods.isEmpty()) {
66+
InputStream stream = getClass().getClassLoader().getResourceAsStream(config.getProperty("train_config_file"));
67+
68+
if (stream != null) {
69+
70+
JSONFileParser fileParser = new JSONFileParser();
71+
methods = fileParser.parseJSONFileStream(new InputStreamReader(stream));
72+
}
73+
}
74+
return methods;
75+
}
76+
77+
public boolean mergeExport(HashMap<String, MethodWrapper> methods, String configFileName) {
78+
79+
HashMap<String, MethodWrapper> trainingMethods = new HashMap<>();
80+
81+
trainingMethods = getTrainingMethods();
82+
HashMap<String, MethodWrapper> mergedMethods = new HashMap<>(methods);
83+
mergedMethods.putAll(trainingMethods);
84+
85+
//Export changes to configuration files
86+
JSONWriter exportFile = new JSONWriter();
87+
try {
88+
exportFile.writeToJsonFile(new ArrayList<>(mergedMethods.values()), configFileName);
89+
} catch (IOException e) {
90+
e.printStackTrace();
91+
}
92+
93+
File file = new File(configFileName);
94+
95+
trainingFile = file.getAbsolutePath();
96+
97+
return file.exists();
98+
99+
}
100+
101+
public boolean exportNew(HashMap<String, MethodWrapper> methods, String projectPath) {
102+
103+
String filename = projectPath +
104+
File.separator +
105+
config.getProperty("swan_new_training_directory") +
106+
File.separator +
107+
config.getProperty("swan_new_training_filename");
108+
109+
File trainingFile = new File(filename);
110+
111+
if(!trainingFile.exists()){
112+
try {
113+
trainingFile.createNewFile();
114+
} catch (IOException e) {
115+
e.printStackTrace();
116+
}
117+
}
118+
119+
return mergeExport(methods, filename);
120+
}
121+
122+
public String getTrainingFile() {
123+
return trainingFile;
124+
}
125+
}

0 commit comments

Comments
 (0)