]>
Commit | Line | Data |
---|---|---|
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 | *******************************************************************************/ | |
11 | package org.eclipse.jdt.internal.ui.preferences; | |
12 | ||
13 | import java.util.Set; | |
14 | ||
15 | import org.eclipse.swt.SWT; | |
16 | import org.eclipse.swt.events.SelectionEvent; | |
17 | import org.eclipse.swt.events.SelectionListener; | |
18 | import org.eclipse.swt.graphics.Font; | |
19 | import org.eclipse.swt.layout.GridData; | |
20 | import org.eclipse.swt.widgets.Button; | |
21 | import org.eclipse.swt.widgets.Composite; | |
22 | import org.eclipse.swt.widgets.Control; | |
23 | import org.eclipse.swt.widgets.Shell; | |
24 | ||
25 | import org.eclipse.core.runtime.IStatus; | |
26 | ||
27 | import org.eclipse.core.resources.ResourcesPlugin; | |
28 | ||
29 | import org.eclipse.jface.dialogs.Dialog; | |
30 | import org.eclipse.jface.dialogs.IDialogSettings; | |
31 | import org.eclipse.jface.viewers.DoubleClickEvent; | |
32 | import org.eclipse.jface.viewers.IDoubleClickListener; | |
33 | import org.eclipse.jface.viewers.ISelectionChangedListener; | |
34 | import org.eclipse.jface.viewers.IStructuredSelection; | |
35 | import org.eclipse.jface.viewers.SelectionChangedEvent; | |
36 | import org.eclipse.jface.viewers.TableViewer; | |
37 | import org.eclipse.jface.viewers.Viewer; | |
38 | import org.eclipse.jface.viewers.ViewerFilter; | |
39 | ||
40 | import org.eclipse.ui.dialogs.SelectionStatusDialog; | |
41 | ||
42 | import org.eclipse.jdt.core.IJavaModel; | |
43 | import org.eclipse.jdt.core.IJavaProject; | |
44 | import org.eclipse.jdt.core.JavaCore; | |
45 | ||
46 | import org.eclipse.jdt.ui.JavaElementComparator; | |
47 | import org.eclipse.jdt.ui.JavaElementLabelProvider; | |
48 | import org.eclipse.jdt.ui.StandardJavaElementContentProvider; | |
49 | ||
50 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
51 | import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; | |
52 | ||
53 | public class ProjectSelectionDialog extends SelectionStatusDialog { | |
54 | ||
55 | // the visual selection widget group | |
56 | private TableViewer fTableViewer; | |
57 | private Set<IJavaProject> fProjectsWithSpecifics; | |
58 | ||
59 | // sizing constants | |
60 | private final static int SIZING_SELECTION_WIDGET_HEIGHT= 250; | |
61 | private final static int SIZING_SELECTION_WIDGET_WIDTH= 300; | |
62 | ||
63 | private final static String DIALOG_SETTINGS_SHOW_ALL= "ProjectSelectionDialog.show_all"; //$NON-NLS-1$ | |
64 | ||
65 | private ViewerFilter fFilter; | |
66 | ||
67 | public ProjectSelectionDialog(Shell parentShell, Set<IJavaProject> projectsWithSpecifics) { | |
68 | super(parentShell); | |
69 | setTitle(PreferencesMessages.ProjectSelectionDialog_title); | |
70 | setMessage(PreferencesMessages.ProjectSelectionDialog_desciption); | |
71 | fProjectsWithSpecifics= projectsWithSpecifics; | |
72 | ||
73 | fFilter= new ViewerFilter() { | |
74 | @Override | |
75 | public boolean select(Viewer viewer, Object parentElement, Object element) { | |
76 | return fProjectsWithSpecifics.contains(element); | |
77 | } | |
78 | }; | |
79 | } | |
80 | ||
81 | ||
82 | /* (non-Javadoc) | |
83 | * Method declared on Dialog. | |
84 | */ | |
85 | @Override | |
86 | protected Control createDialogArea(Composite parent) { | |
87 | // page group | |
88 | Composite composite= (Composite) super.createDialogArea(parent); | |
89 | ||
90 | Font font= parent.getFont(); | |
91 | composite.setFont(font); | |
92 | ||
93 | createMessageArea(composite); | |
94 | ||
95 | fTableViewer= new TableViewer(composite, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); | |
96 | fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() { | |
97 | public void selectionChanged(SelectionChangedEvent event) { | |
98 | doSelectionChanged(((IStructuredSelection) event.getSelection()).toArray()); | |
99 | } | |
100 | }); | |
101 | fTableViewer.addDoubleClickListener(new IDoubleClickListener() { | |
102 | public void doubleClick(DoubleClickEvent event) { | |
103 | okPressed(); | |
104 | } | |
105 | }); | |
106 | GridData data= new GridData(SWT.FILL, SWT.FILL, true, true); | |
107 | data.heightHint= SIZING_SELECTION_WIDGET_HEIGHT; | |
108 | data.widthHint= SIZING_SELECTION_WIDGET_WIDTH; | |
109 | fTableViewer.getTable().setLayoutData(data); | |
110 | ||
111 | fTableViewer.setLabelProvider(new JavaElementLabelProvider()); | |
112 | fTableViewer.setContentProvider(new StandardJavaElementContentProvider()); | |
113 | fTableViewer.setComparator(new JavaElementComparator()); | |
114 | fTableViewer.getControl().setFont(font); | |
115 | ||
116 | Button checkbox= new Button(composite, SWT.CHECK); | |
117 | checkbox.setText(PreferencesMessages.ProjectSelectionDialog_filter); | |
118 | checkbox.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, true, false)); | |
119 | checkbox.addSelectionListener(new SelectionListener() { | |
120 | public void widgetSelected(SelectionEvent e) { | |
121 | updateFilter(((Button) e.widget).getSelection()); | |
122 | } | |
123 | public void widgetDefaultSelected(SelectionEvent e) { | |
124 | updateFilter(((Button) e.widget).getSelection()); | |
125 | } | |
126 | }); | |
127 | IDialogSettings dialogSettings= JavaPlugin.getDefault().getDialogSettings(); | |
128 | boolean doFilter= !dialogSettings.getBoolean(DIALOG_SETTINGS_SHOW_ALL) && !fProjectsWithSpecifics.isEmpty(); | |
129 | checkbox.setSelection(doFilter); | |
130 | updateFilter(doFilter); | |
131 | ||
132 | IJavaModel input= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); | |
133 | fTableViewer.setInput(input); | |
134 | ||
135 | doSelectionChanged(new Object[0]); | |
136 | Dialog.applyDialogFont(composite); | |
137 | return composite; | |
138 | } | |
139 | ||
140 | protected void updateFilter(boolean selected) { | |
141 | if (selected) { | |
142 | fTableViewer.addFilter(fFilter); | |
143 | } else { | |
144 | fTableViewer.removeFilter(fFilter); | |
145 | } | |
146 | JavaPlugin.getDefault().getDialogSettings().put(DIALOG_SETTINGS_SHOW_ALL, !selected); | |
147 | } | |
148 | ||
149 | private void doSelectionChanged(Object[] objects) { | |
150 | if (objects.length != 1) { | |
151 | updateStatus(new StatusInfo(IStatus.ERROR, "")); //$NON-NLS-1$ | |
152 | setSelectionResult(null); | |
153 | } else { | |
154 | updateStatus(new StatusInfo()); | |
155 | setSelectionResult(objects); | |
156 | } | |
157 | } | |
158 | ||
159 | /* (non-Javadoc) | |
160 | * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult() | |
161 | */ | |
162 | @Override | |
163 | protected void computeResult() { | |
164 | } | |
165 | } |