| 1 | /*
|
|---|
| 2 | * Copyright 2006-2008 Kees de Kooter
|
|---|
| 3 | *
|
|---|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
|---|
| 5 | * you may not use this file except in compliance with the License.
|
|---|
| 6 | * You may obtain a copy of the License at
|
|---|
| 7 | *
|
|---|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
|---|
| 9 | *
|
|---|
| 10 | * Unless required by applicable law or agreed to in writing, software
|
|---|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
|---|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|---|
| 13 | * See the License for the specific language governing permissions and
|
|---|
| 14 | * limitations under the License.
|
|---|
| 15 | */
|
|---|
| 16 | package net.boplicity.xmleditor;
|
|---|
| 17 |
|
|---|
| 18 | import java.awt.event.KeyEvent;
|
|---|
| 19 | import java.awt.event.KeyListener;
|
|---|
| 20 | import java.util.logging.Level;
|
|---|
| 21 | import java.util.logging.Logger;
|
|---|
| 22 |
|
|---|
| 23 | import javax.swing.JTextPane;
|
|---|
| 24 | import javax.swing.text.BadLocationException;
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * JTextPane implementation that can handle xml text. The IndentKeyListener
|
|---|
| 28 | * implements smart indenting.
|
|---|
| 29 | *
|
|---|
| 30 | * @author kees
|
|---|
| 31 | */
|
|---|
| 32 | public class XmlTextPane extends JTextPane {
|
|---|
| 33 |
|
|---|
| 34 | private static final Logger LOGGER = Logger.getLogger(XmlTextPane.class.getName());
|
|---|
| 35 | private static final Character NEW_LINE = '\n';
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * Constructs a new {@code XmlTextPane}.
|
|---|
| 39 | */
|
|---|
| 40 | public XmlTextPane() {
|
|---|
| 41 |
|
|---|
| 42 | // Set editor kit
|
|---|
| 43 | this.setEditorKitForContentType("text/xml", new XmlEditorKit());
|
|---|
| 44 | this.setContentType("text/xml");
|
|---|
| 45 |
|
|---|
| 46 | addKeyListener(new IndentKeyListener());
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | private class IndentKeyListener implements KeyListener {
|
|---|
| 50 |
|
|---|
| 51 | private boolean enterFlag;
|
|---|
| 52 |
|
|---|
| 53 | @Override
|
|---|
| 54 | public void keyPressed(KeyEvent event) {
|
|---|
| 55 | enterFlag = false;
|
|---|
| 56 | if ((event.getKeyCode() == KeyEvent.VK_ENTER)
|
|---|
| 57 | && (event.getModifiers() == 0)
|
|---|
| 58 | && getSelectionStart() == getSelectionEnd()) {
|
|---|
| 59 | enterFlag = true;
|
|---|
| 60 | event.consume();
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | @Override
|
|---|
| 65 | public void keyReleased(KeyEvent event) {
|
|---|
| 66 | if ((event.getKeyCode() == KeyEvent.VK_ENTER)
|
|---|
| 67 | && (event.getModifiers() == 0) && enterFlag) {
|
|---|
| 68 | event.consume();
|
|---|
| 69 |
|
|---|
| 70 | int start, end;
|
|---|
| 71 | String text = getText();
|
|---|
| 72 |
|
|---|
| 73 | int caretPosition = getCaretPosition();
|
|---|
| 74 | try {
|
|---|
| 75 | if (text.charAt(caretPosition) == NEW_LINE) {
|
|---|
| 76 | caretPosition--;
|
|---|
| 77 | }
|
|---|
| 78 | } catch (IndexOutOfBoundsException e) {
|
|---|
| 79 | LOGGER.log(Level.FINE, e.toString());
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | start = text.lastIndexOf(NEW_LINE, caretPosition) + 1;
|
|---|
| 83 | end = start;
|
|---|
| 84 | try {
|
|---|
| 85 | if (text.charAt(start) != NEW_LINE) {
|
|---|
| 86 | while (end < text.length()
|
|---|
| 87 | && Character.isWhitespace(text.charAt(end))
|
|---|
| 88 | && text.charAt(end) != NEW_LINE) {
|
|---|
| 89 | end++;
|
|---|
| 90 | }
|
|---|
| 91 | if (end > start) {
|
|---|
| 92 | getDocument()
|
|---|
| 93 | .insertString(
|
|---|
| 94 | getCaretPosition(),
|
|---|
| 95 | NEW_LINE
|
|---|
| 96 | + text.substring(start,
|
|---|
| 97 | end), null);
|
|---|
| 98 | } else {
|
|---|
| 99 | getDocument().insertString(getCaretPosition(),
|
|---|
| 100 | NEW_LINE.toString(), null);
|
|---|
| 101 | }
|
|---|
| 102 | } else {
|
|---|
| 103 | getDocument().insertString(getCaretPosition(),
|
|---|
| 104 | NEW_LINE.toString(), null);
|
|---|
| 105 | }
|
|---|
| 106 | } catch (IndexOutOfBoundsException e) {
|
|---|
| 107 | try {
|
|---|
| 108 | getDocument().insertString(getCaretPosition(),
|
|---|
| 109 | NEW_LINE.toString(), null);
|
|---|
| 110 | } catch (BadLocationException e1) {
|
|---|
| 111 | LOGGER.log(Level.WARNING, e1.toString());
|
|---|
| 112 | }
|
|---|
| 113 | } catch (BadLocationException e) {
|
|---|
| 114 | LOGGER.log(Level.WARNING, e.toString());
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | @Override
|
|---|
| 120 | public void keyTyped(KeyEvent e) {
|
|---|
| 121 | // Do nothing
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|