1 /* 2 * Copyright (C) 2012 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 package com.android.ide.eclipse.adt.internal.wizards.templates; 17 18 import org.eclipse.jface.dialogs.IMessageProvider; 19 import org.eclipse.jface.wizard.WizardPage; 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.events.ModifyEvent; 22 import org.eclipse.swt.events.ModifyListener; 23 import org.eclipse.swt.events.SelectionEvent; 24 import org.eclipse.swt.events.SelectionListener; 25 import org.eclipse.swt.layout.GridData; 26 import org.eclipse.swt.layout.GridLayout; 27 import org.eclipse.swt.widgets.Button; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.DirectoryDialog; 30 import org.eclipse.swt.widgets.Label; 31 import org.eclipse.swt.widgets.Text; 32 33 import java.io.File; 34 35 /** For template developers: Test local template directory */ 36 public class TemplateTestPage extends WizardPage 37 implements SelectionListener, ModifyListener { 38 private Text mLocation; 39 private Button mButton; 40 private static String sLocation; // Persist between repeated invocations 41 private Button mProjectToggle; 42 private File mTemplate; 43 44 TemplateTestPage() { 45 super("testWizardPage"); //$NON-NLS-1$ 46 setTitle("Wizard Tester"); 47 setDescription("Test a new template"); 48 } 49 50 @SuppressWarnings("unused") // SWT constructors have side effects and aren't unused 51 @Override 52 public void createControl(Composite parent) { 53 Composite container = new Composite(parent, SWT.NULL); 54 setControl(container); 55 container.setLayout(new GridLayout(3, false)); 56 57 Label label = new Label(container, SWT.NONE); 58 label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); 59 label.setText("Template Location:"); 60 61 mLocation = new Text(container, SWT.BORDER); 62 GridData gd_mLocation = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1); 63 gd_mLocation.widthHint = 400; 64 mLocation.setLayoutData(gd_mLocation); 65 if (sLocation != null) { 66 mLocation.setText(sLocation); 67 } 68 mLocation.addModifyListener(this); 69 70 mButton = new Button(container, SWT.FLAT); 71 mButton.setText("..."); 72 73 mProjectToggle = new Button(container, SWT.CHECK); 74 mProjectToggle.setEnabled(false); 75 mProjectToggle.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1)); 76 mProjectToggle.setText("Full project template"); 77 new Label(container, SWT.NONE); 78 mButton.addSelectionListener(this); 79 } 80 81 @Override 82 public void setVisible(boolean visible) { 83 super.setVisible(visible); 84 validatePage(); 85 } 86 87 private boolean validatePage() { 88 String error = null; 89 90 String path = mLocation.getText().trim(); 91 if (path == null || path.length() == 0) { 92 error = "Select a template directory"; 93 mTemplate = null; 94 } else { 95 mTemplate = new File(path); 96 if (!mTemplate.exists()) { 97 error = String.format("%1$s does not exist", path); 98 } else { 99 // Preserve across wizard sessions 100 sLocation = path; 101 102 if (mTemplate.isDirectory()) { 103 if (!new File(mTemplate, TemplateHandler.TEMPLATE_XML).exists()) { 104 error = String.format("Not a template: missing template.xml file in %1$s ", 105 path); 106 } 107 } else { 108 if (mTemplate.getName().equals(TemplateHandler.TEMPLATE_XML)) { 109 mTemplate = mTemplate.getParentFile(); 110 } else { 111 error = String.format("Select a directory containing a template"); 112 } 113 } 114 } 115 } 116 117 setPageComplete(error == null); 118 if (error != null) { 119 setMessage(error, IMessageProvider.ERROR); 120 } else { 121 setErrorMessage(null); 122 setMessage(null); 123 } 124 125 return error == null; 126 } 127 128 @Override 129 public void modifyText(ModifyEvent e) { 130 validatePage(); 131 } 132 133 @Override 134 public void widgetSelected(SelectionEvent e) { 135 if (e.getSource() == mButton) { 136 DirectoryDialog dialog = new DirectoryDialog(mButton.getShell(), SWT.OPEN); 137 String path = mLocation.getText().trim(); 138 if (path.length() > 0) { 139 dialog.setFilterPath(path); 140 } 141 String file = dialog.open(); 142 if (file != null) { 143 mLocation.setText(file); 144 } 145 } 146 147 validatePage(); 148 } 149 150 File getLocation() { 151 return mTemplate; 152 } 153 154 boolean isProjectTemplate() { 155 return mProjectToggle.getSelection(); 156 } 157 158 @Override 159 public void widgetDefaultSelected(SelectionEvent e) { 160 } 161 } 162