Skip to content

Commit b3a0958

Browse files
hansonrhansonr
authored andcommitted
JTextArea, JTextField support for CTRL-V
1 parent c822ee2 commit b3a0958

File tree

5 files changed

+35
-43
lines changed

5 files changed

+35
-43
lines changed

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

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ public class JSEditorPaneUI extends JSTextUI {
6464

6565
public JSEditorPaneUI() {
6666
isEditorPane = isTextView = true;
67-
// /**
68-
// * @j2xxu=this;
69-
// */
70-
// // turning this off: setDoPropagate();
7167
}
7268

7369
@Override
@@ -195,14 +191,6 @@ InputMap getInputMap() {
195191

196192
@Override
197193
public DOMNode updateDOMNode() {
198-
// System.out.println("update xxu dom");
199-
// /**
200-
// * @j2sNative
201-
// *
202-
// * xxu = this;
203-
// *
204-
//
205-
// */
206194
if (domNode == null) {
207195
domNode = newDOMObject("div", id);
208196
DOMNode.setStyles(domNode); // default for pre is font-height
@@ -937,7 +925,6 @@ private int[] getJavaMarkAndDot() {
937925
return new int[] { Math.min(x, y), Math.max(x, y) };
938926
}
939927

940-
private int len0;
941928
private String stemp;
942929
private int[] xyTemp;
943930

@@ -947,16 +934,16 @@ private int[] getJavaMarkAndDot() {
947934
*
948935
*/
949936
@Override
950-
protected void handleFutureInsert(boolean trigger) {
937+
protected boolean handleCtrlV(int mode) {
951938
//System.out.println(getJavaMarkAndDot());
952939

953940
getJSMarkAndDot(markDot, 0);
954941
//System.out.println(markDot);
955942
String s = (String) DOMNode.getAttr(domNode, "innerText");
956-
if (!trigger) {
943+
if (mode == KeyEvent.KEY_PRESSED) {
957944
stemp = s;
958945
xyTemp = getJavaMarkAndDot();
959-
return;
946+
return false;
960947
}
961948

962949
// problem here is that JavaScript raw text has extra \n in it that the Java does not.
@@ -966,12 +953,12 @@ protected void handleFutureInsert(boolean trigger) {
966953

967954
//System.out.println("n=" + n + " x=" + x + " newlen=" + s.length() + " len0=" + len0);
968955
if (n <= 0)
969-
return;
956+
return false;
970957
try {
971958

972959
x += (SPACES_PER_TAB - 1) * tabCount(editor.getDocument().getText(0, x));
973960
if (x < 0)
974-
return;
961+
return false;
975962
s = s.substring(x, x + n);
976963

977964
//System.out.println("x=" + x + " n=" + n + " s=" +s);
@@ -985,6 +972,7 @@ protected void handleFutureInsert(boolean trigger) {
985972
//setJSMarkAndDot(markDot.x, markDot.x, false);
986973
} catch (BadLocationException bl) {
987974
}
975+
return true;
988976
}
989977

990978
private int tabCount(String s) {
@@ -994,11 +982,6 @@ private int tabCount(String s) {
994982
n++;
995983
return n;
996984
}
997-
998-
// protected boolean handleEnter() {
999-
// editor.replaceSelection("\n");
1000-
// return true;
1001-
// }
1002985

1003986
@Override
1004987
void setJSText() {

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,6 @@ boolean getJSMarkAndDot(Point pt, int keyCode) {
12561256
* @return null to continue processing, CONSUMED(false) to stop propagation,
12571257
* UNHANLDED(true) to ignore
12581258
*/
1259-
@SuppressWarnings("unused")
12601259
protected Boolean checkAllowEvent(Object jQueryEvent) {
12611260
boolean b = NOT_CONSUMED; // jQuery event will propagate
12621261
boolean checkEditable = false;
@@ -1284,23 +1283,23 @@ protected Boolean checkAllowEvent(Object jQueryEvent) {
12841283
case KeyEvent.VK_V: // paste
12851284
if (!isCTRL)
12861285
return null;
1287-
//TODO -- JEditorPane needs this -- right now we cannot do this correctly with multiple new lines
1288-
1286+
// TODO -- JEditorPane needs this -- right now we cannot do this correctly with
1287+
// multiple new lines
1288+
12891289
if (!isEditorPane)
12901290
allowKeyEvent(jQueryEvent);
12911291
if (type == "keydown")
1292-
handleFutureInsert(false);
1292+
handleCtrlV(KeyEvent.KEY_PRESSED);
12931293
else if (type == "keyup")
1294-
handleFutureInsert(true);
1294+
handleCtrlV(KeyEvent.KEY_RELEASED);
12951295
return NOT_CONSUMED;
12961296
case KeyEvent.VK_C: // copy
12971297
if (!isCTRL)
12981298
return null;
12991299
allowKeyEvent(jQueryEvent);
1300-
return (NOT_CONSUMED); // allow standard browser CTRL-C, with no Java-Event processing
1300+
return NOT_CONSUMED; // allow standard browser CTRL-C, with no Java-Event processing
13011301
case KeyEvent.VK_TAB:
1302-
b = (type == "keydown" ? handleTab(jQueryEvent) : CONSUMED);
1303-
break;
1302+
return (type == "keydown" && handleTab(jQueryEvent) == NOT_CONSUMED ? null : Boolean.valueOf(CONSUMED));
13041303
case KeyEvent.VK_UP:
13051304
case KeyEvent.VK_DOWN:
13061305
case KeyEvent.VK_LEFT:
@@ -1340,12 +1339,24 @@ private void allowKeyEvent(Object jQueryEvent) {
13401339
*/
13411340
}
13421341

1343-
1344-
protected void handleFutureInsert(boolean trigger) {
1342+
protected boolean handleCtrlV(int mode) {
1343+
switch (mode) {
1344+
case KeyEvent.KEY_PRESSED:
1345+
break;
1346+
case KeyEvent.KEY_RELEASED:
1347+
String val = getJSTextValue();
1348+
Point pt = new Point();
1349+
getJSMarkAndDot(pt, 0);
1350+
editor.setTextFromUI(val);
1351+
setJSMarkAndDot(pt.x, pt.x, false);
1352+
setJavaMarkAndDot(pt);
1353+
break;
1354+
}
1355+
return true;
13451356
}
13461357

13471358
protected boolean handleTab(Object jQueryEvent) {
1348-
return CONSUMED;
1359+
return NOT_CONSUMED;
13491360
}
13501361

13511362

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

Lines changed: 0 additions & 6 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import javax.swing.text.JTextComponent;
4646

4747
import swingjs.JSMouse;
48-
//import swingjs.api.JSMinimalAbstractDocument;
48+
4949
import swingjs.api.js.DOMNode;
5050

5151
public class TextListener implements KeyListener, FocusListener, ChangeListener,

sources/net.sf.j2s.java.core/src/test/Test_Editor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public class Test_Editor extends JFrame implements DropTargetListener {
7070

7171
String test = " 345567890112345678903 345\n ";
7272

73+
private JTextArea area;
74+
7375
// private static void logClass(String name) {
7476
// ConsoleHandler consoleHandler = new ConsoleHandler();
7577
// consoleHandler.setLevel(Level.ALL);
@@ -131,7 +133,7 @@ public Test_Editor() {
131133
JScrollPane js = new JScrollPane(editor);
132134
// js.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
133135

134-
JTextArea area = getArea();
136+
area = getArea();
135137
area.addKeyListener(ka);
136138
JScrollPane js2 = new JScrollPane(area);
137139
js2.setPreferredSize(new Dimension(300, 300));
@@ -156,6 +158,7 @@ public void propertyChange(PropertyChangeEvent evt) {
156158

157159
JPanel panel = getButtonPanel(editor, area, area1, field);
158160
panel.add(field2);
161+
field2.addKeyListener(ka);
159162

160163
new DropTarget(editor, this);
161164
new DropTarget(area, this);
@@ -664,6 +667,7 @@ public void keyReleased(KeyEvent e) {
664667
System.out.println("ok5");
665668
showKeyEvent(e);
666669
System.out.println("ok6");
670+
System.out.println(area.getText());
667671
}
668672
};
669673

@@ -852,7 +856,7 @@ protected void showKeyEvent(KeyEvent e) {
852856
String source = /** @j2sNative (xxx = e).bdata.jqevent.originalEvent.target.id || */
853857
"";
854858
System.out.println(
855-
"Test_Editor keyEvent id=" + e.getID() + " " + ((JComponent) e.getSource()).getClass().getName() + " "
859+
"Test_Editor keyEvent id=" + e.getID() + "\n'" + ((JTextComponent) e.getSource()).getText() + "'\n "
856860
+ source + " char=" + e.getKeyChar() + " code=" + e.getKeyCode() + " loc=" + e.getKeyLocation()
857861
+ "\n mod=" + e.getModifiers() + " " + KeyEvent.getKeyModifiersText(e.getModifiers()) + " modx="
858862
+ e.getModifiersEx() + " " + KeyEvent.getKeyModifiersText(e.getModifiersEx()));

0 commit comments

Comments
 (0)