Skip to content

Commit 61e0a7f

Browse files
committed
Implement plugin settings persistence
1 parent bb1cd06 commit 61e0a7f

File tree

11 files changed

+147
-58
lines changed

11 files changed

+147
-58
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import de.fraunhofer.iem.swan.assist.comm.SwanNotifier;
1717
import de.fraunhofer.iem.swan.assist.data.JSONFileLoader;
1818
import de.fraunhofer.iem.swan.assist.data.TrainingFileManager;
19-
import de.fraunhofer.iem.swan.assist.ui.dialog.SwanLauncherDialog;
19+
import de.fraunhofer.iem.swan.assist.ui.dialog.PluginSettingsDialog;
2020
import de.fraunhofer.iem.swan.assist.util.Constants;
2121
import de.fraunhofer.iem.swan.data.Method;
2222

@@ -65,20 +65,20 @@ public void actionPerformed(AnActionEvent anActionEvent) {
6565
}
6666

6767
//Launch Dialog
68-
SwanLauncherDialog dialog = new SwanLauncherDialog(project, true);
68+
PluginSettingsDialog dialog = new PluginSettingsDialog(project, true);
6969
dialog.show();
7070

7171
if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
7272

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

75-
String outputPath = swanParameters.get(Constants.SWAN_OUTPUT_DIR) + File.separator + config.getProperty("input_json_suffix");
75+
String outputPath = swanParameters.get(Constants.OUTPUT_DIRECTORY) + File.separator + config.getProperty("input_json_suffix");
7676
TrainingFileManager trainingFileManager = new TrainingFileManager(project);
7777

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

