summaryrefslogtreecommitdiff
path: root/android-studio-plugin/src/com/google/gct/idea/samples/SampleBrowserStep.java
blob: 038d600838fac12fc32512083be1dbe23a3d2188 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * 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.wizard.dynamic.DynamicWizardStepWithHeaderAndDescription;
import com.android.tools.idea.wizard.dynamic.ScopedStateStore;
import com.android.utils.HtmlBuilder;
import com.appspot.gsamplesindex.samplesindex.model.Sample;
import com.appspot.gsamplesindex.samplesindex.model.SampleCollection;
import com.google.common.base.Strings;
import com.google.gct.idea.util.GctBundle;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.DocumentAdapter;
import com.intellij.ui.HyperlinkLabel;
import com.intellij.ui.SearchTextField;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.treeStructure.Tree;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.text.BadLocationException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Set;

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

/**
 * SampleBrowserStep is the first page in the Sample Import wizard that allows the user to select a sample to import
 */
public class SampleBrowserStep extends DynamicWizardStepWithHeaderAndDescription {
  private Tree mySampleTree;
  private SampleImportTreeManager mySampleTreeManager;
  private JPanel myPanel;
  private HyperlinkLabel myUrlField;
  private JBLabel myDescriptionLabel;
  private SearchTextField mySearchBox;
  private JPanel myDescriptionPanel;
  private SamplePreviewPanel mySamplePreviewPanel;
  private JBScrollPane mySamplePreviewScrollPanel;
  private final SampleCollection mySampleList;
  private static final Key<Sample> SAMPLE_SCREENSHOT = createKey("SampleScreenshot", Scope.STEP, Sample.class);

  public SampleBrowserStep(@NotNull SampleCollection sampleList, Disposable parentDisposable) {
    super(GctBundle.message("sample.browser.title"), GctBundle.message("sample.browser.subtitle"), parentDisposable);
    mySampleList = sampleList;
    setBodyComponent(myPanel);
  }

  @Override
  public void init() {
    super.init();
    initSamplesTree();
    register(SAMPLE_URL, myUrlField, new ComponentBinding<String, HyperlinkLabel>() {
      @Override
      public void setValue(@Nullable String newValue, @NotNull HyperlinkLabel component) {
        component.setHyperlinkTarget(newValue);
        newValue = (StringUtil.isEmpty(newValue)) ? "" : GctBundle.message("sample.browse.source");
        component.setHyperlinkText(newValue);
      }
    });
    register(SAMPLE_SCREENSHOT, mySamplePreviewPanel, new SamplePreviewComponentBinding());
    registerValueDeriver(SAMPLE_URL, new SampleUrlValueDeriver());
    registerValueDeriver(SAMPLE_SCREENSHOT, new SamplePreviewDeriver());
    registerValueDeriver(KEY_DESCRIPTION, new DescriptionValueDeriver());
    mySearchBox.addDocumentListener(new DocumentAdapter() {
      @Override
      protected void textChanged(DocumentEvent e) {
        try {
          String keyword = e.getDocument().getText(0, e.getDocument().getLength());
          mySampleTreeManager.filterTree(keyword);
        }
        catch (BadLocationException e1) {
          // don't do anything if we can't figure out what the location is
        }
      }
    });
    myUrlField.setOpaque(false);
    myDescriptionPanel.setBackground(UIUtil.getTextFieldBackground());
    // for better mouse wheel scrolling
    mySamplePreviewScrollPanel.getVerticalScrollBar().setUnitIncrement(16);
  }

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

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

  @Override
  public boolean validate() {
    if (myState.get(SAMPLE_KEY) == null) {
      setErrorHtml(GctBundle.message("sample.browser.please.select"));
      return false;
    }
    setErrorHtml("");
    return true;
  }

  protected void initSamplesTree() {
    mySampleTreeManager = new SampleImportTreeManager(mySampleTree, mySampleList);
    myState.put(SAMPLE_KEY, mySampleTreeManager.getSelectedSample());
    register(SAMPLE_KEY, mySampleTree, new ComponentBinding<Sample, Tree>() {
      @Nullable
      @Override
      public Sample getValue(@NotNull Tree component) {
        return mySampleTreeManager.getSelectedSample();
      }

      @Override
      public void addActionListener(@NotNull ActionListener listener, @NotNull Tree component) {
        component.addTreeSelectionListener(new SampleSelectionListener(listener));
      }
    });
  }

  private static class SampleSelectionListener implements TreeSelectionListener {

    // this is required to redirect selectionListener to actionListener for use with ComponentBindings
    private final ActionListener myExternalListener;

    public SampleSelectionListener(ActionListener externalListener) {
      myExternalListener = externalListener;
    }

    @Override
    public void valueChanged(TreeSelectionEvent e) {
      if (myExternalListener != null) {
        // populating a nonsense ActionEvent
        myExternalListener.actionPerformed(new ActionEvent(e.getSource(), -1, ""));
      }
    }
  }

  private static class SampleUrlValueDeriver extends ValueDeriver<String> {
    @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 url = sample.getCloneUrl();
      String path = sample.getPath();
      if (!Strings.isNullOrEmpty(path)) {
        return url + (url.endsWith("/") ? "" : "/") + "tree/master/" + SampleImportWizardPath.trimSlashes(path);
      }
      else {
        return url;
      }
    }
  }

  private static class DescriptionValueDeriver extends ValueDeriver<String> {
    @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 "";
      }
      HtmlBuilder description = new HtmlBuilder();
      description.openHtmlBody();
      if (sample.getDescription() != null) {
        description.addHtml(sample.getDescription());
      }
      else {
        description.add(GctBundle.message("sample.browser.no.description"));
      }
      description.newlineIfNecessary().newline();
      description.add("Tags: ");
      description.add(StringUtil.join(sample.getCategories(),","));
      description.newlineIfNecessary().newline();
      description.closeHtmlBody();
      return description.getHtml();
    }
  }

  private static class SamplePreviewComponentBinding extends ComponentBinding<Sample, SamplePreviewPanel> {
    @Override
    public void setValue(@Nullable Sample sample, @NotNull SamplePreviewPanel component) {
      component.setSample(sample);
    }
  }

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

    @Nullable
    @Override
    public Sample deriveValue(@NotNull ScopedStateStore state, Key changedKey, @Nullable Sample currentValue) {
      return state.get(SAMPLE_KEY);
    }
  }

  @Override
  protected JLabel getDescriptionLabel() {
    return myDescriptionLabel;
  }

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