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();
}
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());
}
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());
}
}
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();
}
}
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;
}
}
Aggregations