8383
SwanProcessBuilder processBuilder = new SwanProcessBuilder(project, dialog.getParameters());
8484
processBuilder.start();

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ public void run() {
4848

4949
String currentTimestamp = getCurrentTimestamp();
5050

51-
File outputFolder = new File(parameters.get(Constants.SWAN_OUTPUT_DIR));
51+
File outputFolder = new File(parameters.get(Constants.OUTPUT_DIRECTORY));
5252

5353
if(!outputFolder.exists())
5454
outputFolder.mkdir();
5555

56-
File logFile = new File(outputFolder, currentTimestamp + parameters.get(Constants.SWAN_OUTPUT_LOG));
56+
File logFile = new File(outputFolder, currentTimestamp + parameters.get(Constants.OUTPUT_LOG));
5757
try {
5858
logFile.createNewFile();
59-
parameters.replace(Constants.SWAN_OUTPUT_LOG, logFile.getPath());
59+
parameters.replace(Constants.OUTPUT_LOG, logFile.getPath());
6060

6161
FileOutputStream fileOutputStream = new FileOutputStream(logFile.getAbsolutePath());
6262

@@ -66,16 +66,16 @@ public void run() {
6666
e.printStackTrace();
6767
}
6868

69-
Main.main(new String[]{parameters.get(Constants.SWAN_SOURCE_DIR),
70-
parameters.get(Constants.SWAN_TRAIN_DIR),
71-
parameters.get(Constants.SWAN_CONFIG_FILE),
72-
parameters.get(Constants.SWAN_OUTPUT_DIR)});
69+
Main.main(new String[]{parameters.get(Constants.SOURCE_DIRECTORY),
70+
parameters.get(Constants.TRAIN_DIRECTORY),
71+
parameters.get(Constants.CONFIGURATION_FILE),
72+
parameters.get(Constants.OUTPUT_DIRECTORY)});
7373

7474
System.setOut(System.out);
7575

7676
HashMap<String, String> results = new HashMap<String, String>();
77-
results.put(Constants.SWAN_OUTPUT_FILE, parameters.get(Constants.SWAN_OUTPUT_FILE));
78-
results.put(Constants.SWAN_OUTPUT_LOG, parameters.get(Constants.SWAN_OUTPUT_LOG));
77+
results.put(Constants.OUTPUT_FILE, parameters.get(Constants.OUTPUT_FILE));
78+
results.put(Constants.OUTPUT_LOG, parameters.get(Constants.OUTPUT_LOG));
7979

8080
MessageBus messageBus = project.getMessageBus();
8181
SwanNotifier publisher = messageBus.syncPublisher(SwanNotifier.END_SWAN_PROCESS_TOPIC);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public void actionPerformed(AnActionEvent e) {
3434
PropertiesManager.setProjectOutputPath(project);
3535

3636
SuggestThread suggestThread = new SuggestThread(project,
37-
PropertiesComponent.getInstance(project).getValue(Constants.SWAN_CONFIG_FILE),
38-
PropertiesComponent.getInstance(project).getValue(Constants.SWAN_SOURCE_DIR));
37+
PropertiesComponent.getInstance(project).getValue(Constants.CONFIGURATION_FILE),
38+
PropertiesComponent.getInstance(project).getValue(Constants.SOURCE_DIRECTORY));
3939
suggestThread.start();
4040

4141
SuggestNotifier suggestNotifier = messageBus.syncPublisher(SuggestNotifier.SUGGEST_METHOD_TOPIC);

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/data/JSONFileLoader.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

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

10+
import com.intellij.ide.util.PropertiesComponent;
1011
import com.intellij.openapi.project.Project;
1112
import com.intellij.psi.JavaPsiFacade;
1213
import com.intellij.psi.PsiClass;
@@ -16,10 +17,7 @@
1617
import de.fraunhofer.iem.swan.data.Category;
1718
import javafx.util.Pair;
1819

19-
import java.util.ArrayList;
20-
import java.util.HashMap;
21-
import java.util.HashSet;
22-
import java.util.Set;
20+
import java.util.*;
2321

2422
/**
2523
* Loads JSON configuration file and manipulates data.
@@ -35,15 +33,18 @@ public class JSONFileLoader {
3533

3634
/**
3735
* Set configuration file location
36+
*
3837
* @param path Configuration file path.
3938
*/
40-
public static void setConfigurationFile(String path) {
39+
public static void setConfigurationFile(String path, Project project) {
4140

4241
congFile = path;
42+
PropertiesComponent.getInstance(project).setValue(Constants.CONFIGURATION_FILE, path);
4343
}
4444

4545
/**
4646
* Returns configuration file path or file name
47+
*
4748
* @param path Condition determines if the path or just the filename should be returned.
4849
* @return Returns either the filename or file path.
4950
*/
@@ -75,16 +76,17 @@ public static void loadInitialFile() {
7576
/**
7677
* Compares new JSON file with original file and updates list with merged results. Loads new file
7778
* is a JSON file was not selected.
79+
*
7880
* @param newFilePath File path of new configuration file
7981
*/
80-
public static void loadUpdatedFile(String newFilePath) {
82+
public static void loadUpdatedFile(String newFilePath, Project project) {
8183

82-
if(isFileSelected()){
84+
if (isFileSelected()) {
8385
JSONFileComparator fileComparator = new JSONFileComparator(congFile, newFilePath);
8486
methods = fileComparator.compareJSONFile();
85-
setConfigurationFile(newFilePath);
86-
}else{
87-
setConfigurationFile(newFilePath);
87+
setConfigurationFile(newFilePath, project);
88+
} else {
89+
setConfigurationFile(newFilePath, project);
8890
loadInitialFile();
8991
}
9092
}
@@ -107,9 +109,10 @@ public static HashMap<String, MethodWrapper> getAllMethods() {
107109

108110
/**
109111
* Returns all methods based on the filters and or current file that's selected.
110-
* @param filters Filters that should be applied to the methods returned.
112+
*
113+
* @param filters Filters that should be applied to the methods returned.
111114
* @param currentFile Current active file opened in the IDE's editor.
112-
* @param project Active project in IDE.
115+
* @param project Active project in IDE.
113116
* @return Returns an array list of the methods that meet the specified criteria.
114117
*/
115118
public static ArrayList<MethodWrapper> getMethods(ArrayList<Pair<String, String>> filters, String currentFile, Project project) {
@@ -148,7 +151,8 @@ public static ArrayList<MethodWrapper> getMethods(ArrayList<Pair<String, String>
148151

149152
/**
150153
* Filters method methods using the filters provided.
151-
* @param filters Filters that should be applied to the methods returned.
154+
*
155+
* @param filters Filters that should be applied to the methods returned.
152156
* @param currentFile Current active file opened in the IDE's editor.
153157
* @return Methods matching the filtering criteria.
154158
*/
@@ -179,6 +183,7 @@ private static ArrayList<MethodWrapper> filterList(ArrayList<Pair<String, String
179183

180184
/**
181185
* Return list of categories.
186+
*
182187
* @return Set of categories used by methods.
183188
*/
184189
public static Set<Category> getCategories() {
@@ -197,6 +202,7 @@ public static Set<Category> getCategories() {
197202

198203
/**
199204
* Add new method to the list
205+
*
200206
* @param method Method to be added to the list
201207
* @return Returns whether the method is new or existing.
202208
*/
@@ -213,6 +219,7 @@ public static int addMethod(MethodWrapper method) {
213219

214220
/**
215221
* Checks if method exists in list
222+
*
216223
* @param methodSignature Method signature of method being searched for.
217224
* @return Returns whether or not the method exists.
218225
*/
@@ -223,6 +230,7 @@ public static boolean methodExists(String methodSignature) {
223230

224231
/**
225232
* Returns an instance of the method
233+
*
226234
* @param methodSignature Method Signature of requested method.
227235
* @return Instance of the method
228236
*/
@@ -233,6 +241,7 @@ public static MethodWrapper getMethod(String methodSignature) {
233241

234242
/**
235243
* Remove method from list
244+
*
236245
* @param method Method to be removed
237246
*/
238247
public static void removeMethod(MethodWrapper method) {
@@ -242,8 +251,9 @@ public static void removeMethod(MethodWrapper method) {
242251

243252
/**
244253
* Check if method is used in project
254+
*
245255
* @param classname Method's classname
246-
* @param project Instance of project
256+
* @param project Instance of project
247257
* @return Returns whether or not the method is used in the project
248258
*/
249259
private static boolean inProject(String classname, Project project) {
@@ -257,6 +267,7 @@ private static boolean inProject(String classname, Project project) {
257267

258268
/**
259269
* Gives the status of SWAN
270+
*
260271
* @return Returns whether or not SWAN is reloading
261272
*/
262273
public static boolean isReloading() {
@@ -265,6 +276,7 @@ public static boolean isReloading() {
265276

266277
/**
267278
* Set the status of SWAN
279+
*
268280
* @param reloadingSwan Status of SWAN - reloading or not
269281
*/
270282
public static void setReloading(boolean reloadingSwan) {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.data;
9+
10+
import com.intellij.ide.util.PropertiesComponent;
11+
import com.intellij.openapi.project.Project;
12+
import com.intellij.openapi.ui.DialogWrapper;
13+
import de.fraunhofer.iem.swan.assist.ui.dialog.PluginSettingsDialog;
14+
15+
import java.io.File;
16+
import java.util.HashMap;
17+
18+
import static de.fraunhofer.iem.swan.assist.util.Constants.OUTPUT_DIRECTORY;
19+
20+
public class PropertiesManager {
21+
22+
/**
23+
* Verifies existing plugin settings and prompt user to update settings if file is missing.
24+
* @param project project for which settings should be applied to
25+
* @return the project's output path
26+
*/
27+
public static String setProjectOutputPath(Project project){
28+
//Set output directory for plugin, if not set
29+
if (!PropertiesComponent.getInstance(project).isValueSet(OUTPUT_DIRECTORY)) {
30+
31+
showSettingsDialog(project);
32+
} else {
33+
File trainFile = new File(PropertiesComponent.getInstance(project).getValue(OUTPUT_DIRECTORY));
34+
35+
if (!trainFile.exists())
36+
showSettingsDialog(project);
37+
}
38+
return PropertiesComponent.getInstance(project).getValue(OUTPUT_DIRECTORY);
39+
}
40+
41+
/**
42+
* Displays Plugin settings dialog.
43+
* @param project project for which the settings should be configured
44+
*/
45+
private static void showSettingsDialog(Project project) {
46+
47+
//Launch SWAN Properties Dialog
48+
PluginSettingsDialog dialog = new PluginSettingsDialog(project, true);
49+
dialog.show();
50+
51+
if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
52+
53+
HashMap<String, String> swanParameters = dialog.getParameters();
54+
55+
File outputFolder = new File(swanParameters.get(OUTPUT_DIRECTORY));
56+
57+
if(!outputFolder.exists())
58+
outputFolder.mkdir();
59+
60+
PropertiesComponent.getInstance(project).setValue(OUTPUT_DIRECTORY, swanParameters.get(OUTPUT_DIRECTORY));
61+
}
62+
}
63+
}

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/data/TrainingFileManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,17 @@ public HashMap<String, MethodWrapper> getTrainingMethods() {
5151
//Check if training file was set and if it exists
5252
if (PropertiesComponent.getInstance(currentProject).isValueSet(Constants.TRAIN_FILE_SUGGESTED)) {
5353

54-
File trainFile = new File(PropertiesComponent.getInstance(currentProject).getValue(Constants.SWAN_OUTPUT_DIR));
54+
File trainFile = new File(PropertiesManager.setProjectOutputPath(currentProject));
5555

5656
if (!trainFile.exists())
57-
PropertiesComponent.getInstance(currentProject).unsetValue(Constants.SWAN_OUTPUT_DIR);
57+
PropertiesComponent.getInstance(currentProject).unsetValue(Constants.OUTPUT_DIRECTORY);
5858
else {
5959
JSONFileParser fileParser = new JSONFileParser(PropertiesComponent.getInstance(currentProject).getValue(Constants.TRAIN_FILE_SUGGESTED));
6060
methods = fileParser.parseJSONFileMap();
6161
}
6262
}
6363

64-
//If training file was not found, thedefault training file will be used
64+
//If training file was not found, the default training file will be used
6565
if (methods.isEmpty()) {
6666
InputStream stream = getClass().getClassLoader().getResourceAsStream(config.getProperty("train_config_file"));
6767

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/ui/MethodListTree.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import de.fraunhofer.iem.swan.assist.util.Formatter;
4949
import de.fraunhofer.iem.swan.assist.util.PsiTraversal;
5050
import de.fraunhofer.iem.swan.data.Category;
51-
import de.fraunhofer.iem.swan.data.Method;
5251
import javafx.util.Pair;
5352
import org.jetbrains.annotations.NotNull;
5453

@@ -92,12 +91,10 @@ public MethodListTree(Project project) {
9291
getEmptyText().setText(resource.getString("Messages.Notification.EmptyTree"));
9392
setToggleClickCount(0);
9493

95-
9694
TREE_FILTERS = new ArrayList<>();
9795
RESTORE_METHOD = false;
9896
currentFile = "";
9997

100-
10198
suggestedMethodsList = new HashSet<>();
10299

103100
addMouseListener(new MouseAdapter() {
@@ -182,7 +179,7 @@ public void run(@NotNull ProgressIndicator progressIndicator) {
182179

183180
ApplicationManager.getApplication().runReadAction(new Runnable() {
184181
public void run() {
185-
JSONFileLoader.setConfigurationFile(fileName);
182+
JSONFileLoader.setConfigurationFile(fileName, project);
186183
JSONFileLoader.loadInitialFile();
187184
loadMethods();
188185
}
@@ -202,7 +199,7 @@ public void run(@NotNull ProgressIndicator progressIndicator) {
202199

203200
ApplicationManager.getApplication().runReadAction(new Runnable() {
204201
public void run() {
205-
JSONFileLoader.loadUpdatedFile(fileName);
202+
JSONFileLoader.loadUpdatedFile(fileName, project);
206203
loadMethods();
207204
}
208205
});
@@ -257,7 +254,7 @@ public void afterSuggestAction(ArrayList<MethodWrapper> methods) {
257254

258255
TrainingFileManager trainingFileManager = new TrainingFileManager(project);
259256

260-
if(trainingFileManager.exportNew(suggestedMethods, PropertiesComponent.getInstance(project).getValue(Constants.SWAN_OUTPUT_DIR))){
257+
if(trainingFileManager.exportNew(suggestedMethods, PropertiesComponent.getInstance(project).getValue(Constants.OUTPUT_DIRECTORY))){
261258

262259
PropertiesComponent.getInstance(project).setValue(Constants.TRAIN_FILE_SUGGESTED,trainingFileManager.getTrainingFile());
263260

@@ -324,7 +321,7 @@ public void hyperlinkUpdate(@NotNull Notification notification, @NotNull Hyperli
324321
} else if (hyperlinkEvent.getDescription().equals("load")) {
325322

326323
ConfigurationFileNotifier fileNotifier = bus.syncPublisher(ConfigurationFileNotifier.FILE_NOTIFIER_TOPIC);
327-
fileNotifier.loadUpdatedFile(values.get(Constants.SWAN_OUTPUT_FILE));
324+
fileNotifier.loadUpdatedFile(values.get(Constants.OUTPUT_FILE));
328325
}
329326
}
330327
}

0 commit comments

Comments
 (0)