* the Event Dispatch Thread.
*/
public void loadModel(final Content content,
boolean synchronous,
ModelObserver modelObserver) {
BranchGroup modelRoot;
synchronized (this.loadedModelNodes) {
modelRoot = this.loadedModelNodes.get(content);
}
if (modelRoot != null) {
// Notify cached model to observer with a clone of the model
modelObserver.modelUpdated((BranchGroup)cloneNode(modelRoot));
} else if (synchronous) {
try {
modelRoot = loadModel(content);
synchronized (this.loadedModelNodes) {
// Store in cache model node for future copies
this.loadedModelNodes.put(content, (BranchGroup)modelRoot);
}
modelObserver.modelUpdated((BranchGroup)cloneNode(modelRoot));
} catch (IOException ex) {
modelObserver.modelError(ex);
}
} else if (!EventQueue.isDispatchThread()) {
throw new IllegalStateException("Asynchronous call out of Event Dispatch Thread");
} else {
if (this.modelsLoader == null) {
this.modelsLoader = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
}
List<ModelObserver> observers = this.loadingModelObservers.get(content);
if (observers != null) {
// If observers list exists, content model is already being loaded
// register observer for future notification
observers.add(modelObserver);
} else {
// Create a list of observers that will be notified once content model is loaded
observers = new ArrayList<ModelObserver>();
observers.add(modelObserver);
this.loadingModelObservers.put(content, observers);
// Load the model in an other thread
this.modelsLoader.execute(new Runnable() {
public void run() {
try {
final BranchGroup loadedModel = loadModel(content);
synchronized (loadedModelNodes) {
// Update loaded models cache and notify registered observers
loadedModelNodes.put(content, loadedModel);
}
EventQueue.invokeLater(new Runnable() {