001: /*
002: * LocationBar.java
003: *
004: * Copyright (C) 2002-2003 Peter Graves
005: * $Id: LocationBar.java,v 1.8 2003/07/23 00:30:06 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.Color;
025: import java.awt.Dimension;
026: import java.awt.Font;
027: import java.awt.FontMetrics;
028: import java.awt.Toolkit;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.awt.event.MouseEvent;
032: import java.awt.event.MouseListener;
033: import java.net.URL;
034: import javax.swing.BorderFactory;
035: import javax.swing.BoxLayout;
036: import javax.swing.ImageIcon;
037: import javax.swing.JButton;
038: import javax.swing.JPanel;
039:
040: public final class LocationBar extends JPanel implements Constants,
041: ActionListener, MouseListener {
042: private Editor editor;
043: private final Label label;
044: private final HistoryTextField textField;
045: private JButton closeButton;
046:
047: private static String[] prompts = { "Location:", "Command:",
048: "Tag:", "Pattern:", };
049:
050: public static final int PROMPT_LOCATION = 0;
051: public static final int PROMPT_COMMAND = 1;
052: public static final int PROMPT_TAG = 2;
053: public static final int PROMPT_PATTERN = 3;
054:
055: public LocationBar(final Editor editor) {
056: this .editor = editor;
057: setLayout(new BoxLayout(this , BoxLayout.X_AXIS));
058: setBorder(BorderFactory.createEmptyBorder(3, 3, 4, 3));
059: // Make the label wide enough for the widest string that needs to go
060: // there.
061: label = new Label(getWidestPrompt());
062: label.setBorder(BorderFactory.createEmptyBorder(3, 0, 1, 0));
063: Dimension dim = label.getPreferredSize();
064: label.setPreferredSize(dim);
065: label.setMinimumSize(dim);
066: label.setMaximumSize(dim);
067: label.setHorizontalAlignment(Label.RIGHT);
068: add(label);
069: textField = new HistoryTextField(editor, 20);
070: textField.setFocusTraversalKeysEnabled(false);
071: add(textField);
072: addCloseButton();
073: textField.addMouseListener(this );
074: textField.setHandler(new OpenFileTextFieldHandler(editor,
075: textField));
076: // Don't let the width of the location bar prevent the user from
077: // making the sidebar wider.
078: dim = getPreferredSize();
079: dim.width = 0;
080: setMinimumSize(dim);
081: setLabelText(PROMPT_LOCATION);
082: }
083:
084: private void addCloseButton() {
085: closeButton = new JButton();
086: URL url = Editor.class.getResource("images/close_frame.png");
087: if (url != null)
088: closeButton.setIcon(new ImageIcon(url));
089: Dimension dim = textField.getPreferredSize();
090: dim.width = dim.height = 16;
091: closeButton.setPreferredSize(dim);
092: closeButton.setMinimumSize(dim);
093: closeButton.setMaximumSize(dim);
094: closeButton.setBorder(null);
095: add(javax.swing.Box.createHorizontalStrut(3));
096: add(closeButton);
097: add(javax.swing.Box.createHorizontalStrut(3));
098: closeButton.addActionListener(this );
099: }
100:
101: private static String widest = null;
102:
103: private static String getWidestPrompt() {
104: if (widest == null) {
105: Font font = new Label().getFont();
106: FontMetrics fm = Toolkit.getDefaultToolkit()
107: .getFontMetrics(font);
108: int maxWidth = -1;
109: for (int i = 0; i < prompts.length; i++) {
110: int width = fm.stringWidth(prompts[i]);
111: if (width > maxWidth) {
112: widest = prompts[i];
113: maxWidth = width;
114: }
115: }
116: }
117: return widest;
118: }
119:
120: public final void setLabelText(int index) {
121: if (index >= 0 && index < prompts.length)
122: label.setText(prompts[index]);
123: else
124: Debug.bug();
125: }
126:
127: public void paintComponent(java.awt.Graphics g) {
128: if (editor == Editor.currentEditor()) {
129: label.setForeground(Color.black);
130: textField.setForeground(Color.black);
131: } else {
132: label.setForeground(Color.gray);
133: textField.setForeground(Color.gray);
134: }
135: super .paintComponent(g);
136: }
137:
138: public void update() {
139: setLabelText(PROMPT_LOCATION);
140: textField.setHandler(new OpenFileTextFieldHandler(editor,
141: textField));
142: textField.setHistory(new History("openFile.file", 30));
143: Buffer buffer = editor.getBuffer();
144: if (buffer != null)
145: textField.setText(buffer.getFileNameForDisplay());
146: }
147:
148: public final HistoryTextField getTextField() {
149: return textField;
150: }
151:
152: public final JButton getCloseButton() {
153: return closeButton;
154: }
155:
156: public static void cancelInput() {
157: // Cancel location bar activity (if any).
158: for (EditorIterator it = new EditorIterator(); it.hasNext();) {
159: Editor ed = it.nextEditor();
160: if (ed.getFocusedComponent() == ed
161: .getLocationBarTextField())
162: ed.getLocationBarTextField().getHandler().escape();
163: }
164: }
165:
166: public void actionPerformed(ActionEvent e) {
167: final Frame frame = editor.getFrame();
168: frame.closeEditor(editor);
169: frame.getCurrentEditor().setFocusToDisplay();
170: Sidebar sidebar = frame.getSidebar();
171: if (sidebar != null)
172: sidebar.setUpdateFlag(SIDEBAR_SET_BUFFER);
173: }
174:
175: public void mouseClicked(MouseEvent e) {
176: }
177:
178: public void mouseEntered(MouseEvent e) {
179: }
180:
181: public void mouseExited(MouseEvent e) {
182: }
183:
184: public void mousePressed(MouseEvent e) {
185: editor.ensureActive();
186: editor.getFrame().setFocus(textField);
187: }
188:
189: public void mouseReleased(MouseEvent e) {
190: }
191: }
|