KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > pref > FSPath


1 /*
2  * ==================================================================== The ObjectStyle
3  * Group Software License, version 1.1 ObjectStyle Group - http://objectstyle.org/
4  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors of the
5  * software. All rights reserved. Redistribution and use in source and binary forms, with
6  * or without modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  * conditions and the following disclaimer. 2. Redistributions in binary form must
9  * reproduce the above copyright notice, this list of conditions and the following
10  * disclaimer in the documentation and/or other materials provided with the distribution.
11  * 3. The end-user documentation included with the redistribution, if any, must include
12  * the following acknowlegement: "This product includes software developed by independent
13  * contributors and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
14  * Alternately, this acknowlegement may appear in the software itself, if and wherever
15  * such third-party acknowlegements normally appear. 4. The names "ObjectStyle Group" and
16  * "Cayenne" must not be used to endorse or promote products derived from this software
17  * without prior written permission. For written permission, email "andrus at objectstyle
18  * dot org". 5. Products derived from this software may not be called "ObjectStyle" or
19  * "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their names without prior
20  * written permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
21  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE
23  * GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  * ==================================================================== This software
30  * consists of voluntary contributions made by many individuals and hosted on ObjectStyle
31  * Group web site. For more information on the ObjectStyle Group, please see
32  * <http://objectstyle.org/>.
33  */

34 package org.objectstyle.cayenne.modeler.pref;
35
36 import java.io.File JavaDoc;
37
38 import javax.swing.JFileChooser JavaDoc;
39
40 /**
41  * Represents a preferred directory or file.
42  *
43  * @author Andrei Adamchik
44  */

45 public class FSPath extends _FSPath {
46
47     public void updateFromChooser(JFileChooser JavaDoc chooser) {
48         File JavaDoc file = chooser.getSelectedFile();
49         if (file != null) {
50             setDirectory(file);
51         }
52     }
53
54     public void updateChooser(JFileChooser JavaDoc chooser) {
55         File JavaDoc startDir = getExistingDirectory(false);
56         if (startDir != null) {
57             chooser.setCurrentDirectory(startDir);
58         }
59     }
60
61     public void setDirectory(File JavaDoc file) {
62
63         if (file.isFile()) {
64             setPath(file.getParentFile().getAbsolutePath());
65         }
66         else {
67             setPath(file.getAbsolutePath());
68         }
69     }
70
71     public File JavaDoc getExistingDirectory(boolean create) {
72         if (getPath() == null) {
73             return null;
74         }
75
76         File JavaDoc path = new File JavaDoc(getPath());
77         if (path.isDirectory()) {
78             return path;
79         }
80
81         if (path.isFile()) {
82             return path.getParentFile();
83         }
84
85         if (create) {
86             path.mkdirs();
87             return path;
88         }
89
90         return null;
91     }
92 }
93
94
Popular Tags