KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > launchConfigurations > DeleteAssociatedLaunchConfigurationsDialog


1 /*******************************************************************************
2  * Copyright (c) 2006 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.launchConfigurations;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.debug.core.ILaunchConfiguration;
18 import org.eclipse.debug.internal.ui.DebugUIPlugin;
19 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
20 import org.eclipse.debug.internal.ui.SWTUtil;
21 import org.eclipse.debug.ui.DebugUITools;
22 import org.eclipse.jface.dialogs.Dialog;
23 import org.eclipse.jface.dialogs.IDialogConstants;
24 import org.eclipse.jface.dialogs.IDialogSettings;
25 import org.eclipse.jface.dialogs.MessageDialogWithToggle;
26 import org.eclipse.jface.preference.IPreferenceStore;
27 import org.eclipse.jface.viewers.CheckboxTreeViewer;
28 import org.eclipse.jface.viewers.ITreeContentProvider;
29 import org.eclipse.jface.viewers.Viewer;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.SelectionEvent;
32 import org.eclipse.swt.events.SelectionListener;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Shell;
39 import org.eclipse.swt.widgets.Tree;
40 import org.eclipse.swt.widgets.TreeItem;
41 import org.eclipse.ui.dialogs.SelectionDialog;
42 import org.eclipse.ui.model.AdaptableList;
43
44 /**
45  * Provides a custom dialog for displaying launch configurations seperated by project
46  * in a tree view for the user to select
47  * @since 3.2
48  */

49 public class DeleteAssociatedLaunchConfigurationsDialog extends SelectionDialog {
50     /**
51      * Class to provide content for the DeleteAssociatedLaunchConfigsDialog
52      */

53     class DeleteContentProvider implements ITreeContentProvider {
54
55         private HashMap JavaDoc fInputMap = new HashMap JavaDoc();
56         
57         public DeleteContentProvider(HashMap JavaDoc map) {
58             if(map != null) {
59                 fInputMap = map;
60             }
61         }
62         
63         public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
64             if(parentElement instanceof IProject) {
65                 return ((ArrayList JavaDoc)fInputMap.get(parentElement)).toArray();
66             }
67             if(parentElement instanceof AdaptableList) {
68                 return fInputMap.keySet().toArray();
69             }
70             return new Object JavaDoc[0];
71         }
72
73         public Object JavaDoc getParent(Object JavaDoc element) {return null;}
74
75         public boolean hasChildren(Object JavaDoc element) {
76             if(element instanceof IProject) {
77                 return true;
78             }
79             return false;
80         }
81
82         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
83             return getChildren(inputElement);
84         }
85
86         public void dispose() {
87             fInputMap.clear();
88             fInputMap = null;
89         }
90
91         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {}
92         
93     }
94     
95     /**
96      * Provides a custom viewer for the dialog whic allows us to have custom checked state handling
97      */

98     class LCViewer extends CheckboxTreeViewer {
99
100         /**
101          * Constructor
102          * @param parent the parent to add this viewer to
103          * @param style the style of the viewer
104          */

105         public LCViewer(Composite parent, int style) {
106             super(parent, style);
107         }
108
109         /* (non-Javadoc)
110          * @see org.eclipse.jface.viewers.CheckboxTreeViewer#getCheckedElements()
111          */

112         public Object JavaDoc[] getCheckedElements() {
113             Object JavaDoc[] items = super.getCheckedElements();
114             //filter out the projects
115
ArrayList JavaDoc list = new ArrayList JavaDoc();
116             for (int i = 0; i < items.length; i++) {
117                 if(items[i] instanceof ILaunchConfiguration) {
118                     list.add(items[i]);
119                 }
120             }
121             return list.toArray();
122         }
123
124         /* (non-Javadoc)
125          * @see org.eclipse.jface.viewers.CheckboxTreeViewer#handleSelect(org.eclipse.swt.events.SelectionEvent)
126          */

127         protected void handleSelect(SelectionEvent event) {
128             if(event.detail == SWT.CHECK) {
129                 updateCheckedState((TreeItem)event.item);
130             }
131         }
132         
133          /**
134          * Update the checked state up the given element and all of its children.
135          *
136          * @param element
137          */

138         public void updateCheckedState(TreeItem item) {
139             Object JavaDoc element = item.getData();
140             if (element instanceof ILaunchConfiguration) {
141                 TreeItem parent = item.getParentItem();
142                 TreeItem[] children = parent.getItems();
143                 int checked = 0;
144                 for (int i = 0; i < children.length; i++) {
145                     if(children[i].getChecked()) {
146                         checked++;
147                     }
148                 }
149                 if(checked == 0) {
150                     setGrayChecked(parent.getData(), false);
151                 }
152                 else if(checked == children.length) {
153                     parent.setGrayed(false);
154                     parent.setChecked(true);
155                 }
156                 else {
157                     setGrayChecked(parent.getData(), true);
158                 }
159             }
160             else if (element instanceof IProject) {
161                 item.setGrayed(false);
162                 TreeItem[] children = item.getItems();
163                 for (int i = 0; i < children.length; i++) {
164                     setChecked(children[i].getData(), item.getChecked());
165                 }
166             }
167         }
168     }
169     
170     private static final String JavaDoc SETTINGS_ID = ".DELETE_ASSOCIATED_CONFIGS_DIALOG"; //$NON-NLS-1$
171
private HashMap JavaDoc fMap = null;
172     private Object JavaDoc fInput = null;
173     private Button fPrefButton = null;
174     private LCViewer fViewer = null;
175     private Object JavaDoc[] fResult = null;
176     
177     /**
178      * Constructor
179      * @param parentShell the parent shell for this dialog
180      * @param input the input for the viewer
181      * @param message the message for the top of the dialog
182      * @param map the map of project to listing of configs
183      */

