]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/JARFileSelectionDialog.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / wizards / buildpaths / JARFileSelectionDialog.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.jdt.internal.ui.wizards.buildpaths;
12
13import java.io.File;
14
15import org.eclipse.swt.graphics.Image;
16import org.eclipse.swt.widgets.Shell;
17
18import org.eclipse.core.runtime.IStatus;
19
20import org.eclipse.jface.viewers.ITreeContentProvider;
21import org.eclipse.jface.viewers.LabelProvider;
22import org.eclipse.jface.viewers.Viewer;
23import org.eclipse.jface.viewers.ViewerComparator;
24import org.eclipse.jface.viewers.ViewerFilter;
25
26import org.eclipse.ui.ISharedImages;
27import org.eclipse.ui.PlatformUI;
28import org.eclipse.ui.dialogs.ISelectionStatusValidator;
29
30import org.eclipse.jdt.internal.ui.JavaPlugin;
31import org.eclipse.jdt.internal.ui.JavaPluginImages;
32import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
33import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
34import org.eclipse.jdt.internal.ui.viewsupport.FilteredElementTreeSelectionDialog;
35
36/**
37 * Selection dialog to select a JAR on the file system.
38 * Set input to a java.io.File that point to folder.
39 */
40public class JARFileSelectionDialog extends FilteredElementTreeSelectionDialog {
41
42 /**
43 * Constructor for JARFileSelectionDialog.
44 * @param parent parent shell
45 * @param multiSelect specifies if selecting multiple elements is allowed
46 * @param acceptFolders specifies if folders can be selected as well
47 */
48 public JARFileSelectionDialog(Shell parent, boolean multiSelect, boolean acceptFolders) {
49 this(parent, multiSelect, acceptFolders, false);
50 }
51
52 /**
53 * Constructor for JARFileSelectionDialog.
54 * @param parent parent shell
55 * @param multiSelect specifies if selecting multiple elements is allowed
56 * @param acceptFolders specifies if folders can be selected as well
57 * @param acceptAllArchives specifies if all archives (not just jar and zip) can be selected
58 */
59 public JARFileSelectionDialog(Shell parent, boolean multiSelect, boolean acceptFolders, boolean acceptAllArchives) {
60 super(parent, new FileLabelProvider(), new FileContentProvider(), false);
61 setComparator(new FileViewerComparator());
62 if (!acceptAllArchives) {
63 addFilter(new JARZipFileFilter(acceptFolders));
64 } else {
65 setInitialFilter(ArchiveFileFilter.JARZIP_FILTER_STRING);
66 }
67 setValidator(new FileSelectionValidator(multiSelect, acceptFolders));
68 setHelpAvailable(false);
69 }
70
71
72
73 private static class FileLabelProvider extends LabelProvider {
74 private final Image IMG_FOLDER= PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
75 private final Image IMG_JAR= JavaPlugin.getDefault().getImageRegistry().get(JavaPluginImages.IMG_OBJS_EXTJAR);
76
77 @Override
78 public Image getImage(Object element) {
79 if (element instanceof File) {
80 File curr= (File) element;
81 if (curr.isDirectory()) {
82 return IMG_FOLDER;
83 } else {
84 return IMG_JAR;
85 }
86 }
87 return null;
88 }
89
90 @Override
91 public String getText(Object element) {
92 if (element instanceof File) {
93 return BasicElementLabels.getResourceName(((File) element).getName());
94 }
95 return super.getText(element);
96 }
97 }
98
99 private static class FileContentProvider implements ITreeContentProvider {
100
101 private final Object[] EMPTY= new Object[0];
102
103 public Object[] getChildren(Object parentElement) {
104 if (parentElement instanceof File) {
105 File[] children= ((File) parentElement).listFiles();
106 if (children != null) {
107 return children;
108 }
109 }
110 return EMPTY;
111 }
112
113 public Object getParent(Object element) {
114 if (element instanceof File) {
115 return ((File) element).getParentFile();
116 }
117 return null;
118 }
119
120 public boolean hasChildren(Object element) {
121 return getChildren(element).length > 0;
122 }
123
124 public Object[] getElements(Object element) {
125 return getChildren(element);
126 }
127
128 public void dispose() {
129 }
130
131 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
132 }
133
134 }
135
136 private static class JARZipFileFilter extends ViewerFilter {
137 private final boolean fAcceptFolders;
138
139 public JARZipFileFilter(boolean acceptFolders) {
140 fAcceptFolders= acceptFolders;
141 }
142
143 @Override
144 public boolean select(Viewer viewer, Object parent, Object element) {
145 if (element instanceof File) {
146 File file= (File) element;
147 if (file.isFile()) {
148 return isArchive(file);
149 } else if (fAcceptFolders) {
150 return true;
151 } else {
152 File[] listFiles= file.listFiles();
153 if (listFiles != null) {
154 for (int i= 0; i < listFiles.length; i++) {
155 if (select(viewer, file, listFiles[i])) {
156 return true;
157 }
158 }
159 }
160 }
161 }
162 return false;
163 }
164
165 private static boolean isArchive(File file) {
166 String name= file.getName();
167 int detIndex= name.lastIndexOf('.');
168 return (detIndex != -1 && ArchiveFileFilter.isArchiveFileExtension(name.substring(detIndex + 1)));
169 }
170 }
171
172 private static class FileViewerComparator extends ViewerComparator {
173 @Override
174 public int category(Object element) {
175 if (element instanceof File) {
176 if (((File) element).isFile()) {
177 return 1;
178 }
179 }
180 return 0;
181 }
182 }
183
184 private static class FileSelectionValidator implements ISelectionStatusValidator {
185 private boolean fMultiSelect;
186 private boolean fAcceptFolders;
187
188 public FileSelectionValidator(boolean multiSelect, boolean acceptFolders) {
189 fMultiSelect= multiSelect;
190 fAcceptFolders= acceptFolders;
191 }
192
193 public IStatus validate(Object[] selection) {
194 int nSelected= selection.length;
195 if (nSelected == 0 || (nSelected > 1 && !fMultiSelect)) {
196 return new StatusInfo(IStatus.ERROR, ""); //$NON-NLS-1$
197 }
198 for (int i= 0; i < selection.length; i++) {
199 Object curr= selection[i];
200 if (curr instanceof File) {
201 File file= (File) curr;
202 if (!fAcceptFolders && !file.isFile()) {
203 return new StatusInfo(IStatus.ERROR, ""); //$NON-NLS-1$
204 }
205 }
206 }
207 return new StatusInfo();
208 }
209 }
210
211
212}