46 import javax.swing.JButton;
47 import javax.swing.JViewport;
48 import javax.swing.InputMap;
49 import javax.swing.SwingUtilities;
50 import javax.swing.TransferHandler;
51 import javax.swing.plaf.basic.BasicArrowButton;
52 import javax.swing.plaf.basic.BasicScrollBarUI;
53 import javax.swing.plaf.basic.BasicScrollPaneUI;
54 import java.beans.PropertyChangeEvent;
55 import java.beans.PropertyChangeListener;
56 import javax.swing.text.Caret;
57 import javax.swing.text.DefaultCaret;
58 import javax.swing.text.JTextComponent;
59
60 import javax.swing.plaf.BorderUIResource;
61 import java.awt.im.InputMethodRequests;
62 import sun.awt.CausedFocusEvent;
63 import sun.awt.AWTAccessor;
64 import sun.awt.SunToolkit;
65
66
67 final class XTextAreaPeer extends XComponentPeer implements TextAreaPeer {
68
69 private final AWTTextPane textPane;
70 private final AWTTextArea jtext;
71 private final boolean firstChangeSkipped;
72
73 private final JavaMouseEventHandler javaMouseEventHandler =
74 new JavaMouseEventHandler(this);
75
76 /**
77 * Create a Text area.
78 */
79 XTextAreaPeer(TextArea target) {
80 super(target);
81
82 // some initializations require that target be set even
83 // though init(target) has not been called
84 this.target = target;
85
86 //ComponentAccessor.enableEvents(target,AWTEvent.MOUSE_WHEEL_EVENT_MASK);
598 JTextComponent comp = getComponent();
599
600 UIDefaults uidefaults = XToolkit.getUIDefaults();
601
602 String prefix = getPropertyPrefix();
603
604 InputMap map = (InputMap)uidefaults.get(prefix + ".focusInputMap");
605
606 if (map != null) {
607 SwingUtilities.replaceUIInputMap(comp, JComponent.WHEN_FOCUSED,
608 map);
609 }
610 }
611
612 @Override
613 protected Caret createCaret() {
614 return new XAWTCaret();
615 }
616 }
617
618
619 static final class XAWTCaret extends DefaultCaret {
620 @Override
621 public void focusGained(FocusEvent e) {
622 super.focusGained(e);
623 if (getComponent().isEnabled()){
624 // Make sure the cursor is visible in case of non-editable TextArea
625 super.setVisible(true);
626 }
627 getComponent().repaint();
628 }
629
630 @Override
631 public void focusLost(FocusEvent e) {
632 super.focusLost(e);
633 getComponent().repaint();
634 }
635
636 // Fix for 5100950: textarea.getSelectedText() returns the de-selected text, on XToolkit
637 // Restoring Motif behaviour
638 // If the text is unhighlighted then we should sets the selection range to zero
639 @Override
640 public void setSelectionVisible(boolean vis) {
641 if (vis){
642 super.setSelectionVisible(vis);
643 }else{
644 // In order to de-select the selection
645 setDot(getDot());
646 }
647 }
648 }
649
650 final class XAWTScrollBarButton extends BasicArrowButton {
651
652 private UIDefaults uidefaults = XToolkit.getUIDefaults();
653 private Color darkShadow = SystemColor.controlShadow;
654 private Color lightShadow = SystemColor.controlLtHighlight;
655 private Color buttonBack = uidefaults.getColor("ScrollBar.track");
656
657 XAWTScrollBarButton(int direction) {
658 super(direction);
659
660 switch (direction) {
661 case NORTH:
662 case SOUTH:
663 case EAST:
664 case WEST:
665 this.direction = direction;
666 break;
667 default:
668 throw new IllegalArgumentException("invalid direction");
669 }
890
891 int w = thumbBounds.width;
892 int h = thumbBounds.height;
893
894 g.translate(thumbBounds.x, thumbBounds.y);
895 g.setColor(thumbColor);
896 g.fillRect(0, 0, w-1, h-1);
897
898 g.setColor(thumbHighlightColor);
899 g.drawLine(0, 0, 0, h-1);
900 g.drawLine(1, 0, w-1, 0);
901
902 g.setColor(thumbLightShadowColor);
903 g.drawLine(1, h-1, w-1, h-1);
904 g.drawLine(w-1, 1, w-1, h-2);
905
906 g.translate(-thumbBounds.x, -thumbBounds.y);
907 }
908 }
909
910 final class AWTTextArea extends JTextArea implements DocumentListener {
911
912 private boolean isFocused = false;
913 private final XTextAreaPeer peer;
914
915 AWTTextArea(String text, XTextAreaPeer peer) {
916 super(text);
917 setFocusable(false);
918 this.peer = peer;
919 }
920
921 @Override
922 public void insertUpdate(DocumentEvent e) {
923 if (peer != null) {
924 peer.postEvent(new TextEvent(peer.target,
925 TextEvent.TEXT_VALUE_CHANGED));
926 }
927 }
928
929 @Override
1088 super.uninstallDefaults(c);
1089
1090 JScrollBar vsb = scrollpane.getVerticalScrollBar();
1091 if (vsb != null) {
1092 if (vsb.getBorder() == vsbBorder) {
1093 vsb.setBorder(null);
1094 }
1095 vsbBorder = null;
1096 }
1097
1098 JScrollBar hsb = scrollpane.getHorizontalScrollBar();
1099 if (hsb != null) {
1100 if (hsb.getBorder() == hsbBorder) {
1101 hsb.setBorder(null);
1102 }
1103 hsbBorder = null;
1104 }
1105 }
1106 }
1107
1108 private class AWTTextPane extends JScrollPane implements FocusListener {
1109
1110 private final JTextArea jtext;
1111 private final XWindow xwin;
1112
1113 private final Color control = SystemColor.control;
1114 private final Color focus = SystemColor.activeCaptionBorder;
1115
1116 AWTTextPane(JTextArea jt, XWindow xwin, Container parent) {
1117 super(jt);
1118 this.xwin = xwin;
1119 setDoubleBuffered(true);
1120 jt.addFocusListener(this);
1121 AWTAccessor.getComponentAccessor().setParent(this,parent);
1122 setViewportBorder(new BevelBorder(false,SystemColor.controlDkShadow,SystemColor.controlLtHighlight) );
1123 this.jtext = jt;
1124 setFocusable(false);
1125 addNotify();
1126 }
1127
1173
1174 @Override
1175 public JScrollBar createVerticalScrollBar() {
1176 return new XAWTScrollBar(JScrollBar.VERTICAL);
1177 }
1178
1179 @Override
1180 public JScrollBar createHorizontalScrollBar() {
1181 return new XAWTScrollBar(JScrollBar.HORIZONTAL);
1182 }
1183
1184 public JTextArea getTextArea () {
1185 return this.jtext;
1186 }
1187
1188 @Override
1189 public Graphics getGraphics() {
1190 return xwin.getGraphics();
1191 }
1192
1193 final class XAWTScrollBar extends ScrollBar {
1194
1195 XAWTScrollBar(int i) {
1196 super(i);
1197 setFocusable(false);
1198 }
1199
1200 @Override
1201 public void updateUI() {
1202 ComponentUI ui = new XAWTScrollBarUI();
1203 setUI(ui);
1204 }
1205 }
1206 }
1207
1208 static class BevelBorder extends AbstractBorder implements UIResource {
1209 private Color darkShadow = SystemColor.controlDkShadow;
1210 private Color lightShadow = SystemColor.controlLtHighlight;
1211 private Color control = SystemColor.controlShadow;
1212 private boolean isRaised;
1213
1214 BevelBorder(boolean isRaised, Color darkShadow, Color lightShadow) {
1215 this.isRaised = isRaised;
1216 this.darkShadow = darkShadow;
1217 this.lightShadow = lightShadow;
1218 }
1219
1220 @Override
1221 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
1222 g.setColor((isRaised) ? lightShadow : darkShadow);
1223 g.drawLine(x, y, x+w-1, y); // top
1224 g.drawLine(x, y+h-1, x, y+1); // left
1225
1226 g.setColor(control);
1227 g.drawLine(x+1, y+1, x+w-2, y+1); // top
|
46 import javax.swing.JButton;
47 import javax.swing.JViewport;
48 import javax.swing.InputMap;
49 import javax.swing.SwingUtilities;
50 import javax.swing.TransferHandler;
51 import javax.swing.plaf.basic.BasicArrowButton;
52 import javax.swing.plaf.basic.BasicScrollBarUI;
53 import javax.swing.plaf.basic.BasicScrollPaneUI;
54 import java.beans.PropertyChangeEvent;
55 import java.beans.PropertyChangeListener;
56 import javax.swing.text.Caret;
57 import javax.swing.text.DefaultCaret;
58 import javax.swing.text.JTextComponent;
59
60 import javax.swing.plaf.BorderUIResource;
61 import java.awt.im.InputMethodRequests;
62 import sun.awt.CausedFocusEvent;
63 import sun.awt.AWTAccessor;
64 import sun.awt.SunToolkit;
65
66 final class XTextAreaPeer extends XComponentPeer implements TextAreaPeer {
67
68 private final AWTTextPane textPane;
69 private final AWTTextArea jtext;
70 private final boolean firstChangeSkipped;
71
72 private final JavaMouseEventHandler javaMouseEventHandler =
73 new JavaMouseEventHandler(this);
74
75 /**
76 * Create a Text area.
77 */
78 XTextAreaPeer(TextArea target) {
79 super(target);
80
81 // some initializations require that target be set even
82 // though init(target) has not been called
83 this.target = target;
84
85 //ComponentAccessor.enableEvents(target,AWTEvent.MOUSE_WHEEL_EVENT_MASK);
597 JTextComponent comp = getComponent();
598
599 UIDefaults uidefaults = XToolkit.getUIDefaults();
600
601 String prefix = getPropertyPrefix();
602
603 InputMap map = (InputMap)uidefaults.get(prefix + ".focusInputMap");
604
605 if (map != null) {
606 SwingUtilities.replaceUIInputMap(comp, JComponent.WHEN_FOCUSED,
607 map);
608 }
609 }
610
611 @Override
612 protected Caret createCaret() {
613 return new XAWTCaret();
614 }
615 }
616
617 @SuppressWarnings("serial") // JDK-implementation class
618 static final class XAWTCaret extends DefaultCaret {
619 @Override
620 public void focusGained(FocusEvent e) {
621 super.focusGained(e);
622 if (getComponent().isEnabled()){
623 // Make sure the cursor is visible in case of non-editable TextArea
624 super.setVisible(true);
625 }
626 getComponent().repaint();
627 }
628
629 @Override
630 public void focusLost(FocusEvent e) {
631 super.focusLost(e);
632 getComponent().repaint();
633 }
634
635 // Fix for 5100950: textarea.getSelectedText() returns the de-selected text, on XToolkit
636 // Restoring Motif behaviour
637 // If the text is unhighlighted then we should sets the selection range to zero
638 @Override
639 public void setSelectionVisible(boolean vis) {
640 if (vis){
641 super.setSelectionVisible(vis);
642 }else{
643 // In order to de-select the selection
644 setDot(getDot());
645 }
646 }
647 }
648
649 @SuppressWarnings("serial") // JDK-implementation class
650 final class XAWTScrollBarButton extends BasicArrowButton {
651
652 private UIDefaults uidefaults = XToolkit.getUIDefaults();
653 private Color darkShadow = SystemColor.controlShadow;
654 private Color lightShadow = SystemColor.controlLtHighlight;
655 private Color buttonBack = uidefaults.getColor("ScrollBar.track");
656
657 XAWTScrollBarButton(int direction) {
658 super(direction);
659
660 switch (direction) {
661 case NORTH:
662 case SOUTH:
663 case EAST:
664 case WEST:
665 this.direction = direction;
666 break;
667 default:
668 throw new IllegalArgumentException("invalid direction");
669 }
890
891 int w = thumbBounds.width;
892 int h = thumbBounds.height;
893
894 g.translate(thumbBounds.x, thumbBounds.y);
895 g.setColor(thumbColor);
896 g.fillRect(0, 0, w-1, h-1);
897
898 g.setColor(thumbHighlightColor);
899 g.drawLine(0, 0, 0, h-1);
900 g.drawLine(1, 0, w-1, 0);
901
902 g.setColor(thumbLightShadowColor);
903 g.drawLine(1, h-1, w-1, h-1);
904 g.drawLine(w-1, 1, w-1, h-2);
905
906 g.translate(-thumbBounds.x, -thumbBounds.y);
907 }
908 }
909
910 @SuppressWarnings("serial") // JDK-implementation class
911 final class AWTTextArea extends JTextArea implements DocumentListener {
912
913 private boolean isFocused = false;
914 private final XTextAreaPeer peer;
915
916 AWTTextArea(String text, XTextAreaPeer peer) {
917 super(text);
918 setFocusable(false);
919 this.peer = peer;
920 }
921
922 @Override
923 public void insertUpdate(DocumentEvent e) {
924 if (peer != null) {
925 peer.postEvent(new TextEvent(peer.target,
926 TextEvent.TEXT_VALUE_CHANGED));
927 }
928 }
929
930 @Override
1089 super.uninstallDefaults(c);
1090
1091 JScrollBar vsb = scrollpane.getVerticalScrollBar();
1092 if (vsb != null) {
1093 if (vsb.getBorder() == vsbBorder) {
1094 vsb.setBorder(null);
1095 }
1096 vsbBorder = null;
1097 }
1098
1099 JScrollBar hsb = scrollpane.getHorizontalScrollBar();
1100 if (hsb != null) {
1101 if (hsb.getBorder() == hsbBorder) {
1102 hsb.setBorder(null);
1103 }
1104 hsbBorder = null;
1105 }
1106 }
1107 }
1108
1109 @SuppressWarnings("serial") // JDK-implementation class
1110 private class AWTTextPane extends JScrollPane implements FocusListener {
1111
1112 private final JTextArea jtext;
1113 private final XWindow xwin;
1114
1115 private final Color control = SystemColor.control;
1116 private final Color focus = SystemColor.activeCaptionBorder;
1117
1118 AWTTextPane(JTextArea jt, XWindow xwin, Container parent) {
1119 super(jt);
1120 this.xwin = xwin;
1121 setDoubleBuffered(true);
1122 jt.addFocusListener(this);
1123 AWTAccessor.getComponentAccessor().setParent(this,parent);
1124 setViewportBorder(new BevelBorder(false,SystemColor.controlDkShadow,SystemColor.controlLtHighlight) );
1125 this.jtext = jt;
1126 setFocusable(false);
1127 addNotify();
1128 }
1129
1175
1176 @Override
1177 public JScrollBar createVerticalScrollBar() {
1178 return new XAWTScrollBar(JScrollBar.VERTICAL);
1179 }
1180
1181 @Override
1182 public JScrollBar createHorizontalScrollBar() {
1183 return new XAWTScrollBar(JScrollBar.HORIZONTAL);
1184 }
1185
1186 public JTextArea getTextArea () {
1187 return this.jtext;
1188 }
1189
1190 @Override
1191 public Graphics getGraphics() {
1192 return xwin.getGraphics();
1193 }
1194
1195 @SuppressWarnings("serial") // JDK-implementation class
1196 final class XAWTScrollBar extends ScrollBar {
1197
1198 XAWTScrollBar(int i) {
1199 super(i);
1200 setFocusable(false);
1201 }
1202
1203 @Override
1204 public void updateUI() {
1205 ComponentUI ui = new XAWTScrollBarUI();
1206 setUI(ui);
1207 }
1208 }
1209 }
1210
1211 @SuppressWarnings("serial") // JDK-implementation class
1212 static class BevelBorder extends AbstractBorder implements UIResource {
1213 private Color darkShadow = SystemColor.controlDkShadow;
1214 private Color lightShadow = SystemColor.controlLtHighlight;
1215 private Color control = SystemColor.controlShadow;
1216 private boolean isRaised;
1217
1218 BevelBorder(boolean isRaised, Color darkShadow, Color lightShadow) {
1219 this.isRaised = isRaised;
1220 this.darkShadow = darkShadow;
1221 this.lightShadow = lightShadow;
1222 }
1223
1224 @Override
1225 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
1226 g.setColor((isRaised) ? lightShadow : darkShadow);
1227 g.drawLine(x, y, x+w-1, y); // top
1228 g.drawLine(x, y+h-1, x, y+1); // left
1229
1230 g.setColor(control);
1231 g.drawLine(x+1, y+1, x+w-2, y+1); // top
|