001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Anton Avtamonov
020: * @version $Revision$
021: */package org.apache.harmony.x.swing.filechooser.windows;
022:
023: import java.io.File;
024: import java.io.FileFilter;
025: import java.io.FilenameFilter;
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.LinkedList;
029: import java.util.List;
030:
031: import javax.swing.Icon;
032:
033: import org.apache.harmony.awt.nativebridge.windows.Win32;
034: import org.apache.harmony.x.swing.filechooser.PlatformFile;
035:
036: import org.apache.harmony.awt.nativebridge.windows.WindowsConsts;
037: import org.apache.harmony.awt.nativebridge.windows.WindowsDefs;
038:
039: public class WinFile extends PlatformFile {
040: private final Win32.ITEMIDLIST absoluteItemId;
041:
042: private int description = -1;
043: private String displayName;
044:
045: public String getDisplayName() {
046: Object[] parentInfo = getParentInfo();
047: Win32.IShellFolder parentFolder = (Win32.IShellFolder) parentInfo[0];
048: String result = WinFileManager.getManager()
049: .getChildFolderDisplayName(parentFolder,
050: (Win32.ITEMIDLIST) parentInfo[1],
051: WindowsConsts.SHGDN_FORADDRESSBAR);
052: WinFileManager.getManager().release(parentFolder);
053:
054: if (result != null) {
055: displayName = result;
056: } else {
057: result = displayName;
058: }
059:
060: return result;
061: }
062:
063: public Win32.ITEMIDLIST getAbsoluteItemID() {
064: return absoluteItemId;
065: }
066:
067: public Object[] getParentInfo() {
068: return WinFileManager.getManager().getParentShellInfo(
069: absoluteItemId);
070: }
071:
072: public File getParentFile() {
073: Win32.ITEMIDLIST parentItemId = WinFileManager.getManager()
074: .getParentItemId(absoluteItemId);
075: return parentItemId != null ? WinFileManager.getManager()
076: .getWinFile(parentItemId) : null;
077: }
078:
079: public boolean isDirectory() {
080: return getAttribute(WindowsDefs.SFGAO_FOLDER)
081: && !getAttribute(WindowsDefs.SFGAO_STREAM);
082: }
083:
084: public boolean isHidden() {
085: return getAttribute(WindowsDefs.SFGAO_HIDDEN);
086: }
087:
088: public boolean exists() {
089: return isDirectory() ? true : super .exists();
090: }
091:
092: public File[] listFiles() {
093: List files = getContent();
094: return (File[]) files.toArray(new File[files.size()]);
095: }
096:
097: public File[] listFiles(final FileFilter filter) {
098: List result = getContent();
099: for (Iterator it = result.iterator(); it.hasNext();) {
100: if (!filter.accept((File) it.next())) {
101: it.remove();
102: }
103: }
104:
105: return (File[]) result.toArray(new File[result.size()]);
106: }
107:
108: public File[] listFiles(final FilenameFilter filter) {
109: List result = getContent();
110: for (Iterator it = result.iterator(); it.hasNext();) {
111: File file = (File) it.next();
112: if (!filter.accept(file.getParentFile(), file.getName())) {
113: it.remove();
114: }
115: }
116:
117: return (File[]) result.toArray(new File[result.size()]);
118: }
119:
120: public boolean isFileSystem() {
121: return getAttribute(WindowsDefs.SFGAO_FILESYSTEM);
122: }
123:
124: public boolean isRoot() {
125: Object[] parentInfo = getParentInfo();
126: Win32.IShellFolder parentFolder = (Win32.IShellFolder) parentInfo[0];
127: boolean result = parentFolder != null;
128: WinFileManager.getManager().release(parentFolder);
129:
130: return result;
131: }
132:
133: public boolean isLink() {
134: return getAttribute(WindowsDefs.SFGAO_LINK);
135: }
136:
137: public boolean isDrive() {
138: return isCDROM() || isFixedDrive() || isNetDrive() || isRAM()
139: || isRemovable() || isFloppyDrive();
140: }
141:
142: public boolean isCDROM() {
143: return getDescription() == WindowsDefs.SHDID_COMPUTER_CDROM;
144: }
145:
146: public String toString() {
147: return isFileSystem() ? super .toString() : getDisplayName();
148: }
149:
150: public boolean isFixedDrive() {
151: return getDescription() == WindowsDefs.SHDID_COMPUTER_FIXED;
152: }
153:
154: public boolean isNetDrive() {
155: return getDescription() == WindowsDefs.SHDID_COMPUTER_NETDRIVE;
156: }
157:
158: public boolean isRAM() {
159: return getDescription() == WindowsDefs.SHDID_COMPUTER_RAMDISK;
160: }
161:
162: public boolean isFloppyDrive() {
163: return getDescription() == WindowsDefs.SHDID_COMPUTER_DRIVE35
164: || getDescription() == WindowsDefs.SHDID_COMPUTER_DRIVE525;
165: }
166:
167: public boolean isRemovable() {
168: return getDescription() == WindowsDefs.SHDID_COMPUTER_REMOVABLE;
169: }
170:
171: public boolean isComputerNode() {
172: return getDescription() == WindowsDefs.SHDID_NET_SERVER;
173: }
174:
175: public String getTypeName() {
176: if (isDrive()) {
177: return getDisplayName();
178: } else {
179: Win32.SHFILEINFOW fileInfo = getFileInfo();
180: return fileInfo.get_szTypeName().getString();
181: }
182: }
183:
184: public Icon getIcon() {
185: return null;
186: }
187:
188: WinFile(final String path, final Win32.ITEMIDLIST absoluteItemId) {
189: super (path);
190: this .absoluteItemId = absoluteItemId;
191: }
192:
193: private boolean getAttribute(final int flags) {
194: Object[] parentInfo = getParentInfo();
195: Win32.IShellFolder parentFolder = (Win32.IShellFolder) parentInfo[0];
196: boolean result = (WinFileManager.getManager().getAttribute(
197: parentFolder, (Win32.ITEMIDLIST) parentInfo[1], flags) & flags) != 0;
198: WinFileManager.getManager().release(parentFolder);
199:
200: return result;
201: }
202:
203: private int getDescription() {
204: if (description == -1) {
205: Object[] parentInfo = getParentInfo();
206: Win32.IShellFolder parentFolder = (Win32.IShellFolder) parentInfo[0];
207: description = WinFileManager.getManager().getDescription(
208: parentFolder, (Win32.ITEMIDLIST) parentInfo[1]);
209: WinFileManager.getManager().release(parentFolder);
210: }
211: return description;
212: }
213:
214: List getContent() {
215: return getContent(WindowsConsts.SHCONTF_STORAGE
216: | WindowsConsts.SHCONTF_FOLDERS
217: | WindowsConsts.SHCONTF_INCLUDEHIDDEN);
218: }
219:
220: List getContent(final int contentType) {
221: if (!isDirectory()) {
222: return new ArrayList();
223: }
224:
225: List result = new LinkedList();
226: Win32.ITEMIDLIST[] children = WinFileManager.getManager()
227: .getShellContent(absoluteItemId, contentType);
228: for (int i = 0; i < children.length; i++) {
229: WinFile file = WinFileManager.getManager().getWinFile(
230: children[i]);
231: if (!result.contains(file)
232: && (file.isFileSystem() || file
233: .isFileSystemAncestor())) {
234: result.add(file);
235: }
236: }
237:
238: return result;
239: }
240:
241: private Win32.SHFILEINFOW getFileInfo() {
242: Object[] parentInfo = getParentInfo();
243: WinFileManager.getManager().release(
244: (Win32.IShellFolder) parentInfo[0]);
245: return WinFileManager.getManager().getFileInfo(
246: (Win32.ITEMIDLIST) parentInfo[1],
247: WindowsDefs.SHGFI_TYPENAME
248: | WindowsDefs.SHGFI_DISPLAYNAME
249: | WindowsDefs.SHGFI_ICON
250: | WindowsDefs.SHGFI_ICONLOCATION);
251: }
252:
253: private boolean isFileSystemAncestor() {
254: return getAttribute(WindowsDefs.SFGAO_FILESYSANCESTOR);
255: }
256: }
|