@@ -29,33 +29,51 @@ public class EditUserDefinedCombis {
29
29
30
30
JTable usercombiTable ;
31
31
MyTableModel model ;
32
+ TableModelListener modelListener ;
32
33
List <String > tablerowdata = new ArrayList <String >();
34
+ List <String > defaultValues = new ArrayList <String >();
33
35
String strcustomconfigfile = "" ;
34
36
35
37
/*
36
- / This option makes only the 3rd column (0, 1, 2) editable
37
- / column 1 (no. 0) & 2 (no. 1) are ready-only
38
+ / This option makes only the 3rd (tag value) and 4th (Save checkbox) columns (2, 3) editable
39
+ / column 1 (no. 0) & 2 (no. 1) are ready-only.
40
+ / Also override getColumnClass to allow for the Save checkbox in 4th column.
38
41
*/
39
42
public class MyTableModel extends DefaultTableModel {
40
43
public boolean isCellEditable (int row , int column ){
41
- return column == 2 ;
44
+ return column == 2 || column == 3 ;
42
45
}
43
46
47
+ public Class getColumnClass (int column ) {
48
+ if (column == 3 )
49
+ return Boolean .class ;
50
+ else
51
+ return String .class ;
52
+ }
44
53
}
54
+
45
55
/*
46
56
/ This method updates the table in case we have selected another combi from the JCombobox
47
57
*/
48
58
public void UpdateTable (JPanel rootpanel , JComboBox combicombobox , JScrollPane userCombiPane ) {
59
+ if (model != null && modelListener != null )
60
+ model .removeTableModelListener (modelListener );
49
61
50
- List <String > tablerowdata = new ArrayList <String >();
51
- usercombiTable = new JTable (new MyTableModel ());
62
+ tablerowdata .clear ();
63
+ defaultValues .clear ();
64
+
65
+ if (model == null ) {
66
+ model = new MyTableModel ();
67
+ model .setColumnIdentifiers (new String []{ResourceBundle .getBundle ("translations/program_strings" ).getString ("mduc.columnlabel" ),
68
+ ResourceBundle .getBundle ("translations/program_strings" ).getString ("mduc.columntag" ),
69
+ ResourceBundle .getBundle ("translations/program_strings" ).getString ("mduc.columndefault" ),
70
+ ResourceBundle .getBundle ("translations/program_strings" ).getString ("label.save" )});
71
+ }
72
+ else
73
+ model .setRowCount (0 );
52
74
53
- model = ((MyTableModel ) (usercombiTable .getModel ()));
54
- model .setColumnIdentifiers (new String []{ResourceBundle .getBundle ("translations/program_strings" ).getString ("mduc.columnlabel" ),
55
- ResourceBundle .getBundle ("translations/program_strings" ).getString ("mduc.columntag" ),
56
- ResourceBundle .getBundle ("translations/program_strings" ).getString ("mduc.columndefault" )});
57
- model .setRowCount (0 );
58
- Object [] row = new Object [1 ];
75
+ if (usercombiTable == null )
76
+ usercombiTable = new JTable (model );
59
77
60
78
String setName = combicombobox .getSelectedItem ().toString ();
61
79
String sql = "select screen_label, tag, default_value from custommetadatasetLines where customset_name='" + setName .trim () + "' order by rowcount" ;
@@ -65,19 +83,40 @@ public void UpdateTable(JPanel rootpanel, JComboBox combicombobox, JScrollPane u
65
83
66
84
for (String line : lines ) {
67
85
String [] cells = line .split ("\\ t" , 4 );
68
- model .addRow (new Object []{cells [0 ], cells [1 ], cells [2 ]});
86
+ String defVal = cells [2 ].trim ();
87
+ model .addRow (new Object []{cells [0 ], cells [1 ], defVal , defVal .length () == 0 ? Boolean .FALSE : Boolean .TRUE });
69
88
tablerowdata .add ("" );
89
+ defaultValues .add (defVal ); // We'll use the default values again during a copy operation
70
90
}
71
91
MyVariables .setuserCombiTableValues (tablerowdata );
72
92
}
73
93
userCombiPane .setViewportView (usercombiTable );
94
+ // Make the Save column narrower.
95
+ usercombiTable .getColumnModel ().getColumn (3 ).setMinWidth (50 );
96
+ usercombiTable .getColumnModel ().getColumn (3 ).setMaxWidth (110 );
97
+ usercombiTable .getColumnModel ().getColumn (3 ).setPreferredWidth (70 );
74
98
75
- usercombiTable .getModel ().addTableModelListener (new TableModelListener () {
76
- public void tableChanged (TableModelEvent e ) {
77
- logger .debug ("source {}; firstRow {}; lastRow {}, column{}" , e .getSource (), e .getFirstRow (), e .getLastRow (), e .getColumn ());
78
- logger .debug ("tag {}; original value {}; modified tablecell value {}" , model .getValueAt (e .getFirstRow (),1 ), tablerowdata .get (e .getFirstRow ()), model .getValueAt (e .getFirstRow (),e .getColumn ()));
79
- }
80
- });
99
+ if (modelListener == null ) {
100
+ modelListener = new TableModelListener () {
101
+ // React to edits of tag values. If value is different to original value then tick the Save checkbox
102
+ public void tableChanged (TableModelEvent e ) {
103
+ int col = e .getColumn ();
104
+ if (col == 3 ) // No need to react to checkboxes
105
+ return ;
106
+ int row = e .getFirstRow ();
107
+ Object originalValue = tablerowdata .get (row );
108
+ Object modifiedValue = model .getValueAt (row , col );
109
+ logger .debug ("source {}; firstRow {}; lastRow {}, column{}" , e .getSource (), row , e .getLastRow (), col );
110
+ logger .debug ("tag {}; original value {}; modified tablecell value {}" , model .getValueAt (row , 1 ), originalValue , modifiedValue );
111
+ if (originalValue .equals (modifiedValue ))
112
+ model .setValueAt (Boolean .FALSE , row , 3 );
113
+ else
114
+ model .setValueAt (Boolean .TRUE , row , 3 );
115
+ }
116
+ };
117
+ }
118
+
119
+ model .addTableModelListener (modelListener );
81
120
}
82
121
83
122
public void UpdateCustomConfigLabel (JComboBox combicombobox , JLabel customconfiglabel ) {
@@ -100,8 +139,7 @@ public void ResetFields(JScrollPane userCombiPane) {
100
139
}
101
140
102
141
public void SaveTableValues (JCheckBox udcOverwriteOriginalscheckBox , JProgressBar progressBar ) {
103
- // if changed => save
104
- // else if !empty => save
142
+ // if Save checkedbox ticked => save
105
143
File [] files = MyVariables .getLoadedFiles ();
106
144
int [] selectedIndices = MyVariables .getSelectedFilenamesIndices ();
107
145
tablerowdata = MyVariables .getuserCombiTableValues ();
@@ -125,21 +163,25 @@ public void SaveTableValues(JCheckBox udcOverwriteOriginalscheckBox, JProgressBa
125
163
cmdparams .add ("-overwrite_original" );
126
164
}
127
165
cmdparams .addAll (Utils .AlwaysAdd ());
128
- cmdparams .add ("-n" ); // Need is this for some tags, eg Orientation
166
+ cmdparams .add ("-n" ); // Need this for some tags, eg Orientation
129
167
boolean hasChanged = false ;
168
+ model .removeTableModelListener (modelListener ); // Temporarily remove listener to avoid triggering unnecessary events
130
169
for (String value : tablerowdata ) {
131
- if ( ! model .getValueAt (rowcounter , 2 ). equals ( value ) ) {
170
+ if (( Boolean ) model .getValueAt (rowcounter , 3 ) ) {
132
171
hasChanged = true ;
133
- logger .info ("tag {}; original value {}; modified tablecell value {}" , model .getValueAt (rowcounter ,1 ), tablerowdata .get (rowcounter ), model .getValueAt (rowcounter ,2 ));
172
+ String cellValue = model .getValueAt (rowcounter ,2 ).toString ().trim ();
173
+ logger .info ("tag {}; original value {}; modified tablecell value {}" , model .getValueAt (rowcounter ,1 ), tablerowdata .get (rowcounter ), cellValue );
134
174
if (model .getValueAt (rowcounter ,1 ).toString ().startsWith ("-" )) {
135
- cmdparams .add (model .getValueAt (rowcounter ,1 ).toString () + "=" + model . getValueAt ( rowcounter , 2 ). toString (). trim () );
175
+ cmdparams .add (model .getValueAt (rowcounter ,1 ).toString () + "=" + cellValue );
136
176
} else { //tag without - (minus sign/hyphen) as prefix
137
- cmdparams .add ("-" + model .getValueAt (rowcounter ,1 ).toString () + "=" + model . getValueAt ( rowcounter , 2 ). toString (). trim () );
177
+ cmdparams .add ("-" + model .getValueAt (rowcounter ,1 ).toString () + "=" + cellValue );
138
178
}
139
- tablerowdata .set (rowcounter , model .getValueAt (rowcounter , 2 ).toString ().trim ());
179
+ tablerowdata .set (rowcounter , cellValue );
180
+ model .setValueAt (cellValue , rowcounter , 2 );
140
181
}
141
182
rowcounter ++;
142
183
}
184
+ model .addTableModelListener (modelListener ); // Add back listener
143
185
144
186
for (int index : selectedIndices ) {
145
187
//logger.info("index: {} image path: {}", index, files[index].getPath());
@@ -200,7 +242,9 @@ public void CopyFromSelectedImage() {
200
242
}
201
243
if (res .length () > 0 ) {
202
244
String [] strTagnames = tagnames .stream ().toArray (String []::new );
245
+ model .removeTableModelListener (modelListener ); // Temporarily remove listener to avoid triggering unnecessary events
203
246
displayCopiedInfo ( res , strTagnames );
247
+ model .addTableModelListener (modelListener ); // Add back listener
204
248
}
205
249
206
250
}
@@ -210,8 +254,14 @@ private void displayCopiedInfo(String exiftoolInfo, String[] tagNames) {
210
254
String [] lines = exiftoolInfo .split (SystemPropertyFacade .getPropertyByKey (LINE_SEPARATOR ));
211
255
tablerowdata = MyVariables .getuserCombiTableValues ();
212
256
257
+ // Clear previous values and set defaults
258
+ for (int row = 0 ; row < model .getRowCount (); row ++) {
259
+ tablerowdata .set (row , "" );
260
+ model .setValueAt (defaultValues .get (row ), row , 2 );
261
+ }
262
+
213
263
for (String line : lines ) {
214
- String [] returnedValuesRow = line .split ("::" , 2 ); // Only split on first : as some tags also contain (multiple) :
264
+ String [] returnedValuesRow = line .split ("::" , 2 ); // Only split on first :: as some tags also contain (multiple) : :
215
265
if (returnedValuesRow .length < 2 ) // line does not contain "TAGNAME::VALUE" so skip it, eg warning messages
216
266
continue ;
217
267
String SpaceStrippedTag = returnedValuesRow [0 ];
@@ -221,12 +271,19 @@ private void displayCopiedInfo(String exiftoolInfo, String[] tagNames) {
221
271
for (String tagname : tablerowdata ) {
222
272
if (model .getValueAt (rowcounter ,1 ).toString ().equals (SpaceStrippedTag )) {
223
273
// The model data and tablerowdata values are the same when first retrieved.
224
- // Late, during save, each row is checked if it was changed.
225
- model .setValueAt (returnedValuesRow [1 ].trim (),rowcounter ,2 );
274
+ // Later, during save, each row is checked if it was changed.
226
275
tablerowdata .set (rowcounter , returnedValuesRow [1 ].trim ());
276
+ model .setValueAt (returnedValuesRow [1 ].trim (),rowcounter ,2 );
227
277
}
228
278
rowcounter ++;
229
279
}
230
280
}
281
+ // Tick the Save checkbox for any tags which have defaults still remaining
282
+ for (int row = 0 ; row < model .getRowCount (); row ++) {
283
+ if (model .getValueAt (row , 2 ).equals (tablerowdata .get (row )))
284
+ model .setValueAt (Boolean .FALSE , row , 3 );
285
+ else
286
+ model .setValueAt (Boolean .TRUE , row , 3 );
287
+ }
231
288
}
232
289
}
0 commit comments