001
002 /*****************************************************************************
003
004 jBudget.
005 Copyright (C) 2006 Dmitri Petrov
006
007 This program is free software: you can redistribute it and/or modify
008 it under the terms of the GNU General Public License as published by
009 the Free Software Foundation, either version 3 of the License, or
010 (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, see <http://www.gnu.org/licenses/>.
019
020 /*****************************************************************************
021
022 /*
023 * DirectoryLocationDialog.java
024 *
025 * Created on August 21, 2006, 6:15 PM
026 */
027
028 package org.jbudget.gui;
029
030 import java.awt.BorderLayout;
031 import java.awt.event.ActionEvent;
032 import java.awt.event.ActionListener;
033 import java.io.File;
034 import javax.swing.JFileChooser;
035 import javax.swing.JFrame;
036 import javax.swing.JOptionPane;
037 import org.jbudget.gui.templ.DirectoryLocationPanel;
038
039 /**
040 * Brings up a dialog asking the user to slect the directory for storing
041 * jBudget data.
042 *
043 * @author petrov
044 */
045 public class DirectoryLocationDialog extends BasicDialog
046 {
047 private final DirectoryLocationPanel panel = new DirectoryLocationPanel();
048
049 private boolean validSelectionMade = false;
050 private File dir;
051
052 private final JFrame parent;
053
054 /**
055 * Creates a new instance of DirectoryLocationDialog
056 */
057 public DirectoryLocationDialog(JFrame parent)
058 {
059 super(parent, true);
060 inputPanel.setLayout(new BorderLayout());
061 inputPanel.add(panel, BorderLayout.CENTER);
062
063 this.parent = parent;
064
065 panel.chooseButton.addActionListener(new ActionListener(){
066 public void actionPerformed(ActionEvent ev)
067 {
068 JFileChooser fileChooser = new JFileChooser();
069 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
070 fileChooser.setMultiSelectionEnabled(false);
071 String dirText = panel.dirText.getText();
072 if (dirText.length() > 0)
073 {
074 File currDir = new File(dirText);
075 if (currDir.exists() && currDir.isDirectory())
076 fileChooser.setSelectedFile(currDir);
077 }
078 int returnVal =
079 fileChooser.showDialog(DirectoryLocationDialog.this.parent, "Select");
080 if (returnVal == JFileChooser.APPROVE_OPTION)
081 {
082 File dir = fileChooser.getSelectedFile();
083 File d = dir.getAbsoluteFile();
084 while (!d.exists())
085 d = d.getParentFile();
086 if(!d.canWrite())
087 {
088 JOptionPane.showMessageDialog(DirectoryLocationDialog.this.parent,
089 "You do not have write access to that directory.", "Error",
090 JOptionPane.ERROR_MESSAGE);
091 return;
092 }
093
094 dir.mkdirs();
095 panel.dirText.setText(dir.getAbsolutePath());
096 }
097 }
098 });
099
100 pack();
101 if (parent != null)
102 {
103 setLocation(parent.getX() + parent.getWidth()/2 - getWidth()/2,
104 parent.getY() + parent.getHeight()/2 - getHeight()/2);
105 }
106 setModal(true);
107 setVisible(true);
108 }
109
110
111 public boolean okAction()
112 {
113 String dirText = panel.dirText.getText();
114
115 if (dirText.length() == 0)
116 return false;
117
118 dir = new File(dirText);
119
120 if (dir.exists() && dir.isFile())
121 {
122 JOptionPane.showMessageDialog(parent,
123 "<html>Please choose a <u>directory</u></html>", "Error",
124 JOptionPane.ERROR_MESSAGE);
125 return false;
126 }
127
128 File d = dir.getAbsoluteFile();
129 while (!d.exists())
130 d = d.getParentFile();
131 if(!d.canWrite())
132 {
133 JOptionPane.showMessageDialog(DirectoryLocationDialog.this.parent,
134 "You do not have write access to that directory.", "Error",
135 JOptionPane.ERROR_MESSAGE);
136 return false;
137 }
138
139 dir.mkdirs();
140 validSelectionMade = true;
141
142 return true;
143 }
144
145 public boolean wasSelectionMade(){
146 return validSelectionMade;
147 }
148
149 public File getDirectory(){
150 return dir;
151 }
152
153 static public File showDialog(JFrame parent)
154 {
155 DirectoryLocationDialog dialog = new DirectoryLocationDialog(parent);
156 if (dialog.validSelectionMade)
157 return dialog.dir;
158 return null;
159 }
160
161 }