Repository navigation
Fix the colour picker crashing when a hex value is pasted into it - #3247
Conversation
Pasting text into the hex field of the bundled com.bric.swing.ColorPicker
(com.mastfrog:colorchooser, binary-only) crashes the dialog. The paste makes
AbstractDocument fire an insert notification, and ColorPicker's own
HexDocumentListener reacts by calling setRGB(), which normalizes the field
through updateHexField() -> JTextField.setText(). That writes to the very
document that is still notifying its listeners, so AbstractDocument.writeLock()
throws IllegalStateException("Attempt to mutate in notification"). The picker's
adjustingHexField guard only covers the opposite direction, and the dialog runs
its own modal event pump, so the exception unwinds out of
ColorPicker.showDialog() into whichever Gephi component opened it.
The defect is inside the third-party binary and cannot be patched here, so
guard Gephi's integration points instead. JColorButton and GradientSlider now
catch IllegalStateException around showDialog() and behave as if the user had
cancelled, leaving the current color untouched.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
I dug into this one because the fix depends entirely on where the The exception never reaches the Gephi call siteThe PR description is right that The Reproduced itI put with a stack trace whose colorchooser frames match the Sentry report line for line: So the diagnosis of the defect is correct, but the exception goes to the EDT's uncaught-exception handler, the dialog stays open and usable, and (One detail worth noting for anyone reproducing: Why Sentry blames
|
…alog
The catch added in the previous commit is unreachable. AWT's own modal event
pump swallows the failure long before it could reach a caller of
ColorPicker.showDialog(): EventDispatchThread.pumpOneEventForFilters catches
Throwable and hands it to processException(), which forwards it to the thread's
uncaught exception handler. On the NetBeans Platform that handler is
TopLogging$AWTHandler, installed via Thread.setDefaultUncaughtExceptionHandler,
and it logs the throwable to the "global" logger, whose records reach the root
logger and so ReporterHandler. That is why the reports name the Gephi component
as the culprit even though nothing there ever sees the exception: the Gephi
frame is simply the innermost application frame still on the stack underneath
the modal pump. Verified against the real colorchooser-1.5 jar - showDialog
returned normally and the catch never ran.
So the trigger has to be removed rather than the aftermath handled. The picker
reacts to every hex field edit by rewriting that same field with the canonical
form of the colour, from inside the field's own insert notification, which
AbstractDocument.writeLock() refuses. Pasting "#00FF00" therefore fails while
pasting "00FF00" works. Installing a DocumentFilter that keeps the field to at
most six uppercase hex digits leaves the canonical form already in place, so
there is nothing to rewrite and the reentrant write never happens. Paste and
drag-and-drop both route through the filter, and the picker's own updates are
already canonical so they pass through untouched.
The picker instance is created inside the static showDialog(), so the filter is
installed from a container listener that fires when the picker is added to the
dialog, before the dialog is displayable. The dialog itself is unchanged.
This also removes a second, independent failure on the same path: the picker's
delayed hex update restores the caret to its pre-edit offset, which threw
IllegalArgumentException whenever the pasted text was longer than the six
characters it truncates to ("hello world", "rgb(12, 34, 56)"). Capping the
field at six characters keeps that offset in range.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
The review is right, and the first commit here did not work. I reproduced it against the real Reworked to remove the trigger instead. The picker rewrites its hex field from inside that field's own insert notification, but only when the text differs from canonical six-digit uppercase hex — which is why pasting Two extra things this turned up, both covered in the updated description: capping the field at six characters also fixes an unrelated |
|
I checked this against the real The fix itself worksConfirming the parts that were worth doubting rather than taking on trust:
Gap 1 —
|
…ull hex field JColorBlackWhiteSwitcher's right-click handler still called ColorPicker.showDialog directly, bypassing the hex field guard added for JColorButton and GradientSlider. That call site is a confirmed live crash, GEPHI-68Q: same IllegalStateException, culprit JColorBlackWhiteSwitcher$1. Separately, once the hex field is capped at six digits, editing mid-field without a selection had no room to insert into and silently did nothing - e.g. typing over "FF0000" at the caret between "FF0" and "000" produced no visible change. HexDocumentFilter now evicts characters from the far end of the field to make room, so a full field overtypes like a fixed-width buffer instead of ignoring keystrokes once it hits six digits. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Description
Pasting a colour code into the hex field of the colour picker dialog crashes on the event dispatch thread instead of applying the colour. Reported through the in-app reporter as 29 users / 112 events (GEPHI-5MG, from the colour buttons) and 8 users / 46 events (GEPHI-5RJ, from the gradient slider).
Mechanism
The dialog is
com.bric.swing.ColorPicker, from the binary-onlycom.mastfrog:colorchooser:1.5artifact re-exported bymodules/UILibraryWrapper. 1.5 is the latest published version, so there is no upgrade to take.Its hex field is wired so that every edit rewrites that same field:
JTextComponent.replaceSelection()→AbstractDocument.replace(), which takes the document's write lock and then fires an insert notification withnotifyingListenersset.ColorPicker$HexDocumentListener.insertUpdate(ColorPicker.java:495) →changedUpdate(ColorPicker.java:464) strips the text to hex digits and, when that leaves exactly six, callsColorPicker.setRGB(...)synchronously — still inside the notification.setRGBcallsupdateHexField(), which rewrites the field to canonical six-digit uppercase hex viahexField.setText(...)(ColorPicker.java:1049) whenever the current text differs from it.setTextre-entersAbstractDocument.replace()→writeLock(), which refuses to mutate a document that is still notifying, and throwsIllegalStateException: Attempt to mutate in notification.The picker's
adjustingHexFieldguard (ColorPicker.java:455) only covers the reverse direction,setRGB→updateHexField→ listener. On the inbound paste path it is still zero.Which inputs fail follows directly from step 3: pasting
00FF00works, because the pasted text already equals the canonical form andupdateHexFieldhas nothing to write, while pasting#00FF00— the form you get from copying a colour almost anywhere — always fails.Stack trace (GEPHI-5MG)
Stack trace (GEPHI-5RJ)
Why the Gephi frame appears in the reports
Worth spelling out, because it is misleading. Nothing in Gephi ever sees this exception. The dialog is modal, so it is pumping events inside
Dialog.show(), andEventDispatchThread.pumpOneEventForFilterscatchesThrowableand passes it toprocessException()→getUncaughtExceptionHandler(). On the NetBeans Platform that handler isTopLogging$AWTHandler, installed withThread.setDefaultUncaughtExceptionHandler, which logs the throwable to thegloballogger; the record reaches the root logger and soReporterHandler(modules/DesktopBranding/.../Installer.java:126).ColorPicker.showDialogthen returns normally, as if the dialog had been cancelled.The Gephi frames in the traces above are therefore ancestors of the modal pump, not a propagation path — they are just the innermost application frames still on the stack. A
try/catcharoundColorPicker.showDialog(...)can never run, at any call site. Confirmed against the real jar: withshowDialogcalled from the EDT insidetry/catch (IllegalStateException)and a programmatic paste of#00FF00, the exception went to the default uncaught handler,showDialogreturned normally, and the catch never fired.What changed
Since the exception cannot be intercepted and the third-party class cannot be patched, the fix removes the trigger. New
ColorPickerUtils.showDialog(...)shows the same picker dialog, but first constrains the hex field to hold at most six uppercase hex digits, using aDocumentFilter. The field then always already holds the canonical form,updateHexField()never has anything to rewrite, and the reentrant write cannot occur. Paste and drag-and-drop both go through the filter, and the picker's own updates are canonical already so they pass through untouched.The picker instance is created inside the static
showDialog()and never exposed, so the filter is installed from a container listener that fires when the picker is added to the dialog — before the dialog is displayable, verified — and is removed in afinallywhen the call returns. The dialog the user sees, including its localised title and buttons, is unchanged. Only the vendor's public API is used (getExpertControls(), and the hex field is identified as the one the picker itself listens to); no reflection.JColorButton(both the left-click and right-click paths, which were duplicating the call) andGradientSlider.showColorPicker()now go through it.Measured against the real
colorchooser-1.5jar, driving the actual dialog with programmatic pastes:#00FF00IllegalStateException: Attempt to mutate in notification00FF00#ABCDEFIllegalStateException: Attempt to mutate in notificationABCDEFrgb(12, 34, 56)IllegalArgumentException: bad position: 15B12345hello worldIllegalArgumentException: bad position: 11ED00001a2b3c1a2b3c1A2B3C00FF0000FF0000FF00The two
IllegalArgumentExceptionrows are a second, independent bug on the same path that the fix also closes: the picker's delayed hex update restores the caret to its pre-edit offset after truncating the text to six characters, which is out of range whenever the pasted text was longer. Capping the field at six characters keeps that offset valid. Typing, the opacity slider and the picker-to-field direction were checked separately and are unaffected.The inline mini-picker in
GradientSlider.ColorPickerPopupis not affected by any of this — it is built with expert controls hidden, so its hex field is never reachable.Follow-up:
JColorBlackWhiteSwitcher, and overtyping a full fieldReview on this PR turned up two more things worth folding in here rather than leaving as separate issues.
JColorBlackWhiteSwitcher.java:90still calledColorPicker.showDialogdirectly, bypassing the guard entirely. It now goes throughColorPickerUtils.showDialog, likeJColorButtonandGradientSlider. This isn't just the same defect by inspection — the review pinned it to a confirmed live report, GEPHI-68Q (IllegalStateException: Attempt to mutate in notification, culpritJColorBlackWhiteSwitcher$1.mouseClicked).The review also traced a second path to the same picker that this PR does not fix: the bundled
net.java.dev.colorchooser.ColorChooserswatch widget (used inUniqueColorTransformerPanel,PartitionColorTransformerPanel,DefaultPanel, andEdgeSettingsPanel) opens the dialog from inside its own UI delegate,ColorChooserUI.keyboardInvoke, on Space/Enter or Ctrl/Meta-click — with no Gephi call site to redirect. That accounts for GEPHI-5D7 (51 users / 184 events, the largest report in this cluster), and possibly others in the same open cluster (GEPHI-5FJ, GEPHI-5EZ, GEPHI-5AY) whose call site hasn't been pinned down.ColorChooserisfinaland its UI delegate (DefaultColorChooserUI) is package-private, so there's no non-reflective extension point to hook without reimplementing the widget's rendering — closing this would need a JVM-wideAWTEventListenerinstalled once at application startup instead of per dialog open (the same primitiveColorPickerUtilsalready uses, just scoped differently), which is a passive global guard rather than a call-site convention. Deliberately left as a separately-scoped follow-up rather than bundled in here.Overtyping a full hex field. Capping the field at six digits fixed the crash, but editing mid-field without a selection then has no room to insert into once the field is full, so it silently did nothing — e.g. clicking between the
0and00ofFF0000and typing produced no visible change.HexDocumentFilternow evicts characters from the far end of the field to make room for what's typed or pasted at the caret, so a full field overtypes like a fixed-width buffer instead of ignoring the keystroke: typing9at that position now givesFF0900. Typing at the very end of an already-full field is still a no-op, since there's nothing after the caret to evict. Covered by three new cases inColorPickerUtilsTest.Sentry: GEPHI-5MG, GEPHI-5RJ, GEPHI-68Q.
Checklist
Added tests?
ColorPickerUtilsTestcovers the property the fix rests on: whatever is pasted or typed, the hex document only ever holds up to six uppercase hex digits, which is what leaves the picker with nothing to rewrite from inside its own notification. It also covers the overtype behavior added in the follow-up commit: typing mid-field on a full field evicts from the tail, typing at the very end of a full field is a no-op, and replacing a selection that would overflow the field evicts from the tail too.The end-to-end path cannot be automated here —
new ColorPicker(...)callsToolkit.getScreenSize()and throwsHeadlessException, and CI runs with-Djava.awt.headless=true— so the full dialog-plus-paste sequence was verified manually against the real jar instead; those results are the table above.Added to documentation?