| /* |
| * Copyright (C) 2015 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.appindexing.editor; |
| |
| import com.google.api.client.util.Maps; |
| import com.google.appindexing.fetchasgoogle.FetchAsGoogleTask; |
| import com.google.appindexing.fetchasgoogle.FetchAsGoogleTask.ErrorCode; |
| import com.google.appindexing.fetchasgoogle.FetchAsGoogleTask.Status; |
| import com.google.appindexing.ui.ResultPanel; |
| import com.intellij.codeHighlighting.BackgroundEditorHighlighter; |
| import com.intellij.ide.structureView.StructureViewBuilder; |
| import com.intellij.openapi.fileEditor.FileEditor; |
| import com.intellij.openapi.fileEditor.FileEditorLocation; |
| import com.intellij.openapi.fileEditor.FileEditorState; |
| import com.intellij.openapi.fileEditor.FileEditorStateLevel; |
| import com.intellij.openapi.util.Key; |
| import com.intellij.ui.components.JBLabel; |
| import com.intellij.ui.components.JBScrollPane; |
| import com.intellij.util.ui.AsyncProcessIcon; |
| import org.jetbrains.annotations.NotNull; |
| import org.jetbrains.annotations.Nullable; |
| |
| import javax.swing.JComponent; |
| import javax.swing.JPanel; |
| import java.awt.CardLayout; |
| import java.beans.PropertyChangeListener; |
| import java.util.Map; |
| |
| /** |
| * The file editor for displaying fetch as google result, it shows the users a screenshot of the app and crawling information. |
| */ |
| public class AppIndexingFileEditor implements FileEditor { |
| |
| private JBScrollPane myScrollPane; |
| |
| private JPanel myRootPanel; |
| private JPanel myLoadingPanel; |
| private AsyncProcessIcon myLoadingIcon; |
| private JPanel myLoadingMessagePanel; |
| private JBLabel myLoadingMessage; |
| private JPanel myResultsPanel; |
| |
| private static final Map<Status, String> STATUS_TO_TEXT = Maps.newHashMap(); |
| private static final Map<ErrorCode, String> ERROR_TO_TEXT = Maps.newHashMap(); |
| |
| static { |
| STATUS_TO_TEXT.put(Status.NOT_STARTED, "The task has not started yet."); |
| STATUS_TO_TEXT.put(Status.BUILDING_APK, "Building APK..."); |
| STATUS_TO_TEXT.put(Status.UPLOADING_APK, "Uploading APK to Firebase App Indexing testing service..."); |
| STATUS_TO_TEXT.put(Status.FETCHING_RESULT, "Testing; takes about 2 minutes to complete."); |
| |
| ERROR_TO_TEXT.put(ErrorCode.UNKNOWN, "Test failed for unknown reason."); |
| ERROR_TO_TEXT.put(ErrorCode.BUILD_APK_FAILED, "Build APK failed."); |
| ERROR_TO_TEXT.put(ErrorCode.UPLOAD_APK_FAILED, "Upload APK failed."); |
| ERROR_TO_TEXT.put(ErrorCode.FETCH_RESULT_FAILED, "Fetch App Indexing result failed."); |
| ERROR_TO_TEXT.put(ErrorCode.NEED_SIGNIN, "Please sign in."); |
| ERROR_TO_TEXT.put(ErrorCode.NETWORK_ERROR, "Network error, please try later."); |
| ERROR_TO_TEXT.put(ErrorCode.INTENT_URL_NOT_SUPPORTED, "Intent url is not supported."); |
| } |
| |
| private AppIndexingVirtualFile myFile; |
| |
| public AppIndexingFileEditor(@NotNull AppIndexingVirtualFile file) { |
| myScrollPane = new JBScrollPane(myRootPanel); |
| myFile = file; |
| myFile.addContentChangeCallback(new Runnable() { |
| @Override |
| public void run() { |
| refreshUi(); |
| } |
| }); |
| refreshUi(); |
| } |
| |
| private void createUIComponents() { |
| myLoadingIcon = new AsyncProcessIcon.Big("Fetch as Google"); |
| } |
| |
| /** |
| * Refresh the UI component based on the virtual file content. |
| */ |
| private void refreshUi() { |
| FetchAsGoogleTask task = myFile.getFetchAsGoogleTask(); |
| Status status = task.getStatus(); |
| switch (status) { |
| case NOT_STARTED: |
| case BUILDING_APK: |
| case UPLOADING_APK: |
| case FETCHING_RESULT: |
| myLoadingMessage.setText(STATUS_TO_TEXT.get(status)); |
| break; |
| case FAIL: |
| case SUCCESS: |
| ResultPanel resultPanel; |
| if (status == Status.FAIL) { |
| resultPanel = ResultPanel |
| .buildFromFailedFetch(task.getCreatedTime(), myFile.getDeepLink(), task.getLanguage(), ERROR_TO_TEXT.get(task.getErrorCode())); |
| } |
| else { |
| resultPanel = |
| ResultPanel.buildFromSuccessFetch(task.getCreatedTime(), myFile.getDeepLink(), task.getLanguage(), task.getFetchResponse()); |
| } |
| myResultsPanel.add(resultPanel); |
| ((CardLayout)myRootPanel.getLayout()).show(myRootPanel, "results"); |
| } |
| } |
| |
| @NotNull |
| @Override |
| public JComponent getComponent() { |
| return myScrollPane; |
| } |
| |
| @Nullable |
| @Override |
| public JComponent getPreferredFocusedComponent() { |
| return myScrollPane; |
| } |
| |
| @NotNull |
| @Override |
| public String getName() { |
| return "App Indexing"; |
| } |
| |
| @NotNull |
| @Override |
| public FileEditorState getState(@NotNull FileEditorStateLevel fileEditorStateLevel) { |
| return FileEditorState.INSTANCE; |
| } |
| |
| @Override |
| public void setState(@NotNull FileEditorState fileEditorState) { |
| |
| } |
| |
| @Override |
| public boolean isModified() { |
| return false; |
| } |
| |
| @Override |
| public boolean isValid() { |
| return true; |
| } |
| |
| @Override |
| public void selectNotify() { |
| } |
| |
| @Override |
| public void deselectNotify() { |
| |
| } |
| |
| @Override |
| public void addPropertyChangeListener(@NotNull PropertyChangeListener propertyChangeListener) { |
| } |
| |
| @Override |
| public void removePropertyChangeListener(@NotNull PropertyChangeListener propertyChangeListener) { |
| } |
| |
| @Nullable |
| @Override |
| public BackgroundEditorHighlighter getBackgroundHighlighter() { |
| return null; |
| } |
| |
| @Nullable |
| @Override |
| public FileEditorLocation getCurrentLocation() { |
| return null; |
| } |
| |
| @Nullable |
| @Override |
| public StructureViewBuilder getStructureViewBuilder() { |
| return null; |
| } |
| |
| @Override |
| public void dispose() { |
| } |
| |
| @Nullable |
| @Override |
| public <T> T getUserData(@NotNull Key<T> key) { |
| return null; |
| } |
| |
| @Override |
| public <T> void putUserData(@NotNull Key<T> key, @Nullable T t) { |
| } |
| } |