1 /*
2  * Copyright (C) 2011 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.ndk.internal.preferences;
18 
19 import com.android.ide.eclipse.ndk.internal.Activator;
20 import com.android.ide.eclipse.ndk.internal.Messages;
21 import com.android.ide.eclipse.ndk.internal.NdkManager;
22 
23 import org.eclipse.jface.preference.DirectoryFieldEditor;
24 import org.eclipse.jface.preference.FieldEditorPreferencePage;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Text;
27 import org.eclipse.ui.IWorkbench;
28 import org.eclipse.ui.IWorkbenchPreferencePage;
29 
30 public class NdkPreferencePage extends FieldEditorPreferencePage implements
31         IWorkbenchPreferencePage {
32 
33     private NdkDirectoryFieldEditor mNdkDirectoryEditor;
34 
NdkPreferencePage()35     public NdkPreferencePage() {
36         super(GRID);
37         setPreferenceStore(Activator.getDefault().getPreferenceStore());
38         setDescription(Messages.NDKPreferencePage_Preferences);
39     }
40 
41     @Override
createFieldEditors()42     protected void createFieldEditors() {
43         mNdkDirectoryEditor = new NdkDirectoryFieldEditor(NdkManager.NDK_LOCATION,
44                 Messages.NDKPreferencePage_Location, getFieldEditorParent());
45         addField(mNdkDirectoryEditor);
46     }
47 
48     private static class NdkDirectoryFieldEditor extends DirectoryFieldEditor {
NdkDirectoryFieldEditor(String name, String labelText, Composite parent)49         public NdkDirectoryFieldEditor(String name, String labelText, Composite parent) {
50             super(name, labelText, parent);
51             setEmptyStringAllowed(true);
52         }
53 
54         @Override
doCheckState()55         protected boolean doCheckState() {
56             if (!super.doCheckState()) {
57                 setErrorMessage(Messages.NDKPreferencePage_not_a_valid_directory);
58                 return false;
59             }
60 
61             String dirname = getTextControl().getText().trim();
62             if (!dirname.isEmpty() && !NdkManager.isValidNdkLocation(dirname)) {
63                 setErrorMessage(Messages.NDKPreferencePage_not_a_valid_NDK_directory);
64                 return false;
65             }
66 
67             return true;
68         }
69 
70         @Override
getTextControl(Composite parent)71         public Text getTextControl(Composite parent) {
72             setValidateStrategy(VALIDATE_ON_KEY_STROKE);
73             return super.getTextControl(parent);
74         }
75 
76     }
77 
78     @Override
init(IWorkbench workbench)79     public void init(IWorkbench workbench) {
80         // Nothing to do herea
81     }
82 
83     @Override
dispose()84     public void dispose() {
85         super.dispose();
86 
87         if (mNdkDirectoryEditor != null) {
88             mNdkDirectoryEditor.dispose();
89             mNdkDirectoryEditor = null;
90         }
91     }
92 
93 }
94