summaryrefslogtreecommitdiff
path: root/android-studio-plugin/src/com/google/gct/idea/samples/SampleSetupStep.java
blob: 1c32b7e8e896b0001024dc585ac69e7cd901bf8c (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * 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.google.gct.idea.samples;

import com.android.tools.idea.npw.NewProjectWizardState;
import com.android.tools.idea.npw.WizardUtils;
import com.android.tools.idea.wizard.dynamic.DynamicWizardStepWithHeaderAndDescription;
import com.android.tools.idea.wizard.dynamic.ScopedStateStore;
import com.appspot.gsamplesindex.samplesindex.model.Sample;
import com.google.gct.idea.util.GctBundle;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.HyperlinkLabel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.io.File;
import java.util.HashSet;
import java.util.Set;

import static com.google.gct.idea.samples.SampleImportWizardPath.SAMPLE_NAME;
import static com.google.gct.idea.samples.SampleImportWizardPath.SAMPLE_DIR;
import static com.google.gct.idea.samples.SampleImportWizardPath.SAMPLE_KEY;
import static com.google.gct.idea.samples.SampleImportWizardPath.SAMPLE_URL;
import static com.android.tools.idea.wizard.dynamic.ScopedStateStore.Key;

/**
 * SampleSetupStep is the second/final page in the Sample Import wizard that sets project/module name, location, and other project-global
 * parameters.
 */
public class SampleSetupStep extends DynamicWizardStepWithHeaderAndDescription {
  private JTextField myProjectNameField;
  private HyperlinkLabel myUrlField;
  private TextFieldWithBrowseButton myProjectLocationField;
  private JPanel myPanel;

  private static final String DEFAULT_SAMPLE_NAME = GctBundle.message("sample.default.name");

  public SampleSetupStep(Disposable parentDisposable) {
    super(GctBundle.message("sample.setup.title"), GctBundle.message("sample.setup.subtitle"), parentDisposable);
    setBodyComponent(myPanel);
  }

  @Override
  public void init() {
    super.init();
    myProjectLocationField.addBrowseFolderListener(GctBundle.message("select.project.location"), null, null,
                                                   FileChooserDescriptorFactory.createSingleFolderDescriptor());
    String sampleName = getUniqueName(DEFAULT_SAMPLE_NAME);
    myState.put(SAMPLE_NAME, sampleName);
    myState.put(SAMPLE_DIR, getFileLocation(sampleName).getAbsolutePath());
    register(SAMPLE_NAME, myProjectNameField);
    register(SAMPLE_DIR, myProjectLocationField);
    register(SAMPLE_URL, myUrlField, new ComponentBinding<String, HyperlinkLabel>() {
      @Override
      public void setValue(@Nullable String newValue, @NotNull HyperlinkLabel component) {
        component.setHyperlinkTarget(newValue);
        newValue = (newValue == null) ? "" : newValue;
        component.setHyperlinkText(newValue);
      }
    });
    registerValueDeriver(SAMPLE_NAME, new SampleNameValueDeriver());
    registerValueDeriver(SAMPLE_DIR, new SampleDirValueDeriver());
  }

  private static String getUniqueName(String projectName) {
    File file = getFileLocation(projectName);
    String name = projectName;
    int i = 0;
    while (file.exists()) {
      i++;
      name = projectName + i;
      file = getFileLocation(name);
    }
    return name;
  }

  private static File getFileLocation(String projectName){
    return new File(NewProjectWizardState.getProjectFileDirectory(), projectName.replaceAll("[^a-zA-Z0-9_\\-.]", ""));
  }

  @Override
  public boolean validate() {
    String applicationName = myState.get(SAMPLE_NAME);
    if (applicationName == null || applicationName.trim().isEmpty()) {
      setErrorHtml(GctBundle.message("application.name.not.set"));
      return false;
    }
    String sampleDir = myState.get(SAMPLE_DIR);
    WizardUtils.ValidationResult validationResult = WizardUtils.validateLocation(sampleDir);
    setErrorHtml(validationResult.isOk() ? "" : validationResult.getFormattedMessage());
    return !validationResult.isError();
  }

  @Override
  public JComponent getPreferredFocusedComponent() {
    return myProjectNameField;
  }

  @NotNull
  @Override
  public String getStepName() {
    return GctBundle.message("sample.setup.title");
  }

  private static class SampleNameValueDeriver extends ValueDeriver<String> {
    @Override
    public boolean respectUserEdits() {
      return false;
    }

    @Nullable
    @Override
    public Set<Key<?>> getTriggerKeys() {
      Set<Key<?>> filterKeys = new HashSet<Key<?>>(1);
      filterKeys.add(SAMPLE_KEY);
      return filterKeys;
    }

    @Nullable
    @Override
    public String deriveValue(@NotNull ScopedStateStore state, Key changedKey, @Nullable String currentValue) {
      Sample sample = state.get(SAMPLE_KEY);
      if (sample == null) {
        return "";
      }
      String sampleName = sample.getTitle();
      return getUniqueName(StringUtil.isEmpty(sampleName) ? DEFAULT_SAMPLE_NAME : sampleName);
    }
  }

  private static class SampleDirValueDeriver extends ValueDeriver<String> {
    @Nullable
    @Override
    public Set<Key<?>> getTriggerKeys() {
      Set<Key<?>> filterKeys = new HashSet<Key<?>>(1);
      filterKeys.add(SAMPLE_NAME);
      return filterKeys;
    }

    @Nullable
    @Override
    public String deriveValue(@NotNull ScopedStateStore state, Key changedKey, @Nullable String currentValue) {
      String name = state.get(SAMPLE_NAME);
      if (name == null) {
        return "";
      }
      return getFileLocation(name).getAbsolutePath();
    }
  }

  @NotNull
  @Override
  protected WizardStepHeaderSettings getStepHeader() {
    return WizardStepHeaderSettings.createProductHeader(GctBundle.message("sample.import.title"));
  }
}