001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev, Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.awt.event.ActionEvent;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.InputStreamReader;
026: import java.io.OutputStream;
027: import java.io.OutputStreamWriter;
028: import java.io.Reader;
029: import java.io.Writer;
030: import javax.swing.Action;
031:
032: import org.apache.harmony.awt.text.ActionSet;
033:
034: @SuppressWarnings("serial")
035: public class DefaultEditorKit extends EditorKit {
036: private static class KitAction extends TextAction {
037: public KitAction(final String name) {
038: super (name, true);
039: }
040:
041: public void actionPerformed(final ActionEvent e) {
042: performTextAction(e);
043: }
044: }
045:
046: public static class BeepAction extends TextAction {
047: public BeepAction() {
048: super (beepAction, true);
049: }
050:
051: public void actionPerformed(final ActionEvent e) {
052: performTextAction(e);
053: }
054: }
055:
056: public static class CopyAction extends TextAction {
057: public CopyAction() {
058: super (copyAction, true);
059: }
060:
061: public void actionPerformed(final ActionEvent e) {
062: performTextAction(e);
063: }
064: }
065:
066: public static class CutAction extends TextAction {
067: public CutAction() {
068: super (cutAction, true);
069: }
070:
071: public void actionPerformed(final ActionEvent e) {
072: performTextAction(e);
073: }
074: }
075:
076: public static class DefaultKeyTypedAction extends TextAction {
077: public DefaultKeyTypedAction() {
078: super (defaultKeyTypedAction, true);
079: }
080:
081: public void actionPerformed(final ActionEvent event) {
082: if (event == null) {
083: return;
084: }
085: JTextComponent source = getEditableTextComponent(event);
086: String text = event.getActionCommand();
087: if (source != null) {
088: if ((text == null)
089: || Character.getType(text.charAt(0)) != Character.CONTROL
090: || (text.charAt(0) > 127)) {
091:
092: source.replaceSelection(text);
093: setCurrentPositionAsMagic(source);
094: }
095: }
096: }
097: }
098:
099: public static class InsertBreakAction extends TextAction {
100: public InsertBreakAction() {
101: super (insertBreakAction, true);
102: }
103:
104: public void actionPerformed(final ActionEvent e) {
105: performTextAction(e);
106: }
107: }
108:
109: public static class InsertContentAction extends TextAction {
110: public InsertContentAction() {
111: super (insertContentAction, true);
112: }
113:
114: public void actionPerformed(final ActionEvent event) {
115: if (event == null) {
116: return;
117: }
118: final JTextComponent c = getEditableTextComponent(event);
119: if (c != null) {
120: final String content = event.getActionCommand();
121: if (content != null) {
122: c.replaceSelection(content);
123: }
124: }
125: }
126: }
127:
128: public static class InsertTabAction extends TextAction {
129: public InsertTabAction() {
130: super (insertTabAction, true);
131: }
132:
133: public void actionPerformed(final ActionEvent e) {
134: performTextAction(e);
135: }
136: }
137:
138: public static class PasteAction extends TextAction {
139: public PasteAction() {
140: super (pasteAction, true);
141: }
142:
143: public void actionPerformed(final ActionEvent e) {
144: performTextAction(e);
145: }
146: }
147:
148: static final class ReadOnlyAction extends TextAction {
149: public ReadOnlyAction() {
150: super (readOnlyAction, true);
151: }
152:
153: public void actionPerformed(final ActionEvent event) {
154: JTextComponent source = getEditableTextComponent(event);
155: if (source != null) {
156: source.setEditable(false);
157: }
158: }
159: }
160:
161: static final class WritableAction extends TextAction {
162:
163: public WritableAction() {
164: super (writableAction, true);
165: }
166:
167: public void actionPerformed(final ActionEvent event) {
168: JTextComponent source = getTextComponent(event);
169: if (source != null) {
170: source.setEditable(true);
171: }
172: }
173:
174: }
175:
176: public static final String backwardAction = ActionSet.backwardAction;
177:
178: public static final String beepAction = ActionSet.beepAction;
179:
180: public static final String beginAction = ActionSet.beginAction;
181:
182: public static final String beginLineAction = ActionSet.beginLineAction;
183:
184: public static final String beginParagraphAction = ActionSet.beginParagraphAction;
185:
186: public static final String beginWordAction = ActionSet.beginWordAction;
187:
188: public static final String copyAction = ActionSet.copyAction;
189:
190: public static final String cutAction = ActionSet.cutAction;
191:
192: public static final String defaultKeyTypedAction = ActionSet.defaultKeyTypedAction;
193:
194: public static final String deleteNextCharAction = ActionSet.deleteNextCharAction;
195:
196: public static final String deletePrevCharAction = ActionSet.deletePrevCharAction;
197:
198: public static final String downAction = ActionSet.downAction;
199:
200: static final String dumpModelAction = ActionSet.dumpModelAction;
201:
202: public static final String endAction = ActionSet.endAction;
203:
204: public static final String endLineAction = ActionSet.endLineAction;
205:
206: public static final String EndOfLineStringProperty = "__EndOfLine__";
207:
208: public static final String endParagraphAction = ActionSet.endParagraphAction;
209:
210: public static final String endWordAction = ActionSet.endWordAction;
211:
212: public static final String forwardAction = ActionSet.forwardAction;
213:
214: public static final String insertBreakAction = ActionSet.insertBreakAction;
215:
216: public static final String insertContentAction = ActionSet.insertContentAction;
217:
218: public static final String insertTabAction = ActionSet.insertTabAction;
219:
220: public static final String nextWordAction = ActionSet.nextWordAction;
221:
222: public static final String pageDownAction = ActionSet.pageDownAction;
223:
224: public static final String pageUpAction = ActionSet.pageUpAction;
225:
226: public static final String pasteAction = ActionSet.pasteAction;
227:
228: public static final String previousWordAction = ActionSet.previousWordAction;
229:
230: public static final String readOnlyAction = ActionSet.readOnlyAction;
231:
232: public static final String selectAllAction = ActionSet.selectAllAction;
233:
234: public static final String selectionBackwardAction = ActionSet.selectionBackwardAction;
235:
236: public static final String selectionBeginAction = ActionSet.selectionBeginAction;
237:
238: public static final String selectionBeginLineAction = ActionSet.selectionBeginLineAction;
239:
240: public static final String selectionBeginParagraphAction = ActionSet.selectionBeginParagraphAction;
241:
242: public static final String selectionBeginWordAction = ActionSet.selectionBeginWordAction;
243:
244: public static final String selectionDownAction = ActionSet.selectionDownAction;
245:
246: public static final String selectionEndAction = ActionSet.selectionEndAction;
247:
248: public static final String selectionEndLineAction = ActionSet.selectionEndLineAction;
249:
250: public static final String selectionEndParagraphAction = ActionSet.selectionEndParagraphAction;
251:
252: public static final String selectionEndWordAction = ActionSet.selectionEndWordAction;
253:
254: public static final String selectionForwardAction = ActionSet.selectionForwardAction;
255:
256: public static final String selectionNextWordAction = ActionSet.selectionNextWordAction;
257:
258: static final String selectionPageDownAction = ActionSet.selectionPageDownAction;
259:
260: static final String selectionPageLeftAction = ActionSet.selectionPageLeftAction;
261:
262: static final String selectionPageRightAction = ActionSet.selectionPageRightAction;
263:
264: static final String selectionPageUpAction = ActionSet.selectionPageUpAction;
265:
266: public static final String selectionPreviousWordAction = ActionSet.selectionPreviousWordAction;
267:
268: public static final String selectionUpAction = ActionSet.selectionUpAction;
269:
270: public static final String selectLineAction = ActionSet.selectLineAction;
271:
272: public static final String selectParagraphAction = ActionSet.selectParagraphAction;
273:
274: public static final String selectWordAction = ActionSet.selectWordAction;
275:
276: static final String toggleComponentOrientationAction = ActionSet.toggleComponentOrientationAction;
277:
278: static final String unselectAction = ActionSet.unselectAction;
279:
280: public static final String upAction = ActionSet.upAction;
281:
282: public static final String writableAction = ActionSet.writableAction;
283:
284: static final TextAction selectWordDoing = new KitAction(
285: selectWordAction);
286: static final TextAction selectLineDoing = new KitAction(
287: selectLineAction);
288:
289: private static final int CHARACTERS_TO_READ_AT_ONCE = 256;
290: private static final String CONTENT_TYPE = "text/plain";
291:
292: /**
293: * array containing all actions that DEK provides by default
294: * it is shared by all instances of DEK
295: */
296: private static Action[] actions;
297:
298: @Override
299: public Caret createCaret() {
300: return null;
301: }
302:
303: @Override
304: public Document createDefaultDocument() {
305: return new PlainDocument();
306: }
307:
308: @Override
309: public Action[] getActions() {
310: if (actions == null) {
311: initActions();
312: }
313: return actions.clone();
314: }
315:
316: @Override
317: public String getContentType() {
318: return CONTENT_TYPE;
319: }
320:
321: @Override
322: public ViewFactory getViewFactory() {
323: return null;
324: }
325:
326: @Override
327: public void read(final InputStream in, final Document doc,
328: final int pos) throws IOException, BadLocationException {
329: read(new InputStreamReader(in), doc, pos);
330: }
331:
332: @Override
333: public void read(final Reader in, final Document doc, final int pos)
334: throws IOException, BadLocationException {
335: if (!in.ready()) {
336: return;
337: }
338: int maxCharToRead = CHARACTERS_TO_READ_AT_ONCE;
339: char[] readArray = new char[maxCharToRead];
340: int numCharRead = -1;
341: AttributeSet attributes = doc.getDefaultRootElement()
342: .getAttributes();
343: int offset = pos;
344: boolean delimiterInitialised = false;
345: doc.putProperty(EndOfLineStringProperty, null);
346: while ((numCharRead = in.read(readArray, 0, maxCharToRead)) != -1) {
347: String readStr = new String(readArray, 0, numCharRead);
348: if (!delimiterInitialised) {
349: final String lineDelimeter = checkDelimiters(readStr);
350: if (lineDelimeter != null) {
351: doc.putProperty(EndOfLineStringProperty,
352: lineDelimeter);
353: delimiterInitialised = true;
354: }
355: }
356: if (delimiterInitialised) {
357: readStr = replaceLineDelimiters(readStr);
358: }
359: doc.insertString(offset, readStr, attributes);
360: offset += readStr.length();
361: }
362: }
363:
364: @Override
365: public void write(final OutputStream out, final Document doc,
366: final int pos, final int len) throws IOException,
367: BadLocationException {
368: write(new OutputStreamWriter(out), doc, pos, len);
369: }
370:
371: @Override
372: public void write(final Writer out, final Document doc,
373: final int pos, final int len) throws IOException,
374: BadLocationException {
375: String writeStr = doc.getText(pos, len);
376: String newLine = (String) doc
377: .getProperty(EndOfLineStringProperty);
378: if (newLine != null) {
379: writeStr = writeStr.replaceAll("\n", (String) doc
380: .getProperty(EndOfLineStringProperty));
381: }
382: out.write(writeStr);
383: out.flush();
384: }
385:
386: private String checkDelimiters(final String str) {
387: String lineDelimeter = null;
388: final int length = str.length();
389: for (int i = 0; i < length; i++) {
390: char c = str.charAt(i);
391: if (c == '\n') {
392: lineDelimeter = "\n";
393: break;
394: } else if (c == '\r') {
395: if (i + 1 < length && str.charAt(i + 1) == '\n') {
396: lineDelimeter = "\r\n";
397: } else {
398: lineDelimeter = "\r";
399: }
400: break;
401: }
402: }
403: return lineDelimeter;
404: }
405:
406: private String replaceLineDelimiters(final String str) {
407: int index = str.indexOf('\r');
408: if (index == -1) {
409: return str;
410: }
411: final int length = str.length();
412: final StringBuffer buffer = new StringBuffer(length);
413: int prevIndex = 0;
414: do {
415: buffer.append(str.subSequence(prevIndex, index));
416: buffer.append('\n');
417: if (index + 1 < length && str.charAt(index + 1) == '\n') {
418: index++;
419: }
420: prevIndex = index + 1;
421: index = str.indexOf('\r', prevIndex);
422: } while (index != -1);
423: buffer.append(str.subSequence(prevIndex, length));
424: return buffer.toString();
425: }
426:
427: /*
428: * TODO Simplify this construction.
429: * 1) May be store indexes as in original version
430: * 2) Find solution how to get these TextAction's from AWTTextAction
431: */
432: private void initActions() {
433: actions = new Action[] { new InsertContentAction(),
434: new KitAction(deletePrevCharAction),
435: new KitAction(deleteNextCharAction),
436: new ReadOnlyAction(), new WritableAction(),
437: new CutAction(), new CopyAction(), new PasteAction(),
438: new KitAction(pageUpAction),
439: new KitAction(pageDownAction),
440: new KitAction(selectionPageUpAction),
441: new KitAction(selectionPageDownAction),
442: new KitAction(selectionPageLeftAction),
443: new KitAction(selectionPageRightAction),
444: new InsertBreakAction(), new BeepAction(),
445: new KitAction(forwardAction),
446: new KitAction(backwardAction),
447: new KitAction(selectionForwardAction),
448: new KitAction(selectionBackwardAction),
449: new KitAction(upAction), new KitAction(downAction),
450: new KitAction(selectionUpAction),
451: new KitAction(selectionDownAction),
452: new KitAction(beginWordAction),
453: new KitAction(selectionBeginWordAction),
454: new KitAction(endAction),
455: new KitAction(selectionEndAction),
456: new KitAction(endParagraphAction),
457: new KitAction(selectionEndParagraphAction),
458: new KitAction(endLineAction),
459: new KitAction(selectionEndLineAction),
460: new KitAction(endWordAction),
461: new KitAction(selectionEndWordAction),
462: new KitAction(previousWordAction),
463: new KitAction(selectionPreviousWordAction),
464: new KitAction(nextWordAction),
465: new KitAction(selectionNextWordAction),
466: new KitAction(beginLineAction),
467: new KitAction(selectionBeginLineAction),
468: new KitAction(beginParagraphAction),
469: new KitAction(selectionBeginParagraphAction),
470: new KitAction(beginAction),
471: new KitAction(selectionBeginAction),
472: new DefaultKeyTypedAction(), new InsertTabAction(),
473: selectWordDoing, selectLineDoing,
474: new KitAction(selectParagraphAction),
475: new KitAction(selectAllAction),
476: new KitAction(unselectAction),
477: new KitAction(toggleComponentOrientationAction),
478: new KitAction(dumpModelAction) };
479: }
480: }
|