KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > sourcelookup > AddSourceContainerDialog


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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.debug.internal.ui.sourcelookup;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.debug.core.DebugPlugin;
16 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
17 import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
18 import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
19 import org.eclipse.debug.internal.ui.DebugPluginImages;
20 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
21 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
22 import org.eclipse.debug.ui.DebugUITools;
23 import org.eclipse.debug.ui.sourcelookup.ISourceContainerBrowser;
24 import org.eclipse.jface.dialogs.Dialog;
25 import org.eclipse.jface.dialogs.TitleAreaDialog;
26 import org.eclipse.jface.viewers.ArrayContentProvider;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.ISelectionChangedListener;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.SelectionChangedEvent;
31 import org.eclipse.jface.viewers.TableViewer;
32 import org.eclipse.jface.viewers.ViewerComparator;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Shell;
41 import org.eclipse.swt.widgets.Table;
42 import org.eclipse.ui.PlatformUI;
43
44 /**
45  * The dialog for adding new source containers. Presents the user with a list of
46  * source container types and allows them to select one.
47  *
48  * @since 3.0
49  */

50 public class AddSourceContainerDialog extends TitleAreaDialog {
51     
52     private TableViewer fViewer;
53     private SourceContainerViewer fSourceContainerViewer;
54     private boolean fDoubleClickSelects = true;
55     private ISourceLookupDirector fDirector;
56     
57     /**
58      * Constructor
59      */

60     public AddSourceContainerDialog(Shell shell, SourceContainerViewer viewer, ISourceLookupDirector director) {
61         super(shell);
62         setShellStyle(getShellStyle() | SWT.RESIZE);
63         fSourceContainerViewer=viewer;
64         fDirector = director;
65     }
66     
67     /**
68      * Creates the dialog area to display source container types that are "browseable"
69      */

70     protected Control createDialogArea(Composite ancestor) {
71         
72         getShell().setText(SourceLookupUIMessages.addSourceLocation_title);
73         setTitle(SourceLookupUIMessages.addSourceLocation_description);
74         setTitleImage(DebugPluginImages.getImage(IInternalDebugUIConstants.IMG_ADD_SRC_LOC_WIZ));
75         
76         Composite parent = new Composite(ancestor, SWT.NULL);
77         GridData gd= new GridData(GridData.FILL_BOTH);
78         GridLayout topLayout = new GridLayout();
79         topLayout.numColumns = 1;
80         parent.setLayout(topLayout);
81         parent.setLayoutData(gd);
82                 
83         ISourceContainerType[] types = filterTypes(DebugPlugin.getDefault().getLaunchManager().getSourceContainerTypes());
84         
85         fViewer = new TableViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.SINGLE);
86         final Table table = fViewer.getTable();
87         gd = new GridData(GridData.FILL_BOTH);
88         table.setLayoutData(gd);
89
90         if (fDoubleClickSelects) {
91             table.addSelectionListener(new SelectionAdapter() {
92                 public void widgetDefaultSelected(SelectionEvent e) {
93                     if (table.getSelectionCount() == 1)
94                         okPressed();
95                 }
96             });
97         }
98         
99         fViewer.setLabelProvider(new SourceContainerLabelProvider());
100         fViewer.setContentProvider(new ArrayContentProvider());
101         fViewer.setComparator(new ViewerComparator());
102         fViewer.addSelectionChangedListener(new ISelectionChangedListener() {
103             public void selectionChanged(SelectionChangedEvent event) {
104                 ISelection selection = event.getSelection();
105                 String JavaDoc desc = null;
106                 if (!selection.isEmpty()) {
107                     ISourceContainerType type = (ISourceContainerType) ((IStructuredSelection)selection).getFirstElement();
108                     desc = type.getDescription();
109                 }
110                 setMessage(desc);
111             }
112         });
113         if(types.length != 0) {
114             fViewer.setInput(types);
115         }
116         Dialog.applyDialogFont(parent);
117         PlatformUI.getWorkbench().getHelpSystem().setHelp(getShell(), IDebugHelpContextIds.ADD_SOURCE_CONTAINER_DIALOG);
118         return parent;
119     }
120     
121     /**
122      * Removes types without browsers from the provided list of types.
123      * @param types the complete list of source container types
124      * @return the list of source container types that have browsers
125      */

126     private ISourceContainerType[] filterTypes(ISourceContainerType[] types){
127         ArrayList JavaDoc validTypes = new ArrayList JavaDoc();
128         for (int i=0; i< types.length; i++) {
129             ISourceContainerType type = types[i];
130             if (fDirector.supportsSourceContainerType(type)) {
131                 ISourceContainerBrowser sourceContainerBrowser = DebugUITools.getSourceContainerBrowser(type.getId());
132                 if(sourceContainerBrowser != null && sourceContainerBrowser.canAddSourceContainers(fDirector)) {
133                     validTypes.add(type);
134                 }
135             }
136         }
137         return (ISourceContainerType[]) validTypes.toArray(new ISourceContainerType[validTypes.size()]);
138         
139     }
140     
141     /* (non-Javadoc)
142      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
143      */

144     protected void okPressed() {
145         //single selection dialog, so take first item in array
146
//there will always be a selected item since we set it with viewer.setSelection
147
ISourceContainerType type = (ISourceContainerType) ((IStructuredSelection) fViewer.getSelection()).getFirstElement();
148         if (type != null) {
149             ISourceContainerBrowser browser = DebugUITools.getSourceContainerBrowser(type.getId());
150             if (browser != null) {
151                 ISourceContainer[] results = browser.addSourceContainers(getShell(), fDirector);
152                 if (results != null && results.length > 0) {
153                     fSourceContainerViewer.addEntries(results);
154                 }
155             }
156         }
157         super.okPressed();
158     }
159     
160 }
161
Popular Tags