+
+ /**
+ * Used for loading the component preset database. This method will be called in a background
+ * thread to load the presets asynchronously.
+ */
+ protected abstract void load();
+
+ /**
+ * Start loading the presets.
+ *
+ * @throws IllegalStateException if this method has already been called.
+ */
+ public void startLoading() {
+ if (startedLoading) {
+ throw new IllegalStateException("Already called startLoading");
+ }
+ startedLoading = true;
+ if (asynchronous) {
+ new LoadingThread().start();
+ } else {
+ load();
+ }
+ synchronized (this) {
+ endedLoading = true;
+ this.notifyAll();
+ }
+ }
+
+ /**
+ * Background thread for loading the presets.
+ */
+ private class LoadingThread extends Thread {
+
+ private LoadingThread() {
+ this.setName("PresetLoadingThread");
+ this.setPriority(MIN_PRIORITY);
+ }
+ @Override
+ public void run() {
+ load();
+ }
+ }
+
+ /**
+ * Block the current thread until loading of the presets has been completed.
+ *
+ * @throws IllegalStateException if startLoading() has not been called.
+ */
+ public void blockUntilLoaded() {
+ inUse = true;
+ if (!startedLoading) {
+ throw new IllegalStateException("startLoading() has not been called");
+ }
+ if (!endedLoading) {
+ synchronized (this) {
+ while (!endedLoading) {
+ try {
+ this.wait();
+ } catch (InterruptedException e) {
+ logger.warn("InterruptedException occurred, ignoring", e);
+ }
+ }
+ }
+ }
+ }
+