Skip to content

Commit

Permalink
Minor connect frame fixes
Browse files Browse the repository at this point in the history
Remove shutdown Thread, do it after waiting on executor
  • Loading branch information
Kakifrucht committed Mar 26, 2022
1 parent 6311fab commit 817f84e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
21 changes: 3 additions & 18 deletions src/main/java/io/lightbeat/LightBeat.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.lightbeat.gui.FrameManager;
import io.lightbeat.hue.bridge.HueManager;
import io.lightbeat.hue.bridge.LBHueManager;
import io.lightbeat.util.TimeThreshold;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -89,25 +88,11 @@ public void shutdownAll() {
try {
executorService.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
logger.warn("Executor service forcefully shutdown, some tasks have not completed");
logger.warn("Executor service forcefully shut down, some tasks have not completed");
}

// dispatch thread that force exits if still running after 5 seconds
TimeThreshold forceShutdownThreshold = new TimeThreshold(5000L);
Thread forceShutdownThread = new Thread(() -> {
while (true) {
if (forceShutdownThreshold.isMet()) {
logger.info("Forcing runtime exit");
Runtime.getRuntime().exit(0);
}

try {
Thread.sleep(1000L);
} catch (InterruptedException ignored) {}
}
});
forceShutdownThread.setDaemon(true);
forceShutdownThread.start();
// ensure that we exit
Runtime.getRuntime().exit(0);
}

@Override
Expand Down
27 changes: 21 additions & 6 deletions src/main/java/io/lightbeat/gui/frame/ConnectFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
public class ConnectFrame extends AbstractFrame implements HueStateObserver {

private static final int MANUAL_FIELD_MIN_LENGTH = 6;

private JPanel mainPanel;

private LoadingIndicator statusLabelIndicator;
Expand All @@ -47,16 +49,25 @@ public ConnectFrame(ComponentHolder componentHolder, int x, int y) {

if (setVisible != manualField.isVisible()) {
manualField.setVisible(setVisible);
connectButton.setEnabled(manualField.getText().length() > MANUAL_FIELD_MIN_LENGTH && !manualField.getForeground().equals(Color.GRAY));
getJFrame().pack();
}
});

manualField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
connectButton.doClick();
}
public void keyTyped(KeyEvent e) {

executorService.schedule(() -> {
runOnSwingThread(() -> {
boolean isEnabled = manualField.getText().length() > MANUAL_FIELD_MIN_LENGTH;
connectButton.setEnabled(isEnabled);

if (isEnabled && e.getKeyCode() == KeyEvent.VK_ENTER) {
connectButton.doClick();
}
});
}, 1 , TimeUnit.MILLISECONDS);
}
});

Expand All @@ -66,7 +77,7 @@ public void keyPressed(KeyEvent e) {
public void focusGained(FocusEvent e) {
if (manualField.getText().equals(text)) {
manualField.setText("");
manualField.setForeground(Color.BLACK);
manualField.setForeground(statusLabelIndicator.getForeground());
}
}
@Override
Expand All @@ -92,7 +103,7 @@ public void focusLost(FocusEvent e) {
// manual connect
if (selectBridgeBox.getItemCount() == selectedIndex + 1) {
String address = manualField.getText();
if (address.length() > 6 && manualField.getForeground().equals(Color.BLACK)) {
if (manualField.getForeground().equals(statusLabelIndicator.getForeground())) {
hueManager.setAttemptConnection(new AccessPoint(address));
}
} else {
Expand All @@ -105,6 +116,7 @@ public void focusLost(FocusEvent e) {
}
});

frame.getRootPane().setDefaultButton(connectButton);
drawFrame(mainPanel, true);
}

Expand Down Expand Up @@ -206,6 +218,9 @@ private void toggleButtonAndDropdown(boolean setEnabled, String labelText) {
manualField.setEnabled(setEnabled);
statusLabelIndicator.setText(labelText);
statusLabelIndicator.setRunning(!setEnabled);
if (connectButton.isEnabled()) {
connectButton.requestFocus();
}
frame.pack();
});
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/lightbeat/gui/frame/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,16 +383,17 @@ private void startBeatDetection() {
infoLabel.setText("Running | Some settings cannot be changed during visualisation");
componentHolder.getAudioEventManager().registerBeatObserver(this);
setElementsEnabled(false);
return;
}
} else {
showErrorMessage("Please select at least one light");
return;
}

return;
}
}

showErrorMessage("Selected audio source is no longer available");
refreshDeviceSelectComboBox();
}

private void stopBeatDetection() {
Expand Down

0 comments on commit 817f84e

Please sign in to comment.