< 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                             // Variation Selector and base char should be deleted.
1077                             if ((dot > 2 && isVariationSelector(c0, c1)) ||
1078                                 (isVariationSelector(c1))) {
1079                                 String targetChars = doc.getText(0, dot);
1080                                 int pre = targetChars.codePointBefore(dot - delChars);
1081                                 if (isBaseChar(pre)){
1082                                     if (pre >= 0x10000){
1083                                         delChars += 2;
1084                                     }else{
1085                                         delChars += 1;
1086                                     }
1087                                 }
1088                             }
1089                         }
1090 
1091                         doc.remove(dot - delChars, delChars);
1092                         beep = false;
1093                     }
1094                 } catch (BadLocationException bl) {
1095                 }
1096             }
1097             if (beep) {
1098                 UIManager.getLookAndFeel().provideErrorFeedback(target);
1099             }
1100         }
1101     }
1102 
1103     /*
1104      * Deletes the character of content that follows the
1105      * current caret position.
1106      * @see DefaultEditorKit#deleteNextCharAction
1107      * @see DefaultEditorKit#getActions
1108      */


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


< prev index next >