22 * Copyright 2025 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
33 * Use is subject to license terms.
44 *******************************************************************************/
5+
56package com .espressif .idf .ui .nvs .dialog ;
67
78import java .io .File ;
1617import java .util .stream .Stream ;
1718
1819import org .eclipse .core .resources .IFile ;
19- import org .eclipse .core .resources .IProject ;
2020import org .eclipse .core .resources .IResource ;
21- import org .eclipse .core .resources .ProjectScope ;
2221import org .eclipse .core .runtime .CoreException ;
2322import org .eclipse .core .runtime .NullProgressMonitor ;
24- import org .eclipse .core .runtime .preferences .IEclipsePreferences ;
25- import org .eclipse .core .runtime .preferences .IScopeContext ;
2623import org .eclipse .jface .dialogs .IDialogConstants ;
2724import org .eclipse .jface .dialogs .IMessageProvider ;
2825import org .eclipse .jface .dialogs .MessageDialog ;
5350import org .eclipse .swt .widgets .Label ;
5451import org .eclipse .swt .widgets .Table ;
5552import org .eclipse .swt .widgets .Text ;
56- import org .osgi .service .prefs .BackingStoreException ;
5753
5854import com .espressif .idf .core .build .NvsTableBean ;
5955import com .espressif .idf .core .logging .Logger ;
6864 */
6965public 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}
0 commit comments