From a4aed4182488c99a48c9ab3a21643fccb7e3a54e Mon Sep 17 00:00:00 2001 From: David Dodlek Date: Fri, 26 Aug 2022 17:08:53 +0200 Subject: [PATCH] fix!: autocompletion could not be called consecutively fixes #36 --- src/main/java/de/f0rce/ace/AceEditor.java | 374 ++++++++++++++---- .../ace/interfaces/IAceWordCompleter.java | 12 + .../ace/util/AceDynamicWordCompleter.java | 86 ++++ src/main/java/de/f0rce/ace/util/AceJSON.java | 159 -------- .../ace/util/AceStaticWordCompleter.java | 66 ++++ 5 files changed, 469 insertions(+), 228 deletions(-) create mode 100644 src/main/java/de/f0rce/ace/interfaces/IAceWordCompleter.java create mode 100644 src/main/java/de/f0rce/ace/util/AceDynamicWordCompleter.java create mode 100644 src/main/java/de/f0rce/ace/util/AceStaticWordCompleter.java diff --git a/src/main/java/de/f0rce/ace/AceEditor.java b/src/main/java/de/f0rce/ace/AceEditor.java index 8d32a2c..5dd61a0 100644 --- a/src/main/java/de/f0rce/ace/AceEditor.java +++ b/src/main/java/de/f0rce/ace/AceEditor.java @@ -41,6 +41,9 @@ @JsModule("./@f0rce/lit-ace/lit-ace.js") public class AceEditor extends Component implements HasSize, HasStyle, Focusable { + public static final String DEFAULT_STATIC_CATEGORY = "keyword"; + public static final String DEFAULT_DYNAMIC_CATEGORY = "dynamic"; + private AceTheme theme = AceTheme.eclipse; private AceMode mode = AceMode.javascript; private String value = ""; @@ -64,10 +67,10 @@ public class AceEditor extends Component implements HasSize, HasStyle, Focusable private boolean useWorker = false; private boolean liveAutocompletion = false; private boolean enableSnippets = false; - private List customAutocompletion = new ArrayList(); private List markers = new ArrayList(); private boolean statusbarEnabled = true; private AceStatusbarIndexing statusbarIndexing = AceStatusbarIndexing.ONE_BASED; + private List customWordCompleter = new ArrayList<>(); // Some internal checking private boolean hasBeenDetached = false; @@ -100,8 +103,8 @@ public AceEditor(AceTheme theme, AceMode mode, String height, String width) { @Override protected void onAttach(AttachEvent attachEvent) { - // TODO: rework customAutocompletion, dynamicAutocompletion & markers to be refreshed in here - // aswell + // TODO: rework markers to be refreshed in here aswell (to allow multiple setting of markers at + // the same time) if (this.hasBeenDetached) { if (!this.value.equals("")) { this.setValue(this.value); @@ -112,6 +115,15 @@ protected void onAttach(AttachEvent attachEvent) { if (this.statusbarIndexing != AceStatusbarIndexing.ONE_BASED) { this.setStatusbarIndexing(this.statusbarIndexing); } + if (!this.customWordCompleter.isEmpty()) { + for (IAceWordCompleter completer : this.customWordCompleter) { + if (completer instanceof AceStaticWordCompleter) { + this.getElement().callJsFunction("addStaticWordCompleter", completer.toJSON()); + } else if (completer instanceof AceDynamicWordCompleter) { + this.getElement().callJsFunction("addDynamicWordCompleter", completer.toJSON()); + } + } + } this.hasBeenDetached = false; } } @@ -761,88 +773,184 @@ public boolean isEnableSnippets() { } /** - * Sets a custom autocompletion list for the editor. + * Deprecated since v2.4.0 / v3.4.0! Please use {@link #addStaticWordCompleter(List)}. * - * @param wordList {@link List} + * @param wordList */ + @Deprecated public void setCustomAutocompletion(List wordList) { - if (wordList.size() == 0) { - return; - } - this.getElement() - .setProperty("customAutocompletion", AceJSON.generateCustomAutocompletionJSON(wordList)); - this.customAutocompletion = wordList; + this.addStaticWordCompleter(wordList); } /** - * Sets a custom autocompletion list for the editor and optionally keeps the current completers. + * Deprecated since v2.4.0 / v3.4.0! Please use {@link #addStaticWordCompleter(List, boolean)}. * * @param wordList {@link List} * @param keepCurrentCompleters boolean */ + @Deprecated public void setCustomAutocompletion(List wordList, boolean keepCurrentCompleters) { - if (wordList.size() == 0) { - return; - } - this.getElement() - .setProperty( - "customAutocompletion", - AceJSON.generateCustomAutocompletionJSON(wordList, keepCurrentCompleters)); - if (keepCurrentCompleters) { - this.customAutocompletion.addAll(wordList); - } else { - this.customAutocompletion = wordList; - } + this.addStaticWordCompleter(wordList, keepCurrentCompleters); } /** - * Sets a custom autocompletion list for the editor and sets the category aswell (default: - * "keyword"). + * Deprecated since v2.4.0 / v3.4.0! Please use {@link #addStaticWordCompleter(List, String)}. * * @param wordList {@link List} * @param category {@link String} */ + @Deprecated public void setCustomAutocompletion(List wordList, String category) { - if (wordList.size() == 0) { - return; - } - this.getElement() - .setProperty( - "customAutocompletion", AceJSON.generateCustomAutocompletionJSON(wordList, category)); - this.customAutocompletion = wordList; + this.addStaticWordCompleter(wordList, category); } /** - * Sets a custom autocompletion list for the editor, sets the category (default: "keyword") and - * optionally keeps the current completers. + * Deprecated since v2.4.0 / v3.4.0! Please use {@link #addStaticWordCompleter(List, String, + * boolean)}. * * @param wordList {@link List} * @param category {@link String} * @param keepCurrentCompleters boolean */ + @Deprecated public void setCustomAutocompletion( List wordList, String category, boolean keepCurrentCompleters) { - if (wordList.size() == 0) { + this.addStaticWordCompleter(wordList, category, keepCurrentCompleters); + } + + /** + * Deprecated since v2.4.0 / v3.4.0! Please use {@link #getStaticWordCompleter()}. + * + * @return {@link List} + */ + @Deprecated + public List getCustomAutocompletion() { + + ArrayList words = new ArrayList<>(); + + for (IAceWordCompleter completer : this.customWordCompleter) { + if (completer instanceof AceStaticWordCompleter) { + AceStaticWordCompleter aswc = (AceStaticWordCompleter) completer; + + words.addAll(aswc.getWords()); + } + } + + return words; + } + + /** + * Add a static word completer to the editor's autocompletion. + * + * @param words {@link List} + */ + public void addStaticWordCompleter(List words) { + if (words.size() == 0) { return; } - this.getElement() - .setProperty( - "customAutocompletion", - AceJSON.generateCustomAutocompletionJSON(wordList, category, keepCurrentCompleters)); - if (keepCurrentCompleters) { - this.customAutocompletion.addAll(wordList); + + AceStaticWordCompleter aswc = new AceStaticWordCompleter(words); + + this.getElement().callJsFunction("addStaticWordCompleter", aswc.toJSON()); + this.customWordCompleter = new ArrayList<>(Arrays.asList(aswc)); + } + + /** + * Add a static word completer to the editor's autocompletion. + * + * @param words {@link List} + * @param keepCompleters boolean + */ + public void addStaticWordCompleter(List words, boolean keepCompleters) { + if (words.size() == 0) { + return; + } + + AceStaticWordCompleter aswc = new AceStaticWordCompleter(words, keepCompleters); + + this.getElement().callJsFunction("addStaticWordCompleter", aswc.toJSON()); + + if (keepCompleters) { + this.customWordCompleter.add(aswc); + } else { + this.customWordCompleter = new ArrayList<>(Arrays.asList(aswc)); + } + } + + /** + * Add a static word completer to the editor's autocompletion. + * + * @param words {@link List} + * @param category {@link String} + */ + public void addStaticWordCompleter(List words, String category) { + if (words.size() == 0) { + return; + } + + AceStaticWordCompleter aswc = new AceStaticWordCompleter(words, category); + + this.getElement().callJsFunction("addStaticWordCompleter", aswc.toJSON()); + this.customWordCompleter = new ArrayList<>(Arrays.asList(aswc)); + } + + /** + * Add a static word completer to the editor's autocompletion. + * + * @param words {@link List} + * @param category {@link String} + * @param keepCompleters boolean + */ + public void addStaticWordCompleter(List words, String category, boolean keepCompleters) { + if (words.size() == 0) { + return; + } + + AceStaticWordCompleter aswc = new AceStaticWordCompleter(words, category, keepCompleters); + + this.getElement().callJsFunction("addStaticWordCompleter", aswc.toJSON()); + + if (keepCompleters) { + this.customWordCompleter.add(aswc); } else { - this.customAutocompletion = wordList; + this.customWordCompleter = new ArrayList<>(Arrays.asList(aswc)); } } /** - * Returns a {@link List} of the current custom autocompletion for the editor. + * Add a static word completer to the editor's autocompletion. + * + * @param staticWordCompleter {@link AceStaticWordCompleter} + */ + public void addStaticWordCompleter(AceStaticWordCompleter staticWordCompleter) { + if (staticWordCompleter.getWords().isEmpty()) { + return; + } + + this.getElement().callJsFunction("addStaticWordCompleter", staticWordCompleter.toJSON()); + + if (staticWordCompleter.isKeepCompleters()) { + this.customWordCompleter.add(staticWordCompleter); + } else { + this.customWordCompleter = new ArrayList<>(Arrays.asList(staticWordCompleter)); + } + } + + /** + * Returns a {@link List} of the current static word completer used in the editor. * * @return {@link List} */ - public List getCustomAutocompletion() { - return this.customAutocompletion; + public List getStaticWordCompleter() { + ArrayList staticCompleter = new ArrayList<>(); + + for (IAceWordCompleter completer : this.customWordCompleter) { + if (completer instanceof AceStaticWordCompleter) { + staticCompleter.add((AceStaticWordCompleter) completer); + } + } + + return staticCompleter; } /** @@ -861,6 +969,7 @@ public void disableCustomAutocompletion() { */ public void disableCustomAutocompletion(boolean useDefault) { this.getElement().callJsFunction("disableCustomAutocompletion", useDefault); + this.customWordCompleter = new ArrayList<>(); } /** @@ -1262,16 +1371,6 @@ private void runAfterSync(AceForceSyncDomEvent event, Runnable action) { action.run(); } - /** - * @deprecated As of release 2.0.0, please use {@link #getSelection()} and {@link - * AceSelection#getSelectedText()} - * @return {@link String} - */ - @Deprecated - public String getSelectedText() { - return this.getSelection().getSelectedText(); - } - /** * Folds all fold marker in the editor. (Created automatically when an if is added for example). */ @@ -1358,55 +1457,192 @@ public Registration addAceReadyListener(ComponentEventListener listene } /** - * Adds a dynamic autocompletion to the editor's autocompletion. + * Deprecated since v2.4.0 / v3.4.0! Please use {@link #addDynamicWordCompleter(Map, String)}. * * @param map {@link Map} * @param seperator {@link String} */ + @Deprecated public void addDynamicAutocompletion(Map> map, String seperator) { - String json = AceJSON.generateDynamicAutocompletionJSON(map, seperator); - this.getElement().setProperty("dynamicAutocompletion", json); + this.addDynamicWordCompleter(map, seperator); } /** - * Adds a dynamic autocompletion to the editor's autocompletion. + * Deprecated since v2.4.0 / v3.4.0! Please use {@link #addDynamicWordCompleter(Map, String, + * String)}. * * @param map {@link Map} * @param seperator {@link String} * @param category {@link String} */ + @Deprecated public void addDynamicAutocompletion( Map> map, String seperator, String category) { - String json = AceJSON.generateDynamicAutocompletionJSON(map, seperator, category); - this.getElement().setProperty("dynamicAutocompletion", json); + this.addDynamicWordCompleter(map, seperator, category); } /** - * Adds a dynamic autocompletion to the editor's autocompletion. + * Deprecated since v2.4.0 / v3.4.0! Please use {@link #addDynamicWordCompleter(Map, String, + * boolean)}. * * @param map {@link Map} * @param seperator {@link String} * @param keepCompleters boolean */ + @Deprecated public void addDynamicAutocompletion( Map> map, String seperator, boolean keepCompleters) { - String json = AceJSON.generateDynamicAutocompletionJSON(map, seperator, keepCompleters); - this.getElement().setProperty("dynamicAutocompletion", json); + this.addDynamicAutocompletion(map, seperator, keepCompleters); } /** - * Adds a dynamic autocompletion to the editor's autocompletion. + * Deprecated since v2.4.0 / v3.4.0! Please use {@link #addDynamicWordCompleter(Map, String, + * String, boolean)}. * * @param map {@link Map} * @param seperator {@link String} * @param category {@link String} * @param keepCompleters boolean */ + @Deprecated public void addDynamicAutocompletion( Map> map, String seperator, String category, boolean keepCompleters) { - String json = - AceJSON.generateDynamicAutocompletionJSON(map, seperator, category, keepCompleters); - this.getElement().setProperty("dynamicAutocompletion", json); + this.addDynamicWordCompleter(map, seperator, category, keepCompleters); + } + + /** + * Add a dynamic word completer to the editor's autocompletion. + * + * @param map {@link Map} + * @param seperator {@link String} + */ + public void addDynamicWordCompleter(Map> dynamicWords, String seperator) { + if (dynamicWords.isEmpty()) { + return; + } + + AceDynamicWordCompleter adwc = new AceDynamicWordCompleter(dynamicWords, seperator); + + this.getElement().callJsFunction("addDynamicWordCompleter", adwc.toJSON()); + this.customWordCompleter = new ArrayList<>(Arrays.asList(adwc)); + } + + /** + * Add a dynamic word completer to the editor's autocompletion. + * + * @param map {@link Map} + * @param seperator {@link String} + * @param category {@link String} + */ + public void addDynamicWordCompleter( + Map> dynamicWords, String seperator, String category) { + if (dynamicWords.isEmpty()) { + return; + } + + AceDynamicWordCompleter adwc = new AceDynamicWordCompleter(dynamicWords, seperator, category); + + this.getElement().callJsFunction("addDynamicWordCompleter", adwc.toJSON()); + this.customWordCompleter = new ArrayList<>(Arrays.asList(adwc)); + } + + /** + * Add a dynamic word completer to the editor's autocompletion. + * + * @param map {@link Map} + * @param seperator {@link String} + * @param keepCompleters boolean + */ + public void addDynamicWordCompleter( + Map> dynamicWords, String seperator, boolean keepCompleters) { + if (dynamicWords.isEmpty()) { + return; + } + + AceDynamicWordCompleter adwc = + new AceDynamicWordCompleter(dynamicWords, seperator, keepCompleters); + + this.getElement().callJsFunction("addDynamicWordCompleter", adwc.toJSON()); + + if (keepCompleters) { + this.customWordCompleter.add(adwc); + } else { + this.customWordCompleter = new ArrayList<>(Arrays.asList(adwc)); + } + } + + /** + * Add a dynamic word completer to the editor's autocompletion. + * + * @param map {@link Map} + * @param seperator {@link String} + * @param category {@link String} + * @param keepCompleters boolean + */ + public void addDynamicWordCompleter( + Map> dynamicWords, + String seperator, + String category, + boolean keepCompleters) { + if (dynamicWords.isEmpty()) { + return; + } + + AceDynamicWordCompleter adwc = + new AceDynamicWordCompleter(dynamicWords, seperator, category, keepCompleters); + + this.getElement().callJsFunction("addDynamicWordCompleter", adwc.toJSON()); + + if (keepCompleters) { + this.customWordCompleter.add(adwc); + } else { + this.customWordCompleter = new ArrayList<>(Arrays.asList(adwc)); + } + } + + /** + * Add a dynamic word completer to the editor's autocompletion. + * + * @param dynamicWordCompleter {@link AceDynamicWordCompleter} + */ + public void addDynamicWordCompleter(AceDynamicWordCompleter dynamicWordCompleter) { + if (dynamicWordCompleter.getDynamicWords().isEmpty()) { + return; + } + + this.getElement().callJsFunction("addStaticWordCompleter", dynamicWordCompleter.toJSON()); + + if (dynamicWordCompleter.isKeepCompleters()) { + this.customWordCompleter.add(dynamicWordCompleter); + } else { + this.customWordCompleter = new ArrayList<>(Arrays.asList(dynamicWordCompleter)); + } + } + + /** + * Returns a {@link List} of the current dynamic word completer used in the editor. + * + * @return {@link List} + */ + public List getDynamicWordCompleter() { + ArrayList dynamicCompleter = new ArrayList<>(); + + for (IAceWordCompleter completer : this.customWordCompleter) { + if (completer instanceof AceDynamicWordCompleter) { + dynamicCompleter.add((AceDynamicWordCompleter) completer); + } + } + + return dynamicCompleter; + } + + /** + * Returns a list of all word completer used in the editor. + * + * @return {@link List} + */ + public List getWordCompleter() { + return this.customWordCompleter; } /** Open the autocompletion dialog programatically. */ diff --git a/src/main/java/de/f0rce/ace/interfaces/IAceWordCompleter.java b/src/main/java/de/f0rce/ace/interfaces/IAceWordCompleter.java new file mode 100644 index 0000000..5ba9931 --- /dev/null +++ b/src/main/java/de/f0rce/ace/interfaces/IAceWordCompleter.java @@ -0,0 +1,12 @@ +package de.f0rce.ace.interfaces; + +/** @author David "F0rce" Dodlek */ +public interface IAceWordCompleter { + + /** + * Returns a JSON representation of a word completer. + * + * @return {@link String} + */ + public String toJSON(); +} diff --git a/src/main/java/de/f0rce/ace/util/AceDynamicWordCompleter.java b/src/main/java/de/f0rce/ace/util/AceDynamicWordCompleter.java new file mode 100644 index 0000000..7d72ecf --- /dev/null +++ b/src/main/java/de/f0rce/ace/util/AceDynamicWordCompleter.java @@ -0,0 +1,86 @@ +package de.f0rce.ace.util; + +import java.util.List; +import java.util.Map; +import com.google.gson.Gson; +import de.f0rce.ace.AceEditor; +import de.f0rce.ace.interfaces.IAceWordCompleter; + +/** @author David "F0rce" Dodlek */ +public class AceDynamicWordCompleter implements IAceWordCompleter { + + private Map> dynamicWords; + private String seperator; + private String category; + private boolean keepCompleters = false; + + /** + * @param dynamicWords {@link Map} + * @param seperator {@link String} + */ + public AceDynamicWordCompleter(Map> dynamicWords, String seperator) { + this(dynamicWords, seperator, AceEditor.DEFAULT_DYNAMIC_CATEGORY, false); + } + + /** + * @param dynamicWords {@link Map} + * @param seperator {@link String} + * @param category {@link String} + */ + public AceDynamicWordCompleter( + Map> dynamicWords, String seperator, String category) { + this(dynamicWords, seperator, category, false); + } + + /** + * @param dynamicWords {@link Map} + * @param seperator {@link String} + * @param keepCompleters boolean + */ + public AceDynamicWordCompleter( + Map> dynamicWords, String seperator, boolean keepCompleters) { + this(dynamicWords, seperator, AceEditor.DEFAULT_DYNAMIC_CATEGORY, keepCompleters); + } + + /** + * @param dynamicWords {@link Map} + * @param seperator {@link String} + * @param category {@link String} + * @param keepCompleters boolean + */ + public AceDynamicWordCompleter( + Map> dynamicWords, + String seperator, + String category, + boolean keepCompleters) { + this.dynamicWords = dynamicWords; + this.seperator = seperator; + this.category = category; + this.keepCompleters = keepCompleters; + } + + /** @return {@link Map} */ + public Map> getDynamicWords() { + return this.dynamicWords; + } + + /** @return {@link String} */ + public String getSeperator() { + return this.seperator; + } + + /** @return {@link String} */ + public String getCategory() { + return this.category; + } + + /** @return boolean */ + public boolean isKeepCompleters() { + return this.keepCompleters; + } + + @Override + public String toJSON() { + return new Gson().toJson(this); + } +} diff --git a/src/main/java/de/f0rce/ace/util/AceJSON.java b/src/main/java/de/f0rce/ace/util/AceJSON.java index c831d60..12e8a25 100644 --- a/src/main/java/de/f0rce/ace/util/AceJSON.java +++ b/src/main/java/de/f0rce/ace/util/AceJSON.java @@ -1,9 +1,6 @@ package de.f0rce.ace.util; import de.f0rce.ace.AceEditor; -import java.util.List; -import java.util.Map; -import java.util.Set; /** * Utility Class for generating JSON Objects to be sent to the frontend. @@ -12,95 +9,6 @@ */ public class AceJSON { - private static final String DEFAULT_DYNAMIC_CATEGORY = "dynamic"; - private static final String DEFAULT_CUSTOM_CATEGORY = "keyword"; - - /** - * {@link AceEditor#addDynamicAutocompletion(Map, String, String, boolean)} - * - * @param toConvert {@link Map} - * @param seperator {@link String} - * @param category {@link String} - * @param keepCompleters boolean - * @return {@link String} - */ - public static String generateDynamicAutocompletionJSON( - Map> toConvert, - String seperator, - String category, - boolean keepCompleters) { - Set keys = toConvert.keySet(); - - StringBuilder builder = new StringBuilder(); - builder.append("{"); - builder.append("\"seperator\": " + "\"" + seperator + "\", "); - builder.append("\"category\": " + "\"" + category + "\", "); - builder.append("\"keepcompleters\": " + keepCompleters + ", "); - - builder.append("\"list\": " + "{"); - int counter = 0; - for (String key : keys) { - builder.append("\"" + key + "\": "); - builder.append("["); - List currentList = toConvert.get(key); - for (String word : currentList) { - builder.append("\"" + word + "\""); - if (currentList.indexOf(word) != currentList.size() - 1) { - builder.append(", "); - } - } - builder.append("]"); - if (counter != keys.size() - 1) { - builder.append(", "); - } - counter++; - } - builder.append("}"); - builder.append("}"); - - return builder.toString(); - } - - /** - * {@link AceEditor#addDynamicAutocompletion(Map, String)} - * - * @param toConvert {@link Map} - * @param seperator {@link String} - * @return {@link String} - */ - public static String generateDynamicAutocompletionJSON( - Map> toConvert, String seperator) { - return AceJSON.generateDynamicAutocompletionJSON( - toConvert, seperator, DEFAULT_DYNAMIC_CATEGORY, false); - } - - /** - * {@link AceEditor#addDynamicAutocompletion(Map, String, String)} - * - * @param toConvert {@link Map} - * @param seperator {@link String} - * @param category {@link String} - * @return {@link String} - */ - public static String generateDynamicAutocompletionJSON( - Map> toConvert, String seperator, String category) { - return AceJSON.generateDynamicAutocompletionJSON(toConvert, seperator, category, false); - } - - /** - * {@link AceEditor#addDynamicAutocompletion(Map, String, boolean)} - * - * @param toConvert {@link Map} - * @param seperator {@link String} - * @param keepCompleters boolean - * @return {@link String} - */ - public static String generateDynamicAutocompletionJSON( - Map> toConvert, String seperator, boolean keepCompleters) { - return AceJSON.generateDynamicAutocompletionJSON( - toConvert, seperator, DEFAULT_DYNAMIC_CATEGORY, keepCompleters); - } - /** * {@link AceEditor#setSelection(int, int, int, int)} * @@ -183,71 +91,4 @@ public static String generateCursorPositionJSON(AceCursorPosition cursorPosition return builder.toString(); } - - /** - * {@link AceEditor#setCustomAutocompletion(List, String, boolean)} - * - * @param wordList {@link List} - * @param category {@link String} - * @param keepCurrentCompleters boolean - * @return {@link String} - */ - public static String generateCustomAutocompletionJSON( - List wordList, String category, boolean keepCurrentCompleters) { - StringBuilder builder = new StringBuilder(); - - builder.append("{"); - builder.append("\"category\": " + "\"" + category + "\", "); - builder.append("\"keepcompleters\": " + keepCurrentCompleters + ", "); - - builder.append("\"wordlist\": " + "["); - int counter = 0; - for (String word : wordList) { - builder.append("\"" + word + "\""); - if (counter != wordList.size() - 1) { - builder.append(", "); - } - if (counter == wordList.size() - 1) { - builder.append("]"); - } - counter++; - } - builder.append("}"); - - return builder.toString(); - } - - /** - * {@link AceEditor#setCustomAutocompletion(List)} - * - * @param wordList {@link Map} - * @return {@link String} - */ - public static String generateCustomAutocompletionJSON(List wordList) { - return AceJSON.generateCustomAutocompletionJSON(wordList, DEFAULT_CUSTOM_CATEGORY, false); - } - - /** - * {@link AceEditor#setCustomAutocompletion(List, boolean)} - * - * @param wordList {@link List} - * @param keepCurrentCompleters boolean - * @return {@link String} - */ - public static String generateCustomAutocompletionJSON( - List wordList, boolean keepCurrentCompleters) { - return AceJSON.generateCustomAutocompletionJSON( - wordList, DEFAULT_CUSTOM_CATEGORY, keepCurrentCompleters); - } - - /** - * {@link AceEditor#setCustomAutocompletion(List, String)} - * - * @param wordList {@link List} - * @param category {@link String} - * @return {@link String} - */ - public static String generateCustomAutocompletionJSON(List wordList, String category) { - return AceJSON.generateCustomAutocompletionJSON(wordList, category, false); - } } diff --git a/src/main/java/de/f0rce/ace/util/AceStaticWordCompleter.java b/src/main/java/de/f0rce/ace/util/AceStaticWordCompleter.java new file mode 100644 index 0000000..4b99084 --- /dev/null +++ b/src/main/java/de/f0rce/ace/util/AceStaticWordCompleter.java @@ -0,0 +1,66 @@ +package de.f0rce.ace.util; + +import java.util.List; +import com.google.gson.Gson; +import de.f0rce.ace.AceEditor; +import de.f0rce.ace.interfaces.IAceWordCompleter; + +/** @author David "F0rce" Dodlek */ +public class AceStaticWordCompleter implements IAceWordCompleter { + + private List words; + private String category; + private boolean keepCompleters = false; + + /** @param words {@link List} */ + public AceStaticWordCompleter(List words) { + this(words, AceEditor.DEFAULT_STATIC_CATEGORY, false); + } + + /** + * @param words {@link List} + * @param keepCurrentCompleters boolean + */ + public AceStaticWordCompleter(List words, boolean keepCompleters) { + this(words, AceEditor.DEFAULT_STATIC_CATEGORY, keepCompleters); + } + + /** + * @param words {@link List} + * @param category {@link String} + */ + public AceStaticWordCompleter(List words, String category) { + this(words, AceEditor.DEFAULT_STATIC_CATEGORY, false); + } + + /** + * @param words {@link List} + * @param category {@link String} + * @param keepCompleters boolean + */ + public AceStaticWordCompleter(List words, String category, boolean keepCompleters) { + this.keepCompleters = keepCompleters; + this.category = category; + this.words = words; + } + + /** @return boolean */ + public boolean isKeepCompleters() { + return this.keepCompleters; + } + + /** @return {@link String} */ + public String getCategory() { + return this.category; + } + + /** @return {@link List} */ + public List getWords() { + return this.words; + } + + @Override + public String toJSON() { + return new Gson().toJson(this); + } +}