KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * OpenFileDialog.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: OpenFileDialog.java,v 1.2 2003/06/26 00:43:48 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.BorderLayout JavaDoc;
25 import java.awt.event.FocusEvent JavaDoc;
26 import java.awt.event.FocusListener JavaDoc;
27 import javax.swing.BoxLayout JavaDoc;
28 import javax.swing.JDialog JavaDoc;
29 import javax.swing.JLabel JavaDoc;
30 import javax.swing.JPanel JavaDoc;
31 import javax.swing.border.EmptyBorder JavaDoc;
32
33 public class OpenFileDialog extends JDialog JavaDoc implements FocusListener JavaDoc
34 {
35     private final Editor editor;
36
37     private Object JavaDoc result;
38     private String JavaDoc 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 JavaDoc title)
49     {
50         super(editor.getFrame(), title, true);
51         this.editor = editor;
52         this.title = title;
53         JPanel JavaDoc panel = new JPanel JavaDoc();
54         panel.setLayout(new BoxLayout JavaDoc(panel, BoxLayout.Y_AXIS));
55         panel.setBorder(new EmptyBorder JavaDoc(5, 5, 5, 5));
56         panel.add(new JLabel JavaDoc("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 JavaDoc getResult()
76     {
77         return result;
78     }
79
80     public final void setResult(Object JavaDoc result)
81     {
82         this.result = result;
83     }
84
85     public static File getLocalFile(Editor editor, String JavaDoc 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 JavaDoc e)
114     {
115         textField.requestFocus();
116     }
117
118     public void focusLost(FocusEvent JavaDoc 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 JavaDoc 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