184     public DeleteAssociatedLaunchConfigurationsDialog(Shell parentShell, Object JavaDoc input, String JavaDoc message, HashMap JavaDoc map) {
185         super(parentShell);
186         super.setMessage(message);
187         setShellStyle(getShellStyle() | SWT.RESIZE);
188         fMap = map;
189         fInput = input;
190     }
191
192     /* (non-Javadoc)
193      * @see org.eclipse.ui.dialogs.SelectionDialog#getDialogBoundsSettings()
194      */

195     protected IDialogSettings getDialogBoundsSettings() {
196         IDialogSettings settings = DebugUIPlugin.getDefault().getDialogSettings();
197         IDialogSettings section = settings.getSection(SETTINGS_ID);
198         if (section == null) {
199             section = settings.addNewSection(SETTINGS_ID);
200         }
201         return section;
202     }
203
204     /* (non-Javadoc)
205      * @see org.eclipse.ui.dialogs.ListDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
206      */

207     protected Control createDialogArea(Composite parent) {
208         initializeDialogUnits(parent);
209         Composite comp = (Composite) super.createDialogArea(parent);
210         SWTUtil.createLabel(comp, LaunchConfigurationsMessages.DeleteAssociatedLaunchConfigurationsDialog_0, 2);
211         fViewer = new LCViewer(comp, SWT.BORDER);
212         Tree tree = fViewer.getTree();
213         GridData gd = new GridData(GridData.FILL_BOTH);
214         gd.horizontalSpan = 2;
215         tree.setLayoutData(gd);
216         fViewer.setContentProvider(new DeleteContentProvider(fMap));
217         fViewer.setInput(fInput);
218         fViewer.setLabelProvider(DebugUITools.newDebugModelPresentation());
219         fViewer.expandAll();
220         Composite butcomp = new Composite(comp, SWT.NONE);
221         GridLayout layout = new GridLayout(2, false);
222         layout.marginHeight = 0;
223         layout.marginWidth = 0;
224         layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
225         butcomp.setLayout(layout);
226         gd = new GridData(GridData.FILL_HORIZONTAL);
227         gd.horizontalSpan = 2;
228         butcomp.setLayoutData(gd);
229         Button sall = SWTUtil.createPushButton(butcomp, LaunchConfigurationsMessages.DeleteAssociatedLaunchConfigurationsDialog_1, null);
230         sall.addSelectionListener(new SelectionListener() {
231             public void widgetDefaultSelected(SelectionEvent e) {}
232             public void widgetSelected(SelectionEvent e) {
233                 fViewer.setGrayedElements(new Object JavaDoc[0]);
234                 fViewer.setAllChecked(true);
235             }
236         });
237         Button dsall = SWTUtil.createPushButton(butcomp, LaunchConfigurationsMessages.DeleteAssociatedLaunchConfigurationsDialog_2, null);
238         dsall.addSelectionListener(new SelectionListener() {
239             public void widgetDefaultSelected(SelectionEvent e) {}
240             public void widgetSelected(SelectionEvent e) {
241                 fViewer.setAllChecked(false);
242             }
243         });
244         fPrefButton = new Button(comp, SWT.CHECK);
245         fPrefButton.setText(LaunchConfigurationsMessages.DeleteAssociatedLaunchConfigurationsDialog_3);
246         Dialog.applyDialogFont(comp);
247         return comp;
248     }
249     
250     /* (non-Javadoc)
251      * @see org.eclipse.ui.dialogs.SelectionDialog#getResult()
252      */

253     public Object JavaDoc[] getResult() {
254         return fResult;
255     }
256
257     /* (non-Javadoc)
258      * @see org.eclipse.ui.dialogs.ListDialog#okPressed()
259      */

260     protected void okPressed() {
261         fResult = fViewer.getCheckedElements();
262         //set pref if selected
263
boolean pref = fPrefButton.getSelection();
264         if(pref) {
265             IPreferenceStore store = DebugUIPlugin.getDefault().getPreferenceStore();
266             store.setValue(IInternalDebugUIConstants.PREF_DELETE_CONFIGS_ON_PROJECT_DELETE, MessageDialogWithToggle.ALWAYS);
267         }
268         super.okPressed();
269     }
270 }
271
Popular Tags