Skip to content

Commit

Permalink
added setting path
Browse files Browse the repository at this point in the history
  • Loading branch information
baslo2 committed Jul 12, 2024
1 parent 3b8367a commit 456a6c3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/main/java/ru/taximaxim/pgsqlblocks/dialogs/SettingsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,24 @@

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;

import ru.taximaxim.pgsqlblocks.utils.ImageUtils;
import ru.taximaxim.pgsqlblocks.utils.Images;
import ru.taximaxim.pgsqlblocks.utils.PathBuilder;
import ru.taximaxim.pgsqlblocks.utils.Settings;

public class SettingsDialog extends Dialog {
Expand All @@ -45,6 +52,7 @@ public class SettingsDialog extends Dialog {
private Button confirmRequiredButton;
private Button confirmExitButton;
private Combo languageCombo;
private Text path;

public SettingsDialog(Settings settings, Shell shell) {
super(shell);
Expand All @@ -64,9 +72,38 @@ protected Control createDialogArea(Composite parent) {
populateGeneralGroup(container);
populateProcessGroup(container);
populateNotificationGroup(container);
populateBlockJournalPathGroup(container);
return container;
}

private void populateBlockJournalPathGroup(Composite container) {
Group generalGroup = new Group(container, SWT.SHADOW_IN);
generalGroup.setText(resourceBundle.getString("path"));
generalGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
generalGroup.setLayout(new GridLayout(2, false));

path = new Text(generalGroup, SWT.BORDER);
path.setText(settings.getBlocksJournalPath());
path.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));

Button btnDir = new Button(generalGroup, SWT.PUSH);
btnDir.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, true, true));
btnDir.setImage(ImageUtils.getImage(Images.FOLDER));
btnDir.addSelectionListener(new SelectionAdapter() {

@Override
public void widgetSelected(SelectionEvent e) {
DirectoryDialog dialog = new DirectoryDialog(getShell());
dialog.setText(resourceBundle.getString("choose_dir"));
dialog.setFilterPath(PathBuilder.getInstance().getBlocksJournalsDir().toString());
String p = dialog.open();
if (p != null) {
path.setText(p);
}
}
});
}

private void populateGeneralGroup(Composite container) {
Group generalGroup = new Group(container, SWT.SHADOW_IN);
generalGroup.setText(resourceBundle.getString("general"));
Expand Down Expand Up @@ -170,6 +207,7 @@ protected void okPressed() {
settings.setConfirmExit(confirmExitButton.getSelection());
settings.setLanguage(languageCombo.getText());
settings.setShowBackendPid(showBackendPidButton.getSelection());
settings.setBlocksJournalsPath(path.getText());

super.okPressed();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ protected Object[][] getContents() {
{"columns", "Columns"},
{"general", "General"},
{"select_ui_language", "UI language (requires restart)"},
{"path", "Path to save block journals"},
{"choose_dir", "Choose directory"},

// create database dialog
{"name", "Connection name*"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ protected Object[][] getContents() {
{"columns", "Колонки"},
{"general", "Общие"},
{"select_ui_language", "Язык интерфейса (требует перезапуска)"},
{"path", "Путь хранения журналов блокировок"},
{"choose_dir", "Выберите директорию"},

// create database dialog
{"name", "Имя соединения*"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static PathBuilder getInstance() {
}

public Path getBlocksJournalsDir() {
Path blocksJournalsDir = path.resolve("blocksJournals");
Path blocksJournalsDir = path.resolve(Settings.getInstance().getBlocksJournalPath());
if (!blocksJournalsDir.toFile().exists()) {
try {
Files.createDirectory(blocksJournalsDir);
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/ru/taximaxim/pgsqlblocks/utils/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Properties;
import java.util.ResourceBundle;

Expand Down Expand Up @@ -52,6 +53,7 @@ public final class Settings {
private static final String CONFIRM_EXIT = "confirm_exit";
private static final String SHOW_LOG_MESSAGES = "show_log_messages";
private static final String CURRENT_LOCALE = "current_locale";
private static final String BLOCKS_JOURNAL_PATH = "blocks_journal_path";

private int updatePeriodSeconds;
private int limitBlocks;
Expand All @@ -64,6 +66,7 @@ public final class Settings {
private boolean confirmRequired;
private boolean showBackendPid;
private boolean confirmExit;
private String blocksJournalPath;

private final Properties properties;
private final File propFile;
Expand Down Expand Up @@ -107,6 +110,7 @@ private Settings() {
this.confirmRequired = Boolean.parseBoolean(properties.getProperty(CONFIRM_REQUIRED));
this.confirmExit = Boolean.parseBoolean(properties.getProperty(CONFIRM_EXIT));
this.locale = new Locale.Builder().setLanguageTag(properties.getProperty(CURRENT_LOCALE)).build();
this.blocksJournalPath = properties.getProperty(BLOCKS_JOURNAL_PATH);

resources = ResourceBundle.getBundle(ru.taximaxim.pgsqlblocks.l10n.PgSqlBlocks.class.getName(), locale);
}
Expand Down Expand Up @@ -289,6 +293,30 @@ public ResourceBundle getResourceBundle() {
return resources;
}

public void setBlocksJournalsPath(String path) {
if (path.isEmpty() || path.isBlank()) {
if (blocksJournalPath != null) {
blocksJournalPath = null;
properties.remove(BLOCKS_JOURNAL_PATH);
return;
}
if (path.equals(PathBuilder.getInstance().getBlocksJournalsDir().toString())) {
return;
}
}
if (!Objects.equals(blocksJournalPath, path)) {
blocksJournalPath = path;
saveProperties(BLOCKS_JOURNAL_PATH, blocksJournalPath);
}
}

public String getBlocksJournalPath() {
if (blocksJournalPath != null) {
return blocksJournalPath;
}
return PathBuilder.getInstance().getBlocksJournalsDir().toString();
}

private void saveProperties(String key, String value) {
try (FileOutputStream saveProp = new FileOutputStream(propFile)) {
properties.setProperty(key, value);
Expand Down

0 comments on commit 456a6c3

Please sign in to comment.