1 /* 2 * Copyright (C) 2007 The Android Open Source Project 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 17 package com.android.ddmuilib.explorer; 18 19 import com.android.ddmlib.FileListingService; 20 import com.android.ddmlib.FileListingService.FileEntry; 21 import com.android.ddmlib.FileListingService.IListingReceiver; 22 23 import org.eclipse.jface.viewers.ITreeContentProvider; 24 import org.eclipse.jface.viewers.TreeViewer; 25 import org.eclipse.jface.viewers.Viewer; 26 import org.eclipse.swt.widgets.Display; 27 import org.eclipse.swt.widgets.Tree; 28 29 /** 30 * Content provider class for device Explorer. 31 */ 32 class DeviceContentProvider implements ITreeContentProvider { 33 34 private TreeViewer mViewer; 35 private FileListingService mFileListingService; 36 private FileEntry mRootEntry; 37 38 private IListingReceiver sListingReceiver = new IListingReceiver() { 39 public void setChildren(final FileEntry entry, FileEntry[] children) { 40 final Tree t = mViewer.getTree(); 41 if (t != null && t.isDisposed() == false) { 42 Display display = t.getDisplay(); 43 if (display.isDisposed() == false) { 44 display.asyncExec(new Runnable() { 45 public void run() { 46 if (t.isDisposed() == false) { 47 // refresh the entry. 48 mViewer.refresh(entry); 49 50 // force it open, since on linux and windows 51 // when getChildren() returns null, the node is 52 // not considered expanded. 53 mViewer.setExpandedState(entry, true); 54 } 55 } 56 }); 57 } 58 } 59 } 60 61 public void refreshEntry(final FileEntry entry) { 62 final Tree t = mViewer.getTree(); 63 if (t != null && t.isDisposed() == false) { 64 Display display = t.getDisplay(); 65 if (display.isDisposed() == false) { 66 display.asyncExec(new Runnable() { 67 public void run() { 68 if (t.isDisposed() == false) { 69 // refresh the entry. 70 mViewer.refresh(entry); 71 } 72 } 73 }); 74 } 75 } 76 } 77 }; 78 79 /** 80 * 81 */ DeviceContentProvider()82 public DeviceContentProvider() { 83 } 84 setListingService(FileListingService fls)85 public void setListingService(FileListingService fls) { 86 mFileListingService = fls; 87 } 88 89 /* (non-Javadoc) 90 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) 91 */ getChildren(Object parentElement)92 public Object[] getChildren(Object parentElement) { 93 if (parentElement instanceof FileEntry) { 94 FileEntry parentEntry = (FileEntry)parentElement; 95 96 Object[] oldEntries = parentEntry.getCachedChildren(); 97 Object[] newEntries = mFileListingService.getChildren(parentEntry, 98 true, sListingReceiver); 99 100 if (newEntries != null) { 101 return newEntries; 102 } else { 103 // if null was returned, this means the cache was not valid, 104 // and a thread was launched for ls. sListingReceiver will be 105 // notified with the new entries. 106 return oldEntries; 107 } 108 } 109 return new Object[0]; 110 } 111 112 /* (non-Javadoc) 113 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) 114 */ getParent(Object element)115 public Object getParent(Object element) { 116 if (element instanceof FileEntry) { 117 FileEntry entry = (FileEntry)element; 118 119 return entry.getParent(); 120 } 121 return null; 122 } 123 124 /* (non-Javadoc) 125 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) 126 */ hasChildren(Object element)127 public boolean hasChildren(Object element) { 128 if (element instanceof FileEntry) { 129 FileEntry entry = (FileEntry)element; 130 131 return entry.getType() == FileListingService.TYPE_DIRECTORY; 132 } 133 return false; 134 } 135 136 /* (non-Javadoc) 137 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) 138 */ getElements(Object inputElement)139 public Object[] getElements(Object inputElement) { 140 if (inputElement instanceof FileEntry) { 141 FileEntry entry = (FileEntry)inputElement; 142 if (entry.isRoot()) { 143 return getChildren(mRootEntry); 144 } 145 } 146 147 return null; 148 } 149 150 /* (non-Javadoc) 151 * @see org.eclipse.jface.viewers.IContentProvider#dispose() 152 */ dispose()153 public void dispose() { 154 } 155 156 /* (non-Javadoc) 157 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) 158 */ inputChanged(Viewer viewer, Object oldInput, Object newInput)159 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 160 if (viewer instanceof TreeViewer) { 161 mViewer = (TreeViewer)viewer; 162 } 163 if (newInput instanceof FileEntry) { 164 mRootEntry = (FileEntry)newInput; 165 } 166 } 167 } 168