Search in sources :

Example 1 with AttributesTableModel

use of com.android.tools.idea.editors.theme.attributes.AttributesTableModel in project android by JetBrains.

the class ThemeEditorTable method getPopupMenuAtCell.

private JPopupMenu getPopupMenuAtCell(final int row, final int column) {
    if (row < 0 || column < 0) {
        return null;
    }
    TableModel rawModel = getModel();
    if (!(rawModel instanceof AttributesTableModel)) {
        return null;
    }
    final AttributesTableModel model = (AttributesTableModel) rawModel;
    AttributesTableModel.RowContents contents = model.getRowContents(this.convertRowIndexToModel(row));
    if (contents instanceof AttributesTableModel.AttributeContents) {
        final AttributesTableModel.AttributeContents attribute = (AttributesTableModel.AttributeContents) contents;
        final EditedStyleItem item = attribute.getValue();
        if (item == null) {
            return null;
        }
        final JBPopupMenu popupMenu = new JBPopupMenu();
        if (attribute.getCellClass(1) == ConfiguredThemeEditorStyle.class) {
            popupMenu.add(new AbstractAction(GO_TO_DECLARATION) {

                @Override
                public void actionPerformed(ActionEvent e) {
                    myGoToListener.goTo(item);
                }
            });
        } else {
            final ResourceResolver resolver = myContext.getResourceResolver();
            assert resolver != null;
            final Project project = myContext.getProject();
            final ResourceValue resourceValue = resolver.resolveResValue(item.getSelectedValue());
            final File file = new File(resourceValue.getValue());
            final VirtualFileManager manager = VirtualFileManager.getInstance();
            final VirtualFile virtualFile = file.exists() ? manager.findFileByUrl("file://" + file.getAbsolutePath()) : null;
            if (virtualFile != null) {
                popupMenu.add(new AbstractAction(GO_TO_DECLARATION) {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        final OpenFileDescriptor descriptor = new OpenFileDescriptor(project, virtualFile);
                        FileEditorManager.getInstance(project).openEditor(descriptor, true);
                    }
                });
            }
        }
        myJavadocAction.setCurrentItem(item);
        popupMenu.add(myJavadocAction);
        final ConfiguredThemeEditorStyle selectedStyle = model.getSelectedStyle();
        if (!selectedStyle.isReadOnly() && selectedStyle.hasItem(item)) {
            popupMenu.add(new AbstractAction("Reset value") {

                @Override
                public void actionPerformed(ActionEvent e) {
                    selectedStyle.removeAttribute(item.getQualifiedName());
                    model.fireTableCellUpdated(attribute.getRowIndex(), 0);
                }
            });
        }
        return popupMenu;
    } else if (contents instanceof AttributesTableModel.ParentAttribute) {
        final ConfiguredThemeEditorStyle parentStyle = model.getSelectedStyle().getParent();
        if (parentStyle == null) {
            return null;
        }
        final JBPopupMenu menu = new JBPopupMenu();
        menu.add(new AbstractAction(GO_TO_DECLARATION) {

            @Override
            public void actionPerformed(ActionEvent e) {
                myGoToListener.goToParent();
            }
        });
        return menu;
    }
    return null;
}
Also used : AttributesTableModel(com.android.tools.idea.editors.theme.attributes.AttributesTableModel) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFileManager(com.intellij.openapi.vfs.VirtualFileManager) ActionEvent(java.awt.event.ActionEvent) JBPopupMenu(com.intellij.openapi.ui.JBPopupMenu) EditedStyleItem(com.android.tools.idea.editors.theme.datamodels.EditedStyleItem) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle) Project(com.intellij.openapi.project.Project) ResourceResolver(com.android.ide.common.resources.ResourceResolver) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) TableModel(javax.swing.table.TableModel) AttributesTableModel(com.android.tools.idea.editors.theme.attributes.AttributesTableModel)

Example 2 with AttributesTableModel

use of com.android.tools.idea.editors.theme.attributes.AttributesTableModel in project android by JetBrains.

