Skip to content

Commit 2fd912c

Browse files
hansonrhansonr
authored andcommitted
JSTextPaneUI proper preferredSize, text components "pass through" for
CTRL-keys
1 parent 91adc23 commit 2fd912c

File tree

8 files changed

+167
-63
lines changed

8 files changed

+167
-63
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.beans.PropertyChangeListener;
4343
import javax.swing.event.ChangeEvent;
4444
import javax.swing.event.ChangeListener;
45+
import javax.swing.text.DefaultEditorKit;
4546
import javax.swing.text.Document;
4647
import javax.swing.text.JTextComponent;
4748
import javax.swing.text.PlainDocument;
@@ -857,7 +858,7 @@ boolean hasActionListener() {
857858
private String command;
858859

859860
private static final Action[] defaultActions = {
860-
new NotifyAction()
861+
new NotifyAction(), new DefaultEditorKit.PassThroughAction()
861862
};
862863

863864
// --- Action implementations -----------------------------------

sources/net.sf.j2s.java.core/src/javax/swing/text/DefaultEditorKit.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ public void write(Writer out, Document doc, int pos, int len)
785785
// --- Action implementations ---------------------------------
786786

787787
private static final Action[] defaultActions = {
788+
new PassThroughAction(),
788789
new InsertContentAction(), new DeletePrevCharAction(),
789790
new DeleteNextCharAction(), new ReadOnlyAction(),
790791
// new DeleteWordAction(deletePrevWordAction),
@@ -1099,6 +1100,26 @@ public void actionPerformed(ActionEvent e) {
10991100
}
11001101
}
11011102

1103+
/*
1104+
* Deletes the character of content that follows the
1105+
* current caret position.
1106+
* @see DefaultEditorKit#deleteNextCharAction
1107+
* @see DefaultEditorKit#getActions
1108+
*/
1109+
public static class PassThroughAction extends TextAction {
1110+
1111+
/* Create this object with the appropriate identifier. */
1112+
public PassThroughAction() {
1113+
super("pass-through");
1114+
}
1115+
1116+
/** The operation to perform when this action is triggered. */
1117+
@Override
1118+
public void actionPerformed(ActionEvent e) {
1119+
System.out.println("DefaultEdKit ignoring");
1120+
/** @j2sNative e.bdata.doPropagate = true;*/
1121+
}
1122+
}
11021123

11031124
// /*
11041125
// * Deletes the word that precedes/follows the beginning of the selection.

sources/net.sf.j2s.java.core/src/swingjs/JSKeyEvent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public static JSKeyEvent newJSKeyEvent(JComponent source, Object jqevent, int id
5454
* ev.originalEvent.preventDefault();
5555
*/
5656

57+
//System.out.println(id + " " + evType + " " + jskey + " " + jskeyCode);
5758
if (id == 0)
5859
id = JSMouse.fixEventType(jqevent, 0);
5960
if (id == 0)

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.awt.Font;
3535

3636
import javax.swing.DefaultListCellRenderer;
37+
import javax.swing.JTextField;
3738
import javax.swing.LookAndFeel;
3839
import javax.swing.UIDefaults;
3940
import javax.swing.plaf.BorderUIResource;
@@ -563,6 +564,53 @@ protected void initComponentDefaults(UIDefaults table) {
563564
Color textText = table.getColor("textText");
564565
Color window = table.getColor("window");
565566

567+
568+
569+
570+
Object fieldInputMap = new UIDefaults.LazyInputMap(new Object[] {
571+
"ctrl C", "pass-through",
572+
"ctrl V", "pass-through",
573+
"ctrl X", "pass-through",
574+
// "COPY", DefaultEditorKit.copyAction,
575+
// "PASTE", DefaultEditorKit.pasteAction,
576+
// "CUT", DefaultEditorKit.cutAction,
577+
// "control INSERT", DefaultEditorKit.copyAction,
578+
// "shift INSERT", DefaultEditorKit.pasteAction,
579+
// "shift DELETE", DefaultEditorKit.cutAction,
580+
// "shift LEFT", DefaultEditorKit.selectionBackwardAction,
581+
// "shift KP_LEFT", DefaultEditorKit.selectionBackwardAction,
582+
// "shift RIGHT", DefaultEditorKit.selectionForwardAction,
583+
// "shift KP_RIGHT", DefaultEditorKit.selectionForwardAction,
584+
// "ctrl LEFT", DefaultEditorKit.previousWordAction,
585+
// "ctrl KP_LEFT", DefaultEditorKit.previousWordAction,
586+
// "ctrl RIGHT", DefaultEditorKit.nextWordAction,
587+
// "ctrl KP_RIGHT", DefaultEditorKit.nextWordAction,
588+
// "ctrl shift LEFT", DefaultEditorKit.selectionPreviousWordAction,
589+
// "ctrl shift KP_LEFT", DefaultEditorKit.selectionPreviousWordAction,
590+
// "ctrl shift RIGHT", DefaultEditorKit.selectionNextWordAction,
591+
// "ctrl shift KP_RIGHT", DefaultEditorKit.selectionNextWordAction,
592+
"ctrl A", "pass-through",
593+
// "HOME", DefaultEditorKit.beginLineAction,
594+
// "END", DefaultEditorKit.endLineAction,
595+
// "shift HOME", DefaultEditorKit.selectionBeginLineAction,
596+
// "shift END", DefaultEditorKit.selectionEndLineAction,
597+
// "BACK_SPACE", DefaultEditorKit.deletePrevCharAction,
598+
// "shift BACK_SPACE", DefaultEditorKit.deletePrevCharAction,
599+
// "ctrl H", DefaultEditorKit.deletePrevCharAction,
600+
// "DELETE", DefaultEditorKit.deleteNextCharAction,
601+
// "ctrl DELETE", DefaultEditorKit.deleteNextWordAction,
602+
// "ctrl BACK_SPACE", DefaultEditorKit.deletePrevWordAction,
603+
// "RIGHT", DefaultEditorKit.forwardAction,
604+
// "LEFT", DefaultEditorKit.backwardAction,
605+
// "KP_RIGHT", DefaultEditorKit.forwardAction,
606+
// "KP_LEFT", DefaultEditorKit.backwardAction,
607+
// "ENTER", JTextField.notifyAction,
608+
// "ctrl BACK_SLASH", "unselect"/*DefaultEditorKit.unselectAction*/,
609+
// "control shift O", "toggle-componentOrientation"/*DefaultEditorKit.toggleComponentOrientation*/
610+
});
611+
612+
613+
566614
// *** Shared Insets
567615
InsetsUIResource zeroInsets = new InsetsUIResource(0, 0, 0, 0);
568616
InsetsUIResource twoInsets = new InsetsUIResource(2, 2, 2, 2);
@@ -1859,6 +1907,7 @@ public Object createValue(UIDefaults table) {
18591907
// "TextField.border", textFieldBorder,
18601908
"TextField.margin",
18611909
zeroInsets,
1910+
"TextField.focusInputMap", fieldInputMap,
18621911

18631912
"FormattedTextField.font",
18641913
sansSerifPlain12,
@@ -1876,6 +1925,7 @@ public Object createValue(UIDefaults table) {
18761925
// "FormattedTextField.border", textFieldBorder,
18771926
"FormattedTextField.margin",
18781927
zeroInsets,
1928+
"FormattedTextField.focusInputMap", fieldInputMap,
18791929
// "FormattedTextField.focusInputMap",
18801930
// new UIDefaults.LazyInputMap(new Object[] {
18811931
// "ctrl C", DefaultEditorKit.copyAction,

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

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,10 +1719,10 @@ private Dimension getIconSize(AbstractButton b) {
17191719
: new Dimension(b.getIcon().getIconWidth(), b.getIcon().getIconHeight()));
17201720
}
17211721

1722-
private Dimension getTextSize(AbstractButton b) {
1722+
public Dimension getTextSize(AbstractButton b) {
17231723
if (textNode == null)
17241724
return null;
1725-
DOMNode.setStyle(textNode, "padding", "0");
1725+
DOMNode.setStyle(textNode, "padding", "0px");
17261726
String t = b.getText();
17271727
if (isAWT && t == "")
17281728
t = "\u00A0"; // AWT labels do not hide if ""
@@ -1747,7 +1747,7 @@ protected Dimension setHTMLSize1(DOMNode node, boolean addCSS, boolean usePrefer
17471747
String w0 = null, h0 = null, w0i = null, h0i = null, position = null;
17481748
DOMNode parentNode = null;
17491749
boolean hasFocus = false;
1750-
if (scrollPaneUI != null && scrollPaneUI.c.getWidth() != 0) {
1750+
if (!usePreferred && scrollPaneUI != null && scrollPaneUI.c.getWidth() != 0) {
17511751
w = scrollPaneUI.c.getWidth();
17521752
h = scrollPaneUI.c.getHeight();
17531753
} else if (usePreferred && preferredSize != null) {
@@ -2699,14 +2699,13 @@ protected void updateCenteringNode() {
26992699
protected void setAlignments(AbstractButton b, boolean justGetPreferred) {
27002700
if (alignmentDisabled)
27012701
return;
2702-
boolean hasItemIconAndAction = (!isSimpleButton && isMenuItem && iconNode != null && actionNode != null
2703-
&& iconNode != actionNode);
2704-
27052702
getJSInsets();
27062703
Dimension dimIcon = getIconSize(b);
27072704
Dimension dimText = getTextSize(b);
2705+
boolean hasItemIconAndAction = (!isSimpleButton && isMenuItem && iconNode != null && actionNode != null
2706+
&& iconNode != actionNode);
27082707
int wAction = (hasItemIconAndAction ? 15 : 0);
2709-
int wIcon = (actionNode != null ? (isMenuItem && !hasItemIconAndAction ? 15 : 20)
2708+
int wIcon = (actionNode != null ? (isMenuItem && dimIcon == null ? 5 : 20)
27102709
: dimIcon == null ? 0 : Math.max(0, dimIcon.width));
27112710
int wText = (dimText == null ? 0 : dimText.width);
27122711
int gap = (wText == 0 || wIcon == 0 ? 0 : b.getIconTextGap());
@@ -3011,10 +3010,10 @@ protected void setAlignments(AbstractButton b, boolean justGetPreferred) {
30113010
// if (wIcon > 0)
30123011
// addJSKeyVal(cssTxt, "top", "50%", "transform", "translateY(-50%)");
30133012
if (hasItemIconAndAction) {
3014-
addJSKeyVal(cssAction, "top", "50%", "transform", "translateY(-100%) scale(0.6,0.6)");
3013+
addJSKeyVal(cssAction, "top", "65%", "transform", "translateY(-100%) scale(0.6)");
30153014
addJSKeyVal(cssIcon, "top", "50%", "transform", "translateY(-80%)");
30163015
} else {
3017-
addJSKeyVal(cssIcon, "top", "50%", "transform", "translateY(-80%) scale(0.6,0.6)");
3016+
addJSKeyVal(cssIcon, "top", "50%", "transform", "translateY(-80%) scale(0.6)");
30183017
}
30193018
}
30203019

@@ -3281,7 +3280,7 @@ public void setForegroundCUI(Color c) {
32813280
protected void setForegroundFor(DOMNode node, Color color) {
32823281
if (node != null)
32833282
DOMNode.setStyle(node, "color",
3284-
(color == null ? "rgba(0,0,0,0)" : JSToolkit.getCSSColor(color == null ? Color.black : color)));
3283+
(color == null ? "rgba(0,0,0,0)" : toCSSString(color == null ? Color.black : color)));
32853284
}
32863285

32873286
@Override
@@ -3469,26 +3468,21 @@ protected String dumpEvent(EventObject e) {
34693468
}
34703469

34713470
public static String toCSSString(Color c) {
3472-
int opacity = c.getAlpha();
3473-
if (opacity == 255)
3474-
return "#" + toRGBHexString(c);
3475-
int rgb = c.getRGB();
3476-
return "rgba(" + ((rgb >> 16) & 0xFF) + "," + ((rgb >> 8) & 0xff) + "," + (rgb & 0xff) + "," + opacity / 255f
3477-
+ ")";
3478-
}
3479-
3480-
public static String toRGBHexString(Color c) {
3481-
int rgb = c.getRGB();
3482-
if (rgb == 0)
3483-
return "000000";
3484-
String r = "00" + Integer.toHexString((rgb >> 16) & 0xFF);
3485-
r = r.substring(r.length() - 2);
3486-
String g = "00" + Integer.toHexString((rgb >> 8) & 0xFF);
3487-
g = g.substring(g.length() - 2);
3488-
String b = "00" + Integer.toHexString(rgb & 0xFF);
3489-
b = b.substring(b.length() - 2);
3490-
return r + g + b;
3491-
}
3471+
return JSToolkit.getCSSColor(c, false);
3472+
}
3473+
3474+
// public static String toRGBHexString(Color c) {
3475+
// int rgb = c.getRGB();
3476+
// if (rgb == 0)
3477+
// return "000000";
3478+
// String r = "00" + Integer.toHexString((rgb >> 16) & 0xFF);
3479+
// r = r.substring(r.length() - 2);
3480+
// String g = "00" + Integer.toHexString((rgb >> 8) & 0xFF);
3481+
// g = g.substring(g.length() - 2);
3482+
// String b = "00" + Integer.toHexString(rgb & 0xFF);
3483+
// b = b.substring(b.length() - 2);
3484+
// return r + g + b;
3485+
// }
34923486

34933487
/**
34943488
* We allow here for an off-screen graphic for which the paint operation also
@@ -3747,7 +3741,7 @@ public void paintBackground(JSGraphics2D g) {
37473741

37483742
protected void setBackgroundDOM(DOMNode node, Color color) {
37493743
DOMNode.setStyle(node, "background-color",
3750-
color == null ? null : JSToolkit.getCSSColor(color));
3744+
color == null ? null : toCSSString(color));
37513745
}
37523746

37533747

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.awt.Insets;
77
import java.awt.JSComponent;
88
import java.awt.Point;
9+
import java.awt.Rectangle;
910
import java.awt.event.KeyEvent;
1011
import java.awt.event.KeyListener;
1112
import java.awt.event.MouseEvent;
@@ -164,7 +165,6 @@ InputMap getInputMap() {
164165
(InputMap)DefaultLookup.get(editor, this,
165166
getPropertyPrefix() + ".focusInputMap");
166167
if (shared != null) {
167-
//System.out.println("JSEditorPaneUI inputmap " + shared.keys());
168168
map.setParent(shared);
169169
}
170170
return map;
@@ -529,9 +529,9 @@ else if (isSub)
529529
private String getCSSStyle(AttributeSet a, AttributeSet currAttr) {
530530
String style = "";
531531
if (checkAttr(BACKGROUND, a, currAttr))
532-
style += "background:" + JSToolkit.getCSSColor((Color) getBackground(a)) + ";";
532+
style += "background:" + toCSSString((Color) getBackground(a)) + ";";
533533
if (checkAttr(FOREGROUND, a, currAttr))
534-
style += "color:" + JSToolkit.getCSSColor((Color) getForeground(a)) + ";";
534+
style += "color:" + toCSSString((Color) getForeground(a)) + ";";
535535
if (checkAttr(BOLD, a, currAttr))
536536
style += "font-weight:" + (isBold(a) ? "bold;" : "normal;");
537537
if (checkAttr(ITALIC, a, currAttr))
@@ -1188,9 +1188,25 @@ public Dimension getMaximumSize(JComponent jc) {
11881188
return ANY_SIZE;
11891189
}
11901190

1191+
@Override
1192+
public Dimension getPreferredSize(JComponent c) {
1193+
if (isTextPane) {
1194+
updateDOMNode();
1195+
String sh = DOMNode.getStyle(domNode, "height");
1196+
int w = (scrollPaneUI != null && scrollPaneUI.c.getWidth() != 0 ? scrollPaneUI.c.getWidth() : DOMNode.getWidth(domNode));
1197+
DOMNode.setStyle(domNode, "height", null);
1198+
Rectangle r = this.getBoundingRect(domNode);
1199+
int h = (int) Math.max(0, Math.ceil(r.height));
1200+
DOMNode.setStyle(domNode, "height", sh);
1201+
return new Dimension(w,h);
1202+
} else {
1203+
return super.getPreferredSize(c);
1204+
}
1205+
}
1206+
11911207
@Override
11921208
public Dimension getMinimumSize(JComponent jc) {
1193-
getPreferredSize(jc);
1209+
// getPreferredSize(jc);
11941210
// Document doc = editor.getDocument();
11951211
Insets i = jc.getInsets();
11961212
Dimension d = new Dimension();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ InputMap getInputMap() {
8484
jsmap = new InputMap();
8585
jsmap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), JTextField.notifyAction);
8686
}
87-
map.setParent(jsmap);
87+
map.getParent().setParent(jsmap);
8888
}
8989
return map;
9090
}

0 commit comments

Comments
 (0)