Search in sources :

Example 1 with AndroidThemePreviewPanel

use of com.android.tools.idea.editors.theme.preview.AndroidThemePreviewPanel 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)

Aggregations

ResourceResolver (com.android.ide.common.resources.ResourceResolver)1 AttributesModelColorPaletteModel (com.android.tools.idea.editors.theme.attributes.AttributesModelColorPaletteModel)1 AttributesTableModel (com.android.tools.idea.editors.theme.attributes.AttributesTableModel)1 ConfiguredThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)1 EditedStyleItem (com.android.tools.idea.editors.theme.datamodels.EditedStyleItem)1 AndroidThemePreviewPanel (com.android.tools.idea.editors.theme.preview.AndroidThemePreviewPanel)1 PsiElement (com.intellij.psi.PsiElement)1 ItemEvent (java.awt.event.ItemEvent)1 ItemListener (java.awt.event.ItemListener)1 List (java.util.List)1