1 21 22 package org.armedbear.j; 23 24 import java.awt.BorderLayout ; 25 import java.awt.event.FocusEvent ; 26 import java.awt.event.FocusListener ; 27 import javax.swing.BoxLayout ; 28 import javax.swing.JDialog ; 29 import javax.swing.JLabel ; 30 import javax.swing.JPanel ; 31 import javax.swing.border.EmptyBorder ; 32 33 public class OpenFileDialog extends JDialog implements FocusListener  34 { 35 private final Editor editor; 36 37 private Object result; 38 private String title; 39 private HistoryTextField textField; 40 private OpenFileTextFieldHandler handler; 41 42 public OpenFileDialog(Editor editor) 43 { 44 this(editor, "Open File"); 45 title = "Open File"; 46 } 47 48 private OpenFileDialog(Editor editor, String title) 49 { 50 super(editor.getFrame(), title, true); 51 this.editor = editor; 52 this.title = title; 53 JPanel panel = new JPanel (); 54 panel.setLayout(new BoxLayout (panel, BoxLayout.Y_AXIS)); 55 panel.setBorder(new EmptyBorder (5, 5, 5, 5)); 56 panel.add(new JLabel ("File:")); 57 textField = new HistoryTextField(20); 58 textField.setHistory(new History("openFile.file")); 59 textField.setOwner(this); 60 textField.setHandler(handler = new OpenFileTextFieldHandler(editor, textField)); 61 handler.setTitle(title); 62 panel.add(textField); 63 getContentPane().add(panel, BorderLayout.CENTER); 64 pack(); 65 textField.setFocusTraversalKeysEnabled(false); 66 textField.requestFocus(); 67 addFocusListener(this); 68 } 69 70 public final OpenFileTextFieldHandler getHandler() 71 { 72 return handler; 73 } 74 75 public final Object getResult() 76 { 77 return result; 78 } 79 80 public final void setResult(Object result) 81 { 82 this.result = result; 83 } 84 85 public static File getLocalFile(Editor editor, String title) 86 { 87 OpenFileDialog dialog = new OpenFileDialog(editor, title); 88 OpenFileTextFieldHandler handler = dialog.getHandler(); 89 handler.setAllowRemote(false); 90 handler.setFileMustExist(true); 91 handler.setCheckBuffers(false); 92 handler.setCheckSourcePath(false); 93 editor.centerDialog(dialog); 94 dialog.show(); 95 editor.repaintNow(); 96 if (dialog.result instanceof File) 97 return (File) dialog.result; 98 return null; 99 } 100 101 public void ok() 102 { 103 dispose(); 104 editor.setFocusToDisplay(); 105 } 106 107 public void cancel() 108 { 109 dispose(); 110 editor.setFocusToDisplay(); 111 } 112 113 public void focusGained(FocusEvent e) 114 { 115 textField.requestFocus(); 116 } 117 118 public void focusLost(FocusEvent e) 119 { 120 } 121 122 public void dispose() 123 { 124 super.dispose(); 125 editor.restoreFocus(); 126 } 127 128 public static void openFileInOtherFrame() 129 { 130 final Editor editor = Editor.currentEditor(); 131 OpenFileDialog dialog = new OpenFileDialog(editor); 132 editor.centerDialog(dialog); 133 dialog.show(); 134 editor.repaintNow(); 135 Buffer buf = null; 136 Object obj = dialog.getResult(); 137 if (obj instanceof Buffer) 138 buf = (Buffer) obj; 139 else if (obj instanceof File) 140 buf = editor.openFile((File)obj); 141 if (buf != null) { 142 editor.makeNext(buf); 143 editor.activateInOtherFrame(buf); 144 } 145 } 146 } 147 | Popular Tags |