KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > InsertTagDialog


1 /*
2  * InsertTagDialog.java
3  *
4  * Copyright (C) 2000-2004 Peter Graves
5  * $Id: InsertTagDialog.java,v 1.3 2004/04/22 14:55:47 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.undo.CompoundEdit JavaDoc;
27
28 public final class InsertTagDialog extends InputDialog implements Constants
29 {
30     public InsertTagDialog(Editor editor)
31     {
32         super(editor, "Element:", "Insert Element", null);
33         setHistory(new History("insertTag"));
34     }
35
36     protected List JavaDoc getCompletions(String JavaDoc prefix)
37     {
38         prefix = prefix.toLowerCase();
39         List JavaDoc elements = HtmlMode.elements();
40         int limit = elements.size();
41         ArrayList JavaDoc completions = new ArrayList JavaDoc(limit);
42         for (int i = 0; i < limit; i++) {
43             HtmlElement element = (HtmlElement) elements.get(i);
44             if (element.getName().startsWith(prefix))
45                 completions.add(element.getName());
46         }
47         return completions;
48     }
49
50     public static void insertTag(Editor editor, String JavaDoc tagName, String JavaDoc extra,
51                                  boolean wantEndTag)
52     {
53         if (extra == null)
54             extra = "";
55         final Buffer buffer = editor.getBuffer();
56         if (buffer.getBooleanProperty(Property.FIX_CASE)) {
57             if (buffer.getBooleanProperty(Property.UPPER_CASE_TAG_NAMES))
58                 tagName = tagName.toUpperCase();
59             else
60                 tagName = tagName.toLowerCase();
61         }
62         Region r = null;
63         if (editor.getMark() != null)
64             r = new Region(editor);
65         else {
66             // No selection.
67
final Position dot = editor.getDot();
68             final Line dotLine = dot.getLine();
69             final int offset = dot.getOffset();
70             if (offset > 0 && offset < dotLine.length()) {
71                 char before = dotLine.charAt(offset-1);
72                 char after = dotLine.charAt(offset);
73                 if (!Character.isWhitespace(before) && !Character.isWhitespace(after)) {
74                     int begin = offset;
75                     char c;
76                     while (begin > 0 && !Character.isWhitespace(c = dotLine.charAt(begin-1))) {
77                         if (c == '>')
78                             break;
79                         --begin;
80                     }
81                     int end = offset;
82                     while (end < dotLine.length() && !Character.isWhitespace(c = dotLine.charAt(end))) {
83                         if (",.<".indexOf(c) >= 0)
84                             break;
85                         ++end;
86                     }
87                     if (begin != end)
88                         r = new Region(buffer, new Position(dotLine, begin),
89                                        new Position(dotLine, end));
90                 }
91             }
92         }
93         if (r != null) {
94             boolean wantNewLine = false;
95             if (r.getBeginOffset() == 0 && r.getEndOffset() == 0)
96                 wantNewLine = true;
97             final int offset = editor.getDotOffset();
98             CompoundEdit JavaDoc compoundEdit = editor.beginCompoundEdit();
99             if (wantEndTag) {
100                 editor.moveDotTo(r.getEnd());
101                 editor.addUndo(SimpleEdit.INSERT_STRING);
102                 editor.insertStringInternal("</" + tagName + ">");
103                 Editor.updateInAllEditors(editor.getDotLine());
104                 if (wantNewLine) {
105                     editor.addUndo(SimpleEdit.INSERT_LINE_SEP);
106                     editor.insertLineSeparator();
107                 }
108             }
109             editor.moveDotTo(r.getBegin());
110             editor.addUndo(SimpleEdit.INSERT_STRING);
111             String JavaDoc insert = "<" + tagName + extra + ">";
112             editor.insertStringInternal(insert);
113             Editor.updateInAllEditors(editor.getDotLine());
114             if (wantNewLine) {
115                 Line startTagLine = editor.getDotLine();
116                 editor.addUndo(SimpleEdit.INSERT_LINE_SEP);
117                 editor.insertLineSeparator();
118                 if (buffer.getBooleanProperty(Property.AUTO_INDENT)) {
119                     // Re-indent.
120
// Move dot to start of region.
121
editor.addUndo(SimpleEdit.MOVE);
122                     editor.getDot().moveTo(startTagLine, 0);
123                     Position dot = editor.getDot();
124                     while (dot.getLine() != null && dot.getLine() != r.getEndLine().next()) {
125                         editor.indentLine();
126                         dot.moveTo(dot.getNextLine(), 0);
127                     }
128                 }
129                 // Put dot before '>' of start tag.
130
editor.moveDotTo(startTagLine, startTagLine.length()-1);
131             } else {
132                 int newOffset;
133                 if (extra.endsWith(" "))
134                     newOffset = editor.getDotOffset() - 1;
135                 else
136                     newOffset = offset + insert.length();
137                 editor.moveDotTo(editor.getDotLine(), newOffset);
138             }
139             editor.endCompoundEdit(compoundEdit);
140         } else {
141             CompoundEdit JavaDoc compoundEdit = editor.beginCompoundEdit();
142             editor.fillToCaret();
143             final int offset = editor.getDotOffset();
144             editor.addUndo(SimpleEdit.INSERT_STRING);
145             if (wantEndTag)
146                 editor.insertStringInternal("<" + tagName + extra + "></" + tagName + ">");
147             else
148                 editor.insertStringInternal("<" + tagName + extra + ">");
149             Editor.updateInAllEditors(editor.getDotLine());
150             editor.addUndo(SimpleEdit.MOVE);
151             editor.getDot().setOffset(offset + 1 + tagName.length() + extra.length());
152             editor.moveCaretToDotCol();
153             editor.endCompoundEdit(compoundEdit);
154         }
155     }
156 }
157
Popular Tags