Skip to content

Commit 2dce865

Browse files
authored
Merge pull request #3250 from katysaintin/master
Improve layout management and fix layout issue
2 parents 99c6887 + 8057819 commit 2dce865

File tree

8 files changed

+208
-33
lines changed

8 files changed

+208
-33
lines changed

core/framework/src/main/java/org/phoebus/framework/workbench/Locations.java

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,20 @@ public static void initialize()
4040

4141
private static void initInstall() throws Exception
4242
{
43+
//First force logging level to CONFIG and memorize user level
44+
Level userLoggingLevel = logger.getLevel();
45+
logger.setLevel(Level.CONFIG);
46+
4347
// Check for location of installation,
4448
// i.e. the directory that should contain the lib/
4549
// and doc/ folders.
4650
String phoebus_install = System.getProperty(PHOEBUS_INSTALL);
51+
String foundFrom = "$("+ PHOEBUS_INSTALL +") system property";
4752
if (phoebus_install == null)
4853
{
54+
//If the environment variable is not set
55+
//Read from the workbench preferences before
56+
foundFrom = "from the framework*.jar archive";
4957
// Determine location of this class
5058
// During development in the IDE, it's /some/path/phoebus/core/framework/target/classes
5159
// In the product, it's /some/path/lib/framework*.jar
@@ -63,21 +71,58 @@ private static void initInstall() throws Exception
6371
phoebus_install = path.getAbsolutePath();
6472
System.setProperty(PHOEBUS_INSTALL, phoebus_install);
6573
}
74+
if(phoebus_install != null) {
75+
logger.log(Level.CONFIG, "phoebus_install is set to {0} found from {1}", new Object[] {phoebus_install , foundFrom});
76+
}
77+
//Put back user logging level
78+
logger.setLevel(userLoggingLevel);
6679
}
6780

6881
private static void initUser()
6982
{
70-
String phoebus_user = System.getProperty(PHOEBUS_USER);
83+
//First force logging level to CONFIG and memorize user level
84+
Level userLoggingLevel = logger.getLevel();
85+
logger.setLevel(Level.CONFIG);
86+
7187
String folder_name_preference = System.getProperty(FOLDER_NAME_PREFERENCE);
72-
if (phoebus_user == null)
88+
String foundFrom = "$("+ FOLDER_NAME_PREFERENCE +") system property";
89+
if (folder_name_preference == null)
7390
{
74-
if (folder_name_preference == null)
75-
{
76-
folder_name_preference = ".phoebus";
91+
//Test preference folder_name_preference before
92+
folder_name_preference = WorkbenchPreferences.phoebus_folder_name;
93+
foundFrom = "org.phoebus.framework.workbench/phoebus_folder_name preference in settings.ini file";
94+
if(folder_name_preference == null || folder_name_preference.contains("$(")) {//If it is still null
95+
foundFrom = " default value";
96+
folder_name_preference = ".phoebus";
7797
}
78-
phoebus_user = new File(System.getProperty("user.home"), folder_name_preference).getAbsolutePath();
79-
System.setProperty(PHOEBUS_USER, phoebus_user);
8098
}
99+
100+
logger.log(Level.CONFIG, "folder_name_preference is set to {0} found from {1}", new Object[] {folder_name_preference , foundFrom});
101+
102+
String userHome = System.getProperty(PHOEBUS_USER);
103+
foundFrom = "$("+ PHOEBUS_USER +") system property";
104+
if (userHome == null)
105+
{
106+
//Test preference phoebus_user before
107+
File userFile = WorkbenchPreferences.phoebus_user;
108+
if(userFile != null && userFile.exists()) {
109+
foundFrom = "org.phoebus.framework.workbench/phoebus_user preference in settings.ini file";
110+
userHome = userFile.getAbsolutePath();
111+
}
112+
else {
113+
foundFrom = "$(user.home) system property";
114+
userHome = System.getProperty("user.home");
115+
}
116+
}
117+
118+
logger.log(Level.CONFIG, "user home is set to {0} found from {1}", new Object[] {userHome , foundFrom});
119+
120+
String phoebus_user = new File(userHome, folder_name_preference).getAbsolutePath();
121+
logger.log(Level.CONFIG, "phoebus_user folder is set to " + phoebus_user);
122+
System.setProperty(PHOEBUS_USER, phoebus_user);
123+
124+
//Put back user logging level
125+
logger.setLevel(userLoggingLevel);
81126
}
82127

83128
/** 'Install' location contains the lib/ and doc/ directories.
@@ -89,6 +134,10 @@ private static void initUser()
89134
*/
90135
public static File install()
91136
{
137+
String install = System.getProperty(PHOEBUS_INSTALL);
138+
if(install == null) {
139+
initialize();
140+
}
92141
return new File(System.getProperty(PHOEBUS_INSTALL));
93142
}
94143

@@ -102,6 +151,10 @@ public static File install()
102151
*/
103152
public static File user()
104153
{
154+
String user = System.getProperty(PHOEBUS_USER);
155+
if(user == null) {
156+
initialize();
157+
}
105158
return new File(System.getProperty(PHOEBUS_USER));
106159
}
107160
}

