Skip to content

Commit

Permalink
рефакторинг и настройки
Browse files Browse the repository at this point in the history
  • Loading branch information
YanSergey committed Dec 10, 2022
1 parent 0864bde commit 6deadf3
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ enum TaskState {
static Image taskRunning = Helper.getImage("taskRunning.png"); // $NON-NLS-1$
static Image taskCompleted = Helper.getImage("taskCompleted.png"); // $NON-NLS-1$
static Image taskError = Helper.getImage("taskError.png"); // $NON-NLS-1$
// Image currentIcon;

Date startDate;
Date finishDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
import org.eclipse.swt.widgets.Shell;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;
import ru.yanygin.clusterAdminLibrary.ColumnProperties.RowSortDirection;
import ru.yanygin.clusterAdminLibrary.InfoBaseInfoShortExt.InfobasesSortDirection;
Expand Down Expand Up @@ -122,6 +123,14 @@ public class Config {
@Expose
private InfobasesSortDirection infobasesSortDirection = InfobasesSortDirection.DISABLE;

@SerializedName("ListRefreshRate")
@Expose
private int listRefreshRate = 5000;

@SerializedName("LoggerLevel")
@Expose
private String loggerLevel = "error";

@SerializedName("Servers")
@Expose
private Map<String, Server> servers = new HashMap<>();
Expand All @@ -147,8 +156,12 @@ public class Config {
private ColumnProperties wsColumnProperties = new ColumnProperties(0);

private static final Logger LOGGER =
LoggerFactory.getLogger("clusterAdminLibrary"); //$NON-NLS-1$
private static final String DEFAULT_CONFIG_PATH = "config.json"; //$NON-NLS-1$
(Logger) LoggerFactory.getLogger(Config.class.getSimpleName());

private static final Logger ROOT_LOGGER =
(Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);

private static final String DEFAULT_CONFIG_PATH = "config.json"; // $NON-NLS-1$
private static final String TEMP_CONFIG_PATH = "config_temp.json"; //$NON-NLS-1$

public static Config currentConfig;
Expand Down Expand Up @@ -844,6 +857,56 @@ public void setInfobasesSortDirection(InfobasesSortDirection sortDirection) {
this.infobasesSortDirection = sortDirection;
}

/**
* Получить установленный уровень логирования.
*
* @return уровень логирования
*/
public String getLoggerLevel() {
return loggerLevel;
}

/**
* Установить уровень логирования.
*
* @param level - уровень логирования
*/
public void setLoggerLevel(String level) {
this.loggerLevel = level;
applyLoggerLevel();
}

/** Применить уровень логирования из конфига. */
private void applyLoggerLevel() {

ROOT_LOGGER.setLevel(Level.toLevel("info"));
ROOT_LOGGER.info("logger level switching to <{}>", loggerLevel); // $NON-NLS-1$
ROOT_LOGGER.setLevel(Level.toLevel(loggerLevel));

ROOT_LOGGER.error("test logger level = error"); // $NON-NLS-1$
ROOT_LOGGER.warn("test logger level = warn"); // $NON-NLS-1$
ROOT_LOGGER.info("test logger level = info"); // $NON-NLS-1$
ROOT_LOGGER.debug("test logger level = debug"); // $NON-NLS-1$
}

/**
* Получить частоту обновления списка.
*
* @return частота обновления списка (миллисекунд)
*/
public int getListRrefreshRate() {
return listRefreshRate;
}

/**
* Установка частоты обновления списка.
*
* @param refreshRate - частота обновления списка (миллисекунд)
*/
public void setListRrefreshRate(int refreshRate) {
this.listRefreshRate = refreshRate;
}

/**
* Получение свойства колонок списков.
*
Expand Down Expand Up @@ -1019,6 +1082,8 @@ public static Config readConfig(String configPath) {
java.util.Locale.setDefault(locale);
Messages.reloadBundle(locale); // TODO не совсем понятно как работает
}

config.applyLoggerLevel();
}
LOGGER.info("Config file read successfully"); //$NON-NLS-1$
return config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static Image getImage(String name) {
public static void showMessageBox(String message) {
MessageBox messageBox =
new MessageBox(Display.getDefault().getActiveShell()); // SWT.ICON_INFORMATION | SWT.OK
// messageBox.setText("Information");
messageBox.setText("Information");
messageBox.setMessage(message);
messageBox.open();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public class Server implements Comparable<Server> {
private boolean available;
private Process localRasProcess;
private String connectionError = ""; //$NON-NLS-1$;
boolean silentConnectionMode = false;
// private String agentVersion = ""; //$NON-NLS-1$
private String agentVersion = Messages.getString("Server.NotConnect"); //$NON-NLS-1$

Expand Down Expand Up @@ -371,6 +372,15 @@ public String getConnectionError() {
return connectionError;
}

/**
* Показывает, нужно ли выводить ошибку подключения.
*
* @return выводить ошибку подключения
*/
public boolean needShowConnectionError() {
return !connectionError.isBlank() && !silentConnectionMode;
}

/**
* Получение ключа сервера в виде "Server:1541".
*
Expand Down Expand Up @@ -777,7 +787,10 @@ private void computeServerParams(String serverAddress) {
public boolean isConnected() {
boolean isConnected = (agentConnection != null);

if (!isConnected) {
if (isConnected) {
serverState = ServerState.CONNECTED;
} else if (serverState != ServerState.CONNECTING) {
serverState = ServerState.DISCONNECT;
LOGGER.info(
"The connection a server <{}> is not established", //$NON-NLS-1$
this.getServerKey());
Expand All @@ -803,6 +816,7 @@ public boolean connectToServer(boolean disconnectAfter, boolean silentMode) {
available = false;
connectionError = "";
serverState = ServerState.CONNECTING;
silentConnectionMode = silentMode;

// все вызовы здесь проверить на Helper.showMessageBox
// этого тут быть не должно, потому что выполняется в отдельном потоке
Expand All @@ -816,8 +830,8 @@ public boolean connectToServer(boolean disconnectAfter, boolean silentMode) {

int currentRasPort = useLocalRas ? localRasPort : this.rasPort;
// try {
// Thread.sleep(30000);
// } catch (InterruptedException e) { // TODO Auto-generated catch block
// Thread.sleep(5000);
// } catch (InterruptedException e) { // TODO Искусственная задержка подключения
// e.printStackTrace();
// }
try {
Expand Down Expand Up @@ -1046,6 +1060,7 @@ public void disconnectFromAgent() {
} finally {
agentConnection = null;
agentConnector = null;
serverState = ServerState.DISCONNECT;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public class SettingsDialog extends Dialog {
private Button btnRowSortAsPrevious;
private Button btnRowSortAsc;
private Button btnRowSortDesc;
private Button btnLoggerLevelOff;
private Button btnLoggerLevelError;
private Button btnLoggerLevelWarning;
private Button btnLoggerLevelInfo;
private Button btnLoggerLevelDebug;

private static final String LOCALE_RU = "ru-RU"; //$NON-NLS-1$

Expand Down Expand Up @@ -130,7 +135,7 @@ protected Control createDialogArea(Composite parent) {

Group grpLocale = new Group(container, SWT.NONE);
grpLocale.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
grpLocale.setText(Strings.LOCALE);
grpLocale.setText(Strings.LOCALE_TITLE);
grpLocale.setLayout(new GridLayout(1, false));

btnLocaleSystem = new Button(grpLocale, SWT.RADIO);
Expand All @@ -143,7 +148,8 @@ protected Control createDialogArea(Composite parent) {
btnLocaleRussian.setText(Strings.LOCALE_RUSSIAN);

Group grpHighlight = new Group(container, SWT.NONE);
grpHighlight.setText(Strings.HIGHLIGHT);
grpHighlight.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
grpHighlight.setText(Strings.HIGHLIGHT_TITLE);
grpHighlight.setLayout(new GridLayout(2, false));

btnHighlightNewItems = new Button(grpHighlight, SWT.CHECK);
Expand Down Expand Up @@ -187,7 +193,8 @@ protected Control createDialogArea(Composite parent) {
lblWatchSessionsColor.setLayoutData(gdLblWatchSessionsColor);

Group grpRowSortDirection = new Group(container, SWT.NONE);
grpRowSortDirection.setText(Strings.ROW_SORT_DIRECTION);
grpRowSortDirection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
grpRowSortDirection.setText(Strings.ROW_SORT_DIRECTION_TITLE);
grpRowSortDirection.setLayout(new GridLayout(1, false));

btnRowSortAsPrevious = new Button(grpRowSortDirection, SWT.RADIO);
Expand All @@ -199,14 +206,35 @@ protected Control createDialogArea(Composite parent) {
btnRowSortDesc = new Button(grpRowSortDirection, SWT.RADIO);
btnRowSortDesc.setText(Strings.ROW_SORT_DIRECTION_DESCENDING);

btnReadClipboard = new Button(container, SWT.CHECK);
btnReadClipboard.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
Group grpLoggerLevel = new Group(container, SWT.NONE);
grpLoggerLevel.setLayout(new GridLayout(1, false));
grpLoggerLevel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
grpLoggerLevel.setText(Strings.LOGGER_LEVEL_TITLE);

btnLoggerLevelOff = new Button(grpLoggerLevel, SWT.RADIO);
btnLoggerLevelOff.setText("off");

btnLoggerLevelError = new Button(grpLoggerLevel, SWT.RADIO);
btnLoggerLevelError.setText("error");

btnLoggerLevelWarning = new Button(grpLoggerLevel, SWT.RADIO);
btnLoggerLevelWarning.setText("warning");

btnLoggerLevelInfo = new Button(grpLoggerLevel, SWT.RADIO);
btnLoggerLevelInfo.setText("info");

btnLoggerLevelDebug = new Button(grpLoggerLevel, SWT.RADIO);
btnLoggerLevelDebug.setText("debug");

Group grpOther = new Group(container, SWT.NONE);
grpOther.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
grpOther.setLayout(new GridLayout(1, false));

btnReadClipboard = new Button(grpOther, SWT.CHECK);
btnReadClipboard.setText(Strings.READ_CLIPBOARD);

btnCheckUpdate = new Button(container, SWT.CHECK);
btnCheckUpdate.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
btnCheckUpdate = new Button(grpOther, SWT.CHECK);
btnCheckUpdate.setText(Strings.CHECK_UPDATE);

new Label(container, SWT.NONE);

initProperties();
Expand Down Expand Up @@ -236,17 +264,25 @@ private void initProperties() {
btnReadClipboard.setSelection(config.isReadClipboard());
btnCheckUpdate.setSelection(config.checkingUpdate());

if (config.getLocale() == null) {
final String locale = config.getLocale();
if (locale == null) {
btnLocaleSystem.setSelection(true);
} else {
btnLocaleEnglish.setSelection(config.getLocale().equals(Locale.ENGLISH.toLanguageTag()));
btnLocaleRussian.setSelection(config.getLocale().equals(LOCALE_RU));
btnLocaleEnglish.setSelection(locale.equals(Locale.ENGLISH.toLanguageTag()));
btnLocaleRussian.setSelection(locale.equals(LOCALE_RU));
}

final RowSortDirection rowSortDirection = config.getRowSortDirection();
btnRowSortAsPrevious.setSelection(rowSortDirection == RowSortDirection.DISABLE);
btnRowSortAsc.setSelection(rowSortDirection == RowSortDirection.ASC);
btnRowSortDesc.setSelection(rowSortDirection == RowSortDirection.DESC);

final String loggerLevel = config.getLoggerLevel();
btnLoggerLevelOff.setSelection(loggerLevel.equals(btnLoggerLevelOff.getText()));
btnLoggerLevelError.setSelection(loggerLevel.equals(btnLoggerLevelError.getText()));
btnLoggerLevelWarning.setSelection(loggerLevel.equals(btnLoggerLevelWarning.getText()));
btnLoggerLevelInfo.setSelection(loggerLevel.equals(btnLoggerLevelInfo.getText()));
btnLoggerLevelDebug.setSelection(loggerLevel.equals(btnLoggerLevelDebug.getText()));
}

private void saveProperties() {
Expand Down Expand Up @@ -289,6 +325,21 @@ private void saveProperties() {
config.setRowSortDirection(RowSortDirection.DISABLE);
}

String loggerLevel;
if (btnLoggerLevelOff.getSelection()) {
loggerLevel = btnLoggerLevelOff.getText();
} else if (btnLoggerLevelError.getSelection()) {
loggerLevel = btnLoggerLevelError.getText();
} else if (btnLoggerLevelWarning.getSelection()) {
loggerLevel = btnLoggerLevelWarning.getText();
} else if (btnLoggerLevelInfo.getSelection()) {
loggerLevel = btnLoggerLevelInfo.getText();
} else if (btnLoggerLevelDebug.getSelection()) {
loggerLevel = btnLoggerLevelDebug.getText();
} else {
loggerLevel = btnLoggerLevelOff.getText();
}
config.setLoggerLevel(loggerLevel);
}

/**
Expand Down Expand Up @@ -333,24 +384,26 @@ private static class Strings {
static final String SHOW_INFOBASE_DESCRIPTION = getString("ShowInfobaseDescription");
static final String SHOW_LOCAL_RAS_CONNECTINFO = getString("ShowLocalRASConnectInfo");

static final String LOCALE = getString("Locale");
static final String LOCALE_TITLE = getString("LocaleTitle");
static final String LOCALE_SYSTEM = getString("LocaleSystem");
static final String LOCALE_ENGLISH = getString("LocaleEnglish");
static final String LOCALE_RUSSIAN = getString("LocaleRussian");

static final String HIGHLIGHT = getString("Highlight");
static final String HIGHLIGHT_TITLE = getString("HighlightTitle");
static final String HIGHLIGHT_NEW_ITEMS = getString("HighlightNewItems");
static final String HIGHLIGHT_DURATION = getString("HighlightDuration");
static final String SHADOW_SLEEP_SESSIONS = getString("ShadowSleepSessions");
static final String WATCH_SESSIONS = getString("WatchSessions");

static final String ROW_SORT_DIRECTION = getString("RowSortDirection");
static final String ROW_SORT_DIRECTION_TITLE = getString("RowSortDirectionTitle");
static final String ROW_SORT_DIRECTION_AS_PREVIOUS = getString("RowSortDirectionAsPrevious");
static final String ROW_SORT_DIRECTION_ASCENDING = getString("RowSortDirectionAscending");
static final String ROW_SORT_DIRECTION_DESCENDING = getString("RowSortDirectionDescending");

static final String READ_CLIPBOARD = getString("ReadClipboard");
static final String CHECK_UPDATE = getString("CheckUpdate");

static final String LOGGER_LEVEL_TITLE = getString("LoggerLevelTitle");

static String getString(String key) {
return Messages.getString("SettingsDialog." + key); //$NON-NLS-1$
Expand Down

0 comments on commit 6deadf3

Please sign in to comment.