KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * LocationBar.java
3  *
4  * Copyright (C) 2002-2003 Peter Graves
5  * $Id: LocationBar.java,v 1.8 2003/07/23 00:30:06 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.awt.Color JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Font JavaDoc;
27 import java.awt.FontMetrics JavaDoc;
28 import java.awt.Toolkit JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.awt.event.MouseEvent JavaDoc;
32 import java.awt.event.MouseListener JavaDoc;
33 import java.net.URL JavaDoc;
34 import javax.swing.BorderFactory JavaDoc;
35 import javax.swing.BoxLayout JavaDoc;
36 import javax.swing.ImageIcon JavaDoc;
37 import javax.swing.JButton JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39
40 public final class LocationBar extends JPanel JavaDoc implements Constants,
41     ActionListener JavaDoc, MouseListener JavaDoc
42 {
43     private Editor editor;
44     private final Label label;
45     private final HistoryTextField textField;
46     private JButton JavaDoc closeButton;
47
48     private static String JavaDoc[] prompts = {
49         "Location:",
50         "Command:",
51         "Tag:",
52         "Pattern:",
53     };
54
55     public static final int PROMPT_LOCATION = 0;
56     public static final int PROMPT_COMMAND = 1;
57     public static final int PROMPT_TAG = 2;
58     public static final int PROMPT_PATTERN = 3;
59
60     public LocationBar(final Editor editor)
61     {
62         this.editor = editor;
63         setLayout(new BoxLayout JavaDoc(this, BoxLayout.X_AXIS));
64         setBorder(BorderFactory.createEmptyBorder(3, 3, 4, 3));
65         // Make the label wide enough for the widest string that needs to go
66
// there.
67
label = new Label(getWidestPrompt());
68         label.setBorder(BorderFactory.createEmptyBorder(3, 0, 1, 0));
69         Dimension JavaDoc dim = label.getPreferredSize();
70         label.setPreferredSize(dim);
71         label.setMinimumSize(dim);
72         label.setMaximumSize(dim);
73         label.setHorizontalAlignment(Label.RIGHT);
74         add(label);
75         textField = new HistoryTextField(editor, 20);
76         textField.setFocusTraversalKeysEnabled(false);
77         add(textField);
78         addCloseButton();
79         textField.addMouseListener(this);
80         textField.setHandler(new OpenFileTextFieldHandler(editor, textField));
81         // Don't let the width of the location bar prevent the user from
82
// making the sidebar wider.
83
dim = getPreferredSize();
84         dim.width = 0;
85         setMinimumSize(dim);
86         setLabelText(PROMPT_LOCATION);
87     }
88
89     private void addCloseButton()
90     {
91         closeButton = new JButton JavaDoc();
92         URL JavaDoc url = Editor.class.getResource("images/close_frame.png");
93         if (url != null)
94             closeButton.setIcon(new ImageIcon JavaDoc(url));
95         Dimension JavaDoc dim = textField.getPreferredSize();
96         dim.width = dim.height = 16;
97         closeButton.setPreferredSize(dim);
98         closeButton.setMinimumSize(dim);
99         closeButton.setMaximumSize(dim);
100         closeButton.setBorder(null);
101         add(javax.swing.Box.createHorizontalStrut(3));
102         add(closeButton);
103         add(javax.swing.Box.createHorizontalStrut(3));
104         closeButton.addActionListener(this);
105     }
106
107     private static String JavaDoc widest = null;
108
109     private static String JavaDoc getWidestPrompt()
110     {
111         if (widest == null) {
112             Font JavaDoc font = new Label().getFont();
113             FontMetrics JavaDoc fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
114             int maxWidth = -1;
115             for (int i = 0; i < prompts.length; i++) {
116                 int width = fm.stringWidth(prompts[i]);
117                 if (width > maxWidth) {
118                     widest = prompts[i];
119                     maxWidth = width;
120                 }
121             }
122         }
123         return widest;
124     }
125
126     public final void setLabelText(int index)
127     {
128         if (index >= 0 && index < prompts.length)
129             label.setText(prompts[index]);
130         else
131             Debug.bug();
132     }
133
134     public void paintComponent(java.awt.Graphics JavaDoc g)
135     {
136         if (editor == Editor.currentEditor()) {
137             label.setForeground(Color.black);
138             textField.setForeground(Color.black);
139         } else {
140             label.setForeground(Color.gray);
141             textField.setForeground(Color.gray);
142         }
143         super.paintComponent(g);
144     }
145
146     public void update()
147     {
148         setLabelText(PROMPT_LOCATION);
149         textField.setHandler(new OpenFileTextFieldHandler(editor, textField));
150         textField.setHistory(new History("openFile.file", 30));
151         Buffer buffer = editor.getBuffer();
152         if (buffer != null)
153             textField.setText(buffer.getFileNameForDisplay());
154     }
155
156     public final HistoryTextField getTextField()
157     {
158         return textField;
159     }
160
161     public final JButton JavaDoc getCloseButton()
162     {
163         return closeButton;
164     }
165
166     public static void cancelInput()
167     {
168         // Cancel location bar activity (if any).
169
for (EditorIterator it = new EditorIterator(); it.hasNext();) {
170             Editor ed = it.nextEditor();
171             if (ed.getFocusedComponent() == ed.getLocationBarTextField())
172                 ed.getLocationBarTextField().getHandler().escape();
173         }
174     }
175
176     public void actionPerformed(ActionEvent JavaDoc e)
177     {
178         final Frame frame = editor.getFrame();
179         frame.closeEditor(editor);
180         frame.getCurrentEditor().setFocusToDisplay();
181         Sidebar sidebar = frame.getSidebar();
182         if (sidebar != null)
183             sidebar.setUpdateFlag(SIDEBAR_SET_BUFFER);
184     }
185
186     public void mouseClicked(MouseEvent JavaDoc e) {}
187
188     public void mouseEntered(MouseEvent JavaDoc e) {}
189
190     public void mouseExited(MouseEvent JavaDoc e) {}
191
192     public void mousePressed(MouseEvent JavaDoc e)
193     {
194         editor.ensureActive();
195         editor.getFrame().setFocus(textField);
196     }
197
198     public void mouseReleased(MouseEvent JavaDoc e) {}
199 }
200
Popular Tags