]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/ajaxplorer/file/FileType.java
48d45a2774ffe5d3dcc3aac87f1413d213079edb
[gpl/argeo-slc.git] / legacy / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / ajaxplorer / file / FileType.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.web.ajaxplorer.file;
17
18 import java.io.File;
19
20 import org.argeo.slc.web.ajaxplorer.AjxpDriverException;
21
22 public enum FileType {
23 FOLDER("folder.png", "Directory"), UNKNOWN("mime_empty.png", "Unkown"), GIF(
24 "image.png", "GIF Picture"), JPEG("image.png", "JPEG Picture"), PNG(
25 "image.png", "PNG Picture");
26
27 private final String icon;
28 private final String mimeString;
29
30 FileType(String icon, String mimeString) {
31 this.icon = icon;
32 this.mimeString = mimeString;
33 }
34
35 public String getIcon() {
36 return icon;
37 }
38
39 public String getMimeString() {
40 return mimeString;
41 }
42
43 public boolean isImage() {
44 return this == GIF || this == JPEG || this == PNG;
45 }
46
47 public String getImageType() {
48 switch (this) {
49 case GIF:
50 return "image/gif";
51 case JPEG:
52 return "image/jpeg";
53 case PNG:
54 return "image/png";
55 }
56 throw new AjxpDriverException("Image type undefined for " + this);
57 }
58
59 public static FileType findType(File file) {
60 String ext = file.isDirectory() ? null : file.getName().substring(
61 file.getName().indexOf('.') + 1);
62 return findType(ext);
63 }
64
65 /**
66 * Find the type based on the extension.
67 *
68 * @param ext
69 * the extension, null for a directory
70 */
71 public static FileType findType(String extArg) {
72 if (extArg == null)
73 return FOLDER;
74
75 String ext = extArg.toLowerCase();
76 if (ext.equals("jpg") || ext.equals("jpeg"))
77 return JPEG;
78 else if (ext.equals("gif"))
79 return GIF;
80 else if (ext.equals("png"))
81 return PNG;
82 else
83 return UNKNOWN;
84 }
85 }