aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/TemplateTestPage.java
blob: e461d5597dd2597f32a951d0bf6306ad802e8a59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.ide.eclipse.adt.internal.wizards.templates;

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import java.io.File;

/** For template developers: Test local template directory */
public class TemplateTestPage extends WizardPage
        implements SelectionListener, ModifyListener {
    private Text mLocation;
    private Button mButton;
    private static String sLocation; // Persist between repeated invocations
    private Button mProjectToggle;
    private File mTemplate;

    TemplateTestPage() {
        super("testWizardPage"); //$NON-NLS-1$
        setTitle("Wizard Tester");
        setDescription("Test a new template");
    }

    @SuppressWarnings("unused") // SWT constructors have side effects and aren't unused
    @Override
    public void createControl(Composite parent) {
        Composite container = new Composite(parent, SWT.NULL);
        setControl(container);
        container.setLayout(new GridLayout(3, false));

        Label label = new Label(container, SWT.NONE);
        label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        label.setText("Template Location:");

        mLocation = new Text(container, SWT.BORDER);
        GridData gd_mLocation = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
        gd_mLocation.widthHint = 400;
        mLocation.setLayoutData(gd_mLocation);
        if (sLocation != null) {
            mLocation.setText(sLocation);
        }
        mLocation.addModifyListener(this);

        mButton = new Button(container, SWT.FLAT);
        mButton.setText("...");

        mProjectToggle = new Button(container, SWT.CHECK);
        mProjectToggle.setEnabled(false);
        mProjectToggle.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
        mProjectToggle.setText("Full project template");
        new Label(container, SWT.NONE);
        mButton.addSelectionListener(this);
    }

    @Override
    public void setVisible(boolean visible) {
        super.setVisible(visible);
        validatePage();
    }

    private boolean validatePage() {
        String error = null;

        String path = mLocation.getText().trim();
        if (path == null || path.length() == 0) {
            error = "Select a template directory";
            mTemplate = null;
        } else {
            mTemplate = new File(path);
            if (!mTemplate.exists()) {
                error = String.format("%1$s does not exist", path);
            } else {
                // Preserve across wizard sessions
                sLocation = path;

                if (mTemplate.isDirectory()) {
                    if (!new File(mTemplate, TemplateHandler.TEMPLATE_XML).exists()) {
                        error = String.format("Not a template: missing template.xml file in %1$s ",
                                path);
                    }
                } else {
                    if (mTemplate.getName().equals(TemplateHandler.TEMPLATE_XML)) {
                        mTemplate = mTemplate.getParentFile();
                    } else {
                        error = String.format("Select a directory containing a template");
                    }
                }
            }
        }

        setPageComplete(error == null);
        if (error != null) {
            setMessage(error, IMessageProvider.ERROR);
        } else {
            setErrorMessage(null);
            setMessage(null);
        }

        return error == null;
    }

    @Override
    public void modifyText(ModifyEvent e) {
        validatePage();
    }

    @Override
    public void widgetSelected(SelectionEvent e) {
        if (e.getSource() == mButton) {
            DirectoryDialog dialog = new DirectoryDialog(mButton.getShell(), SWT.OPEN);
            String path = mLocation.getText().trim();
            if (path.length() > 0) {
                dialog.setFilterPath(path);
            }
            String file = dialog.open();
            if (file != null) {
                mLocation.setText(file);
            }
        }

        validatePage();
    }

    File getLocation() {
        return mTemplate;
    }

    boolean isProjectTemplate() {
        return mProjectToggle.getSelection();
    }

    @Override
    public void widgetDefaultSelected(SelectionEvent e) {
    }
}