the class DelegatingCellRenderer method getTableCellRendererComponent.

@Override
public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) {
    EditedStyleItem item = (value instanceof EditedStyleItem) ? (EditedStyleItem) value : null;
    final Component returnedComponent = myDelegate.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    ConfiguredThemeEditorStyle selectedStyle = ((AttributesTableModel) table.getModel()).getSelectedStyle();
    // Displays in bold attributes that are overriding their inherited value
    returnedComponent.setFont(selectedStyle.hasItem(item) ? returnedComponent.getFont().deriveFont(Font.BOLD) : returnedComponent.getFont().deriveFont(Font.PLAIN));
    returnedComponent.setForeground((item != null && !item.isPublicAttribute()) ? JBColor.LIGHT_GRAY : table.getForeground());
    return returnedComponent;
}
Also used : AttributesTableModel(com.android.tools.idea.editors.theme.attributes.AttributesTableModel) EditedStyleItem(com.android.tools.idea.editors.theme.datamodels.EditedStyleItem) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)

Example 3 with AttributesTableModel

use of com.android.tools.idea.editors.theme.attributes.AttributesTableModel in project android by JetBrains.

the class DelegatingCellEditor method getTableCellEditorComponent.

@Override
public Component getTableCellEditorComponent(final JTable table, final Object value, final boolean isSelected, final int row, final int column) {
    final Object stringValue;
    final CellSpanModel model = (CellSpanModel) table.getModel();
    boolean boldFont = false;
    if (value instanceof EditedStyleItem) {
        final EditedStyleItem item = (EditedStyleItem) value;
        stringValue = ThemeEditorUtils.extractRealValue(item, model.getCellClass(row, column));
        ConfiguredThemeEditorStyle selectedStyle = ((AttributesTableModel) table.getModel()).getSelectedStyle();
        // Displays in bold attributes that are overriding their inherited value
        boldFont = selectedStyle.hasItem(item);
    } else {
        // Not an EditedStyleItem for theme name and theme parent.
        stringValue = value;
    }
    final Component returnedComponent = myDelegate.getTableCellEditorComponent(table, myConvertValueToString ? stringValue : value, isSelected, row, column);
    returnedComponent.setFont(boldFont ? returnedComponent.getFont().deriveFont(Font.BOLD) : returnedComponent.getFont().deriveFont(Font.PLAIN));
    return returnedComponent;
}
Also used : AttributesTableModel(com.android.tools.idea.editors.theme.attributes.AttributesTableModel) CellSpanModel(spantable.CellSpanModel) EventObject(java.util.EventObject) EditedStyleItem(com.android.tools.idea.editors.theme.datamodels.EditedStyleItem) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)

Example 4 with AttributesTableModel

use of com.android.tools.idea.editors.theme.attributes.AttributesTableModel 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

AttributesTableModel (com.android.tools.idea.editors.theme.attributes.AttributesTableModel)4 ConfiguredThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)4 EditedStyleItem (com.android.tools.idea.editors.theme.datamodels.EditedStyleItem)4 ResourceResolver (com.android.ide.common.resources.ResourceResolver)2 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)1 AttributesModelColorPaletteModel (com.android.tools.idea.editors.theme.attributes.AttributesModelColorPaletteModel)1 AndroidThemePreviewPanel (com.android.tools.idea.editors.theme.preview.AndroidThemePreviewPanel)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 Project (com.intellij.openapi.project.Project)1 JBPopupMenu (com.intellij.openapi.ui.JBPopupMenu)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 VirtualFileManager (com.intellij.openapi.vfs.VirtualFileManager)1 PsiElement (com.intellij.psi.PsiElement)1 ActionEvent (java.awt.event.ActionEvent)1 ItemEvent (java.awt.event.ItemEvent)1 ItemListener (java.awt.event.ItemListener)1 File (java.io.File)1 EventObject (java.util.EventObject)1 List (java.util.List)1 TableModel (javax.swing.table.TableModel)1