implement blocking queue in MenuPanel, that way other threads like PiIR could post actions
This commit is contained in:
parent
b4557637c2
commit
248092dfcf
4 changed files with 48 additions and 4 deletions
|
|
@ -5,6 +5,8 @@ import com.mykola2312.mptv.db.DB;
|
|||
import com.mykola2312.mptv.task.ProcessService;
|
||||
import com.mykola2312.mptv.task.TaskDispatcher;
|
||||
import com.mykola2312.mptv.ui.MainFrame;
|
||||
import com.mykola2312.mptv.ui.MenuAction;
|
||||
|
||||
import org.apache.commons.cli.*;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.slf4j.Logger;
|
||||
|
|
@ -91,5 +93,17 @@ public class Main {
|
|||
frame.create(config.frame);
|
||||
|
||||
logger.info("mptv started");
|
||||
|
||||
// TODO: remove this mock thread test
|
||||
new Thread(new Runnable() {
|
||||
@Override()
|
||||
public void run() {
|
||||
try { Thread.sleep(5000L); } catch (InterruptedException e) {}
|
||||
|
||||
frame.action(MenuAction.ACTION_RIGHT);
|
||||
}
|
||||
}).start();
|
||||
|
||||
frame.loop();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ public class PiIRBindItem {
|
|||
public String preData;
|
||||
|
||||
@NonNull
|
||||
public String code;
|
||||
public String data;
|
||||
|
||||
@NonNull
|
||||
public MenuAction menuAction;
|
||||
|
|
|
|||
|
|
@ -31,8 +31,17 @@ public class MainFrame {
|
|||
}
|
||||
|
||||
public void create(FrameConfig config) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
spawn(config);
|
||||
});
|
||||
// SwingUtilities.invokeLater(() -> {
|
||||
// spawn(config);
|
||||
// });
|
||||
spawn(config);
|
||||
}
|
||||
|
||||
public void loop() {
|
||||
menu.actionLoop();
|
||||
}
|
||||
|
||||
public void action(MenuAction action) {
|
||||
menu.postAction(action);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.mykola2312.mptv.ui;
|
|||
|
||||
import javax.swing.*;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
|
@ -166,6 +167,26 @@ public class MenuPanel extends JPanel {
|
|||
if (load) menuPosition = newMenuPosition;
|
||||
}
|
||||
|
||||
private LinkedBlockingQueue<MenuAction> actionQueue = new LinkedBlockingQueue<>();
|
||||
|
||||
public void actionLoop() {
|
||||
while (!Thread.interrupted()) {
|
||||
try {
|
||||
MenuAction action = actionQueue.take();
|
||||
logger.info("executing action " + action.toString());
|
||||
|
||||
handleMenuAction(action);
|
||||
} catch (InterruptedException e) {
|
||||
logger.warn("interrupted");
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void postAction(MenuAction action) {
|
||||
actionQueue.add(action);
|
||||
}
|
||||
|
||||
public MenuPanel(Font font) {
|
||||
super(new BorderLayout());
|
||||
setFont(font);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue