001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.preferences;
011:
012: import java.util.Set;
013:
014: import org.eclipse.core.runtime.IStatus;
015:
016: import org.eclipse.core.resources.ResourcesPlugin;
017:
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.events.SelectionEvent;
020: import org.eclipse.swt.events.SelectionListener;
021: import org.eclipse.swt.graphics.Font;
022: import org.eclipse.swt.layout.GridData;
023: import org.eclipse.swt.widgets.Button;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Control;
026: import org.eclipse.swt.widgets.Shell;
027:
028: import org.eclipse.jface.dialogs.Dialog;
029: import org.eclipse.jface.dialogs.IDialogSettings;
030: import org.eclipse.jface.viewers.DoubleClickEvent;
031: import org.eclipse.jface.viewers.IDoubleClickListener;
032: import org.eclipse.jface.viewers.ISelectionChangedListener;
033: import org.eclipse.jface.viewers.IStructuredSelection;
034: import org.eclipse.jface.viewers.SelectionChangedEvent;
035: import org.eclipse.jface.viewers.TableViewer;
036: import org.eclipse.jface.viewers.Viewer;
037: import org.eclipse.jface.viewers.ViewerFilter;
038:
039: import org.eclipse.ui.dialogs.SelectionStatusDialog;
040:
041: import org.eclipse.jdt.core.IJavaModel;
042: import org.eclipse.jdt.core.JavaCore;
043:
044: import org.eclipse.jdt.ui.JavaElementComparator;
045: import org.eclipse.jdt.ui.JavaElementLabelProvider;
046: import org.eclipse.jdt.ui.StandardJavaElementContentProvider;
047:
048: import org.eclipse.jdt.internal.ui.JavaPlugin;
049: import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
050:
051: public class ProjectSelectionDialog extends SelectionStatusDialog {
052:
053: // the visual selection widget group
054: private TableViewer fTableViewer;
055: private Set fProjectsWithSpecifics;
056:
057: // sizing constants
058: private final static int SIZING_SELECTION_WIDGET_HEIGHT = 250;
059: private final static int SIZING_SELECTION_WIDGET_WIDTH = 300;
060:
061: private final static String DIALOG_SETTINGS_SHOW_ALL = "ProjectSelectionDialog.show_all"; //$NON-NLS-1$
062:
063: private ViewerFilter fFilter;
064:
065: public ProjectSelectionDialog(Shell parentShell,
066: Set projectsWithSpecifics) {
067: super (parentShell);
068: setTitle(PreferencesMessages.ProjectSelectionDialog_title);
069: setMessage(PreferencesMessages.ProjectSelectionDialog_desciption);
070: fProjectsWithSpecifics = projectsWithSpecifics;
071:
072: int shellStyle = getShellStyle();
073: setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE);
074:
075: fFilter = new ViewerFilter() {
076: public boolean select(Viewer viewer, Object parentElement,
077: Object element) {
078: return fProjectsWithSpecifics.contains(element);
079: }
080: };
081:
082: }
083:
084: /* (non-Javadoc)
085: * Method declared on Dialog.
086: */
087: protected Control createDialogArea(Composite parent) {
088: // page group
089: Composite composite = (Composite) super
090: .createDialogArea(parent);
091:
092: Font font = parent.getFont();
093: composite.setFont(font);
094:
095: createMessageArea(composite);
096:
097: fTableViewer = new TableViewer(composite, SWT.H_SCROLL
098: | SWT.V_SCROLL | SWT.BORDER);
099: fTableViewer
100: .addSelectionChangedListener(new ISelectionChangedListener() {
101: public void selectionChanged(
102: SelectionChangedEvent event) {
103: doSelectionChanged(((IStructuredSelection) event
104: .getSelection()).toArray());
105: }
106: });
107: fTableViewer.addDoubleClickListener(new IDoubleClickListener() {
108: public void doubleClick(DoubleClickEvent event) {
109: okPressed();
110: }
111: });
112: GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
113: data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
114: data.widthHint = SIZING_SELECTION_WIDGET_WIDTH;
115: fTableViewer.getTable().setLayoutData(data);
116:
117: fTableViewer.setLabelProvider(new JavaElementLabelProvider());
118: fTableViewer
119: .setContentProvider(new StandardJavaElementContentProvider());
120: fTableViewer.setComparator(new JavaElementComparator());
121: fTableViewer.getControl().setFont(font);
122:
123: Button checkbox = new Button(composite, SWT.CHECK);
124: checkbox
125: .setText(PreferencesMessages.ProjectSelectionDialog_filter);
126: checkbox.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER,
127: true, false));
128: checkbox.addSelectionListener(new SelectionListener() {
129: public void widgetSelected(SelectionEvent e) {
130: updateFilter(((Button) e.widget).getSelection());
131: }
132:
133: public void widgetDefaultSelected(SelectionEvent e) {
134: updateFilter(((Button) e.widget).getSelection());
135: }
136: });
137: IDialogSettings dialogSettings = JavaPlugin.getDefault()
138: .getDialogSettings();
139: boolean doFilter = !dialogSettings
140: .getBoolean(DIALOG_SETTINGS_SHOW_ALL)
141: && !fProjectsWithSpecifics.isEmpty();
142: checkbox.setSelection(doFilter);
143: updateFilter(doFilter);
144:
145: IJavaModel input = JavaCore.create(ResourcesPlugin
146: .getWorkspace().getRoot());
147: fTableViewer.setInput(input);
148:
149: doSelectionChanged(new Object[0]);
150: Dialog.applyDialogFont(composite);
151: return composite;
152: }
153:
154: protected void updateFilter(boolean selected) {
155: if (selected) {
156: fTableViewer.addFilter(fFilter);
157: } else {
158: fTableViewer.removeFilter(fFilter);
159: }
160: JavaPlugin.getDefault().getDialogSettings().put(
161: DIALOG_SETTINGS_SHOW_ALL, !selected);
162: }
163:
164: private void doSelectionChanged(Object[] objects) {
165: if (objects.length != 1) {
166: updateStatus(new StatusInfo(IStatus.ERROR, "")); //$NON-NLS-1$
167: setSelectionResult(null);
168: } else {
169: updateStatus(new StatusInfo());
170: setSelectionResult(objects);
171: }
172: }
173:
174: /* (non-Javadoc)
175: * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
176: */
177: protected void computeResult() {
178: }
179: }
|