Browse Source

Improve texture cancellation fix to preserve dialog initialization

Co-authored-by: neph1 <[email protected]>
copilot-swe-agent[bot] 2 months ago
parent
commit
91992b518c

+ 16 - 10
jme3-materialeditor/src/com/jme3/gde/materials/multiview/widgets/TexturePanel.java

@@ -266,30 +266,36 @@ public class TexturePanel extends MaterialPropertyWidget implements TextureDropT
     }//GEN-LAST:event_jCheckBox2ActionPerformed
 
     private void texturePreviewMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_texturePreviewMouseClicked
+        // Set the current texture on the editor so the dialog initializes correctly
+        if (textureName != null && !textureName.equals("\"\"")) {
+            String currentTexture = extractTextureName(textureName);
+            editor.setAsText(currentTexture);
+        } else {
+            editor.setAsText(null);
+        }
+        
         Component view = editor.getCustomEditor();
-        String originalValue = property.getValue(); // Store original value before clearing
-        String originalTextureName = textureName; // Store original texture name
-        property.setValue(EMPTY);
         view.setVisible(true);
+        
         if (editor.getValue() != null) {
+            // A texture was selected
+            property.setValue(EMPTY); // Clear before setting new value
             textureName = "\"" + editor.getAsText() + "\"";
             displayPreview();
             updateFlipRepeat();
             fireChanged();
-        } else { // "No Texture" has been clicked or dialog was cancelled
+        } else {
+            // getValue() is null - either "No Texture" was selected or dialog was cancelled
             String asText = editor.getAsText();
             if (asText == null) {
-                // "No Texture" was explicitly selected
+                // "No Texture" was explicitly selected (setAsText(null) was called)
+                property.setValue(EMPTY);
                 textureName = "\"\"";
                 texturePreview.setIcon(null);
                 texturePreview.setToolTipText("");
                 fireChanged();
-            } else {
-                // Dialog was cancelled, restore original values
-                property.setValue(originalValue);
-                textureName = originalTextureName;
-                displayPreview(); // Restore the preview
             }
+            // If asText is not null, it means dialog was cancelled - do nothing to preserve original state
         }
     }//GEN-LAST:event_texturePreviewMouseClicked
 

+ 11 - 23
jme3-materialeditor/test/unit/src/com/jme3/gde/materials/multiview/widgets/TexturePanelTest.java

@@ -187,21 +187,16 @@ public class TexturePanelTest {
         texturePanel.textureName = originalTexture;
         
         // Simulate the user clicking on texture preview and then canceling
-        // This is a simplified version of what happens in texturePreviewMouseClicked
-        String originalValue = texturePanel.property.getValue();
-        String originalTextureName = texturePanel.textureName;
+        // In the fixed implementation, the property is not cleared until we know the user's choice
         
-        // Clear the property (this happens when the dialog opens)
-        texturePanel.property.setValue("");
+        // Simulate cancel: editor.getValue() returns null and editor.getAsText() returns original texture
+        // (the editor was initialized with the current texture, and cancel doesn't change it)
+        String asText = "original_texture.jpg"; // Would be returned by editor.getAsText() on cancel
         
-        // Simulate cancel: editor.getValue() returns null and editor.getAsText() returns original value
-        // (not null, because setAsText was never called)
-        // This is the logic from the fixed texturePreviewMouseClicked method
-        String asText = originalTexture; // Would be returned by editor.getAsText() on cancel
+        // This simulates the logic from the fixed texturePreviewMouseClicked method
         if (asText != null) {
-            // Dialog was cancelled, restore original values
-            texturePanel.property.setValue(originalValue);
-            texturePanel.textureName = originalTextureName;
+            // Dialog was cancelled - do nothing to preserve original state
+            // Property and textureName remain unchanged
         }
         
         // Verify that the original texture is preserved
@@ -222,23 +217,16 @@ public class TexturePanelTest {
         texturePanel.textureName = originalTexture;
         
         // Simulate the user clicking on texture preview and then selecting "No Texture"
-        String originalValue = texturePanel.property.getValue();
-        String originalTextureName = texturePanel.textureName;
-        
-        // Clear the property (this happens when the dialog opens)
-        texturePanel.property.setValue("");
+        // In the fixed implementation, the property is not cleared until we know the user's choice
         
         // Simulate "No Texture" selection: editor.getValue() returns null and editor.getAsText() returns null
-        // This is the logic from the fixed texturePreviewMouseClicked method
         String asText = null; // Would be returned by editor.getAsText() when "No Texture" is selected
+        
+        // This simulates the logic from the fixed texturePreviewMouseClicked method
         if (asText == null) {
             // "No Texture" was explicitly selected
-            texturePanel.textureName = "\"\"";
             texturePanel.property.setValue("");
-        } else {
-            // Dialog was cancelled, restore original values
-            texturePanel.property.setValue(originalValue);
-            texturePanel.textureName = originalTextureName;
+            texturePanel.textureName = "\"\"";
         }
         
         // Verify that the texture is properly cleared