2025-11-17 14:45:58 +01:00

443 lines
15 KiB
Java

package com.kfmanager.config;
import java.awt.*;
import java.io.*;
import java.util.Properties;
/**
* Class for saving and loading the application configuration
*/
public class AppConfig {
private static final String CONFIG_FILE = System.getProperty("user.home") +
File.separator + ".kfmanager" + File.separator + "config.properties";
private Properties properties;
public AppConfig() {
properties = new Properties();
loadConfig();
}
/**
* Load configuration from file
*/
private void loadConfig() {
File configFile = new File(CONFIG_FILE);
if (configFile.exists()) {
try (FileInputStream fis = new FileInputStream(configFile)) {
properties.load(fis);
} catch (IOException e) {
System.err.println("Nepodařilo se načíst konfiguraci: " + e.getMessage());
}
}
}
/**
* Save configuration to file
*/
public void saveConfig() {
File configFile = new File(CONFIG_FILE);
File configDir = configFile.getParentFile();
// Create configuration directory if it does not exist
if (!configDir.exists()) {
configDir.mkdirs();
}
try (FileOutputStream fos = new FileOutputStream(configFile)) {
properties.store(fos, "KF File Manager Configuration");
} catch (IOException e) {
System.err.println("Nepodařilo se uložit konfiguraci: " + e.getMessage());
}
}
// Getters and setters for individual configuration values
public int getWindowX() {
return Integer.parseInt(properties.getProperty("window.x", "100"));
}
public void setWindowX(int x) {
properties.setProperty("window.x", String.valueOf(x));
}
public int getWindowY() {
return Integer.parseInt(properties.getProperty("window.y", "100"));
}
public void setWindowY(int y) {
properties.setProperty("window.y", String.valueOf(y));
}
public int getWindowWidth() {
return Integer.parseInt(properties.getProperty("window.width", "1200"));
}
public void setWindowWidth(int width) {
properties.setProperty("window.width", String.valueOf(width));
}
public int getWindowHeight() {
return Integer.parseInt(properties.getProperty("window.height", "700"));
}
public void setWindowHeight(int height) {
properties.setProperty("window.height", String.valueOf(height));
}
public boolean isWindowMaximized() {
return Boolean.parseBoolean(properties.getProperty("window.maximized", "false"));
}
public void setWindowMaximized(boolean maximized) {
properties.setProperty("window.maximized", String.valueOf(maximized));
}
public String getLeftPanelPath() {
return properties.getProperty("leftPanel.path", System.getProperty("user.home"));
}
public void setLeftPanelPath(String path) {
properties.setProperty("leftPanel.path", path);
}
public String getRightPanelPath() {
return properties.getProperty("rightPanel.path", System.getProperty("user.home"));
}
public void setRightPanelPath(String path) {
properties.setProperty("rightPanel.path", path);
}
// ViewMode konfigurace
public String getLeftPanelViewMode() {
return properties.getProperty("leftPanel.viewMode", "FULL");
}
public void setLeftPanelViewMode(String viewMode) {
properties.setProperty("leftPanel.viewMode", viewMode);
}
public String getRightPanelViewMode() {
return properties.getProperty("rightPanel.viewMode", "FULL");
}
public void setRightPanelViewMode(String viewMode) {
properties.setProperty("rightPanel.viewMode", viewMode);
}
// --- Tab/session persistence ---
public int getLeftPanelTabCount() {
return Integer.parseInt(properties.getProperty("leftPanel.tabs.count", "0"));
}
public String getLeftPanelTabPath(int index) {
return properties.getProperty("leftPanel.tab." + index + ".path", null);
}
public String getLeftPanelTabViewMode(int index) {
return properties.getProperty("leftPanel.tab." + index + ".viewMode", "FULL");
}
public int getLeftPanelSelectedIndex() {
return Integer.parseInt(properties.getProperty("leftPanel.selectedIndex", "0"));
}
public void saveLeftPanelTabs(java.util.List<String> paths, java.util.List<String> viewModes, int selectedIndex) {
properties.setProperty("leftPanel.tabs.count", String.valueOf(paths.size()));
for (int i = 0; i < paths.size(); i++) {
properties.setProperty("leftPanel.tab." + i + ".path", paths.get(i));
properties.setProperty("leftPanel.tab." + i + ".viewMode", viewModes.get(i));
}
properties.setProperty("leftPanel.selectedIndex", String.valueOf(selectedIndex));
}
public int getRightPanelTabCount() {
return Integer.parseInt(properties.getProperty("rightPanel.tabs.count", "0"));
}
public String getRightPanelTabPath(int index) {
return properties.getProperty("rightPanel.tab." + index + ".path", null);
}
public String getRightPanelTabViewMode(int index) {
return properties.getProperty("rightPanel.tab." + index + ".viewMode", "FULL");
}
public int getRightPanelSelectedIndex() {
return Integer.parseInt(properties.getProperty("rightPanel.selectedIndex", "0"));
}
public void saveRightPanelTabs(java.util.List<String> paths, java.util.List<String> viewModes, int selectedIndex) {
properties.setProperty("rightPanel.tabs.count", String.valueOf(paths.size()));
for (int i = 0; i < paths.size(); i++) {
properties.setProperty("rightPanel.tab." + i + ".path", paths.get(i));
properties.setProperty("rightPanel.tab." + i + ".viewMode", viewModes.get(i));
}
properties.setProperty("rightPanel.selectedIndex", String.valueOf(selectedIndex));
}
// Font konfigurace
public String getEditorFontName() {
return properties.getProperty("editor.font.name", "Monospaced");
}
public void setEditorFontName(String fontName) {
properties.setProperty("editor.font.name", fontName);
}
public int getEditorFontSize() {
return Integer.parseInt(properties.getProperty("editor.font.size", "12"));
}
public void setEditorFontSize(int fontSize) {
properties.setProperty("editor.font.size", String.valueOf(fontSize));
}
public int getEditorFontStyle() {
return Integer.parseInt(properties.getProperty("editor.font.style", String.valueOf(Font.PLAIN)));
}
public void setEditorFontStyle(int style) {
properties.setProperty("editor.font.style", String.valueOf(style));
}
public Font getEditorFont() {
return new Font(getEditorFontName(), getEditorFontStyle(), getEditorFontSize());
}
public void setEditorFont(Font font) {
setEditorFontName(font.getName());
setEditorFontSize(font.getSize());
setEditorFontStyle(font.getStyle());
}
// --- Appearance (global) settings ---
public String getGlobalFontName() {
return properties.getProperty("global.font.name", "Monospaced");
}
public void setGlobalFontName(String name) {
properties.setProperty("global.font.name", name);
}
public int getGlobalFontSize() {
return Integer.parseInt(properties.getProperty("global.font.size", "12"));
}
public void setGlobalFontSize(int size) {
properties.setProperty("global.font.size", String.valueOf(size));
}
public Font getGlobalFont() {
return new Font(getGlobalFontName(), getGlobalFontStyle(), getGlobalFontSize());
}
public void setGlobalFont(Font font) {
setGlobalFontName(font.getName());
setGlobalFontSize(font.getSize());
setGlobalFontStyle(font.getStyle());
}
public int getGlobalFontStyle() {
return Integer.parseInt(properties.getProperty("global.font.style", String.valueOf(Font.PLAIN)));
}
public void setGlobalFontStyle(int style) {
properties.setProperty("global.font.style", String.valueOf(style));
}
// Colors stored as hex strings (e.g. #RRGGBB)
public Color getBackgroundColor() {
String v = properties.getProperty("appearance.bg", null);
if (v == null) return null;
try { return Color.decode(v); } catch (Exception ex) { return null; }
}
public void setBackgroundColor(Color c) {
if (c == null) {
properties.remove("appearance.bg");
} else {
properties.setProperty("appearance.bg", String.format("#%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue()));
}
}
public Color getSelectionColor() {
String v = properties.getProperty("appearance.selection", null);
if (v == null) return null;
try { return Color.decode(v); } catch (Exception ex) { return null; }
}
public void setSelectionColor(Color c) {
if (c == null) {
properties.remove("appearance.selection");
} else {
properties.setProperty("appearance.selection", String.format("#%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue()));
}
}
public Color getMarkedColor() {
String v = properties.getProperty("appearance.marked", null);
if (v == null) return null;
try { return Color.decode(v); } catch (Exception ex) { return null; }
}
public void setMarkedColor(Color c) {
if (c == null) {
properties.remove("appearance.marked");
} else {
properties.setProperty("appearance.marked", String.format("#%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue()));
}
}
// -- Sorting persistence (global default)
public int getDefaultSortColumn() {
return Integer.parseInt(properties.getProperty("global.sort.column", "-1"));
}
public void setDefaultSortColumn(int col) {
properties.setProperty("global.sort.column", String.valueOf(col));
}
public boolean getDefaultSortAscending() {
return Boolean.parseBoolean(properties.getProperty("global.sort.ascending", "true"));
}
public void setDefaultSortAscending(boolean asc) {
properties.setProperty("global.sort.ascending", String.valueOf(asc));
}
/**
* Save window state
*/
public void saveWindowState(Frame frame) {
if (frame.getExtendedState() == Frame.MAXIMIZED_BOTH) {
setWindowMaximized(true);
} else {
setWindowMaximized(false);
setWindowX(frame.getX());
setWindowY(frame.getY());
setWindowWidth(frame.getWidth());
setWindowHeight(frame.getHeight());
}
}
/**
* Restore window state
*/
public void restoreWindowState(Frame frame) {
frame.setLocation(getWindowX(), getWindowY());
frame.setSize(getWindowWidth(), getWindowHeight());
if (isWindowMaximized()) {
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
}
}
// --- Search dialog persistence ---
public int getSearchDialogX() {
return Integer.parseInt(properties.getProperty("searchDialog.x", "100"));
}
public void setSearchDialogX(int x) {
properties.setProperty("searchDialog.x", String.valueOf(x));
}
public int getSearchDialogY() {
return Integer.parseInt(properties.getProperty("searchDialog.y", "100"));
}
public void setSearchDialogY(int y) {
properties.setProperty("searchDialog.y", String.valueOf(y));
}
public int getSearchDialogWidth() {
return Integer.parseInt(properties.getProperty("searchDialog.width", "700"));
}
public void setSearchDialogWidth(int w) {
properties.setProperty("searchDialog.width", String.valueOf(w));
}
public int getSearchDialogHeight() {
return Integer.parseInt(properties.getProperty("searchDialog.height", "500"));
}
public void setSearchDialogHeight(int h) {
properties.setProperty("searchDialog.height", String.valueOf(h));
}
/** Save search dialog bounds */
public void saveSearchDialogState(Window win) {
if (win == null) return;
setSearchDialogX(win.getX());
setSearchDialogY(win.getY());
setSearchDialogWidth(win.getWidth());
setSearchDialogHeight(win.getHeight());
}
/** Restore search dialog bounds */
public void restoreSearchDialogState(Window win) {
if (win == null) return;
win.setLocation(getSearchDialogX(), getSearchDialogY());
win.setSize(getSearchDialogWidth(), getSearchDialogHeight());
}
// --- Search history persistence ---
public java.util.List<String> getSearchHistory() {
java.util.List<String> list = new java.util.ArrayList<>();
int count = Integer.parseInt(properties.getProperty("search.history.count", "0"));
for (int i = 0; i < count; i++) {
String v = properties.getProperty("search.history." + i, null);
if (v != null && !v.isEmpty()) list.add(v);
}
return list;
}
public void saveSearchHistory(java.util.List<String> history) {
if (history == null) {
properties.setProperty("search.history.count", "0");
return;
}
int limit = Math.min(history.size(), 50); // cap stored entries
properties.setProperty("search.history.count", String.valueOf(limit));
for (int i = 0; i < limit; i++) {
properties.setProperty("search.history." + i, history.get(i));
}
// remove any old entries beyond limit
int old = Integer.parseInt(properties.getProperty("search.history.count", "0"));
for (int i = limit; i < old; i++) {
properties.remove("search.history." + i);
}
}
// --- Content search history persistence ---
public java.util.List<String> getContentSearchHistory() {
java.util.List<String> list = new java.util.ArrayList<>();
int count = Integer.parseInt(properties.getProperty("search.content.history.count", "0"));
for (int i = 0; i < count; i++) {
String v = properties.getProperty("search.content.history." + i, null);
if (v != null && !v.isEmpty()) list.add(v);
}
return list;
}
public void saveContentSearchHistory(java.util.List<String> history) {
if (history == null) {
properties.setProperty("search.content.history.count", "0");
return;
}
int limit = Math.min(history.size(), 50);
properties.setProperty("search.content.history.count", String.valueOf(limit));
for (int i = 0; i < limit; i++) {
properties.setProperty("search.content.history." + i, history.get(i));
}
// remove old entries beyond limit
int old = Integer.parseInt(properties.getProperty("search.content.history.count", "0"));
for (int i = limit; i < old; i++) {
properties.remove("search.content.history." + i);
}
}
}