001: /*
002: * AliasDialog.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: AliasDialog.java,v 1.2 2003/07/24 15:08:00 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.awt.event.FocusEvent;
025: import java.awt.event.FocusListener;
026:
027: public final class AliasDialog extends AbstractDialog implements
028: FocusListener {
029: private final Editor editor;
030: private final HistoryTextField keyTextField;
031: private final HistoryTextField valueTextField;
032:
033: public AliasDialog(Editor editor) {
034: this (editor, null);
035: }
036:
037: public AliasDialog(Editor editor, String key) {
038: super (editor.getFrame(), "Alias", true);
039: this .editor = editor;
040: keyTextField = new HistoryTextField(20);
041: valueTextField = new HistoryTextField(20);
042: Label label = new Label("Key:");
043: label.setDisplayedMnemonic('K');
044: addLabelAndTextField(label, keyTextField);
045: addVerticalStrut();
046: label = new Label("Value:");
047: label.setDisplayedMnemonic('V');
048: addLabelAndTextField(label, valueTextField);
049: addVerticalStrut();
050: addOKCancel();
051: pack();
052: keyTextField.addFocusListener(this );
053: if (key != null) {
054: keyTextField.setText(key);
055: String value = editor.getAlias(key);
056: if (value != null)
057: valueTextField.setText(value);
058: valueTextField.requestFocus();
059: } else
060: keyTextField.requestFocus();
061: }
062:
063: protected void ok() {
064: dispose();
065: String key = getKey();
066: if (Aliases.isSystemAlias(key))
067: MessageDialog.showMessageDialog(editor,
068: "System aliases cannot be changed.", "Error");
069: else {
070: String value = getValue();
071: if (value.length() == 0)
072: editor.removeAlias(key);
073: else if (value.equals("here"))
074: editor.setAliasForBuffer(key, editor.getBuffer());
075: else
076: editor.setAlias(key, value);
077: }
078: }
079:
080: private final String getKey() {
081: final String key = keyTextField.getText();
082: return key != null ? key.trim() : "";
083: }
084:
085: private final String getValue() {
086: final String value = valueTextField.getText();
087: return value != null ? value.trim() : "";
088: }
089:
090: public void focusGained(FocusEvent e) {
091: }
092:
093: public void focusLost(FocusEvent e) {
094: String value = editor.getAlias(getKey());
095: if (value != null)
096: valueTextField.setText(value);
097: }
098:
099: public static void alias() {
100: final Editor editor = Editor.currentEditor();
101: AliasDialog d = new AliasDialog(editor);
102: editor.centerDialog(d);
103: d.show();
104: }
105:
106: public static void alias(String args) {
107: final Editor editor = Editor.currentEditor();
108: args = args.trim();
109: final FastStringBuffer sb = new FastStringBuffer();
110: int i = 0;
111: for (; i < args.length(); i++) {
112: char c = args.charAt(i);
113: if (Character.isWhitespace(c))
114: break;
115: else if (c == '=')
116: break;
117: else
118: sb.append(c);
119: }
120: if (sb.length() == 0)
121: return;
122: final String key = sb.toString();
123: boolean seenEquals = false;
124: for (; i < args.length(); i++) {
125: char c = args.charAt(i);
126: if (Character.isWhitespace(c))
127: continue;
128: if (c == '=' && !seenEquals)
129: seenEquals = true;
130: else
131: break;
132: }
133: sb.setLength(0);
134: for (; i < args.length(); i++)
135: sb.append(args.charAt(i));
136: String value = sb.toString();
137: if (value.length() == 0) {
138: if (seenEquals) {
139: // Unset alias.
140: if (Aliases.isSystemAlias(key))
141: MessageDialog.showMessageDialog(editor,
142: "System aliases cannot be changed.",
143: "Error");
144: else
145: editor.removeAlias(key);
146: } else {
147: AliasDialog d = new AliasDialog(editor, key);
148: editor.centerDialog(d);
149: d.show();
150: }
151: } else {
152: if (Aliases.isSystemAlias(key))
153: MessageDialog.showMessageDialog(editor,
154: "System aliases cannot be changed.", "Error");
155: else if (value.equals("here"))
156: editor.setAliasForBuffer(key, editor.getBuffer());
157: else
158: editor.setAlias(key, value);
159: }
160: }
161: }
|