~ubuntu-branches/ubuntu/precise/triplea/precise

« back to all changes in this revision

Viewing changes to src/util/image/FileOpen.java

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2011-11-11 21:40:11 UTC
  • Revision ID: package-import@ubuntu.com-20111111214011-sehf2rwat36o2xqf
Tags: upstream-1.3.2.2
ImportĀ upstreamĀ versionĀ 1.3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This program is free software; you can redistribute it and/or modify
 
3
 * it under the terms of the GNU General Public License as published by
 
4
 * the Free Software Foundation; either version 2 of the License, or
 
5
 * (at your option) any later version.
 
6
 * This program is distributed in the hope that it will be useful,
 
7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
8
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
9
 * GNU General Public License for more details.
 
10
 * You should have received a copy of the GNU General Public License
 
11
 * along with this program; if not, write to the Free Software
 
12
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
13
 */
 
14
 
 
15
 
 
16
/*
 
17
  @author George El-Haddad
 
18
  @email  nekromancer@users.sourceforge.net
 
19
 */
 
20
 
 
21
package util.image;
 
22
 
 
23
import java.io.*;
 
24
import javax.swing.*;
 
25
 
 
26
public class FileOpen 
 
27
{
 
28
        private final String ERR_MSG_1 = "Warning! Could not load the file!";
 
29
        
 
30
        private File file = null;
 
31
 
 
32
        /**
 
33
           Default Constructor.
 
34
           
 
35
           @param java.lang.String title  the title of the JFileChooser
 
36
           
 
37
           @exception java.lang.Exception  ex
 
38
           
 
39
           Creates a file selection dialog starting at the current
 
40
           working directory. Filters out all non-txt files and
 
41
           handles possible file load errors.
 
42
        */
 
43
        public FileOpen(String title)    
 
44
        {
 
45
                this(title, ".txt", ".gif", ".png");
 
46
                
 
47
        }
 
48
        
 
49
        public FileOpen(String title, final String... extensions)  
 
50
        {
 
51
                JFileChooser chooser = new JFileChooser();
 
52
                chooser.setDialogTitle(title);
 
53
                chooser.setCurrentDirectory(new File(System.getProperties().getProperty("user.dir")));
 
54
 
 
55
                /*
 
56
                 * Show only text and gif files
 
57
                 */
 
58
                chooser.setFileFilter(new javax.swing.filechooser.FileFilter()
 
59
                {
 
60
                        public boolean accept(File f)
 
61
                        {
 
62
                                if(f.isDirectory()) 
 
63
                                {
 
64
                                        return true;
 
65
                                }
 
66
                                for(String ex : extensions) 
 
67
                                {
 
68
                                        if(f.getName().endsWith(ex)) {
 
69
                                                return true;
 
70
                                        }                                       
 
71
                                }
 
72
                                return false;
 
73
                        }
 
74
 
 
75
                        public String getDescription()
 
76
                        {
 
77
                                StringBuffer buf = new StringBuffer();
 
78
                                for(String ex : extensions) 
 
79
                                {
 
80
                                        buf.append("*").append(ex).append(" ");                                 
 
81
                                }
 
82
                                return buf.toString();
 
83
                        }
 
84
                });
 
85
 
 
86
                int result = chooser.showOpenDialog(null);
 
87
 
 
88
                if(result == JFileChooser.CANCEL_OPTION)
 
89
                {
 
90
                        return;
 
91
                }
 
92
 
 
93
                try
 
94
                {
 
95
                        file = chooser.getSelectedFile();   //get the file
 
96
                }
 
97
                catch(Exception ex)
 
98
                {
 
99
                        JOptionPane.showMessageDialog(null,ERR_MSG_1,"Warning!", JOptionPane.WARNING_MESSAGE);
 
100
                        file = null;
 
101
                }
 
102
 
 
103
        }//constructor
 
104
        
 
105
        
 
106
        /**
 
107
           Returns the newly selected file.
 
108
           Will return null if no file is selected.
 
109
 
 
110
           @return java.io.File
 
111
        */
 
112
        public File getFile()
 
113
        {
 
114
                return file;
 
115
        }
 
116
 
 
117
 
 
118
        /**
 
119
           Returns the newly selected file.
 
120
           Will return null if no file is selected.
 
121
 
 
122
           @return java.lang.String
 
123
        */
 
124
        public String getPathString()
 
125
        {
 
126
                if(file == null)
 
127
                {
 
128
                        return null;
 
129
                }
 
130
                else
 
131
                {
 
132
                        return file.getPath();
 
133
                }
 
134
        }
 
135
 
 
136
}//end class FileOpen