Skip to content

Commit 2317bb2

Browse files
committed
feat: refactoring - moved storing preference logic to the separate class
1 parent 7592697 commit 2317bb2

File tree

4 files changed

+136
-60
lines changed

4 files changed

+136
-60
lines changed

bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/nvs/dialog/NvsCsvEditorPage.java

Lines changed: 38 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright 2025 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
33
* Use is subject to license terms.
44
*******************************************************************************/
5+
56
package com.espressif.idf.ui.nvs.dialog;
67

78
import java.io.File;
@@ -16,13 +17,9 @@
1617
import java.util.stream.Stream;
1718

1819
import org.eclipse.core.resources.IFile;
19-
import org.eclipse.core.resources.IProject;
2020
import org.eclipse.core.resources.IResource;
21-
import org.eclipse.core.resources.ProjectScope;
2221
import org.eclipse.core.runtime.CoreException;
2322
import org.eclipse.core.runtime.NullProgressMonitor;
24-
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
25-
import org.eclipse.core.runtime.preferences.IScopeContext;
2623
import org.eclipse.jface.dialogs.IDialogConstants;
2724
import org.eclipse.jface.dialogs.IMessageProvider;
2825
import org.eclipse.jface.dialogs.MessageDialog;
@@ -53,7 +50,6 @@
5350
import org.eclipse.swt.widgets.Label;
5451
import org.eclipse.swt.widgets.Table;
5552
import org.eclipse.swt.widgets.Text;
56-
import org.osgi.service.prefs.BackingStoreException;
5753

