Skip to content

Commit

Permalink
Begin work on better color theming such as for color blindness so use…
Browse files Browse the repository at this point in the history
…rs can specify their own colors for Bram-Hub#426.
  • Loading branch information
FisherLuba committed Mar 19, 2024
1 parent 32371ed commit 46b7abb
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 38 deletions.
2 changes: 2 additions & 0 deletions src/main/java/edu/rpi/legup/Legup.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.rpi.legup;

import edu.rpi.legup.app.GameBoardFacade;
import edu.rpi.legup.ui.color.ColorPreferences;
import edu.rpi.legup.utility.Logger;

public class Legup {
Expand All @@ -14,5 +15,6 @@ public static void main(String[] args) {
Logger.initLogger();
GameBoardFacade.getInstance();
GameBoardFacade.setupConfig();
ColorPreferences.loadColorScheme();
}
}
2 changes: 1 addition & 1 deletion src/main/java/edu/rpi/legup/ui/HomePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void actionPerformed(ActionEvent e) {
}
};

public HomePanel(FileDialog fileDialog, JFrame frame, LegupUI legupUI) {
public HomePanel(JFrame frame, LegupUI legupUI) {
this.legupUI = legupUI;
this.frame = frame;
setLayout(new GridLayout(1, 2));
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/edu/rpi/legup/ui/LegupUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import edu.rpi.legup.ui.proofeditorui.treeview.TreePanel;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.security.InvalidParameterException;
import java.util.Objects;
import javax.swing.*;
Expand All @@ -17,7 +18,8 @@
public class LegupUI extends JFrame implements WindowListener {
private static final Logger LOGGER = LogManager.getLogger(LegupUI.class.getName());

protected FileDialog fileDialog;
// protected FileDialog fileDialog;
protected JFileChooser fileChooser;
protected JPanel window;
protected LegupPanel[] panels;

Expand Down Expand Up @@ -52,7 +54,9 @@ public LegupUI() {
System.err.println("Not supported ui look and feel");
}

fileDialog = new FileDialog(this);
// fileDialog = new FileDialog(this);
fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(LegupPreferences.WORK_DIRECTORY));

initPanels();
displayPanel(0);
Expand Down Expand Up @@ -97,9 +101,9 @@ private void initPanels() {
add(window);
panels = new LegupPanel[3];

panels[0] = new HomePanel(this.fileDialog, this, this);
panels[1] = new ProofEditorPanel(this.fileDialog, this, this);
panels[2] = new PuzzleEditorPanel(this.fileDialog, this, this);
panels[0] = new HomePanel(this, this);
panels[1] = new ProofEditorPanel(this.fileChooser, this, this);
panels[2] = new PuzzleEditorPanel(this.fileChooser, this, this);
}

protected void displayPanel(int option) {
Expand Down
27 changes: 16 additions & 11 deletions src/main/java/edu/rpi/legup/ui/ProofEditorPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import javax.swing.*;
Expand All @@ -40,7 +41,7 @@ public class ProofEditorPanel extends LegupPanel implements IHistoryListener {
private static final Logger LOGGER = LogManager.getLogger(ProofEditorPanel.class.getName());
private JMenuBar mBar;
private TreePanel treePanel;
private FileDialog fileDialog;
private JFileChooser fileChooser;
private JFrame frame;
private RuleFrame ruleFrame;
private DynamicView dynamicBoardView;
Expand Down Expand Up @@ -111,8 +112,8 @@ public class ProofEditorPanel extends LegupPanel implements IHistoryListener {
protected JMenuItem testAI = new JMenuItem("Test AI!");
protected JMenuItem hintAI = new JMenuItem("Hint");

public ProofEditorPanel(FileDialog fileDialog, JFrame frame, LegupUI legupUI) {
this.fileDialog = fileDialog;
public ProofEditorPanel(JFileChooser fileChooser, JFrame frame, LegupUI legupUI) {
this.fileChooser = fileChooser;
this.frame = frame;
this.legupUI = legupUI;
setLayout(new BorderLayout());
Expand Down Expand Up @@ -581,21 +582,25 @@ private void saveProofAs() {
return;
}

fileDialog.setMode(FileDialog.SAVE);
fileDialog.setTitle("Save As");
fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
// fileChooser.setMode(JFileChooser.SAVE);
// fileChooser.setTitle("Save As");
fileChooser.setDialogTitle("Save as");
String curFileName = GameBoardFacade.getInstance().getCurFileName();
if (curFileName == null) {
fileDialog.setDirectory(
LegupPreferences.getInstance().getUserPref(LegupPreferences.WORK_DIRECTORY));
fileChooser.setCurrentDirectory(
// fileChooser.setDirectory(
Path.of(LegupPreferences.getInstance().getUserPref(LegupPreferences.WORK_DIRECTORY)).toFile());
} else {
File curFile = new File(curFileName);
fileDialog.setDirectory(curFile.getParent());
// fileChooser.setDirectory(curFile.getParent());
fileChooser.setCurrentDirectory(curFile.getParentFile());
}
fileDialog.setVisible(true);
fileChooser.setVisible(true);

String fileName = null;
if (fileDialog.getDirectory() != null && fileDialog.getFile() != null) {
fileName = fileDialog.getDirectory() + File.separator + fileDialog.getFile();
if (fileChooser.getCurrentDirectory() != null && fileChooser.getSelectedFile() != null) {
fileName = fileChooser.getCurrentDirectory() + File.separator + fileChooser.getSelectedFile();
}

if (fileName != null) {
Expand Down
56 changes: 35 additions & 21 deletions src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.util.Objects;
import javax.swing.*;
import javax.swing.border.TitledBorder;
Expand All @@ -44,16 +45,16 @@ public class PuzzleEditorPanel extends LegupPanel implements IHistoryListener {
private BoardView boardView;
private TitledBorder boardBorder;
// private JSplitPane splitPanel, topHalfPanel;
private FileDialog fileDialog;
private JFileChooser fileChooser;
private JMenuItem undo, redo, fitBoardToScreen;
private ElementFrame elementFrame;
private JPanel treePanel;
private LegupUI legupUI;
private EditorElementController editorElementController;
static final int[] TOOLBAR_SEPARATOR_BEFORE = {2, 4, 8};

public PuzzleEditorPanel(FileDialog fileDialog, JFrame frame, LegupUI legupUI) {
this.fileDialog = fileDialog;
public PuzzleEditorPanel(JFileChooser fileChooser, JFrame frame, LegupUI legupUI) {
this.fileChooser = fileChooser;
this.frame = frame;
this.legupUI = legupUI;
setLayout(new BorderLayout());
Expand Down Expand Up @@ -384,20 +385,25 @@ public Object[] promptPuzzle() {
return new Object[0];
}
}
if (fileDialog == null) {
fileDialog = new FileDialog(this.frame);
if (fileChooser == null) {
// fileChooser = new JFileChooser(this.frame);
fileChooser = new JFileChooser();
}
LegupPreferences preferences = LegupPreferences.getInstance();
String preferredDirectory = preferences.getUserPref(LegupPreferences.WORK_DIRECTORY);

fileDialog.setMode(FileDialog.LOAD);
fileDialog.setTitle("Select Puzzle");
fileDialog.setDirectory(preferredDirectory);
fileDialog.setVisible(true);
// fileChooser.setMode(JFileChooser.LOAD);
fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
// fileChooser.setTitle("Select Puzzle");
fileChooser.setDialogTitle("Select Puzzle");
// fileChooser.setDirectory(preferredDirectory);
fileChooser.setCurrentDirectory(Path.of(preferredDirectory).toFile());
fileChooser.showOpenDialog(this);
fileChooser.setVisible(true);
String fileName = null;
File puzzleFile = null;
if (fileDialog.getDirectory() != null && fileDialog.getFile() != null) {
fileName = fileDialog.getDirectory() + File.separator + fileDialog.getFile();
if (fileChooser.getCurrentDirectory() != null && fileChooser.getSelectedFile() != null) {
fileName = fileChooser.getCurrentDirectory() + File.separator + fileChooser.getSelectedFile();
puzzleFile = new File(fileName);
} else {
// The attempt to prompt a puzzle ended gracefully (cancel)
Expand Down Expand Up @@ -538,25 +544,33 @@ private String savePuzzle() {
}
}

if (fileDialog == null) {
fileDialog = new FileDialog(this.frame);
if (fileChooser == null) {
// fileChooser = new JFileChooser(this.frame);
fileChooser = new JFileChooser();
fileChooser.showOpenDialog(this);
fileChooser.setVisible(true);
}

fileDialog.setMode(FileDialog.SAVE);
fileDialog.setTitle("Save Proof");
// fileChooser.setMode(JFileChooser.SAVE);
fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
// fileChooser.setTitle("Save Proof");
fileChooser.setDialogTitle("Save Proof");
String curFileName = GameBoardFacade.getInstance().getCurFileName();
if (curFileName == null) {
fileDialog.setDirectory(
LegupPreferences.getInstance().getUserPref(LegupPreferences.WORK_DIRECTORY));
// fileChooser.setDirectory(
fileChooser.setCurrentDirectory(
Path.of(LegupPreferences.getInstance().getUserPref(LegupPreferences.WORK_DIRECTORY)).toFile());
} else {
File curFile = new File(curFileName);
fileDialog.setDirectory(curFile.getParent());
// fileChooser.setDirectory(curFile.getParent());
fileChooser.setCurrentDirectory(curFile.getParentFile());
}
fileDialog.setVisible(true);
fileChooser.showOpenDialog(this);
fileChooser.setVisible(true);

String fileName = null;
if (fileDialog.getDirectory() != null && fileDialog.getFile() != null) {
fileName = fileDialog.getDirectory() + File.separator + fileDialog.getFile();
if (fileChooser.getCurrentDirectory() != null && fileChooser.getSelectedFile() != null) {
fileName = fileChooser.getCurrentDirectory() + File.separator + fileChooser.getSelectedFile();
}

if (fileName != null) {
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/edu/rpi/legup/ui/color/ColorPreferences.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package edu.rpi.legup.ui.color;

import java.awt.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

public class ColorPreferences {

private static final String COLOR_THEME_FILE_NAME = "color-theme.txt";

private static final Map<UIColor, Color> COLOR_MAP = new EnumMap<>(UIColor.class);

public enum UIColor {

CORRECT,
INCORRECT

;

public String configKey() {
return this.toString().toLowerCase().replace('_', '-');
}

public Color get() {
return COLOR_MAP.get(this);
}
}

public static void loadColorScheme() {
final File file = Path.of(COLOR_THEME_FILE_NAME).toFile();
final InputStream input = ClassLoader.getSystemClassLoader().getResourceAsStream(COLOR_THEME_FILE_NAME);
BufferedReader reader;
boolean copyResourceToFile = false;
if (!file.exists()) {
try {
file.createNewFile();
copyResourceToFile = true;
}
catch (IOException e) {
System.err.println("Could not create " + COLOR_THEME_FILE_NAME);
}
if (input == null) {
throw new RuntimeException("Could not find resource " + COLOR_THEME_FILE_NAME);
}
reader = new BufferedReader(new InputStreamReader(input));
}
else {
try {
reader = new BufferedReader(new FileReader(file));
}
catch (FileNotFoundException e) {
throw new RuntimeException("Could not find file " + file.getAbsoluteFile());
}
}
final List<String> lines = reader.lines().toList();
if (copyResourceToFile) {
final Path path = file.toPath();
lines.forEach(line -> {
try {
Files.writeString(path, line + System.lineSeparator(), StandardOpenOption.APPEND);
}
catch (IOException e) {
throw new RuntimeException(e);
}
});
}

COLOR_MAP.putAll(lines.stream()
.filter(l -> !l.startsWith("//")) // Use // for comments
.map(l -> l.split(":"))
.filter(a -> a.length == 2)
.peek(a -> System.out.println(Arrays.toString(a)))
.collect(Collectors.toMap(e -> UIColor.valueOf(e[0]), e -> colorFromString(e[1].strip()))));
System.out.println("Colors: " + COLOR_MAP);
}

public static Color colorFromString(String color) {
try {
return (Color) Color.class.getField(color).get(null);
}
catch (NullPointerException | NoSuchFieldException | IllegalAccessException | ClassCastException e) {
return Color.getColor(color);
}
}

}
2 changes: 2 additions & 0 deletions src/main/resources/color-theme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CORRECT: BLUE
INCORRECT: RED

0 comments on commit 46b7abb

Please sign in to comment.