Skip to content

Commit

Permalink
Simplify launch
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Dec 25, 2023
1 parent 782e4f8 commit f8502b2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 49 deletions.
7 changes: 2 additions & 5 deletions src/main/java/mpo/dayon/assistant/AssistantRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ public static void main(String[] args) {

private static String[] appendAssistant(String[] args) {
String[] combined = Arrays.copyOf(args, args.length + 1);
String[] additional = new String[1];
additional[0] = "assistant";
String[] additional = new String[]{"assistant"};
System.arraycopy(additional, 0, combined, args.length, 1);
return combined;
}

public static void launchAssistant() {
final Assistant assistant = new Assistant();
assistant.configure();
assistant.start();
new Assistant();
}
}
38 changes: 14 additions & 24 deletions src/main/java/mpo/dayon/assistant/gui/Assistant.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public class Assistant implements ClipboardOwner {

private final NetworkAssistantEngine network;

private final ControlEngine control;

private final BitCounter receivedBitCounter;

private final TileCounter receivedTileCounter;
Expand All @@ -73,7 +71,7 @@ public class Assistant implements ClipboardOwner {

private AssistantFrame frame;

private AssistantActions actions;
private final AssistantActions actions;

private AssistantConfiguration configuration;

Expand All @@ -91,8 +89,6 @@ public class Assistant implements ClipboardOwner {

private int prevHeight = -1;

private final Set<Counter<?>> counters;

private final AtomicBoolean fitToScreenActivated = new AtomicBoolean(false);

private String token;
Expand All @@ -117,7 +113,7 @@ public Assistant() {
captureCompressionCounter = new CaptureCompressionCounter("captureCompression", translate("captureCompression"));
captureCompressionCounter.start(1000);

counters = new HashSet<>(Arrays.asList(receivedBitCounter, receivedTileCounter, skippedTileCounter, mergedTileCounter, captureCompressionCounter));
Set<Counter<?>> counters = new HashSet<>(Arrays.asList(receivedBitCounter, receivedTileCounter, skippedTileCounter, mergedTileCounter, captureCompressionCounter));

DeCompressorEngine decompressor = new DeCompressorEngine(new MyDeCompressorEngineListener());
decompressor.start(8);
Expand All @@ -129,28 +125,21 @@ public Assistant() {
network.configure(networkConfiguration);
network.addListener(new MyNetworkAssistantEngineListener());

control = new ControlEngine(network);
control.start();

captureEngineConfiguration = new CaptureEngineConfiguration();
compressorEngineConfiguration = new CompressorEngineConfiguration();
}

public void configure() {
this.configuration = new AssistantConfiguration();
final String lnf = configuration.getLookAndFeelClassName();
try {
UIManager.setLookAndFeel(lnf);
} catch (Exception ex) {
Log.warn("Could not set the [" + lnf + "] L&F!", ex);
}
}

public void start() {
actions = createAssistantActions();
frame = new AssistantFrame(actions, counters);
FatalErrorHandler.attachFrame(frame);
frame.addListener(control);
frame.addListener(new ControlEngine(network));
frame.setVisible(true);
initUpnp();
}
Expand Down Expand Up @@ -716,32 +705,33 @@ private JMenu createLookAndFeelSubmenu() {
submenu.setIcon(getOrCreateIcon(ImageNames.LNF));
submenu.setToolTipText(translate("lnf.switch"));

final LookAndFeel current = UIManager.getLookAndFeel();
submenu.add(new JMenuItem(current.getName()));
final String current = UIManager.getLookAndFeel().getName();
submenu.add(new JMenuItem(current));
submenu.addSeparator();

for (final UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if (info.getName().equals(current.getName())) {
String lnf = info.getName();
if (lnf.equals(current)) {
continue;
}
final JMenuItem mi = new JMenuItem(info.getName());
mi.addActionListener(ev1 -> switchLookAndFeel(info));
mi.setText(info.getName());
final JMenuItem mi = new JMenuItem(lnf);
mi.addActionListener(ev1 -> switchLookAndFeel(lnf));
mi.setText(lnf);
submenu.add(mi);
}
return submenu;
}

private void switchLookAndFeel(UIManager.LookAndFeelInfo lnf) {
private void switchLookAndFeel(String lnf) {
try {
if (frame != null) {
UIManager.setLookAndFeel(lnf.getClassName());
UIManager.setLookAndFeel(lnf);
SwingUtilities.updateComponentTreeUI(frame);
configuration = new AssistantConfiguration(lnf.getClassName());
configuration = new AssistantConfiguration(lnf);
configuration.persist();
}
} catch (Exception ex) {
Log.warn("Could not set the L&F [" + lnf.getName() + "]", ex);
Log.warn(format("Could not set the L&F [%s]", lnf), ex);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/mpo/dayon/assisted/AssistedRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public static void main(String[] args) {

public static void launchAssisted(String assistantHost, String assistantPort) {
final Assisted assisted = new Assisted();
assisted.setup();
// cli args have precedence
if (assistantHost == null || assistantPort == null) {
final Map<String, String> config = readPresetFile();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mpo/dayon/assisted/gui/Assisted.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public class Assisted implements Subscriber, ClipboardOwner {

private final AtomicBoolean shareAllScreens = new AtomicBoolean(false);

public void setup() {
public Assisted() {
final String lnf = getDefaultLookAndFeel();
try {
UIManager.setLookAndFeel(lnf);
} catch (Exception ex) {
Log.warn(format("Could not set the [%s] L&F", lnf), ex);
Log.warn(format("Could not set the L&F [%s]", lnf), ex);
}
}

Expand Down
25 changes: 8 additions & 17 deletions src/test/java/mpo/dayon/assisted/gui/AssistedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

class AssistedTest {

Assisted assisted;
LogAppender logApp;
private Assisted assisted;
private LogAppender logApp;

@BeforeEach
void init() throws NoSuchFieldException, IllegalAccessException {
Expand All @@ -33,41 +33,32 @@ void init() throws NoSuchFieldException, IllegalAccessException {
void startWithoutConfig() {
// given
if (!GraphicsEnvironment.isHeadless()) {
// when
boolean value = assisted.start("localhost", null, false);

// then
// when then
assertTrue(assisted.start("localhost", null, false));
verify(logApp).append(LogLevel.INFO, "Assisted start");
verify(logApp, never()).append(LogLevel.INFO, "Autoconfigured [ip:localhost][port:null]");
assertTrue(value);
}
}

@Test
void startAutoconnect() {
// given
if (!GraphicsEnvironment.isHeadless()) {
// when
boolean value = assisted.start("localhost", "12345", true);

// then
// when then
assertTrue(assisted.start("localhost", "12345", true));
verify(logApp).append(LogLevel.INFO, "Autoconfigured [ip:localhost][port:12345]");
verify(logApp).append(LogLevel.INFO, "Connecting to [localhost][12345]...");
assertTrue(value);
}
}

@Test
void startAutoconnectFalse() {
// given
if (!GraphicsEnvironment.isHeadless()) {
// when
boolean value = assisted.start("localhost", "23456", false);

// then
// when then
assertTrue(assisted.start("localhost", "23456", false));
verify(logApp).append(LogLevel.INFO, "Autoconfigured [ip:localhost][port:23456]");
verify(logApp, never()).append(LogLevel.INFO, "Connecting to [localhost][23456]...");
assertTrue(value);
}
}
}

0 comments on commit f8502b2

Please sign in to comment.