< prev index next >

src/java.desktop/share/classes/javax/swing/text/DefaultEditorKit.java

Print this page




  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.text;
  26 
  27 import sun.awt.SunToolkit;
  28 
  29 import java.io.*;
  30 import java.awt.*;
  31 import java.awt.event.ActionEvent;
  32 import java.text.*;
  33 import javax.swing.Action;
  34 import javax.swing.KeyStroke;
  35 import javax.swing.SwingConstants;
  36 import javax.swing.UIManager;


  37 
  38 /**
  39  * This is the set of things needed by a text component
  40  * to be a reasonably functioning editor for some <em>type</em>
  41  * of text document.  This implementation provides a default
  42  * implementation which treats text as plain text and
  43  * provides a minimal set of actions for a simple editor.
  44  *
  45  * <dl>
  46  * <dt><b>Newlines</b>
  47  * <dd>
  48  * There are two properties which deal with newlines.  The
  49  * system property, <code>line.separator</code>, is defined to be
  50  * platform-dependent, either "\n", "\r", or "\r\n".  There is also
  51  * a property defined in <code>DefaultEditorKit</code>, called
  52  * <a href=#EndOfLineStringProperty><code>EndOfLineStringProperty</code></a>,
  53  * which is defined automatically when a document is loaded, to be
  54  * the first occurrence of any of the newline characters.
  55  * When a document is loaded, <code>EndOfLineStringProperty</code>
  56  * is set appropriately, and when the document is written back out, the


1053                 try {
1054                     Document doc = target.getDocument();
1055                     Caret caret = target.getCaret();
1056                     int dot = caret.getDot();
1057                     int mark = caret.getMark();
1058                     if (dot != mark) {
1059                         doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
1060                         beep = false;
1061                     } else if (dot > 0) {
1062                         int delChars = 1;
1063 
1064                         if (dot > 1) {
1065                             String dotChars = doc.getText(dot - 2, 2);
1066                             char c0 = dotChars.charAt(0);
1067                             char c1 = dotChars.charAt(1);
1068 
1069                             if (c0 >= '\uD800' && c0 <= '\uDBFF' &&
1070                                 c1 >= '\uDC00' && c1 <= '\uDFFF') {
1071                                 delChars = 2;
1072                             }















1073                         }
1074 
1075                         doc.remove(dot - delChars, delChars);
1076                         beep = false;
1077                     }
1078                 } catch (BadLocationException bl) {
1079                 }
1080             }
1081             if (beep) {
1082                 UIManager.getLookAndFeel().provideErrorFeedback(target);
1083             }
1084         }
1085     }
1086 
1087     /*
1088      * Deletes the character of content that follows the
1089      * current caret position.
1090      * @see DefaultEditorKit#deleteNextCharAction
1091      * @see DefaultEditorKit#getActions
1092      */


