KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > ChooseFolderDialog


1 /*
2  * ChooseFolderDialog.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: ChooseFolderDialog.java,v 1.1.1.1 2002/09/24 16:10:04 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.mail;
23
24 import org.armedbear.j.Debug;
25 import org.armedbear.j.Editor;
26 import org.armedbear.j.File;
27 import org.armedbear.j.History;
28 import org.armedbear.j.InputDialog;
29 import org.armedbear.j.MessageDialog;
30
31 public final class ChooseFolderDialog extends InputDialog
32 {
33     private ChooseFolderDialog(Editor editor, String JavaDoc prompt, String JavaDoc title)
34     {
35         super(editor, prompt, title, null);
36         History history = new History("chooseFolder");
37         setHistory(history);
38         setDefaultValue(history.getPrevious());
39     }
40
41     public static String JavaDoc chooseFolder(Editor editor, String JavaDoc title)
42     {
43         return chooseFolder(editor, "Folder:", title);
44     }
45
46     // Returns null if user cancels.
47
// Returns null if user input (or dereferenced alias) is empty string.
48
public static String JavaDoc chooseFolder(Editor editor, String JavaDoc prompt,
49         String JavaDoc title)
50     {
51         String JavaDoc errorText = null;
52         while (true) {
53             if (errorText != null)
54                 MessageDialog.showMessageDialog(editor, errorText, title);
55             ChooseFolderDialog dialog =
56                 new ChooseFolderDialog(editor, prompt, title);
57             editor.centerDialog(dialog);
58             dialog.show();
59             editor.repaintNow();
60             final String JavaDoc input = dialog.getInput();
61             if (input == null || input.length() == 0)
62                 return null;
63             final String JavaDoc value = editor.getAlias(input);
64             final String JavaDoc destination = value != null ? value : input;
65             Debug.assertTrue(destination != null);
66             if (destination.length() == 0) // Shouldn't happen.
67
return null;
68             if (destination.startsWith("mailbox:")) {
69                 // Local folder. Do a little validation.
70
File file = File.getInstance(destination.substring(8));
71                 if (file == null) {
72                     errorText = "Invalid path";
73                     continue;
74                 }
75                 if (file.isDirectory()) {
76                     return "mailbox:".concat(File.getInstance(file, "mbox").canonicalPath());
77                 }
78                 if (file.isFile()) {
79                     // Assume it's really a mailbox...
80
return "mailbox:".concat(file.canonicalPath());
81                 }
82                 errorText = "Folder does not exist";
83                 continue;
84             }
85             // Not local.
86
return destination;
87         }
88     }
89 }
90
Popular Tags