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.dialogs.PropertyPage; 41 42 /** 43 * Property page for "Android" project. 44 * This is accessible from the Package Explorer when right clicking a project and choosing 45 * "Properties". 46 * 47 */ 48 public class AndroidPropertyPage extends PropertyPage { 49 50 private IProject mProject; 51 private SdkTargetSelector mSelector; 52 private Button mIsLibrary; 53 // APK-SPLIT: This is not yet supported, so we hide the UI 54 // private Button mSplitByDensity; 55 private LibraryProperties mLibraryDependencies; 56 private ProjectPropertiesWorkingCopy mPropertiesWorkingCopy; 57 AndroidPropertyPage()58 public AndroidPropertyPage() { 59 // pass 60 } 61 62 @Override createContents(Composite parent)63 protected Control createContents(Composite parent) { 64 // get the element (this is not yet valid in the constructor). 65 mProject = (IProject)getElement(); 66 67 // get the targets from the sdk 68 IAndroidTarget[] targets = null; 69 if (Sdk.getCurrent() != null) { 70 targets = Sdk.getCurrent().getTargets(); 71 } 72 73 // build the UI. 74 Composite top = new Composite(parent, SWT.NONE); 75 top.setLayoutData(new GridData(GridData.FILL_BOTH)); 76 top.setLayout(new GridLayout(1, false)); 77 78 Group targetGroup = new Group(top, SWT.NONE); 79 targetGroup.setLayoutData(new GridData(GridData.FILL_BOTH)); 80 targetGroup.setLayout(new GridLayout(1, false)); 81 targetGroup.setText("Project Build Target"); 82 83 mSelector = new SdkTargetSelector(targetGroup, targets); 84 85 Group libraryGroup = new Group(top, SWT.NONE); 86 libraryGroup.setLayoutData(new GridData(GridData.FILL_BOTH)); 87 libraryGroup.setLayout(new GridLayout(1, false)); 88 libraryGroup.setText("Library"); 89 90 mIsLibrary = new Button(libraryGroup, SWT.CHECK); 91 mIsLibrary.setText("Is Library"); 92 93 mLibraryDependencies = new LibraryProperties(libraryGroup); 94 95 /* 96 * APK-SPLIT: This is not yet supported, so we hide the UI 97 Group g = new Group(top, SWT.NONE); 98 g.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 99 g.setLayout(new GridLayout(1, false)); 100 g.setText("APK Generation"); 101 102 mSplitByDensity = new Button(g, SWT.CHECK); 103 mSplitByDensity.setText("One APK per density"); 104 105 */ 106 // fill the ui 107 fillUi(); 108 109 // add callbacks 110 mSelector.setSelectionListener(new SelectionAdapter() { 111 @Override 112 public void widgetSelected(SelectionEvent e) { 113 updateValidity(); 114 } 115 }); 116 117 if (mProject.isOpen() == false) { 118 // disable the ui. 119 } 120 121 return top; 122 } 123 124 @Override performOk()125 public boolean performOk() { 126 Sdk currentSdk = Sdk.getCurrent(); 127 if (currentSdk != null && mProject.isOpen()) { 128 ProjectState state = Sdk.getProjectState(mProject); 129 130 // simply update the properties copy. Eclipse will be notified of the file change 131 // and will reload it smartly (detecting differences) and updating the ProjectState. 132 // See Sdk.mFileListener 133 boolean mustSaveProp = false; 134 135 IAndroidTarget newTarget = mSelector.getSelected(); 136 if (state == null || newTarget != state.getTarget()) { 137 mPropertiesWorkingCopy.setProperty(ProjectProperties.PROPERTY_TARGET, 138 newTarget.hashString()); 139 mustSaveProp = true; 140 } 141 142 if (state == null || mIsLibrary.getSelection() != state.isLibrary()) { 143 mPropertiesWorkingCopy.setProperty(ProjectProperties.PROPERTY_LIBRARY, 144 Boolean.toString(mIsLibrary.getSelection())); 145 mustSaveProp = true; 146 } 147 148 if (mLibraryDependencies.save()) { 149 mustSaveProp = true; 150 } 151 152 if (mustSaveProp) { 153 try { 154 mPropertiesWorkingCopy.save(); 155 156 IResource projectProp = mProject.findMember(SdkConstants.FN_PROJECT_PROPERTIES); 157 projectProp.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor()); 158 } catch (Exception e) { 159 String msg = String.format( 160 "Failed to save %1$s for project %2$s", 161 SdkConstants.FN_PROJECT_PROPERTIES, mProject.getName()); 162 AdtPlugin.log(e, msg); 163 } 164 } 165 } 166 167 return true; 168 } 169 170 @Override performDefaults()171 protected void performDefaults() { 172 fillUi(); 173 updateValidity(); 174 } 175 fillUi()176 private void fillUi() { 177 if (Sdk.getCurrent() != null && mProject.isOpen()) { 178 ProjectState state = Sdk.getProjectState(mProject); 179 180 // make a working copy of the properties 181 mPropertiesWorkingCopy = state.getProperties().makeWorkingCopy(); 182 183 // get the target 184 IAndroidTarget target = state.getTarget(); 185 if (target != null) { 186 mSelector.setSelection(target); 187 } 188 189 mIsLibrary.setSelection(state.isLibrary()); 190 mLibraryDependencies.setContent(state, mPropertiesWorkingCopy); 191 192 /* 193 * APK-SPLIT: This is not yet supported, so we hide the UI 194 // get the project settings 195 ApkSettings settings = currentSdk.getApkSettings(mProject); 196 mSplitByDensity.setSelection(settings.isSplitByDpi()); 197 */ 198 } 199 200 } 201 updateValidity()202 private void updateValidity() { 203 // look for the selection and validate the page if there is a selection 204 IAndroidTarget target = mSelector.getSelected(); 205 setValid(target != null); 206 } 207 } 208