Skip to content

Commit fdefd0c

Browse files
committed
fixes for Frame.revalidate() not working
1 parent 5d9eb1f commit fdefd0c

File tree

6 files changed

+104
-12
lines changed

6 files changed

+104
-12
lines changed

sources/net.sf.j2s.java.core/src/java/awt/Component.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,6 +2727,53 @@ protected final void invalidateIfValid() {
27272727
}
27282728
}
27292729

2730+
// SwingJS see JComponent
2731+
// /**
2732+
// * Revalidates the component hierarchy up to the nearest validate root.
2733+
// * <p>
2734+
// * This method first invalidates the component hierarchy starting from this
2735+
// * component up to the nearest validate root. Afterwards, the component
2736+
// * hierarchy is validated starting from the nearest validate root.
2737+
// * <p>
2738+
// * This is a convenience method supposed to help application developers
2739+
// * avoid looking for validate roots manually. Basically, it's equivalent to
2740+
// * first calling the {@link #invalidate()} method on this component, and
2741+
// * then calling the {@link #validate()} method on the nearest validate
2742+
// * root.
2743+
// *
2744+
// * @see Container#isValidateRoot
2745+
// * @since 1.7
2746+
// */
2747+
// public void revalidate() {
2748+
// revalidateSynchronously();
2749+
// }
2750+
//
2751+
// /**
2752+
// * Revalidates the component synchronously.
2753+
// */
2754+
// final void revalidateSynchronously() {
2755+
// synchronized (getTreeLock()) {
2756+
// invalidate();
2757+
//
2758+
// Container root = getContainer();
2759+
// if (root == null) {
2760+
// // There's no parents. Just validate itself.
2761+
// validate();
2762+
// } else {
2763+
// while (!root.isValidateRoot()) {
2764+
// if (root.getContainer() == null) {
2765+
// // If there's no validate roots, we'll validate the
2766+
// // topmost container
2767+
// break;
2768+
// }
2769+
//
2770+
// root = root.getContainer();
2771+
// }
2772+
//
2773+
// root.validate();
2774+
// }
2775+
// }
2776+
// }
27302777
/**
27312778
*
27322779
* Unused in SwingJS. For SwingJS, we have the graphics without needing to get it from a peer.

sources/net.sf.j2s.java.core/src/java/awt/Window.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848
import javax.swing.JComponent;
4949
import javax.swing.JLayeredPane;
5050
import javax.swing.JRootPane;
51+
import javax.swing.RepaintManager;
5152
import javax.swing.RootPaneContainer;
53+
import javax.swing.SwingUtilities;
5254

5355
import sun.awt.AppContext;
5456
import swingjs.JSToolkit;
@@ -62,6 +64,8 @@
6264
* with this, but the reason was to allow JInternalFrame to subclass JFrame
6365
* instead of duplicating in code all of its functionality.
6466
*
67+
* In addition, it allows all sorts of windows to be JComponents and have UIs.
68+
*
6569
*
6670
* A <code>Window</code> object is a top-level window with no borders and no
6771
* menubar.
@@ -3632,6 +3636,33 @@ final Point getLocationOnWindow() {
36323636
// y + h * securityWarningAlignmentY + securityWarningPointY);
36333637
// }
36343638

3639+
3640+
/**
3641+
* Indicates if this container is a validate root.
3642+
* <p>
3643+
* {@code Window} objects are the validate roots, and, therefore, they
3644+
* override this method to return {@code true}.
3645+
*
3646+
* @return {@code true}
3647+
* @since 1.7
3648+
* @see java.awt.Container#isValidateRoot
3649+
*/
3650+
@Override
3651+
public boolean isValidateRoot() {
3652+
return true;
3653+
}
3654+
3655+
@Override
3656+
public void revalidate() {
3657+
// added to see if JFrame can be revalidated this way after a change in contentPane.
3658+
// SwingJS - set this UI to rebuild itself due to some
3659+
// internal structural change.
3660+
if (ui != null)
3661+
((JSComponentUI)ui).setTainted();
3662+
invalidate();
3663+
validate();
3664+
}
3665+
36353666
} // class Window
36363667

36373668

