001: /*
002: * InsertTagDialog.java
003: *
004: * Copyright (C) 2000-2004 Peter Graves
005: * $Id: InsertTagDialog.java,v 1.3 2004/04/22 14:55:47 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.util.ArrayList;
025: import java.util.List;
026: import javax.swing.undo.CompoundEdit;
027:
028: public final class InsertTagDialog extends InputDialog implements
029: Constants {
030: public InsertTagDialog(Editor editor) {
031: super (editor, "Element:", "Insert Element", null);
032: setHistory(new History("insertTag"));
033: }
034:
035: protected List getCompletions(String prefix) {
036: prefix = prefix.toLowerCase();
037: List elements = HtmlMode.elements();
038: int limit = elements.size();
039: ArrayList completions = new ArrayList(limit);
040: for (int i = 0; i < limit; i++) {
041: HtmlElement element = (HtmlElement) elements.get(i);
042: if (element.getName().startsWith(prefix))
043: completions.add(element.getName());
044: }
045: return completions;
046: }
047:
048: public static void insertTag(Editor editor, String tagName,
049: String extra, boolean wantEndTag) {
050: if (extra == null)
051: extra = "";
052: final Buffer buffer = editor.getBuffer();
053: if (buffer.getBooleanProperty(Property.FIX_CASE)) {
054: if (buffer
055: .getBooleanProperty(Property.UPPER_CASE_TAG_NAMES))
056: tagName = tagName.toUpperCase();
057: else
058: tagName = tagName.toLowerCase();
059: }
060: Region r = null;
061: if (editor.getMark() != null)
062: r = new Region(editor);
063: else {
064: // No selection.
065: final Position dot = editor.getDot();
066: final Line dotLine = dot.getLine();
067: final int offset = dot.getOffset();
068: if (offset > 0 && offset < dotLine.length()) {
069: char before = dotLine.charAt(offset - 1);
070: char after = dotLine.charAt(offset);
071: if (!Character.isWhitespace(before)
072: && !Character.isWhitespace(after)) {
073: int begin = offset;
074: char c;
075: while (begin > 0
076: && !Character.isWhitespace(c = dotLine
077: .charAt(begin - 1))) {
078: if (c == '>')
079: break;
080: --begin;
081: }
082: int end = offset;
083: while (end < dotLine.length()
084: && !Character.isWhitespace(c = dotLine
085: .charAt(end))) {
086: if (",.<".indexOf(c) >= 0)
087: break;
088: ++end;
089: }
090: if (begin != end)
091: r = new Region(buffer, new Position(dotLine,
092: begin), new Position(dotLine, end));
093: }
094: }
095: }
096: if (r != null) {
097: boolean wantNewLine = false;
098: if (r.getBeginOffset() == 0 && r.getEndOffset() == 0)
099: wantNewLine = true;
100: final int offset = editor.getDotOffset();
101: CompoundEdit compoundEdit = editor.beginCompoundEdit();
102: if (wantEndTag) {
103: editor.moveDotTo(r.getEnd());
104: editor.addUndo(SimpleEdit.INSERT_STRING);
105: editor.insertStringInternal("</" + tagName + ">");
106: Editor.updateInAllEditors(editor.getDotLine());
107: if (wantNewLine) {
108: editor.addUndo(SimpleEdit.INSERT_LINE_SEP);
109: editor.insertLineSeparator();
110: }
111: }
112: editor.moveDotTo(r.getBegin());
113: editor.addUndo(SimpleEdit.INSERT_STRING);
114: String insert = "<" + tagName + extra + ">";
115: editor.insertStringInternal(insert);
116: Editor.updateInAllEditors(editor.getDotLine());
117: if (wantNewLine) {
118: Line startTagLine = editor.getDotLine();
119: editor.addUndo(SimpleEdit.INSERT_LINE_SEP);
120: editor.insertLineSeparator();
121: if (buffer.getBooleanProperty(Property.AUTO_INDENT)) {
122: // Re-indent.
123: // Move dot to start of region.
124: editor.addUndo(SimpleEdit.MOVE);
125: editor.getDot().moveTo(startTagLine, 0);
126: Position dot = editor.getDot();
127: while (dot.getLine() != null
128: && dot.getLine() != r.getEndLine().next()) {
129: editor.indentLine();
130: dot.moveTo(dot.getNextLine(), 0);
131: }
132: }
133: // Put dot before '>' of start tag.
134: editor.moveDotTo(startTagLine,
135: startTagLine.length() - 1);
136: } else {
137: int newOffset;
138: if (extra.endsWith(" "))
139: newOffset = editor.getDotOffset() - 1;
140: else
141: newOffset = offset + insert.length();
142: editor.moveDotTo(editor.getDotLine(), newOffset);
143: }
144: editor.endCompoundEdit(compoundEdit);
145: } else {
146: CompoundEdit compoundEdit = editor.beginCompoundEdit();
147: editor.fillToCaret();
148: final int offset = editor.getDotOffset();
149: editor.addUndo(SimpleEdit.INSERT_STRING);
150: if (wantEndTag)
151: editor.insertStringInternal("<" + tagName + extra
152: + "></" + tagName + ">");
153: else
154: editor
155: .insertStringInternal("<" + tagName + extra
156: + ">");
157: Editor.updateInAllEditors(editor.getDotLine());
158: editor.addUndo(SimpleEdit.MOVE);
159: editor.getDot().setOffset(
160: offset + 1 + tagName.length() + extra.length());
161: editor.moveCaretToDotCol();
162: editor.endCompoundEdit(compoundEdit);
163: }
164: }
165: }
|