core/framework/src/main/java/org/phoebus/framework/workbench/WorkbenchPreferences.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public class WorkbenchPreferences
2727
/** directory of external applications */
2828
@Preference public static File external_apps_directory;
2929

30+
/** Phoebus memento folder name default from $(phoebus.folder.name.preference) System property */
31+
@Preference public static String phoebus_folder_name;
32+
33+
/** Phoebus user home directory contents memento default from $(phoebus.user) System property */
34+
@Preference public static File phoebus_user;
35+
3036
/** external applications */
3137
public static final Collection<String> external_apps;
3238

core/framework/src/main/resources/workbench_preferences.properties

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,13 @@
3232
# external_app_alog=Alignment Log,alog,/path/to/alog_viewer
3333

3434
# Directory where external applications are started
35-
# May use system properties
36-
external_apps_directory=$(user.home)
35+
# May use system properties $(user.home)
36+
external_apps_directory=$(user.home)
37+
38+
# Phoebus folder name by default .phoebus
39+
# May use system properties $(phoebus.folder.name.preference)
40+
phoebus_folder_name=$(phoebus.folder.name.preference)
41+
42+
# Phoebus user home directory path name by default $HOME or %USERPROFILE%
43+
# May use system properties $(phoebus.folder.name.preference
44+
phoebus_user=$(phoebus.user)

core/launcher/src/main/java/org/phoebus/product/Launcher.java

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.phoebus.ui.application.PhoebusApplication;
1111

1212
import java.io.File;
13+
import java.io.InputStream;
1314
import java.io.FileInputStream;
1415
import java.io.FileOutputStream;
1516
import java.lang.reflect.Method;
@@ -26,10 +27,55 @@
2627