sources/net.sf.j2s.java.core/src/javax/swing/JComponent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4245,7 +4245,7 @@ public void revalidate() {
42454245
// internal structural change.
42464246
if (ui != null)
42474247
((JSComponentUI)ui).setTainted();
4248-
if (getParent() == null) {
4248+
if (getParent() == null && !isValidateRoot()) {
42494249
// Note: We don't bother invalidating here as once added
42504250
// to a valid parent invalidate will be invoked (addImpl
42514251
// invokes addNotify which will invoke invalidate on the

sources/net.sf.j2s.java.core/src/javax/swing/JRootPane.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ public JMenuBar getMenuBar() {
578578
* by the root pane.
579579
* <p>
580580
* Swing's painting architecture requires an opaque <code>JComponent</code> in
581-
* the containment hiearchy. This is typically provided by the content pane.
581+
* the containment hierarchy. This is typically provided by the content pane.
582582
* If you replace the content pane it is recommended you replace it with an
583583
* opaque <code>JComponent</code>.
584584
*
@@ -592,12 +592,17 @@ public void setContentPane(Container content) {
592592
if (content == null)
593593
throw new IllegalComponentStateException(
594594
"contentPane cannot be set to null.");
595+
if (contentPane == content)
596+
return;
595597
if (contentPane != null && contentPane.getParent() == layeredPane)
596598
layeredPane.remove(contentPane);
597599
content = 秘transferFrameTo((JComponent)content);
598600
content.秘isContentPane = true;
601+
Container old = contentPane;
599602
contentPane = content;
600603
layeredPane.add(contentPane, JLayeredPane.FRAME_CONTENT_LAYER);
604+
if (old != null)
605+
validate();
601606
}
602607

603608
/**

sources/net.sf.j2s.java.core/src/javax/swing/JViewport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ private void paintViaBackingStore(Graphics g) {
612612
Graphics bsg = getBackingStoreGraphics(g);
613613
try {
614614
super.paint(bsg);
615-
((JSGraphics2D)(Object)g).drawImagePriv(backingStoreImage, 0, 0, this);
615+
((JSGraphics2D)(Object)g).drawImageFromRaster(backingStoreImage, 0, 0, this);
616616
} finally {
617617
bsg.dispose();
618618
}

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSSplitPaneUI.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public Dimension getPreferredSize() {
8686
}
8787

8888
SplitPaneDivider(JSSplitPaneUI ui) {
89-
super();
89+
super(" ");
9090
paneui = ui;
9191
setOpaque(true);
9292
}
@@ -672,7 +672,7 @@ public void actionPerformed(ActionEvent ev) {
672672
* Canvas that fills the background in dark gray.
673673
*/
674674
protected Component createDefaultNonContinuousLayoutDivider() {
675-
return new JLabel() {
675+
return new JLabel(" ") {
676676
{
677677
setOpaque(true);
678678
setBackground(Color.BLACK);
@@ -1144,7 +1144,7 @@ public void layoutContainer(Container container) {
11441144
setDividerLocation(spDividerLocation - dOffset, availableSize);
11451145
dividerLocationIsSet = false;
11461146
} else if (availableSize != lastSplitPaneSize) {
1147-
distributeSpace(availableSize - lastSplitPaneSize, getKeepHidden());
1147+
distributeSpace(availableSize - lastSplitPaneSize, getKeepHidden(), null);
11481148
}
11491149
doReset = false;
11501150
dividerLocationIsSet = false;
@@ -1675,7 +1675,7 @@ void resetToPreferredSizes(int availableSize) {
16751675
}
16761676
}
16771677
setSizes(testSizes);
1678-
distributeSpace(availableSize - totalSize, false);
1678+
distributeSpace(availableSize - totalSize, false, testSizes);
16791679
}
16801680

16811681
/**
@@ -1687,7 +1687,7 @@ void resetToPreferredSizes(int availableSize) {
16871687
* if true and one of the components is 0x0 it gets none of the
16881688
* extra space
16891689
*/
1690-
void distributeSpace(int space, boolean keepHidden) {
1690+
void distributeSpace(int space, boolean keepHidden, int[] prefSizes) {
16911691
boolean lValid = (components[0] != null && components[0].isVisible());
16921692
boolean rValid = (components[1] != null && components[1].isVisible());
16931693

@@ -1703,19 +1703,28 @@ && getSizeForPrimaryAxis(components[1].getSize()) == 0) {
17031703
rValid = false;
17041704
}
17051705
}
1706+
int lMin = getMinimumSizeOfComponent(components[0]);
1707+
int rMin = getMinimumSizeOfComponent(components[1]);
17061708
if (lValid && rValid) {
17071709
double weight = splitPane.getResizeWeight();
1708-
int lExtra = (int) (weight * (double) space);
1710+
// if (prefSizes != null && weight == 0 && (rMin + lMin + prefSizes[2]) <= space) {
1711+
// // SwingJS better!
1712+
// weight = 1.0 * prefSizes[0] / (prefSizes[0] + prefSizes[1]);
1713+
// }
1714+
int lExtra = (int) (weight * space);
17091715
int rExtra = (space - lExtra);
17101716

17111717
sizes[0] += lExtra;
17121718
sizes[1] += rExtra;
17131719

1714-
int lMin = getMinimumSizeOfComponent(components[0]);
1715-
int rMin = getMinimumSizeOfComponent(components[1]);
17161720
boolean lMinValid = (sizes[0] >= lMin);
17171721
boolean rMinValid = (sizes[1] >= rMin);
1718-
1722+
if (prefSizes != null && (!lMinValid || !rMinValid)) {
1723+
distributeSpace(space, keepHidden, null);
1724+
return;
1725+
}
1726+
1727+
17191728
if (!lMinValid && !rMinValid) {
17201729
if (sizes[0] < 0) {
17211730
sizes[1] += sizes[0];

0 commit comments

Comments
 (0)