Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f8cd8e
commit 764d521
Showing
4 changed files
with
154 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/org/jboss/forge/plugin/idea/service/threads/CommandLoadingThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2014 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.plugin.idea.service.threads; | ||
|
||
import org.jboss.forge.addon.ui.command.UICommand; | ||
import org.jboss.forge.addon.ui.context.UIContext; | ||
import org.jboss.forge.plugin.idea.util.CommandUtil; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CountDownLatch; | ||
|
||
/** | ||
* Asynchronously loads command list. <p/> | ||
* | ||
* Use {@link org.jboss.forge.plugin.idea.service.PluginService} to access command cache. | ||
* | ||
* @author Adam Wyłuda | ||
*/ | ||
public class CommandLoadingThread extends Thread | ||
{ | ||
private CountDownLatch latch = new CountDownLatch(1); | ||
private volatile List<UICommand> commands; | ||
private volatile UIContext uiContext; | ||
|
||
public CommandLoadingThread() | ||
{ | ||
super("ForgeCommandLoadingThread"); | ||
} | ||
|
||
/** | ||
* Invalidates stored command list. | ||
*/ | ||
public void invalidate() | ||
{ | ||
commands = null; | ||
} | ||
|
||
/** | ||
* Makes (asynchronous) request to refresh a list of commands. | ||
*/ | ||
public void reload(UIContext uiContext) | ||
{ | ||
this.uiContext = uiContext; | ||
latch.countDown(); | ||
} | ||
|
||
/** | ||
* Returns stored commands, if they are not yet available, it loads them synchronously. | ||
*/ | ||
public List<UICommand> getCommands(UIContext uiContext) | ||
{ | ||
if (commands == null) | ||
{ | ||
commands = CommandUtil.getEnabledCommands(uiContext); | ||
} | ||
|
||
return commands; | ||
} | ||
|
||
@Override | ||
public void run() | ||
{ | ||
while (true) | ||
{ | ||
try | ||
{ | ||
// Wait for reload() call | ||
latch.await(); | ||
commands = CommandUtil.getEnabledCommands(uiContext); | ||
} | ||
catch (Exception ex) | ||
{ | ||
ex.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/org/jboss/forge/plugin/idea/service/threads/ValidationThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2014 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.plugin.idea.service.threads; | ||
|
||
import org.jboss.forge.plugin.idea.service.callbacks.FormUpdateCallback; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
|
||
/** | ||
* Updates and validates Forge wizard forms. Ensures that many request are performed serially. | ||
* | ||
* @author Adam Wyłuda | ||
* @see org.jboss.forge.plugin.idea.service.callbacks.FormUpdateCallback | ||
*/ | ||
public class ValidationThread extends Thread | ||
{ | ||
private BlockingQueue<FormUpdateCallback> queue = new LinkedBlockingQueue<>(); | ||
|
||
public ValidationThread() | ||
{ | ||
super("ForgeValidationThread"); | ||
} | ||
|
||
/** | ||
* Appends form update request to the queue. If there is already request pending for the | ||
* same component, it will be replaced with new callback. | ||
*/ | ||
public void setNextCallback(FormUpdateCallback nextCallback) | ||
{ | ||
// Discard previous request for this component | ||
for (FormUpdateCallback callback : queue) | ||
{ | ||
if (callback.getInput() == nextCallback.getInput()) | ||
{ | ||
queue.remove(callback); | ||
break; | ||
} | ||
} | ||
|
||
queue.add(nextCallback); | ||
} | ||
|
||
@Override | ||
public void run() | ||
{ | ||
while (true) | ||
{ | ||
try | ||
{ | ||
queue.take().run(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
ex.printStackTrace(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters