2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.ddmuilib.explorer;
19 import com.android.ddmlib.FileListingService;
20 import com.android.ddmlib.FileListingService.FileEntry;
21 import com.android.ddmlib.FileListingService.IListingReceiver;
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;
30 * Content provider class for device Explorer.
32 class DeviceContentProvider implements ITreeContentProvider {
34 private TreeViewer mViewer;
35 private FileListingService mFileListingService;
36 private FileEntry mRootEntry;
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() {
46 if (t.isDisposed() == false) {
48 mViewer.refresh(entry);
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);
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() {
68 if (t.isDisposed() == false) {
70 mViewer.refresh(entry);
82 public DeviceContentProvider() {
85 public void setListingService(FileListingService fls) {
86 mFileListingService = fls;
90 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
92 public Object[] getChildren(Object parentElement) {
93 if (parentElement instanceof FileEntry) {
94 FileEntry parentEntry = (FileEntry)parentElement;
96 Object[] oldEntries = parentEntry.getCachedChildren();
97 Object[] newEntries = mFileListingService.getChildren(parentEntry,
98 true, sListingReceiver);
100 if (newEntries != null) {
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.
109 return new Object[0];
113 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
115 public Object getParent(Object element) {
116 if (element instanceof FileEntry) {
117 FileEntry entry = (FileEntry)element;
119 return entry.getParent();
125 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
127 public boolean hasChildren(Object element) {
128 if (element instanceof FileEntry) {
129 FileEntry entry = (FileEntry)element;
131 return entry.getType() == FileListingService.TYPE_DIRECTORY;
137 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
139 public Object[] getElements(Object inputElement) {
140 if (inputElement instanceof FileEntry) {
141 FileEntry entry = (FileEntry)inputElement;
142 if (entry.isRoot()) {
143 return getChildren(mRootEntry);
151 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
153 public void dispose() {
157 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
159 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
160 if (viewer instanceof TreeViewer) {
161 mViewer = (TreeViewer)viewer;
163 if (newInput instanceof FileEntry) {
164 mRootEntry = (FileEntry)newInput;