1 21 22 package org.armedbear.j; 23 24 import java.awt.Color ; 25 import java.awt.Dimension ; 26 import java.awt.Font ; 27 import java.awt.FontMetrics ; 28 import java.awt.Toolkit ; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.awt.event.MouseEvent ; 32 import java.awt.event.MouseListener ; 33 import java.net.URL ; 34 import javax.swing.BorderFactory ; 35 import javax.swing.BoxLayout ; 36 import javax.swing.ImageIcon ; 37 import javax.swing.JButton ; 38 import javax.swing.JPanel ; 39 40 public final class LocationBar extends JPanel implements Constants, 41 ActionListener , MouseListener 42 { 43 private Editor editor; 44 private final Label label; 45 private final HistoryTextField textField; 46 private JButton closeButton; 47 48 private static String [] 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 (this, BoxLayout.X_AXIS)); 64 setBorder(BorderFactory.createEmptyBorder(3, 3, 4, 3)); 65 label = new Label(getWidestPrompt()); 68 label.setBorder(BorderFactory.createEmptyBorder(3, 0, 1, 0)); 69 Dimension 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 dim = getPreferredSize(); 84 dim.width = 0; 85 setMinimumSize(dim); 86 setLabelText(PROMPT_LOCATION); 87 } 88 89 private void addCloseButton() 90 { 91 closeButton = new JButton (); 92 URL url = Editor.class.getResource("images/close_frame.png"); 93 if (url != null) 94 closeButton.setIcon(new ImageIcon (url)); 95 Dimension 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 widest = null; 108 109 private static String getWidestPrompt() 110 { 111 if (widest == null) { 112 Font font = new Label().getFont(); 113 FontMetrics 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 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 getCloseButton() 162 { 163 return closeButton; 164 } 165 166 public static void cancelInput() 167 { 168 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 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 e) {} 187 188 public void mouseEntered(MouseEvent e) {} 189 190 public void mouseExited(MouseEvent e) {} 191 192 public void mousePressed(MouseEvent e) 193 { 194 editor.ensureActive(); 195 editor.getFrame().setFocus(textField); 196 } 197 198 public void mouseReleased(MouseEvent e) {} 199 } 200 | Popular Tags |