6060import java .awt .GridBagLayout ;
6161import java .awt .Insets ;
6262import java .awt .event .ActionEvent ;
63- import java .awt .event .ActionListener ;
6463import java .awt .event .MouseAdapter ;
6564import java .awt .event .MouseEvent ;
6665import java .io .BufferedReader ;
7170import java .io .InputStreamReader ;
7271import java .nio .file .Path ;
7372import java .sql .Types ;
73+ import java .util .Objects ;
7474
7575/**
7676 * @author gwg
@@ -90,6 +90,11 @@ public class PopupEditableIOPanel extends JPanel
9090 public static final String ACTION_APPLY = "apply" ;
9191 public static final String ACTION_IMPORT = "import" ;
9292
93+ public static final String RADIX_HEX = "Hex" ;
94+ public static final String RADIX_DECIMAL = "Decimal" ;
95+ public static final String RADIX_OCTAL = "Octal" ;
96+ public static final String RADIX_BINARY = "Binary" ;
97+
9398 // The text area displaying the object contents
9499 private final JTextArea _textArea ;
95100
@@ -129,34 +134,7 @@ public class PopupEditableIOPanel extends JPanel
129134 private IOUtilities _iou = new IOUtilitiesImpl ();
130135
131136 private final ReformatHandler _reformatHandler ;
132-
133- class BinaryOptionActionListener implements ActionListener {
134- public void actionPerformed (ActionEvent e ) {
135-
136- // user asked to see binary data in a different format
137- int base = 16 ; // default to hex
138- if (previousRadixListItem .equals ("Decimal" )) base = 10 ;
139- else if (previousRadixListItem .equals ("Octal" )) base = 8 ;
140- else if (previousRadixListItem .equals ("Binary" )) base = 2 ;
141-
142- Byte [] bytes = BinaryDisplayConverter .convertToBytes (_textArea .getText (),
143- base , previousShowAscii );
144-
145- // return the expected format for this data
146- base = 16 ; // default to hex
147- if (radixList .getSelectedItem ().equals ("Decimal" )) base = 10 ;
148- else if (radixList .getSelectedItem ().equals ("Octal" )) base = 8 ;
149- else if (radixList .getSelectedItem ().equals ("Binary" )) base = 2 ;
150-
151- ((RestorableJTextArea ) _textArea ).updateText (
152- BinaryDisplayConverter .convertToString (bytes ,
153- base , showAscii .isSelected ()));
154-
155- previousRadixListItem = (String )radixList .getSelectedItem ();
156- previousShowAscii = showAscii .isSelected ();
157- }
158- }
159- private BinaryOptionActionListener optionActionListener = new BinaryOptionActionListener ();
137+ private boolean _dontReactToBinaryDisplayChanges ;
160138
161139 // text put in file name field to indicate that we should
162140 // create a temp file for export
@@ -180,7 +158,7 @@ public PopupEditableIOPanel(ColumnDisplayDefinition colDef, Object value, boolea
180158 _colDef = colDef ;
181159 _textArea = CellComponentFactory .getJTextArea (colDef , value );
182160
183- _reformatHandler = new ReformatHandler (_textArea );
161+ _reformatHandler = new ReformatHandler (_textArea , CellComponentFactory . useBinaryEditingPanel ( _colDef ), () -> onOriginalTextWasRestored () );
184162
185163 if (isEditable )
186164 {
@@ -222,14 +200,14 @@ public PopupEditableIOPanel(ColumnDisplayDefinition colDef, Object value, boolea
222200 {
223201 // this is a binary field, so allow for multiple viewing options
224202
225- String [] radixListData = { "Hex" , "Decimal" , "Octal" , "Binary" };
203+ String [] radixListData = {RADIX_HEX , RADIX_DECIMAL , RADIX_OCTAL , RADIX_BINARY };
226204 radixList = new JComboBox (radixListData );
227- radixList .addActionListener (optionActionListener );
228- previousRadixListItem = "Hex" ;
205+ radixList .addActionListener (e -> updateBinaryDisplay () );
206+ previousRadixListItem = RADIX_HEX ;
229207
230208 showAscii = new JCheckBox (s_stringMgr .getString ("popupeditableIoPanel.showAscii" ));
231209 previousShowAscii = false ;
232- showAscii .addActionListener (optionActionListener );
210+ showAscii .addActionListener (e -> updateBinaryDisplay () );
233211
234212 JPanel displayControlsPanel = new JPanel ();
235213 // use default sequential layout
@@ -266,6 +244,62 @@ public PopupEditableIOPanel(ColumnDisplayDefinition colDef, Object value, boolea
266244 new PasteFromHistoryAttach (_textArea );
267245 }
268246
247+ private void updateBinaryDisplay ()
248+ {
249+ if (_dontReactToBinaryDisplayChanges )
250+ {
251+ return ;
252+ }
253+
254+ if ( _reformatHandler .isReformatted () )
255+ {
256+ Main .getApplication ().getMessageHandler ().showErrorMessage (s_stringMgr .getString ("popupEditableIoPanel.cannot.update.binary.display.while.reformatted" ));
257+ return ;
258+ }
259+
260+ // user asked to see binary data in a different format
261+ int base = 16 ; // default to hex
262+ if (previousRadixListItem .equals (RADIX_DECIMAL )) base = 10 ;
263+ else if (previousRadixListItem .equals (RADIX_OCTAL )) base = 8 ;
264+ else if (previousRadixListItem .equals (RADIX_BINARY )) base = 2 ;
265+
266+ Byte [] bytes = BinaryDisplayConverter .convertToBytes (_textArea .getText (),
267+ base , previousShowAscii );
268+
269+ // return the expected format for this data
270+ base = 16 ; // default to hex
271+ if (Objects .equals (radixList .getSelectedItem (), RADIX_DECIMAL )) base = 10 ;
272+ else if (Objects .equals (radixList .getSelectedItem (), RADIX_OCTAL )) base = 8 ;
273+ else if (Objects .equals (radixList .getSelectedItem (), RADIX_BINARY )) base = 2 ;
274+
275+ ((RestorableJTextArea ) _textArea ).updateText (
276+ BinaryDisplayConverter .convertToString (bytes , base , showAscii .isSelected ()));
277+
278+ previousRadixListItem = (String )radixList .getSelectedItem ();
279+ previousShowAscii = showAscii .isSelected ();
280+ }
281+
282+ private void onOriginalTextWasRestored ()
283+ {
284+ if (null == radixList || null == showAscii )
285+ {
286+ return ;
287+ }
288+
289+ try
290+ {
291+ _dontReactToBinaryDisplayChanges = true ;
292+ previousRadixListItem = RADIX_HEX ;
293+ radixList .setSelectedItem (RADIX_HEX );
294+ showAscii .setSelected (false );
295+ }
296+ finally
297+ {
298+ _dontReactToBinaryDisplayChanges = false ;
299+ }
300+ }
301+
302+
269303 /**
270304 * build the user interface for export/import operations
271305 */
@@ -995,13 +1029,13 @@ private void importData(File file) {
9951029 // If the user has selected a non-cannonical Binary format, we need
9961030 // to convert the text appropriately
9971031 if (radixList != null &&
998- ! (radixList .getSelectedItem ().equals ("Hex" ) &&
1032+ ! (radixList .getSelectedItem ().equals (RADIX_HEX ) &&
9991033 showAscii .isSelected () == false ) ) {
10001034 // we need to convert to a different format
10011035 int base = 16 ; // default to hex
1002- if (radixList .getSelectedItem ().equals ("Decimal" )) base = 10 ;
1003- else if (radixList .getSelectedItem ().equals ("Octal" )) base = 8 ;
1004- else if (radixList .getSelectedItem ().equals ("Binary" )) base = 2 ;
1036+ if (radixList .getSelectedItem ().equals (RADIX_DECIMAL )) base = 10 ;
1037+ else if (radixList .getSelectedItem ().equals (RADIX_OCTAL )) base = 8 ;
1038+ else if (radixList .getSelectedItem ().equals (RADIX_BINARY )) base = 2 ;
10051039
10061040 Byte [] bytes = BinaryDisplayConverter .convertToBytes (replacementText ,
10071041 16 , false );
@@ -1269,16 +1303,16 @@ private String getTextAreaCannonicalForm() {
12691303 // if the data is not binary, then there is no need for conversion.
12701304 // if the data is Hex with ASCII not shown as chars, then no conversion needed.
12711305 if (radixList == null ||
1272- (radixList .getSelectedItem ().equals ("Hex" ) && ! showAscii .isSelected ()) ) {
1306+ (radixList .getSelectedItem ().equals (RADIX_HEX ) && ! showAscii .isSelected ()) ) {
12731307 // no need for conversion
12741308 return _textArea .getText ();
12751309 }
12761310
12771311 // The field is binary and not in the format expected by the DataType
12781312 int base = 16 ; // default to hex
1279- if (radixList .getSelectedItem ().equals ("Decimal" )) base = 10 ;
1280- else if (radixList .getSelectedItem ().equals ("Octal" )) base = 8 ;
1281- else if (radixList .getSelectedItem ().equals ("Binary" )) base = 2 ;
1313+ if (radixList .getSelectedItem ().equals (RADIX_DECIMAL )) base = 10 ;
1314+ else if (radixList .getSelectedItem ().equals (RADIX_OCTAL )) base = 8 ;
1315+ else if (radixList .getSelectedItem ().equals (RADIX_BINARY )) base = 2 ;
12821316
12831317 // the following can cause and exception if the text is not formatted correctly
12841318 Byte [] bytes = BinaryDisplayConverter .convertToBytes (_textArea .getText (),
0 commit comments