2728
@SuppressWarnings("nls")
2829
public class Launcher {
30+
private static final String SETTINGS_OPTION = "-settings";
31+
private static final String LOGGING_OPTION = "-logging";
32+
private static final String DEFAULT_LOGGING_FILE="/logging.properties";
33+
private static final String LOGGING_PROP = "java.util.logging.config.file";
34+
2935
public static void main(final String[] original_args) throws Exception {
30-
LogManager.getLogManager().readConfiguration(Launcher.class.getResourceAsStream("/logging.properties"));
31-
final Logger logger = Logger.getLogger(Launcher.class.getName());
32-
36+
// First Handle arguments, potentially not even starting the UI
37+
// settings and logging will define the phoebus.install value if not exist
38+
final List<String> args = new ArrayList<>(List.of(original_args));
39+
40+
//Handle logging first
41+
int indexOf = args.indexOf(LOGGING_OPTION);
42+
String loggingFilePath = null;
43+
InputStream loggingStream = null;
44+
String errorLoggingOption = null;
45+
if(indexOf > 0 && indexOf <= args.size()) {
46+
loggingFilePath = args.get(indexOf + 1);
47+
File loggingFile = new File(loggingFilePath);
48+
if(loggingFile.exists()) {
49+
try {
50+
loggingStream = new FileInputStream(loggingFile);
51+
System.setProperty(LOGGING_PROP, loggingFilePath);
52+
}
53+
catch (Exception e) {
54+
loggingStream = null;
55+
//Memorize errorMessage to log the error after logging instanciation
56+
errorLoggingOption = "Cannot read user logging file " + loggingFile + " " + e.getMessage();
57+
}
58+
}
59+
else {
60+
errorLoggingOption = "User logging file " + loggingFilePath + " not found";
61+
}
62+
}
63+
64+
//If no logging found use the default one
65+
if(loggingStream == null) {
66+
loggingFilePath = Launcher.class.getResource(DEFAULT_LOGGING_FILE).getFile();
67+
loggingStream = Launcher.class.getResourceAsStream(DEFAULT_LOGGING_FILE);
68+
}
69+
70+
//Load logging configuration
71+
LogManager.getLogManager().readConfiguration(loggingStream);
72+
final Logger logger = Logger.getLogger(Launcher.class.getPackageName());
73+
logger.log(Level.CONFIG, "Loading logging configuration from " + loggingFilePath);
74+
75+
if(errorLoggingOption != null) {
76+
logger.log(Level.WARNING, errorLoggingOption);
77+
}
78+
3379
boolean showLaunchError = false;
3480

3581
// Can't change default charset, but warn if it's not UTF-8.
@@ -46,17 +92,36 @@ public static void main(final String[] original_args) throws Exception {
4692
logger.severe("Default charset is " + cs.displayName() + " instead of UTF-8.");
4793
logger.severe("Add -D\"file.encoding=UTF-8\" to java command line or JAVA_TOOL_OPTIONS");
4894
}
49-
Locations.initialize();
50-
// Check for site-specific settings.ini bundled into distribution
51-
// before potentially adding command-line settings.
52-
final File site_settings = new File(Locations.install(), "settings.ini");
53-
if (site_settings.canRead()) {
54-
logger.log(Level.CONFIG, "Loading settings from " + site_settings);
55-
PropertyPreferenceLoader.load(new FileInputStream(site_settings));
95+
96+
//Handle user settings.ini in order to get Locations informations
97+
//as user home directory and phoebus folder name
98+
//Install path will be set on call of install()
99+
//Locations.initialize();
100+
indexOf = args.indexOf(SETTINGS_OPTION);
101+
File site_settings = null;
102+
if(indexOf > 0 && indexOf <= args.size()) {
103+
String settingsFilePath = args.get(indexOf + 1);
104+
site_settings = new File(settingsFilePath);
56105
}
57-
106+
107+
if(site_settings == null || !site_settings.exists()) {
108+
// Check for site-specific settings.ini bundled into distribution
109+
// before potentially adding command-line settings.
110+
String settingsError = site_settings != null ? site_settings.getAbsolutePath() + " not found" : "is not defined";
111+
logger.log(Level.WARNING, "Settings file " + settingsError);
112+
site_settings = new File(Locations.install(), "settings.ini");
113+
}
114+
115+
if(site_settings != null && site_settings.exists()) {
116+
logger.info("Loading settings from " + site_settings.getAbsolutePath());
117+
FileInputStream fileInputStream = new FileInputStream(site_settings);
118+
if (site_settings.getName().endsWith(".xml"))
119+
Preferences.importPreferences(fileInputStream);
120+
else
121+
PropertyPreferenceLoader.load(fileInputStream);
122+
}
123+
58124
// Handle arguments, potentially not even starting the UI
59-
final List<String> args = new ArrayList<>(List.of(original_args));
60125
final Iterator<String> iter = args.iterator();
61126
int port = -1;
62127
try {

core/ui/src/main/java/org/phoebus/ui/Preferences.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public class Preferences
4343
@Preference public static String default_save_path;
4444
/** layout_dir */
4545
@Preference public static String layout_dir;
46+
/** layout_default */
47+
@Preference public static String layout_default;
4648
/** print_landscape */
4749
@Preference public static boolean print_landscape;
4850
/** ok_severity_text_color */

core/ui/src/main/java/org/phoebus/ui/application/PhoebusApplication.java

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,7 @@ private void possiblySelectIniFile(CopyOnWriteArrayList<String> application_para
325325
String errorMessage = errorTitleAndErrorMessage.getValue();
326326

327327
logger.log(Level.SEVERE, errorMessage);
328-
329-
Dialog errorDialog = new Alert(AlertType.ERROR);
328+
Alert errorDialog = new Alert(AlertType.ERROR);
330329
errorDialog.setTitle(errorTitle);
331330
errorDialog.setHeaderText(errorTitle);
332331
errorDialog.setContentText(errorMessage + "\n\n" + Messages.PhoebusWillQuit);
@@ -348,7 +347,7 @@ private void possiblySelectIniFile(CopyOnWriteArrayList<String> application_para
348347
ObservableList<File> iniFilesInDirectory_ObservableList = FXCollections.observableArrayList(iniFilesInDirectory_List);
349348

350349
if (iniFilesInDirectory_List.size() > 0) {
351-
Dialog<File> iniFileSelectionDialog = new Dialog();
350+
Dialog<File> iniFileSelectionDialog = new Dialog<>();
352351
iniFileSelectionDialog.setTitle(Messages.SelectPhoebusConfiguration);
353352
iniFileSelectionDialog.setHeaderText(Messages.SelectPhoebusConfiguration);
354353
iniFileSelectionDialog.setGraphic(null);
@@ -357,7 +356,7 @@ private void possiblySelectIniFile(CopyOnWriteArrayList<String> application_para
357356
iniFileSelectionDialog.setHeight(400);
358357
iniFileSelectionDialog.setResizable(false);
359358

360-
ListView listView = new ListView(iniFilesInDirectory_ObservableList);
359+
ListView<File> listView = new ListView<>(iniFilesInDirectory_ObservableList);
361360
listView.getSelectionModel().select(0);
362361

363362
Runnable setReturnValueAndCloseDialog = () -> {
@@ -419,20 +418,20 @@ private void possiblySelectIniFile(CopyOnWriteArrayList<String> application_para
419418
PropertyPreferenceLoader.load(selectedFile_FileInputStream);
420419
}
421420
} catch (Exception exception) {
422-
displayErrorMessageAndQuit.accept(new Pair(Messages.ErrorLoadingPhoebusConfiguration, Messages.ErrorLoadingPhoebusConfiguration + " '" + selectedFile.getAbsolutePath() + "': " + exception.getMessage()));
421+
displayErrorMessageAndQuit.accept(new Pair<>(Messages.ErrorLoadingPhoebusConfiguration, Messages.ErrorLoadingPhoebusConfiguration + " '" + selectedFile.getAbsolutePath() + "': " + exception.getMessage()));
423422
}
424423
} catch (FileNotFoundException e) {
425-
displayErrorMessageAndQuit.accept(new Pair(Messages.ErrorLoadingPhoebusConfiguration, Messages.ErrorLoadingPhoebusConfiguration + " '" + selectedFile.getAbsolutePath() + "': " + Messages.FileDoesNotExist));
424+
displayErrorMessageAndQuit.accept(new Pair<>(Messages.ErrorLoadingPhoebusConfiguration, Messages.ErrorLoadingPhoebusConfiguration + " '" + selectedFile.getAbsolutePath() + "': " + Messages.FileDoesNotExist));
426425
}
427426
} else {
428427
// Selecting a configuration was cancelled either by pressing the "X"-button or by pressing the ESC-key.
429428
stop();
430429
}
431430
} else {
432-
displayErrorMessageAndQuit.accept(new Pair(Messages.ErrorDuringEvalutationOfTheFlagSelectSettings, Messages.ErrorDuringEvalutationOfTheFlagSelectSettings + ": " + MessageFormat.format(Messages.TheDirectoryDoesNotContainConfigurationFiles, iniFilesLocation_String)));
431+
displayErrorMessageAndQuit.accept(new Pair<>(Messages.ErrorDuringEvalutationOfTheFlagSelectSettings, Messages.ErrorDuringEvalutationOfTheFlagSelectSettings + ": " + MessageFormat.format(Messages.TheDirectoryDoesNotContainConfigurationFiles, iniFilesLocation_String)));
433432
}
434433
} else {
435-
displayErrorMessageAndQuit.accept(new Pair(Messages.ErrorDuringEvalutationOfTheFlagSelectSettings, Messages.ErrorDuringEvalutationOfTheFlagSelectSettings + ": " + MessageFormat.format(Messages.TheArgumentIsNotADirectory, iniFilesLocation_String)));
434+
displayErrorMessageAndQuit.accept(new Pair<>(Messages.ErrorDuringEvalutationOfTheFlagSelectSettings, Messages.ErrorDuringEvalutationOfTheFlagSelectSettings + ": " + MessageFormat.format(Messages.TheArgumentIsNotADirectory, iniFilesLocation_String)));
436435
}
437436
}
438437
}
@@ -561,7 +560,28 @@ private void startUI(final MementoTree memento, final JobMonitor monitor) throws
561560
Preferences.ui_monitor_period, TimeUnit.MILLISECONDS);
562561

563562
closeAllTabsMenuItem.acceleratorProperty().setValue(closeAllTabsKeyCombination);
564-
563+
564+
//Load a custom layout at start if layout_default is defined in preferences
565+
String layoutFileName = Preferences.layout_default;
566+
if(layoutFileName != null && !layoutFileName.isBlank()) {
567+
layoutFileName = !layoutFileName.endsWith(".memento")? layoutFileName + ".memento" :layoutFileName;
568+
String layout_dir = Preferences.layout_dir;
569+
File parentFolder = null;
570+
File user = Locations.user();
571+
if(layout_dir != null && !layout_dir.isBlank() && !layout_dir.contains("$(")) {
572+
parentFolder = new File(user, layout_dir);
573+
}
574+
if(parentFolder == null) {
575+
parentFolder = user;
576+
}
577+
File layoutFile = new File(parentFolder,layoutFileName );
578+
if(layoutFile.exists()) {
579+
startLayoutReplacement(layoutFile);
580+
}
581+
else {
582+
logger.log(Level.WARNING, "Layout file " + layoutFileName + " is not found");
583+
}
584+
}
565585
}
566586

