Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ResourceTreeSelectionDialog |
|
| 0.0;0 | ||||
ResourceTreeSelectionDialog$1 |
|
| 0.0;0 | ||||
ResourceTreeSelectionDialog$ResourceLabelProvider |
|
| 0.0;0 | ||||
ResourceTreeSelectionDialog$ResourceTreeContentProvider |
|
| 0.0;0 |
1 | /* | |
2 | * Copyright 2004-2008 the Seasar Foundation and the Others. | |
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, | |
13 | * either express or implied. See the License for the specific language | |
14 | * governing permissions and limitations under the License. | |
15 | */ | |
16 | package org.seasar.eclipse.common.wiget; | |
17 | ||
18 | import java.util.ArrayList; | |
19 | import java.util.List; | |
20 | ||
21 | import org.eclipse.core.resources.IContainer; | |
22 | import org.eclipse.core.resources.IResource; | |
23 | import org.eclipse.core.resources.ResourcesPlugin; | |
24 | import org.eclipse.core.runtime.CoreException; | |
25 | import org.eclipse.jface.resource.ImageDescriptor; | |
26 | import org.eclipse.jface.viewers.ILabelProvider; | |
27 | import org.eclipse.jface.viewers.ITreeContentProvider; | |
28 | import org.eclipse.jface.viewers.LabelProvider; | |
29 | import org.eclipse.jface.viewers.Viewer; | |
30 | import org.eclipse.swt.graphics.Image; | |
31 | import org.eclipse.swt.widgets.Shell; | |
32 | import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; | |
33 | import org.eclipse.ui.model.IWorkbenchAdapter; | |
34 | import org.seasar.eclipse.common.util.LogUtil; | |
35 | ||
36 | /** | |
37 | * @author taichi | |
38 | * | |
39 | */ | |
40 | public class ResourceTreeSelectionDialog extends ElementTreeSelectionDialog { | |
41 | ||
42 | /** | |
43 | * @param parent | |
44 | * @param root | |
45 | * @param flags | |
46 | */ | |
47 | public ResourceTreeSelectionDialog(final Shell parent, | |
48 | final IContainer root, final int flags) { | |
49 | 0 | super(parent, new ResourceLabelProvider(), |
50 | new ResourceTreeContentProvider(flags)); | |
51 | 0 | setInput(root); |
52 | 0 | } |
53 | ||
54 | 0 | private static class ResourceLabelProvider extends LabelProvider implements |
55 | ILabelProvider { | |
56 | @Override | |
57 | public Image getImage(final Object element) { | |
58 | 0 | Image result = null; |
59 | 0 | if (element instanceof IResource) { |
60 | 0 | IResource r = (IResource) element; |
61 | 0 | IWorkbenchAdapter adapter = (IWorkbenchAdapter) r |
62 | .getAdapter(IWorkbenchAdapter.class); | |
63 | 0 | if (adapter != null) { |
64 | 0 | ImageDescriptor desc = adapter.getImageDescriptor(r); |
65 | 0 | if (desc != null) { |
66 | 0 | result = desc.createImage(); |
67 | } | |
68 | } | |
69 | } | |
70 | 0 | return result; |
71 | } | |
72 | ||
73 | @Override | |
74 | public String getText(final Object element) { | |
75 | 0 | if (element instanceof IResource) { |
76 | 0 | IResource r = (IResource) element; |
77 | 0 | return r.getName(); |
78 | } | |
79 | 0 | return super.getText(element); |
80 | } | |
81 | } | |
82 | ||
83 | private static class ResourceTreeContentProvider implements | |
84 | ITreeContentProvider { | |
85 | 0 | private int flags = IResource.NONE; |
86 | ||
87 | 0 | public ResourceTreeContentProvider(final int flags) { |
88 | 0 | this.flags = flags; |
89 | 0 | } |
90 | ||
91 | /* | |
92 | * (non-Javadoc) | |
93 | * | |
94 | * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) | |
95 | */ | |
96 | public Object[] getChildren(final Object parentElement) { | |
97 | 0 | if (parentElement instanceof IContainer) { |
98 | 0 | IContainer c = (IContainer) parentElement; |
99 | 0 | return members(c); |
100 | } | |
101 | 0 | return null; |
102 | } | |
103 | ||
104 | /* | |
105 | * (non-Javadoc) | |
106 | * | |
107 | * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) | |
108 | */ | |
109 | public Object getParent(final Object element) { | |
110 | 0 | if (element instanceof IResource) { |
111 | 0 | IResource r = (IResource) element; |
112 | 0 | return r.getParent(); |
113 | } | |
114 | 0 | return null; |
115 | } | |
116 | ||
117 | /* | |
118 | * (non-Javadoc) | |
119 | * | |
120 | * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) | |
121 | */ | |
122 | public boolean hasChildren(final Object element) { | |
123 | 0 | if (element instanceof IResource) { |
124 | 0 | IResource r = (IResource) element; |
125 | 0 | return r.getType() == IResource.PROJECT |
126 | || r.getType() == IResource.FOLDER; | |
127 | } | |
128 | 0 | return false; |
129 | } | |
130 | ||
131 | /* | |
132 | * (non-Javadoc) | |
133 | * | |
134 | * @see org.eclipse.jface.viewers.IContentProvider#dispose() | |
135 | */ | |
136 | public void dispose() { | |
137 | 0 | } |
138 | ||
139 | /* | |
140 | * (non-Javadoc) | |
141 | * | |
142 | * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, | |
143 | * java.lang.Object, java.lang.Object) | |
144 | */ | |
145 | public void inputChanged(final Viewer viewer, final Object oldInput, | |
146 | final Object newInput) { | |
147 | 0 | } |
148 | ||
149 | /* | |
150 | * (non-Javadoc) | |
151 | * | |
152 | * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) | |
153 | */ | |
154 | public Object[] getElements(final Object inputElement) { | |
155 | 0 | if (inputElement instanceof IContainer) { |
156 | 0 | IContainer c = (IContainer) inputElement; |
157 | 0 | return members(c); |
158 | } | |
159 | 0 | return null; |
160 | } | |
161 | ||
162 | private Object[] members(final IContainer c) { | |
163 | 0 | final List<IResource> result = new ArrayList<IResource>(); |
164 | try { | |
165 | 0 | for (IResource resource : c.members()) { |
166 | 0 | if ((resource.getType() & flags) != 0) { |
167 | 0 | result.add(resource); |
168 | } | |
169 | } | |
170 | 0 | } catch (CoreException e) { |
171 | 0 | LogUtil.log(ResourcesPlugin.getPlugin(), e); |
172 | 0 | } |
173 | 0 | return result.toArray(new IResource[result.size()]); |
174 | } | |
175 | } | |
176 | } |