~ubuntu-branches/ubuntu/trusty/cdk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/applications/jchempaint/io/JCPExportFileFilter.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $RCSfile$
 
2
 * $Author: egonw $
 
3
 * $Date: 2007-01-04 18:26:00 +0100 (Thu, 04 Jan 2007) $
 
4
 * $Revision: 7634 $
 
5
 * 
 
6
 * Copyright (C) 2003-2007  The JChemPaint project
 
7
 *
 
8
 * Contact: jchempaint-devel@lists.sourceforge.net
 
9
 *
 
10
 * This program is free software; you can redistribute it and/or
 
11
 * modify it under the terms of the GNU Lesser General Public License
 
12
 * as published by the Free Software Foundation; either version 2.1
 
13
 * of the License, or (at your option) any later version.
 
14
 * All we ask is that proper credit is given for our work, which includes
 
15
 * - but is not limited to - adding the above copyright notice to the beginning
 
16
 * of your source code files, and to any copyright notice that you may distribute
 
17
 * with programs based on this work.
 
18
 * 
 
19
 * This program is distributed in the hope that it will be useful,
 
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
 * GNU Lesser General Public License for more details.
 
23
 * 
 
24
 * You should have received a copy of the GNU Lesser General Public License
 
25
 * along with this program; if not, write to the Free Software
 
26
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 
27
 */
 
28
package org.openscience.cdk.applications.jchempaint.io;
 
29
 
 
30
import java.io.File;
 
31
import java.util.List;
 
32
import java.util.ArrayList;
 
33
 
 
34
import javax.swing.JFileChooser;
 
35
 
 
36
/**
 
37
 * An export filter for JCP file formats
 
38
 *
 
39
 * @cdk.module jchempaint 
 
40
 * @author  Egon Willighagen
 
41
 * @cdk.created 2003-04-01
 
42
 */
 
43
public class JCPExportFileFilter extends javax.swing.filechooser.FileFilter implements IJCPFileFilter {
 
44
 
 
45
    // only those extensions are given here that are *not* on JCPFileFilter
 
46
    public final static String bmp = "bmp";
 
47
    public final static String png = "png";
 
48
    public final static String tiff = "tiff";
 
49
    public final static String jpg = "jpg";
 
50
    public final static String svg = "svg";
 
51
 
 
52
    protected List types;
 
53
 
 
54
    public JCPExportFileFilter(String type) {
 
55
                super();
 
56
                types = new ArrayList();
 
57
        types.add(type);
 
58
    }
 
59
 
 
60
    /**
 
61
     * Adds the JCPFileFilter to the JFileChooser object.
 
62
     */
 
63
    public static void addChoosableFileFilters(JFileChooser chooser) {
 
64
        chooser.addChoosableFileFilter(new JCPExportFileFilter(JCPExportFileFilter.bmp));
 
65
        chooser.addChoosableFileFilter(new JCPExportFileFilter(JCPExportFileFilter.jpg));
 
66
        chooser.addChoosableFileFilter(new JCPExportFileFilter(JCPExportFileFilter.png));
 
67
        chooser.addChoosableFileFilter(new JCPExportFileFilter(JCPExportFileFilter.svg));
 
68
        chooser.addChoosableFileFilter(new JCPExportFileFilter(JCPExportFileFilter.tiff));
 
69
    }
 
70
 
 
71
    /**
 
72
     * The description of this filter.
 
73
     */
 
74
    public String getDescription() {
 
75
        String type = (String)types.get(0);
 
76
        String result = "Unknown";
 
77
        if (type.equals(png)) {
 
78
            result = "PNG";
 
79
        } else if (type.equals(bmp)) {
 
80
            result = "BMP";
 
81
        } else if (type.equals(tiff)) {
 
82
            result = "TIFF";
 
83
        } else if (type.equals(jpg)) {
 
84
            result = "JPEG";
 
85
        } else if (type.equals(svg)) {
 
86
            result = "Scalable Vector Graphics";
 
87
        }
 
88
        return result;
 
89
    }
 
90
 
 
91
    // Accept all directories and all gif, jpg, or tiff files.
 
92
    public boolean accept(File f) {
 
93
        boolean accepted = false;
 
94
        if (f.isDirectory()) {
 
95
            accepted = true;
 
96
        }
 
97
 
 
98
        String extension = getExtension(f);
 
99
                if (extension != null) {
 
100
            if (types.contains(extension)) {
 
101
                accepted = true;
 
102
            }
 
103
        }
 
104
        return accepted;
 
105
    }
 
106
 
 
107
    /*
 
108
     * Get the extension of a file.
 
109
     */
 
110
    public static String getExtension(File f) 
 
111
        {
 
112
        String ext = null;
 
113
        String s = f.getName();
 
114
        int i = s.lastIndexOf('.');
 
115
 
 
116
        if (i > 0 &&  i < s.length() - 1) {
 
117
            ext = s.substring(i+1).toLowerCase();
 
118
        }
 
119
        return ext;
 
120
    }
 
121
 
 
122
        public String getType() {
 
123
                return (String)types.get(0);
 
124
        }
 
125
 
 
126
        public void setType(String type) {
 
127
                types.add(type);
 
128
        }
 
129
}