Search in sources :

Example 21 with ConfiguredThemeEditorStyle

use of com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle in project android by JetBrains.

the class ThemeEditorComponent method loadStyleAttributes.

/**
   * Loads the theme attributes table for the current selected theme or substyle.
   */
private void loadStyleAttributes() {
    ConfiguredThemeEditorStyle selectedTheme = getPreviewTheme();
    ConfiguredThemeEditorStyle selectedStyle = null;
    if (selectedTheme == null) {
        selectedTheme = getSelectedTheme();
        selectedStyle = getCurrentSubStyle();
    }
    // Clean any previous row sorters.
    myAttributesTable.setRowSorter(null);
    myPanel.setSubstyleName(mySubStyleName);
    myPanel.getBackButton().setVisible(mySubStyleName != null);
    AndroidThemePreviewPanel previewPanel = myPreviewComponent.getPreviewPanel();
    if (selectedTheme == null) {
        if (myThemeName != null) {
            previewPanel.setErrorMessage("The theme " + myThemeName + " cannot be rendered in the current configuration");
        } else {
            previewPanel.setErrorMessage("No theme selected");
        }
        myAttributesTable.setModel(EMPTY_TABLE_MODEL);
        return;
    }
    previewPanel.setErrorMessage(null);
    myPanel.setShowThemeNotUsedWarning(false);
    if (selectedTheme.isProjectStyle()) {
        // Check whenever we reload the theme as any external file could have been changed that would affect this.
        // e.g. change to the manifest to use a theme.
        final PsiElement name = selectedTheme.getNamePsiElement();
        mySwingWorker = new SwingWorker<Boolean, Object>() {

            @Override
            protected Boolean doInBackground() throws Exception {
                // it's a project theme, so we should always have a name.
                assert name != null;
                return ReferencesSearch.search(name).findFirst() == null;
            }

            @Override
            protected void done() {
                if (isCancelled()) {
                    return;
                }
                try {
                    myPanel.setShowThemeNotUsedWarning(get());
                } catch (Exception ex) {
                    // should never happen, as we are calling get from done.
                    throw new RuntimeException(ex);
                }
            }
        };
        mySwingWorker.execute();
    }
    myThemeEditorContext.setCurrentTheme(selectedTheme);
    final Configuration configuration = myThemeEditorContext.getConfiguration();
    configuration.setTheme(selectedTheme.getStyleResourceUrl());
    myModel = new AttributesTableModel(selectedStyle != null ? selectedStyle : selectedTheme, getSelectedAttrGroup(), myThemeEditorContext);
    myModel.addThemePropertyChangedListener(new AttributesTableModel.ThemePropertyChangedListener() {

        @Override
        public void attributeChangedOnReadOnlyTheme(final EditedStyleItem attribute, final String newValue) {
            createNewThemeWithAttributeValue(attribute, newValue);
        }
    });
    myAttributesSorter = new TableRowSorter<AttributesTableModel>(myModel);
    // This is only used when the sort keys are set (only set in simple mode).
    myAttributesSorter.setComparator(0, SIMPLE_MODE_COMPARATOR);
    configureFilter();
    int row = myAttributesTable.getEditingRow();
    int column = myAttributesTable.getEditingColumn();
    // If an editor is present, remove it to update the entire table, do nothing otherwise
    myAttributesTable.removeEditor();
    // update the table
    myAttributesTable.setModel(myModel);
    myAttributesTable.setRowSorter(myAttributesSorter);
    myAttributesTable.updateRowHeights();
    // as a refresh can happen at any point, we want to restore the editor if one was present, do nothing otherwise
    myAttributesTable.editCellAt(row, column);
    myPanel.getPalette().setModel(new AttributesModelColorPaletteModel(configuration, myModel));
    myPanel.getPalette().addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                AttributesModelColorPaletteModel model = (AttributesModelColorPaletteModel) myPanel.getPalette().getModel();
                List<EditedStyleItem> references = model.getReferences((Color) e.getItem());
                if (references.isEmpty()) {
                    return;
                }
                HashSet<String> attributeNames = new HashSet<String>(references.size());
                for (EditedStyleItem item : references) {
                    attributeNames.add(item.getQualifiedName());
                }
                myAttributesFilter.setAttributesFilter(attributeNames);
                myAttributesFilter.setFilterEnabled(true);
            } else {
                myAttributesFilter.setFilterEnabled(false);
            }
            if (myAttributesTable.isEditing()) {
                myAttributesTable.getCellEditor().cancelCellEditing();
            }
            ((TableRowSorter) myAttributesTable.getRowSorter()).sort();
            myPanel.getAdvancedFilterCheckBox().getModel().setSelected(!myAttributesFilter.myIsFilterEnabled);
        }
    });
    myAttributesTable.updateRowHeights();
    ResourceResolver resourceResolver = myThemeEditorContext.getResourceResolver();
    assert resourceResolver != null;
    myPreviewComponent.setPreviewBackground(ThemeEditorUtils.getGoodContrastPreviewBackground(selectedTheme, resourceResolver));
    myPreviewComponent.reloadPreviewContents();
    myAttributesTable.repaint();
    myPanel.getThemeCombo().repaint();
}
Also used : AttributesTableModel(com.android.tools.idea.editors.theme.attributes.AttributesTableModel) ItemEvent(java.awt.event.ItemEvent) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle) EditedStyleItem(com.android.tools.idea.editors.theme.datamodels.EditedStyleItem) AndroidThemePreviewPanel(com.android.tools.idea.editors.theme.preview.AndroidThemePreviewPanel) AttributesModelColorPaletteModel(com.android.tools.idea.editors.theme.attributes.AttributesModelColorPaletteModel) ResourceResolver(com.android.ide.common.resources.ResourceResolver) ItemListener(java.awt.event.ItemListener) List(java.util.List) PsiElement(com.intellij.psi.PsiElement)