567587
/**
@@ -778,8 +798,9 @@ void createLoadLayoutsMenu() {
778798
}
779799

780800
// Get every momento file from the configured layout
781-
if (Preferences.layout_dir != null && !Preferences.layout_dir.isBlank()) {
782-
final File layoutDir = new File(Preferences.layout_dir);
801+
String layout_dir = Preferences.layout_dir;
802+
if (layout_dir != null && !layout_dir.isBlank() && !layout_dir.contains("$(")) {
803+
final File layoutDir = new File(Locations.user(), layout_dir);
783804
if (layoutDir.exists()) {
784805
final File[] systemLayoutFiles = layoutDir.listFiles();
785806
if (systemLayoutFiles != null) {

core/ui/src/main/java/org/phoebus/ui/application/SaveLayoutHelper.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.phoebus.framework.autocomplete.ProposalService;
2323
import org.phoebus.framework.jobs.JobManager;
2424
import org.phoebus.framework.workbench.Locations;
25+
import org.phoebus.ui.Preferences;
2526
import org.phoebus.ui.autocomplete.AutocompleteMenu;
2627
import org.phoebus.ui.dialog.DialogHelper;
2728
import org.phoebus.ui.docking.DockItem;
@@ -156,7 +157,23 @@ private static void positionDialog(final Dialog<?> dialog, Stage stage)
156157
private static boolean saveState(List<Stage> stagesToSave, final String layout)
157158
{
158159
final String memento_filename = layout + ".memento";
159-
final File memento_file = new File(Locations.user(), memento_filename);
160+
//Take in account layout dir and add Layout folder in the path
161+
String layout_dir = Preferences.layout_dir;
162+
File user = Locations.user();
163+
File parentFolder = null;
164+
if(layout_dir != null && !layout_dir.isEmpty() && !layout_dir.contains("$(")) {
165+
parentFolder = new File(user , layout_dir);
166+
if(!parentFolder.exists()) {
167+
parentFolder.mkdir();
168+
}
169+
}
170+
171+
if(parentFolder == null) {
172+
parentFolder = user;
173+
}
174+
175+
final File memento_file = new File(parentFolder, memento_filename);
176+
160177
// File.exists() is blocking in nature.
161178
// To combat this the phoebus application maintains a list of *.memento files that are in the default directory.
162179
// Check if the file name is in the list, and confirm a file overwrite with the user.

0 commit comments

Comments
 (0)