5854
import com.espressif.idf.core.build.NvsTableBean;
5955
import com.espressif.idf.core.logging.Logger;
@@ -68,25 +64,15 @@
6864
*/
6965
public class NvsCsvEditorPage
7066
{
71-
// --- Preference Constants ---
72-
private static final String PLUGIN_ID = "com.espressif.idf.core"; //$NON-NLS-1$
73-
74-
private static final String PREF_PARTITION_SIZE = "nvsPartitionSize"; //$NON-NLS-1$
75-
private static final String PREF_ENCRYPT_ENABLED = "nvsEncryptEnabled"; //$NON-NLS-1$
76-
private static final String PREF_GENERATE_KEY_ENABLED = "nvsGenerateKeyEnabled"; //$NON-NLS-1$
77-
private static final String PREF_ENCRYPTION_KEY_PATH = "nvsEncryptionKeyPath"; //$NON-NLS-1$
78-
// --- End of Preference Constants ---
79-
80-
private static final String DEFAULT_PARTITION_SIZE = "0x3000"; //$NON-NLS-1$
8167
private Composite mainControl;
8268
private IFile csvFile;
8369
private Consumer<Boolean> dirtyStateListener;
8470

8571
private Text statusText;
8672
private Table csvTable;
8773
private TableViewer tableViewer;
88-
private Text sizeText;
8974

75+
private Text sizeText;
9076
private Composite encryptionComposite;
9177
private Text encryptionKeyText;
9278
private Button generateEncryptionKeyCheckBox;
@@ -101,15 +87,21 @@ enum GeneratePartitionValidationError
10187

10288
private final EnumMap<GeneratePartitionValidationError, String> validationErrors = new EnumMap<>(
10389
GeneratePartitionValidationError.class);
90+
10491
private final EnumMap<NvsColumn, CellEditor> cellEditors = new EnumMap<>(NvsColumn.class);
10592

93+
private NvsEditorPreferenceService preferenceService;
94+
10695
public NvsCsvEditorPage(Composite parent, IFile csvFile, Consumer<Boolean> dirtyStateListener)
10796
{
10897
this.csvFile = csvFile;
10998
this.dirtyStateListener = dirtyStateListener;
11099

111100
mainControl = new Composite(parent, SWT.NONE);
112101
mainControl.setLayout(new GridLayout(1, false));
102+
103+
// Initialize the preference service
104+
this.preferenceService = new NvsEditorPreferenceService(csvFile.getProject());
113105
}
114106

115107
/**
@@ -136,8 +128,8 @@ public void createControl()
136128
createToolButtonBar(group);
137129
createTableViewer();
138130

139-
// Load saved preferences
140-
loadPreferences();
131+
// Load saved preferences using the new service
132+
loadSettingsToUi();
141133

142134
// Initial setup
143135
setMessage(Messages.NvsEditorDialog_DefaultMessage, IMessageProvider.INFORMATION);
@@ -264,7 +256,9 @@ public boolean getSaveAction()
264256
Logger.log(e);
265257
}
266258
dirtyStateListener.accept(false);
267-
savePreferences();
259+
260+
// Save preferences using the new service
261+
saveSettingsFromUi();
268262
return true;
269263
}
270264

@@ -507,27 +501,33 @@ private void createSizeOfPartitionLable(Composite parent)
507501

508502
private void createTableViewer()
509503
{
504+
NvsEditorSupportFactory supportFactory;
510505
tableViewer = new TableViewer(csvTable);
511506
tableViewer.setContentProvider(ArrayContentProvider.getInstance());
512507
ColumnViewerToolTipSupport.enableFor(tableViewer);
513508

509+
// Use the NvsColumn enum to get column properties
514510
tableViewer.setColumnProperties(NvsColumn.getColumnProperties());
515511

512+
// Populate the EnumMap with cell editors
516513
cellEditors.put(NvsColumn.KEY, new TextCellEditor(csvTable));
517514
cellEditors.put(NvsColumn.TYPE,
518515
new ComboBoxCellEditor(csvTable, NvsTableDataService.getTypes(), SWT.READ_ONLY));
519516
cellEditors.put(NvsColumn.ENCODING,
520517
new ComboBoxCellEditor(csvTable, NvsTableDataService.getEncodings(StringUtil.EMPTY), SWT.READ_ONLY));
521518
cellEditors.put(NvsColumn.VALUE, new TextCellEditor(csvTable));
522519

523-
NvsEditorSupportFactory supportFactory = new NvsEditorSupportFactory(tableViewer, cellEditors, this::markDirty);
520+
// Initialize the factory, passing it the state it needs
521+
supportFactory = new NvsEditorSupportFactory(tableViewer, cellEditors, this::markDirty);
524522

523+
// Loop through the enum values to create columns
525524
for (NvsColumn column : NvsColumn.values())
526525
{
527526
TableViewerColumn tvColumn = new TableViewerColumn(tableViewer, SWT.NONE);
528527
tvColumn.getColumn().setText(column.getDisplayName());
529528
tvColumn.getColumn().setWidth(column.getDefaultWidth());
530529

530+
// Use the factory to create the providers
531531
tvColumn.setLabelProvider(supportFactory.createLabelProvider(column));
532532
tvColumn.setEditingSupport(supportFactory.createEditingSupport(column));
533533
}
@@ -647,6 +647,7 @@ private boolean validateBeansBeforeSaving(List<NvsTableBean> beansToSave)
647647

648648
for (NvsTableBean bean : beansToSave)
649649
{
650+
// Loop through the enum, passing the type-safe index to the validator.
650651
for (NvsColumn column : NvsColumn.values())
651652
{
652653
errorMsg = new NvsBeanValidator().validateBean(bean, column.getIndex());
@@ -711,55 +712,33 @@ private void getAddRowAction()
711712
markDirty();
712713
}
713714

714-
// ========================================================================
715-
// Preference Handling Methods
716-
// ========================================================================
717-
718715
/**
719-
* Returns the preference node for the current project.
716+
* Loads settings from the service and applies them to the UI widgets.
720717
*/
721-
private IEclipsePreferences getProjectPreferences()
718+
private void loadSettingsToUi()
722719
{
723-
IProject project = csvFile.getProject();
724-
IScopeContext projectScope = new ProjectScope(project);
725-
return projectScope.getNode(PLUGIN_ID);
726-
}
727-
728-
/**
729-
* Loads settings from the project's preferences and applies them to the UI.
730-
*/
731-
private void loadPreferences()
732-
{
733-
IEclipsePreferences prefs = getProjectPreferences();
720+
NvsEditorSettings settings = preferenceService.loadSettings();
734721

735-
// Load and set values, using your class defaults as fallbacks
736-
sizeText.setText(prefs.get(PREF_PARTITION_SIZE, DEFAULT_PARTITION_SIZE));
737-
encryptAction.setSelection(prefs.getBoolean(PREF_ENCRYPT_ENABLED, false));
738-
generateEncryptionKeyCheckBox.setSelection(prefs.getBoolean(PREF_GENERATE_KEY_ENABLED, true));
739-
encryptionKeyText.setText(prefs.get(PREF_ENCRYPTION_KEY_PATH, StringUtil.EMPTY));
722+
sizeText.setText(settings.partitionSize());
723+
encryptAction.setSelection(settings.encryptEnabled());
724+
generateEncryptionKeyCheckBox.setSelection(settings.generateKeyEnabled());
725+
encryptionKeyText.setText(settings.encryptionKeyPath());
740726
}
741727

742728
/**
743-
* Saves the current UI settings to the project's preferences.
729+
* Reads the current state of the UI widgets and saves it using the service.
744730
*/
745-
private void savePreferences()
731+
private void saveSettingsFromUi()
746732
{
747-
IEclipsePreferences prefs = getProjectPreferences();
733+
// Read all values from the UI first
734+
String partitionSize = sizeText.getText();
735+
boolean encryptEnabled = encryptAction.getSelection();
736+
boolean generateKeyEnabled = generateEncryptionKeyCheckBox.getSelection();
737+
String encryptionKeyPath = encryptionKeyText.getText();
748738

749-
// Store the current values
750-
prefs.put(PREF_PARTITION_SIZE, sizeText.getText());
751-
prefs.putBoolean(PREF_ENCRYPT_ENABLED, encryptAction.getSelection());
752-
prefs.putBoolean(PREF_GENERATE_KEY_ENABLED, generateEncryptionKeyCheckBox.getSelection());
753-
prefs.put(PREF_ENCRYPTION_KEY_PATH, encryptionKeyText.getText());
739+
NvsEditorSettings settings = new NvsEditorSettings(partitionSize, encryptEnabled, generateKeyEnabled,
740+
encryptionKeyPath);
754741

755-
try
756-
{
757-
// Flush the changes to disk
758-
prefs.flush();
759-
}
760-
catch (BackingStoreException e)
761-
{
762-
Logger.log(e);
763-
}
742+
preferenceService.saveSettings(settings);
764743
}
765744
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*******************************************************************************
2+
* Copyright 2025 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
3+
* Use is subject to license terms.
4+
*******************************************************************************/
5+
6+
package com.espressif.idf.ui.nvs.dialog;
7+
8+
import org.eclipse.core.resources.IProject;
9+
import org.eclipse.core.resources.ProjectScope;
10+
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
11+
import org.eclipse.core.runtime.preferences.IScopeContext;
12+
import org.osgi.service.prefs.BackingStoreException;
13+
14+
import com.espressif.idf.core.logging.Logger;
15+
import com.espressif.idf.core.util.StringUtil;
16+
17+
/**
18+
* Manages loading and saving NvsEditorSettings to and from the Eclipse preference store for a specific project.
19+
*/
20+
public class NvsEditorPreferenceService
21+
{
22+
private static final String PLUGIN_ID = "com.espressif.idf.core"; //$NON-NLS-1$
23+
private static final String PREF_PARTITION_SIZE = "nvsPartitionSize"; //$NON-NLS-1$
24+
private static final String PREF_ENCRYPT_ENABLED = "nvsEncryptEnabled"; //$NON-NLS-1$
25+
private static final String PREF_GENERATE_KEY_ENABLED = "nvsGenerateKeyEnabled"; //$NON-NLS-1$
26+
private static final String PREF_ENCRYPTION_KEY_PATH = "nvsEncryptionKeyPath"; //$NON-NLS-1$
27+
28+
private IEclipsePreferences preferences;
29+
30+
public NvsEditorPreferenceService(IProject project)
31+
{
32+
IScopeContext projectScope = new ProjectScope(project);
33+
this.preferences = projectScope.getNode(PLUGIN_ID);
34+
}
35+
36+
/**
37+
* Loads settings from the service and applies them to the UI.
38+
*/
39+
public NvsEditorSettings loadSettings()
40+
{
41+
// Read all values from the preference store first
42+
String partitionSize = preferences.get(PREF_PARTITION_SIZE, NvsEditorSettings.DEFAULT_PARTITION_SIZE);
43+
boolean encryptEnabled = preferences.getBoolean(PREF_ENCRYPT_ENABLED, false);
44+
boolean generateKeyEnabled = preferences.getBoolean(PREF_GENERATE_KEY_ENABLED, true);
45+
String encryptionKeyPath = preferences.get(PREF_ENCRYPTION_KEY_PATH, StringUtil.EMPTY);
46+
47+
// Create the immutable record in one call
48+
return new NvsEditorSettings(partitionSize, encryptEnabled, generateKeyEnabled, encryptionKeyPath);
49+
}
50+
51+
/**
52+
* Saves the given settings to the project's preferences.
53+
*/
54+
public void saveSettings(NvsEditorSettings settings)
55+
{
56+
preferences.put(PREF_PARTITION_SIZE, settings.partitionSize());
57+
preferences.putBoolean(PREF_ENCRYPT_ENABLED, settings.encryptEnabled());
58+
preferences.putBoolean(PREF_GENERATE_KEY_ENABLED, settings.generateKeyEnabled());
59+
preferences.put(PREF_ENCRYPTION_KEY_PATH, settings.encryptionKeyPath());
60+
61+
try
62+
{
63+
preferences.flush();
64+
}
65+
catch (BackingStoreException e)
66+
{
67+
Logger.log(e);
68+
}
69+
}
70+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*******************************************************************************
2+
* Copyright 2025 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
3+
* Use is subject to license terms.
4+
*******************************************************************************/
5+
6+
package com.espressif.idf.ui.nvs.dialog;
7+
8+
import com.espressif.idf.core.util.StringUtil;
9+
10+
/**
11+
* A Data Transfer Object (DTO) that holds the settings for the NVS Editor page, implemented as an immutable Java
12+
* Record.
13+
*/
14+
public record NvsEditorSettings(String partitionSize, boolean encryptEnabled, boolean generateKeyEnabled,
15+
String encryptionKeyPath)
16+
{
17+
18+
public static final String DEFAULT_PARTITION_SIZE = "0x3000"; //$NON-NLS-1$
19+
20+
/**
21+
* Creates a new instance with default values.
22+
*/
23+
public static NvsEditorSettings createDefault()
24+
{
25+
return new NvsEditorSettings(DEFAULT_PARTITION_SIZE, false, true, StringUtil.EMPTY);
26+
}
27+
}

bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/nvs/dialog/NvsEditorSupportFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,4 @@ protected void setValue(Object element, Object value)
266266
markDirtyRunnable.run();
267267
}
268268
}
269-
}
269+
}

0 commit comments

Comments
 (0)