Skip to content

Commit 9d36d65

Browse files
Implement observer pattern for game log path setting
1 parent bdb6c90 commit 9d36d65

File tree

8 files changed

+102
-16
lines changed

8 files changed

+102
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package tools.sctrade.companion.domain.gamelog;
2+
3+
import java.nio.file.Path;
4+
5+
public abstract class FilePathObserver {
6+
private FilePathSubject subject;
7+
protected Path filePath;
8+
9+
protected FilePathObserver(FilePathSubject subject) {
10+
this.subject = subject;
11+
}
12+
13+
protected void update() {
14+
this.filePath = subject.getState();
15+
}
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package tools.sctrade.companion.domain.gamelog;
2+
3+
import java.nio.file.Path;
4+
import java.util.Collection;
5+
6+
public abstract class FilePathSubject {
7+
Collection<FilePathObserver> observers;
8+
Path filePath;
9+
10+
public void attach(FilePathObserver observer) {
11+
observers.add(observer);
12+
}
13+
14+
public void detach(FilePathObserver observer) {
15+
observers.remove(observer);
16+
}
17+
18+
public Path getState() {
19+
return filePath;
20+
}
21+
22+
protected abstract void setState();
23+
24+
protected void notifyObservers() {
25+
observers.forEach(n -> n.update());
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package tools.sctrade.companion.domain.gamelog;
2+
3+
import org.apache.commons.io.input.TailerListener;
4+
import org.apache.commons.io.input.TailerListenerAdapter;
5+
6+
public class GameLogListener extends TailerListenerAdapter implements TailerListener {
7+
@Override
8+
public void handle(String line) {}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,49 @@
11
package tools.sctrade.companion.domain.gamelog;
22

3+
import java.nio.file.Path;
4+
import java.util.ArrayList;
35
import java.util.Optional;
46
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
68
import tools.sctrade.companion.domain.setting.Setting;
79
import tools.sctrade.companion.domain.setting.SettingRepository;
810

9-
public class GameLogService {
10-
private static final String GAME_LOG = "\\Game.log";
11+
public class GameLogPathSubject extends FilePathSubject {
12+
static final String GAME_LOG_FILE = "Game.log";
1113

12-
private final Logger logger = LoggerFactory.getLogger(GameLogService.class);
14+
private final Logger logger = LoggerFactory.getLogger(GameLogPathSubject.class);
1315

1416
private SettingRepository settings;
1517

16-
public GameLogService(SettingRepository settings) {
18+
public GameLogPathSubject(SettingRepository settings) {
19+
super();
1720
this.settings = settings;
21+
this.observers = new ArrayList<>();
22+
setState();
1823
}
1924

20-
public void updateStarCitizenLivePath(String starCitizenLivePath) {
25+
public void setStarCitizenLivePath(String starCitizenLivePath) {
2126
if (starCitizenLivePath == null || starCitizenLivePath.strip().isEmpty()) {
2227
logger.warn("Star Citizen LIVE path is empty");
2328
return;
2429
}
2530

2631
starCitizenLivePath = starCitizenLivePath.strip();
2732
settings.set(Setting.STAR_CITIZEN_LIVE_PATH, starCitizenLivePath.replace("\\", "\\\\"));
33+
setState();
2834
}
2935

3036
public Optional<String> getStarCitizenLivePath() {
3137
return Optional.ofNullable(settings.get(Setting.STAR_CITIZEN_LIVE_PATH).toString());
3238
}
3339

40+
@Override
41+
protected void setState() {
42+
if (getStarCitizenLivePath().isEmpty()) {
43+
return;
44+
}
45+
46+
filePath = Path.of(getStarCitizenLivePath().get(), GAME_LOG_FILE);
47+
notifyObservers();
48+
}
3449
}

src/main/java/tools/sctrade/companion/gui/CompanionGui.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import javax.swing.JMenuBar;
1616
import javax.swing.JMenuItem;
1717
import javax.swing.JTabbedPane;
18-
import tools.sctrade.companion.domain.gamelog.GameLogService;
18+
import tools.sctrade.companion.domain.gamelog.GameLogPathSubject;
1919
import tools.sctrade.companion.domain.notification.NotificationLevel;
2020
import tools.sctrade.companion.domain.notification.NotificationRepository;
2121
import tools.sctrade.companion.domain.setting.SettingRepository;
@@ -28,12 +28,12 @@ public class CompanionGui extends JFrame implements NotificationRepository {
2828
private static final long serialVersionUID = -983766141308946535L;
2929

3030
private transient UserService userService;
31-
private transient GameLogService gameLogService;
31+
private transient GameLogPathSubject gameLogService;
3232
private transient SettingRepository settings;
3333
private final String version;
3434
private LogsTab logsTab;
3535

36-
public CompanionGui(UserService userService, GameLogService gameLogService,
36+
public CompanionGui(UserService userService, GameLogPathSubject gameLogService,
3737
SettingRepository settings, String version) {
3838
this.userService = userService;
3939
this.gameLogService = gameLogService;

src/main/java/tools/sctrade/companion/gui/SettingsTab.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import javax.swing.JTextField;
88
import javax.swing.event.DocumentEvent;
99
import javax.swing.event.DocumentListener;
10-
import tools.sctrade.companion.domain.gamelog.GameLogService;
10+
import tools.sctrade.companion.domain.gamelog.GameLogPathSubject;
1111
import tools.sctrade.companion.domain.setting.Setting;
1212
import tools.sctrade.companion.domain.setting.SettingRepository;
1313
import tools.sctrade.companion.domain.user.UserService;
@@ -16,7 +16,7 @@
1616
public class SettingsTab extends JPanel {
1717
private static final long serialVersionUID = -3532718267415423680L;
1818

19-
public SettingsTab(UserService userService, GameLogService gameLogService,
19+
public SettingsTab(UserService userService, GameLogPathSubject gameLogService,
2020
SettingRepository settings) {
2121
super();
2222
setLayout(new GridBagLayout());
@@ -61,7 +61,7 @@ private void updateUsername() {
6161
buildLabel(2, " ");
6262
}
6363

64-
private void buildStarCitizenLivePathField(GameLogService gameLogService) {
64+
private void buildStarCitizenLivePathField(GameLogPathSubject gameLogService) {
6565
var starCitizenLivePathLabel = buildLabel(3, LocalizationUtil.get("labelStarCitizenLivePath"));
6666
var starCitizenLivePathField = buildTextField(3, gameLogService.getStarCitizenLivePath().get());
6767
starCitizenLivePathField.putClientProperty("JTextField.placeholderText",
@@ -85,7 +85,7 @@ public void changedUpdate(DocumentEvent e) {
8585
}
8686

8787
private void updateStarCitizenLivePath() {
88-
gameLogService.updateStarCitizenLivePath(starCitizenLivePathField.getText());
88+
gameLogService.setStarCitizenLivePath(starCitizenLivePathField.getText());
8989
}
9090
});
9191

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package tools.sctrade.companion.input;
2+
3+
import tools.sctrade.companion.domain.gamelog.FilePathObserver;
4+
import tools.sctrade.companion.domain.gamelog.FilePathSubject;
5+
6+
public class FileTailer extends FilePathObserver {
7+
8+
protected FileTailer(FilePathSubject subject) {
9+
super(subject);
10+
}
11+
12+
@Override
13+
protected void update() {
14+
super.update();
15+
}
16+
17+
}

src/main/java/tools/sctrade/companion/spring/AppConfig.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import tools.sctrade.companion.domain.commodity.CommodityRepository;
2222
import tools.sctrade.companion.domain.commodity.CommodityService;
2323
import tools.sctrade.companion.domain.commodity.CommoditySubmissionFactory;
24-
import tools.sctrade.companion.domain.gamelog.GameLogService;
24+
import tools.sctrade.companion.domain.gamelog.GameLogPathSubject;
2525
import tools.sctrade.companion.domain.image.ImageManipulation;
2626
import tools.sctrade.companion.domain.image.ImageWriter;
2727
import tools.sctrade.companion.domain.image.manipulations.CommodityKioskTextThreshold1;
@@ -81,12 +81,12 @@ public UserService buildUserService(SettingRepository settings) {
8181
}
8282

8383
@Bean("GameLogService")
84-
public GameLogService buildGameLogService(SettingRepository settings) {
85-
return new GameLogService(settings);
84+
public GameLogPathSubject buildGameLogService(SettingRepository settings) {
85+
return new GameLogPathSubject(settings);
8686
}
8787

8888
@Bean("CompanionGui")
89-
public CompanionGui buildCompanionGui(UserService userService, GameLogService gameLogService,
89+
public CompanionGui buildCompanionGui(UserService userService, GameLogPathSubject gameLogService,
9090
SettingRepository settings) {
9191
return new CompanionGui(userService, gameLogService, settings, getVersion());
9292
}

0 commit comments

Comments
 (0)