1 /*******************************************************************************
2  * Copyright (c) 2000, 2009 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 
12 package com.android.ide.eclipse.adt.internal.wizards.newproject;
13 
14 import org.eclipse.jdt.internal.ui.JavaPlugin;
15 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
16 import org.eclipse.jdt.internal.ui.workingsets.IWorkingSetIDs;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Group;
23 import org.eclipse.ui.IWorkingSet;
24 import org.eclipse.ui.dialogs.WorkingSetConfigurationBlock;
25 
26 /**
27  * Copied from
28  * org.eclipse.jdt.ui.wizards.NewJavaProjectWizardPageOne$WorkingSetGroup
29  *
30  * Creates the working set group with controls that allow
31  * the selection of working sets
32  */
33 @SuppressWarnings("restriction")
34 public class WorkingSetGroup {
35 
36     private WorkingSetConfigurationBlock fWorkingSetBlock;
37     private Button mEnableButton;
38 
WorkingSetGroup()39     public WorkingSetGroup() {
40         String[] workingSetIds = new String[] {
41                 IWorkingSetIDs.JAVA, IWorkingSetIDs.RESOURCE
42         };
43         fWorkingSetBlock = new WorkingSetConfigurationBlock(workingSetIds, JavaPlugin.getDefault()
44                 .getDialogSettings());
45     }
46 
createControl(Composite composite)47     public Composite createControl(Composite composite) {
48         Group workingSetGroup = new Group(composite, SWT.NONE);
49         workingSetGroup.setFont(composite.getFont());
50         workingSetGroup.setText(NewWizardMessages.NewJavaProjectWizardPageOne_WorkingSets_group);
51         workingSetGroup.setLayout(new GridLayout(1, false));
52 
53         fWorkingSetBlock.createContent(workingSetGroup);
54 
55         // WorkingSetGroup is implemented in such a way that the checkbox it contains
56         // can only be programmatically set if there's an existing working set associated
57         // *before* we construct the control. However the control is created when the
58         // wizard is opened, not when the page is first shown.
59         //
60         // One choice is to duplicate the class in our project.
61         // Or find the checkbox we want and trigger it manually.
62         mEnableButton = findCheckbox(workingSetGroup);
63 
64         return workingSetGroup;
65     }
66 
setWorkingSets(IWorkingSet[] workingSets)67     public void setWorkingSets(IWorkingSet[] workingSets) {
68         fWorkingSetBlock.setWorkingSets(workingSets);
69     }
70 
getSelectedWorkingSets()71     public IWorkingSet[] getSelectedWorkingSets() {
72         try {
73             return fWorkingSetBlock.getSelectedWorkingSets();
74         } catch (Throwable t) {
75             // Test scenarios; no UI is created, which the fWorkingSetBlock assumes
76             // (it dereferences the enabledButton)
77             return new IWorkingSet[0];
78         }
79     }
80 
isChecked()81     public boolean isChecked() {
82         return mEnableButton == null ? false : mEnableButton.getSelection();
83     }
84 
setChecked(boolean state)85     public void setChecked(boolean state) {
86         if (mEnableButton != null) {
87             mEnableButton.setSelection(state);
88         }
89     }
90 
91     /**
92      * Finds the first button of style Checkbox in the given parent composite.
93      * Returns null if not found.
94      */
findCheckbox(Composite parent)95     private Button findCheckbox(Composite parent) {
96         for (Control control : parent.getChildren()) {
97             if (control instanceof Button && (control.getStyle() & SWT.CHECK) == SWT.CHECK) {
98                 return (Button) control;
99             } else if (control instanceof Composite) {
100                 Button found = findCheckbox((Composite) control);
101                 if (found != null) {
102                     return found;
103                 }
104             }
105         }
106 
107         return null;
108     }
109 }
110