1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ide.eclipse.adt.internal.properties; 18 19 import com.android.ide.eclipse.adt.AdtPlugin; 20 import com.android.ide.eclipse.adt.internal.sdk.ProjectState; 21 import com.android.ide.eclipse.adt.internal.sdk.Sdk; 22 import com.android.sdklib.IAndroidTarget; 23 import com.android.sdklib.SdkConstants; 24 import com.android.sdklib.internal.project.ProjectProperties; 25 import com.android.sdklib.internal.project.ProjectPropertiesWorkingCopy; 26 import com.android.sdkuilib.internal.widgets.SdkTargetSelector; 27 28 import org.eclipse.core.resources.IProject; 29 import org.eclipse.core.resources.IResource; 30 import org.eclipse.core.runtime.NullProgressMonitor; 31 import org.eclipse.swt.SWT; 32 import org.eclipse.swt.events.SelectionAdapter; 33 import org.eclipse.swt.events.SelectionEvent; 34 import org.eclipse.swt.layout.GridData; 35 import org.eclipse.swt.layout.GridLayout; 36 import org.eclipse.swt.widgets.Button; 37 import org.eclipse.swt.widgets.Composite; 38 import org.eclipse.swt.widgets.Control; 39 import org.eclipse.swt.widgets.Group; 40 import org.eclipse.ui.IWorkbenchPropertyPage; 41 import org.eclipse.ui.dialogs.PropertyPage; 42 43 /** 44 * Property page for "Android" project. 45 * This is accessible from the Package Explorer when right clicking a project and choosing 46 * "Properties". 47 * 48 */ 49 public class AndroidPropertyPage extends PropertyPage implements IWorkbenchPropertyPage { 50 51 private IProject mProject; 52 private SdkTargetSelector mSelector; 53 private Button mIsLibrary; 54 // APK-SPLIT: This is not yet supported, so we hide the UI 55 // private Button mSplitByDensity; 56 private LibraryProperties mLibraryDependencies; 57 private ProjectPropertiesWorkingCopy mPropertiesWorkingCopy; 58 AndroidPropertyPage()59 public AndroidPropertyPage() { 60 // pass 61 } 62 63 @Override createContents(Composite parent)64 protected Control createContents(Composite parent) { 65 // get the element (this is not yet valid in the constructor). 66 mProject = (IProject)getElement(); 67 68 // get the targets from the sdk 69 IAndroidTarget[] targets = null; 70 if (Sdk.getCurrent() != null) { 71 targets = Sdk.getCurrent().getTargets(); 72 } 73 74 // build the UI. 75 Composite top = new Composite(parent, SWT.NONE); 76 top.setLayoutData(new GridData(GridData.FILL_BOTH)); 77 top.setLayout(new GridLayout(1, false)); 78 79 Group targetGroup = new Group(top, SWT.NONE); 80 targetGroup.setLayoutData(new GridData(GridData.FILL_BOTH)); 81 targetGroup.setLayout(new GridLayout(1, false)); 82 targetGroup.setText("Project Build Target"); 83 84 mSelector = new SdkTargetSelector(targetGroup, targets); 85 86 Group libraryGroup = new Group(top, SWT.NONE); 87 libraryGroup.setLayoutData(new GridData(GridData.FILL_BOTH)); 88 libraryGroup.setLayout(new GridLayout(1, false)); 89 libraryGroup.setText("Library"); 90 91 mIsLibrary = new Button(libraryGroup, SWT.CHECK); 92 mIsLibrary.setText("Is Library"); 93 94 mLibraryDependencies = new LibraryProperties(libraryGroup); 95 96 /* 97 * APK-SPLIT: This is not yet supported, so we hide the UI 98 Group g = new Group(top, SWT.NONE); 99 g.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 100 g.setLayout(new GridLayout(1, false)); 101 g.setText("APK Generation"); 102 103 mSplitByDensity = new Button(g, SWT.CHECK); 104 mSplitByDensity.setText("One APK per density"); 105 106 */ 107 // fill the ui 108 fillUi(); 109 110 // add callbacks 111 mSelector.setSelectionListener(new SelectionAdapter() { 112 @Override 113 public void widgetSelected(SelectionEvent e) { 114 updateValidity(); 115 } 116 }); 117 118 if (mProject.isOpen() == false) { 119 // disable the ui. 120 } 121 122 return top; 123 } 124 125 @Override performOk()126 public boolean performOk() { 127 Sdk currentSdk = Sdk.getCurrent(); 128 if (currentSdk != null && mProject.isOpen()) { 129 ProjectState state = Sdk.getProjectState(mProject); 130 131 // simply update the properties copy. Eclipse will be notified of the file change 132 // and will reload it smartly (detecting differences) and updating the ProjectState. 133 // See Sdk.mFileListener 134 boolean mustSaveProp = false; 135 136 IAndroidTarget newTarget = mSelector.getSelected(); 137 if (newTarget != state.getTarget()) { 138 mPropertiesWorkingCopy.setProperty(ProjectProperties.PROPERTY_TARGET, 139 newTarget.hashString()); 140 mustSaveProp = true; 141 } 142 143 if (mIsLibrary.getSelection() != state.isLibrary()) { 144 mPropertiesWorkingCopy.setProperty(ProjectProperties.PROPERTY_LIBRARY, 145 Boolean.toString(mIsLibrary.getSelection())); 146 mustSaveProp = true; 147 } 148 149 if (mLibraryDependencies.save()) { 150 mustSaveProp = true; 151 } 152 153 // TODO: update ApkSettings. 154 155 if (mustSaveProp) { 156 try { 157 mPropertiesWorkingCopy.save(); 158 159 IResource defaultProp = mProject.findMember(SdkConstants.FN_DEFAULT_PROPERTIES); 160 defaultProp.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor()); 161 } catch (Exception e) { 162 String msg = String.format( 163 "Failed to save %1$s for project %2$s", 164 SdkConstants.FN_DEFAULT_PROPERTIES, mProject.getName()); 165 AdtPlugin.log(e, msg); 166 } 167 } 168 } 169 170 return true; 171 } 172 173 @Override performDefaults()174 protected void performDefaults() { 175 fillUi(); 176 updateValidity(); 177 } 178 fillUi()179 private void fillUi() { 180 if (Sdk.getCurrent() != null && mProject.isOpen()) { 181 ProjectState state = Sdk.getProjectState(mProject); 182 183 // make a working copy of the properties 184 mPropertiesWorkingCopy = state.getProperties().makeWorkingCopy(); 185 186 // get the target 187 IAndroidTarget target = state.getTarget();; 188 if (target != null) { 189 mSelector.setSelection(target); 190 } 191 192 mIsLibrary.setSelection(state.isLibrary()); 193 mLibraryDependencies.setContent(state, mPropertiesWorkingCopy); 194 195 /* 196 * APK-SPLIT: This is not yet supported, so we hide the UI 197 // get the project settings 198 ApkSettings settings = currentSdk.getApkSettings(mProject); 199 mSplitByDensity.setSelection(settings.isSplitByDpi()); 200 */ 201 } 202 203 } 204 updateValidity()205 private void updateValidity() { 206 // look for the selection and validate the page if there is a selection 207 IAndroidTarget target = mSelector.getSelected(); 208 setValid(target != null); 209 } 210 211 } 212