Example 22 with ConfiguredThemeEditorStyle

use of com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle in project android by JetBrains.

the class ThemeEditor method getState.

@NotNull
@Override
public FileEditorState getState(@NotNull FileEditorStateLevel fileEditorStateLevel) {
    ConfiguredThemeEditorStyle theme = myComponent.getSelectedTheme();
    ConfiguredThemeEditorStyle subStyle = myComponent.getCurrentSubStyle();
    return new ThemeEditorState(theme == null ? null : theme.getQualifiedName(), subStyle == null ? null : subStyle.getQualifiedName(), myComponent.getProportion(), myComponent.getSelectedModule().getName());
}
Also used : ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with ConfiguredThemeEditorStyle

use of com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle in project android by JetBrains.

the class ThemeEditorComponent method goToParent.

public void goToParent() {
    ConfiguredThemeEditorStyle selectedStyle = getUsedStyle();
    if (selectedStyle == null) {
        LOG.error("No style selected.");
        return;
    }
    ConfiguredThemeEditorStyle parent = getUsedStyle().getParent(myThemeEditorContext.getThemeResolver());
    assert parent != null;
    // substyle or theme navigation.
    if (isSubStyleSelected()) {
        mySubStyleName = parent.getQualifiedName();
        loadStyleAttributes();
    } else {
        myPanel.setSelectedTheme(parent.getQualifiedName());
    }
}
Also used : ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)

Example 24 with ConfiguredThemeEditorStyle

use of com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle in project android by JetBrains.

the class ThemeEditorComponent method refreshPreviewPanel.

/**
   * Refreshes the preview panel for theme previews
   */
private void refreshPreviewPanel(@NotNull String previewThemeName) {
    if (!previewThemeName.equals(myPreviewThemeName)) {
        myPreviewThemeName = previewThemeName;
        // Only refresh when we select a different theme
        ConfiguredThemeEditorStyle previewTheme = getPreviewTheme();
        if (previewTheme == null) {
            // previewTheme is not a valid theme in the current configuration
            return;
        }
        myThemeEditorContext.setCurrentTheme(previewTheme);
        final Configuration configuration = myThemeEditorContext.getConfiguration();
        configuration.setTheme(previewTheme.getStyleResourceUrl());
        ResourceResolver resourceResolver = myThemeEditorContext.getResourceResolver();
        assert resourceResolver != null;
        myPreviewComponent.setPreviewBackground(ThemeEditorUtils.getGoodContrastPreviewBackground(previewTheme, resourceResolver));
        myPreviewComponent.reloadPreviewContents();
    }
}
Also used : ResourceResolver(com.android.ide.common.resources.ResourceResolver) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)

Example 25 with ConfiguredThemeEditorStyle

use of com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle in project android by JetBrains.

the class ThemeEditorComponent method renameTheme.

/**
   * Uses Android Studio refactoring to rename the current theme
   */
private void renameTheme() {
    ConfiguredThemeEditorStyle selectedTheme = getSelectedTheme();
    assert selectedTheme != null;
    assert selectedTheme.isProjectStyle();
    PsiElement namePsiElement = selectedTheme.getNamePsiElement();
    if (namePsiElement == null) {
        return;
    }
    RenameDialog renameDialog = new RenameDialog(myThemeEditorContext.getProject(), namePsiElement, null, null);
    renameDialog.show();
    if (renameDialog.isOK()) {
        String newName = renameDialog.getNewName();
        // We don't need to call reload here, because myResourceChangeListener will take care of it
        myThemeName = selectedTheme.getQualifiedName().replace(selectedTheme.getName(), newName);
        mySubStyleName = null;
    }
}
Also used : RenameDialog(com.intellij.refactoring.rename.RenameDialog) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle) PsiElement(com.intellij.psi.PsiElement)

Aggregations

ConfiguredThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)41 Configuration (com.android.tools.idea.configurations.Configuration)21 VirtualFile (com.intellij.openapi.vfs.VirtualFile)19 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)13 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)9 EditedStyleItem (com.android.tools.idea.editors.theme.datamodels.EditedStyleItem)9 ConfigurationManager (com.android.tools.idea.configurations.ConfigurationManager)8 NotNull (org.jetbrains.annotations.NotNull)6 ResourceResolver (com.android.ide.common.resources.ResourceResolver)5 AttributesTableModel (com.android.tools.idea.editors.theme.attributes.AttributesTableModel)4 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)2 ParentThemesListModel (com.android.tools.idea.editors.theme.ParentThemesListModel)2 ConfiguredElement (com.android.tools.idea.editors.theme.datamodels.ConfiguredElement)2 PsiElement (com.intellij.psi.PsiElement)2 TableModel (javax.swing.table.TableModel)2 Nullable (org.jetbrains.annotations.Nullable)2 ResourceType (com.android.resources.ResourceType)1 IAndroidTarget (com.android.sdklib.IAndroidTarget)1 ThemeResolver (com.android.tools.idea.editors.theme.ThemeResolver)1 AttributesModelColorPaletteModel (com.android.tools.idea.editors.theme.attributes.AttributesModelColorPaletteModel)1