1101         /** The operation to perform when this action is triggered. */
1102         public void actionPerformed(ActionEvent e) {
1103             JTextComponent target = getTextComponent(e);
1104             boolean beep = true;
1105             if ((target != null) && (target.isEditable())) {
1106                 try {
1107                     Document doc = target.getDocument();
1108                     Caret caret = target.getCaret();
1109                     int dot = caret.getDot();
1110                     int mark = caret.getMark();
1111                     if (dot != mark) {
1112                         doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
1113                         beep = false;
1114                     } else if (dot < doc.getLength()) {
1115                         int delChars = 1;
1116 
1117                         if (dot < doc.getLength() - 1) {
1118                             String dotChars = doc.getText(dot, 2);
1119                             char c0 = dotChars.charAt(0);
1120                             char c1 = dotChars.charAt(1);

1121 
1122                             if (c0 >= '\uD800' && c0 <= '\uDBFF' &&
1123                                 c1 >= '\uDC00' && c1 <= '\uDFFF') {
1124                                 delChars = 2;




















1125                             }
1126                         }
1127 
1128                         doc.remove(dot, delChars);
1129                         beep = false;
1130                     }
1131                 } catch (BadLocationException bl) {
1132                 }
1133             }
1134             if (beep) {
1135                 UIManager.getLookAndFeel().provideErrorFeedback(target);
1136             }
1137         }
1138     }
1139 
1140 
1141     /*
1142      * Deletes the word that precedes/follows the beginning of the selection.
1143      * @see DefaultEditorKit#getActions
1144      */




  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.text;
  26 
  27 import sun.awt.SunToolkit;
  28 
  29 import java.io.*;
  30 import java.awt.*;
  31 import java.awt.event.ActionEvent;
  32 import java.text.*;
  33 import javax.swing.Action;
  34 import javax.swing.KeyStroke;
  35 import javax.swing.SwingConstants;
  36 import javax.swing.UIManager;
  37 import static sun.font.CharToGlyphMapper.isVariationSelector;
  38 import static sun.font.CharToGlyphMapper.isBaseChar;
  39 
  40 /**
  41  * This is the set of things needed by a text component
  42  * to be a reasonably functioning editor for some <em>type</em>
  43  * of text document.  This implementation provides a default
  44  * implementation which treats text as plain text and
  45  * provides a minimal set of actions for a simple editor.
  46  *
  47  * <dl>
  48  * <dt><b>Newlines</b>
  49  * <dd>
  50  * There are two properties which deal with newlines.  The
  51  * system property, <code>line.separator</code>, is defined to be
  52  * platform-dependent, either "\n", "\r", or "\r\n".  There is also
  53  * a property defined in <code>DefaultEditorKit</code>, called
  54  * <a href=#EndOfLineStringProperty><code>EndOfLineStringProperty</code></a>,
  55  * which is defined automatically when a document is loaded, to be
  56  * the first occurrence of any of the newline characters.
  57  * When a document is loaded, <code>EndOfLineStringProperty</code>
  58  * is set appropriately, and when the document is written back out, the


1055                 try {
1056                     Document doc = target.getDocument();
1057                     Caret caret = target.getCaret();
1058                     int dot = caret.getDot();
1059                     int mark = caret.getMark();
1060                     if (dot != mark) {
1061                         doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
1062                         beep = false;
1063                     } else if (dot > 0) {
1064                         int delChars = 1;
1065 
1066                         if (dot > 1) {
1067                             String dotChars = doc.getText(dot - 2, 2);
1068                             char c0 = dotChars.charAt(0);
1069                             char c1 = dotChars.charAt(1);
1070 
1071                             if (c0 >= '\uD800' && c0 <= '\uDBFF' &&
1072                                 c1 >= '\uDC00' && c1 <= '\uDFFF') {
1073                                 delChars = 2;
1074                             }
1075 
1076                             // For Variation Selector
1077                             if ((dot > 2 && isVariationSelector(c0, c1)) ||
1078                                 (isVariationSelector(c1))) {
1079                                 String targetChars = doc.getText(0, dot);
1080                                 int pre = targetChars.codePointBefore(
1081                                                   dot - delChars);
1082                                 if (isBaseChar(pre)) {
1083                                     if (pre >= 0x10000) {
1084                                         delChars += 2;
1085                                     } else {
1086                                         delChars += 1;
1087                                     }
1088                                 }
1089                             }
1090                         }
1091 
1092                         doc.remove(dot - delChars, delChars);
1093                         beep = false;
1094                     }
1095                 } catch (BadLocationException bl) {
1096                 }
1097             }
1098             if (beep) {
1099                 UIManager.getLookAndFeel().provideErrorFeedback(target);
1100             }
1101         }
1102     }
1103 
1104     /*
1105      * Deletes the character of content that follows the
1106      * current caret position.
1107      * @see DefaultEditorKit#deleteNextCharAction
1108      * @see DefaultEditorKit#getActions
1109      */


1118         /** The operation to perform when this action is triggered. */
1119         public void actionPerformed(ActionEvent e) {
1120             JTextComponent target = getTextComponent(e);
1121             boolean beep = true;
1122             if ((target != null) && (target.isEditable())) {
1123                 try {
1124                     Document doc = target.getDocument();
1125                     Caret caret = target.getCaret();
1126                     int dot = caret.getDot();
1127                     int mark = caret.getMark();
1128                     if (dot != mark) {
1129                         doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
1130                         beep = false;
1131                     } else if (dot < doc.getLength()) {
1132                         int delChars = 1;
1133 
1134                         if (dot < doc.getLength() - 1) {
1135                             String dotChars = doc.getText(dot, 2);
1136                             char c0 = dotChars.charAt(0);
1137                             char c1 = dotChars.charAt(1);
1138                             int baseChar = (int)c0;
1139 
1140                             if (c0 >= '\uD800' && c0 <= '\uDBFF' &&
1141                                 c1 >= '\uDC00' && c1 <= '\uDFFF') {
1142                                 delChars = 2;
1143                                 baseChar = (c0 - 0xD800) * 0x400 + c1 - 0xDC00
1144                                            + 0x10000;
1145                             }
1146 
1147                             // For Variation Selector
1148                             if (isBaseChar(baseChar) &&
1149                                 dot < doc.getLength() - delChars) {
1150                                 String nextChar = doc.getText(dot+delChars, 1);
1151                                 char c2 = nextChar.charAt(0);
1152                                 if (isVariationSelector(c2)) {
1153                                     delChars += 1;
1154                                 } else if (dot < doc.getLength()
1155                                                      - delChars - 1) {
1156                                     nextChar = doc.getText(
1157                                                        dot + delChars + 1, 1);
1158                                     char c3 = nextChar.charAt(0);
1159                                     if (isVariationSelector(c2,c3)) {
1160                                         delChars += 2;
1161                                     }
1162                                 }
1163                             }
1164                         }
1165 
1166                         doc.remove(dot, delChars);
1167                         beep = false;
1168                     }
1169                 } catch (BadLocationException bl) {
1170                 }
1171             }
1172             if (beep) {
1173                 UIManager.getLookAndFeel().provideErrorFeedback(target);
1174             }
1175         }
1176     }
1177 
1178 
1179     /*
1180      * Deletes the word that precedes/follows the beginning of the selection.
1181      * @see DefaultEditorKit#getActions
1182      */


< prev index next >