001: /*
002: * AlignString.java
003: *
004: * Copyright (C) 2002 Peter Graves
005: * $Id: AlignStrings.java,v 1.1.1.1 2002/09/24 16:08:01 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 gnu.regexp.RE;
025: import gnu.regexp.REException;
026: import gnu.regexp.REMatch;
027: import javax.swing.undo.CompoundEdit;
028:
029: public final class AlignStrings {
030: public static void alignStrings() {
031: final Editor editor = Editor.currentEditor();
032: InputDialog d = new InputDialog(editor, "Regular Expression:",
033: "Align Strings", null);
034: d.setHistory(new History("alignStrings"));
035: editor.centerDialog(d);
036: d.show();
037: String input = d.getInput();
038: if (input != null)
039: alignStrings(input);
040: }
041:
042: public static void alignStrings(String s) {
043: final Editor editor = Editor.currentEditor();
044: if (!editor.checkReadOnly())
045: return;
046: if (editor.getMark() == null)
047: return;
048: final Region region = new Region(editor);
049: if (region.getEndLineNumber() - region.getBeginLineNumber() < 2)
050: return;
051: s = unquote(s);
052: RE re;
053: try {
054: re = new RE(s);
055: } catch (REException e) {
056: MessageDialog.showMessageDialog(editor, e.getMessage(),
057: "Error");
058: return;
059: }
060: final Buffer buffer = editor.getBuffer();
061: try {
062: buffer.lockWrite();
063: } catch (InterruptedException e) {
064: Log.error(e);
065: return;
066: }
067: try {
068: _alignStrings(editor, buffer, region, re);
069: } finally {
070: buffer.unlockWrite();
071: }
072: }
073:
074: private static void _alignStrings(Editor editor, Buffer buffer,
075: Region region, RE re) {
076: int maxCol = -1;
077: for (Line line = region.getBeginLine(); line != region
078: .getEndLine(); line = line.next()) {
079: String text = line.getText();
080: if (text != null) {
081: REMatch match = re.getMatch(text);
082: if (match != null) {
083: int offset = match.getStartIndex();
084: int col = buffer.getCol(line, offset);
085: if (col > maxCol)
086: maxCol = col;
087: }
088: }
089: }
090: if (maxCol < 0)
091: return;
092: Position savedDot = new Position(editor.getDot());
093: CompoundEdit compoundEdit = buffer.beginCompoundEdit();
094: for (Line line = region.getBeginLine(); line != region
095: .getEndLine(); line = line.next()) {
096: String text = line.getText();
097: if (text != null) {
098: REMatch match = re.getMatch(text);
099: if (match != null) {
100: int offset = match.getStartIndex();
101: int col = buffer.getCol(line, offset);
102: if (col < maxCol) {
103: editor.addUndo(SimpleEdit.MOVE);
104: editor.getDot().moveTo(line, offset);
105: editor.addUndo(SimpleEdit.LINE_EDIT);
106: buffer.insertChars(editor.getDot(), Utilities
107: .spaces(maxCol - col));
108: Editor.updateInAllEditors(buffer, line);
109: }
110: }
111: }
112: }
113: editor.getDot().moveTo(savedDot);
114: editor.moveCaretToDotCol();
115: buffer.endCompoundEdit(compoundEdit);
116: }
117:
118: private static String unquote(String s) {
119: int length = s.length();
120: if (length >= 2) {
121: char c = s.charAt(0);
122: if (c == '"' || c == '\'') {
123: if (s.charAt(length - 1) == c)
124: return s.substring(1, length - 1);
125: }
126: }
127: return s;
128: }
129: }
|