001: /*
002: * OpenFileDialog.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: OpenFileDialog.java,v 1.2 2003/06/26 00:43:48 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.BorderLayout;
025: import java.awt.event.FocusEvent;
026: import java.awt.event.FocusListener;
027: import javax.swing.BoxLayout;
028: import javax.swing.JDialog;
029: import javax.swing.JLabel;
030: import javax.swing.JPanel;
031: import javax.swing.border.EmptyBorder;
032:
033: public class OpenFileDialog extends JDialog implements FocusListener {
034: private final Editor editor;
035:
036: private Object result;
037: private String title;
038: private HistoryTextField textField;
039: private OpenFileTextFieldHandler handler;
040:
041: public OpenFileDialog(Editor editor) {
042: this (editor, "Open File");
043: title = "Open File";
044: }
045:
046: private OpenFileDialog(Editor editor, String title) {
047: super (editor.getFrame(), title, true);
048: this .editor = editor;
049: this .title = title;
050: JPanel panel = new JPanel();
051: panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
052: panel.setBorder(new EmptyBorder(5, 5, 5, 5));
053: panel.add(new JLabel("File:"));
054: textField = new HistoryTextField(20);
055: textField.setHistory(new History("openFile.file"));
056: textField.setOwner(this );
057: textField.setHandler(handler = new OpenFileTextFieldHandler(
058: editor, textField));
059: handler.setTitle(title);
060: panel.add(textField);
061: getContentPane().add(panel, BorderLayout.CENTER);
062: pack();
063: textField.setFocusTraversalKeysEnabled(false);
064: textField.requestFocus();
065: addFocusListener(this );
066: }
067:
068: public final OpenFileTextFieldHandler getHandler() {
069: return handler;
070: }
071:
072: public final Object getResult() {
073: return result;
074: }
075:
076: public final void setResult(Object result) {
077: this .result = result;
078: }
079:
080: public static File getLocalFile(Editor editor, String title) {
081: OpenFileDialog dialog = new OpenFileDialog(editor, title);
082: OpenFileTextFieldHandler handler = dialog.getHandler();
083: handler.setAllowRemote(false);
084: handler.setFileMustExist(true);
085: handler.setCheckBuffers(false);
086: handler.setCheckSourcePath(false);
087: editor.centerDialog(dialog);
088: dialog.show();
089: editor.repaintNow();
090: if (dialog.result instanceof File)
091: return (File) dialog.result;
092: return null;
093: }
094:
095: public void ok() {
096: dispose();
097: editor.setFocusToDisplay();
098: }
099:
100: public void cancel() {
101: dispose();
102: editor.setFocusToDisplay();
103: }
104:
105: public void focusGained(FocusEvent e) {
106: textField.requestFocus();
107: }
108:
109: public void focusLost(FocusEvent e) {
110: }
111:
112: public void dispose() {
113: super .dispose();
114: editor.restoreFocus();
115: }
116:
117: public static void openFileInOtherFrame() {
118: final Editor editor = Editor.currentEditor();
119: OpenFileDialog dialog = new OpenFileDialog(editor);
120: editor.centerDialog(dialog);
121: dialog.show();
122: editor.repaintNow();
123: Buffer buf = null;
124: Object obj = dialog.getResult();
125: if (obj instanceof Buffer)
126: buf = (Buffer) obj;
127: else if (obj instanceof File)
128: buf = editor.openFile((File) obj);
129: if (buf != null) {
130: editor.makeNext(buf);
131: editor.activateInOtherFrame(buf);
132: }
133: }
134: }
|