Search in sources :

Example 1 with FormatExpression

use of com.android.tools.idea.ui.properties.expressions.string.FormatExpression in project android by JetBrains.

the class SyncFieldsDemo method init.

public void init() {
    // Wrap UI elements in properties
    final StringProperty projectText = new TextProperty(myProjectNameField);
    final StringProperty activityText = new TextProperty(myActivityNameField);
    final BoolProperty isSynced = new SelectedProperty(mySyncActivityNameCheckBox);
    final BoolProperty createActivity = new SelectedProperty(myCreateActivityCheckBox);
    BoolProperty isActivityEnabled = new EnabledProperty(myActivityNameField);
    BoolProperty isLinkEnabled = new EnabledProperty(mySyncActivityNameCheckBox);
    projectText.set("MyProject");
    // Bind activityText <- nameExpression(projectText), but only if conditions are met
    final FormatExpression activityNameExpression = new FormatExpression("%1$sActivity", projectText);
    myBindings.bind(activityText, activityNameExpression, isSynced.and(createActivity));
    myBindings.bind(isActivityEnabled, createActivity);
    myBindings.bind(isLinkEnabled, createActivity);
    // Listen to activityText - if it is changed by the user and not its binding, break syncing!
    activityText.addListener(new InvalidationListener() {

        @Override
        public void onInvalidated(@NotNull ObservableValue<?> sender) {
            isSynced.set(activityText.get().equals(activityNameExpression.get()));
        }
    });
}
Also used : StringProperty(com.android.tools.idea.ui.properties.core.StringProperty) EnabledProperty(com.android.tools.idea.ui.properties.swing.EnabledProperty) InvalidationListener(com.android.tools.idea.ui.properties.InvalidationListener) BoolProperty(com.android.tools.idea.ui.properties.core.BoolProperty) TextProperty(com.android.tools.idea.ui.properties.swing.TextProperty) FormatExpression(com.android.tools.idea.ui.properties.expressions.string.FormatExpression) SelectedProperty(com.android.tools.idea.ui.properties.swing.SelectedProperty)

Example 2 with FormatExpression

use of com.android.tools.idea.ui.properties.expressions.string.FormatExpression in project android by JetBrains.

the class ConfigureIconPanel method initializeListenersAndBindings.

private void initializeListenersAndBindings() {
    final BoolProperty trimmed = new SelectedProperty(myTrimmedRadioButton);
    final IntProperty paddingPercent = new SliderValueProperty(myPaddingSlider);
    final StringProperty paddingValueString = new TextProperty(myPaddingValueLabel);
    myGeneralBindings.bind(paddingValueString, new FormatExpression("%d %%", paddingPercent));
    myIgnoreForegroundColor = new SelectedProperty(myImageRadioButton);
    myForegroundColor = new OptionalToValuePropertyAdapter<>(new ColorProperty(myForegroundColorPanel));
    myBackgroundColor = new OptionalToValuePropertyAdapter<>(new ColorProperty(myBackgroundColorPanel));
    myCropped = new SelectedProperty(myCropRadioButton);
    myDogEared = new SelectedProperty(myDogEarRadioButton);
    myTheme = new OptionalToValuePropertyAdapter<>(new SelectedItemProperty<>(myThemeComboBox));
    myThemeColor = new OptionalToValuePropertyAdapter<>(new ColorProperty(myCustomThemeColorPanel));
    myShape = new OptionalToValuePropertyAdapter<>(new SelectedItemProperty<>(myShapeComboBox));
    updateBindingsAndUiForActiveIconType();
    ActionListener radioSelectedListener = e -> {
        JRadioButton source = ((JRadioButton) e.getSource());
        AssetComponent assetComponent = myAssetPanelMap.get(source);
        myActiveAsset.set(assetComponent.getAsset());
    };
    myClipartRadioButton.addActionListener(radioSelectedListener);
    myImageRadioButton.addActionListener(radioSelectedListener);
    myTextRadioButton.addActionListener(radioSelectedListener);
    // If any of our underlying asset panels change, we should pass that on to anyone listening to
    // us as well.
    ActionListener assetPanelListener = e -> fireAssetListeners();
    for (AssetComponent assetComponent : myAssetPanelMap.values()) {
        assetComponent.addAssetListener(assetPanelListener);
    }
    final Runnable onAssetModified = this::fireAssetListeners;
    myListeners.listenAll(trimmed, paddingPercent, myForegroundColor, myBackgroundColor, myCropped, myDogEared, myTheme, myThemeColor, myShape).with(onAssetModified);
    myListeners.listenAndFire(myActiveAsset, sender -> {
        myActiveAssetBindings.releaseAll();
        myActiveAssetBindings.bindTwoWay(trimmed, myActiveAsset.get().trimmed());
        myActiveAssetBindings.bindTwoWay(paddingPercent, myActiveAsset.get().paddingPercent());
        myActiveAssetBindings.bindTwoWay(myForegroundColor, myActiveAsset.get().color());
        getIconGenerator().sourceAsset().setValue(myActiveAsset.get());
        onAssetModified.run();
    });
    ObservableBool isLauncherIcon = new BoolValueProperty(myIconType.equals(AndroidIconType.LAUNCHER));
    ObservableBool isActionBarIcon = new BoolValueProperty(myIconType.equals(AndroidIconType.ACTIONBAR));
    ObservableBool isCustomTheme = myTheme.isEqualTo(ActionBarIconGenerator.Theme.CUSTOM);
    ObservableValue<Boolean> isClipartOrText = myActiveAsset.transform(asset -> myClipartAssetButton.getAsset() == asset || myTextAssetEditor.getAsset() == asset);
    ObservableBool supportsEffects = new BooleanExpression(myShape) {

        @NotNull
        @Override
        public Boolean get() {
            GraphicGenerator.Shape shape = myShape.get();
            switch(shape) {
                case SQUARE:
                case VRECT:
                case HRECT:
                    return true;
                default:
                    return false;
            }
        }
    };
    /**
     * Hook up a bunch of UI <- boolean expressions, so that when certain conditions are met,
     * various components show/hide. This also requires refreshing the panel explicitly, as
     * otherwise Swing doesn't realize it should trigger a relayout.
     */
    ImmutableMap.Builder<BoolProperty, ObservableBool> layoutPropertiesBuilder = ImmutableMap.builder();
    layoutPropertiesBuilder.put(new VisibleProperty(myImageAssetRowPanel), new SelectedProperty(myImageRadioButton));
    layoutPropertiesBuilder.put(new VisibleProperty(myClipartAssetRowPanel), new SelectedProperty(myClipartRadioButton));
    layoutPropertiesBuilder.put(new VisibleProperty(myTextAssetRowPanel), new SelectedProperty(myTextRadioButton));
    layoutPropertiesBuilder.put(new VisibleProperty(myForegroundRowPanel), isLauncherIcon.and(isClipartOrText));
    layoutPropertiesBuilder.put(new VisibleProperty(myBackgroundRowPanel), isLauncherIcon);
    layoutPropertiesBuilder.put(new VisibleProperty(myScalingRowPanel), isLauncherIcon);
    layoutPropertiesBuilder.put(new VisibleProperty(myShapeRowPanel), isLauncherIcon);
    layoutPropertiesBuilder.put(new VisibleProperty(myEffectRowPanel), isLauncherIcon);
    layoutPropertiesBuilder.put(new EnabledProperty(myDogEarRadioButton), supportsEffects);
    layoutPropertiesBuilder.put(new VisibleProperty(myThemeRowPanel), isActionBarIcon);
    layoutPropertiesBuilder.put(new VisibleProperty(myCustomThemeRowPanel), isActionBarIcon.and(isCustomTheme));
    ImmutableMap<BoolProperty, ObservableBool> layoutProperties = layoutPropertiesBuilder.build();
    for (Map.Entry<BoolProperty, ObservableBool> e : layoutProperties.entrySet()) {
        // Initialize everything off, as this makes sure the frame that uses this panel won't start
        // REALLY LARGE by default.
        e.getKey().set(false);
        myGeneralBindings.bind(e.getKey(), e.getValue());
    }
    myListeners.listenAll(layoutProperties.keySet()).with(() -> SwingUtilities.updateComponentTreeUI(myAllOptionsPanel));
}
Also used : com.android.tools.idea.ui.properties.core(com.android.tools.idea.ui.properties.core) ActionListener(java.awt.event.ActionListener) JBLabel(com.intellij.ui.components.JBLabel) FormatExpression(com.android.tools.idea.ui.properties.expressions.string.FormatExpression) Lists(com.google.common.collect.Lists) AndroidIconGenerator(com.android.tools.idea.npw.assetstudio.icon.AndroidIconGenerator) Map(java.util.Map) Disposer(com.intellij.openapi.util.Disposer) GenerateIconsPanel(com.android.tools.idea.npw.assetstudio.wizard.GenerateIconsPanel) AndroidActionBarIconGenerator(com.android.tools.idea.npw.assetstudio.icon.AndroidActionBarIconGenerator) com.android.tools.idea.ui.properties(com.android.tools.idea.ui.properties) AndroidIconType(com.android.tools.idea.npw.assetstudio.icon.AndroidIconType) ImmutableMap(com.google.common.collect.ImmutableMap) ColorPanel(com.intellij.ui.ColorPanel) GraphicGenerator(com.android.assetstudiolib.GraphicGenerator) BaseAsset(com.android.tools.idea.npw.assetstudio.assets.BaseAsset) AsOptionalExpression(com.android.tools.idea.ui.properties.expressions.optional.AsOptionalExpression) AndroidLauncherIconGenerator(com.android.tools.idea.npw.assetstudio.icon.AndroidLauncherIconGenerator) ListCellRendererWrapper(com.intellij.ui.ListCellRendererWrapper) ActionEvent(java.awt.event.ActionEvent) Disposable(com.intellij.openapi.Disposable) JBScrollPane(com.intellij.ui.components.JBScrollPane) java.awt(java.awt) OptionalToValuePropertyAdapter(com.android.tools.idea.ui.properties.adapters.OptionalToValuePropertyAdapter) List(java.util.List) IdeBorderFactory(com.intellij.ui.IdeBorderFactory) VectorAsset(com.android.tools.idea.npw.assetstudio.assets.VectorAsset) ActionBarIconGenerator(com.android.assetstudiolib.ActionBarIconGenerator) BooleanExpression(com.android.tools.idea.ui.properties.expressions.bool.BooleanExpression) NotNull(org.jetbrains.annotations.NotNull) com.android.tools.idea.ui.properties.swing(com.android.tools.idea.ui.properties.swing) javax.swing(javax.swing) BooleanExpression(com.android.tools.idea.ui.properties.expressions.bool.BooleanExpression) GraphicGenerator(com.android.assetstudiolib.GraphicGenerator) FormatExpression(com.android.tools.idea.ui.properties.expressions.string.FormatExpression) ImmutableMap(com.google.common.collect.ImmutableMap) ActionListener(java.awt.event.ActionListener) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with FormatExpression

use of com.android.tools.idea.ui.properties.expressions.string.FormatExpression in project android by JetBrains.

the class NewVectorAssetStep method onWizardStarting.

@Override
protected void onWizardStarting(@NotNull ModelWizard.Facade wizard) {
    final Runnable onAssetModified = myPreviewUpdater::enqueueUpdate;
    loadAssetPath();
    SelectedProperty iconSelected = new SelectedProperty(myMaterialIconRadioButton);
    myListeners.receiveAndFire(iconSelected, isIconActive -> {
        myIconPickerPanel.setVisible(isIconActive);
        myBrowserPanel.setVisible(!isIconActive);
        myActiveAsset.set(isIconActive ? myIconButton.getAsset() : myBrowser.getAsset());
    });
    ActionListener assetListener = actionEvent -> {
        onAssetModified.run();
        saveAssetPath();
    };
    myIconButton.addAssetListener(assetListener);
    myBrowser.addAssetListener(assetListener);
    Disposer.register(this, myIconButton);
    Disposer.register(this, myBrowser);
    final BoolProperty overrideSize = new SelectedProperty(myOverrideSizeCheckBox);
    final IntProperty width = new IntValueProperty();
    final IntProperty height = new IntValueProperty();
    myGeneralBindings.bindTwoWay(new StringToIntAdapterProperty(new TextProperty(myWidthTextField)), width);
    myGeneralBindings.bindTwoWay(new StringToIntAdapterProperty(new TextProperty(myHeightTextField)), height);
    myGeneralBindings.bind(new EnabledProperty(myWidthTextField), overrideSize);
    myGeneralBindings.bind(new EnabledProperty(myHeightTextField), overrideSize);
    myListeners.listenAll(overrideSize, myOriginalSize).withAndFire(() -> {
        if (!overrideSize.get() || !myOriginalSize.get().isPresent()) {
            width.set(DEFAULT_MATERIAL_ICON_SIZE);
            height.set(DEFAULT_MATERIAL_ICON_SIZE);
        } else {
            width.set(myOriginalSize.getValue().width);
            height.set(myOriginalSize.getValue().height);
        }
    });
    final IntProperty opacityValue = new SliderValueProperty(myOpacitySlider);
    myGeneralBindings.bind(new TextProperty(myOpacityValueLabel), new FormatExpression("%d %%", opacityValue));
    final BoolProperty autoMirrored = new SelectedProperty(myEnableAutoMirroredCheckBox);
    myListeners.listenAll(myActiveAsset, overrideSize, width, height, opacityValue, autoMirrored).with(onAssetModified);
    final StringProperty name = new TextProperty(myOutputNameField);
    myListeners.listenAndFire(myActiveAsset, sender -> {
        myActiveAssetBindings.releaseAll();
        myActiveAssetBindings.bind(name, new Expression<String>(myActiveAsset.get().path()) {

            @NotNull
            @Override
            public String get() {
                File path = myActiveAsset.get().path().get();
                if (path.exists() && !path.isDirectory()) {
                    String name1 = FileUtil.getNameWithoutExtension(path).toLowerCase(Locale.getDefault());
                    if (!name1.startsWith(ICON_PREFIX)) {
                        name1 = ICON_PREFIX + AndroidResourceUtil.getValidResourceFileName(name1);
                    }
                    return AndroidResourceUtil.getValidResourceFileName(name1);
                } else {
                    return "ic_vector_name";
                }
            }
        });
        myActiveAssetBindings.bind(myActiveAsset.get().opacity(), opacityValue);
        myActiveAssetBindings.bind(myActiveAsset.get().autoMirrored(), autoMirrored);
        myActiveAssetBindings.bind(myActiveAsset.get().outputWidth(), width);
        myActiveAssetBindings.bind(myActiveAsset.get().outputHeight(), height);
    });
    // Refresh the asset preview, but fire using invokeLater, as this lets the UI lay itself out,
    // which should happen before the "generate preview" logic runs.
    ApplicationManager.getApplication().invokeLater(onAssetModified, ModalityState.any());
    // Cast VectorAsset -> BaseAsset
    myGeneralBindings.bind(myIconGenerator.sourceAsset(), new AsOptionalExpression<>(myActiveAsset));
    myGeneralBindings.bind(myIconGenerator.name(), name);
}
Also used : com.android.tools.idea.ui.properties.core(com.android.tools.idea.ui.properties.core) ActionListener(java.awt.event.ActionListener) AndroidSourceSet(com.android.tools.idea.npw.project.AndroidSourceSet) ModalityState(com.intellij.openapi.application.ModalityState) ModelWizard(com.android.tools.idea.wizard.model.ModelWizard) VectorImageComponent(com.android.tools.idea.ui.VectorImageComponent) VectorIconButton(com.android.tools.idea.npw.assetstudio.ui.VectorIconButton) JBLabel(com.intellij.ui.components.JBLabel) FormatExpression(com.android.tools.idea.ui.properties.expressions.string.FormatExpression) AndroidVectorIconGenerator(com.android.tools.idea.npw.assetstudio.icon.AndroidVectorIconGenerator) TextProperty(com.android.tools.idea.ui.properties.swing.TextProperty) VectorAssetBrowser(com.android.tools.idea.npw.assetstudio.ui.VectorAssetBrowser) Locale(java.util.Locale) Disposer(com.intellij.openapi.util.Disposer) AndroidResourceUtil(org.jetbrains.android.util.AndroidResourceUtil) BindingsManager(com.android.tools.idea.ui.properties.BindingsManager) Project(com.intellij.openapi.project.Project) StringToIntAdapterProperty(com.android.tools.idea.ui.properties.adapters.StringToIntAdapterProperty) SliderValueProperty(com.android.tools.idea.ui.properties.swing.SliderValueProperty) FileUtil(com.intellij.openapi.util.io.FileUtil) PropertiesComponent(com.intellij.ide.util.PropertiesComponent) Collection(java.util.Collection) AsOptionalExpression(com.android.tools.idea.ui.properties.expressions.optional.AsOptionalExpression) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) ModelWizardStep(com.android.tools.idea.wizard.model.ModelWizardStep) SelectedProperty(com.android.tools.idea.ui.properties.swing.SelectedProperty) File(java.io.File) JBScrollPane(com.intellij.ui.components.JBScrollPane) EnabledProperty(com.android.tools.idea.ui.properties.swing.EnabledProperty) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) VectorAsset(com.android.tools.idea.npw.assetstudio.assets.VectorAsset) ApplicationManager(com.intellij.openapi.application.ApplicationManager) NotNull(org.jetbrains.annotations.NotNull) Expression(com.android.tools.idea.ui.properties.expressions.Expression) Collections(java.util.Collections) ListenerManager(com.android.tools.idea.ui.properties.ListenerManager) javax.swing(javax.swing) StringToIntAdapterProperty(com.android.tools.idea.ui.properties.adapters.StringToIntAdapterProperty) SliderValueProperty(com.android.tools.idea.ui.properties.swing.SliderValueProperty) NotNull(org.jetbrains.annotations.NotNull) ActionListener(java.awt.event.ActionListener) EnabledProperty(com.android.tools.idea.ui.properties.swing.EnabledProperty) File(java.io.File) TextProperty(com.android.tools.idea.ui.properties.swing.TextProperty) FormatExpression(com.android.tools.idea.ui.properties.expressions.string.FormatExpression) SelectedProperty(com.android.tools.idea.ui.properties.swing.SelectedProperty)

Aggregations

FormatExpression (com.android.tools.idea.ui.properties.expressions.string.FormatExpression)3 VectorAsset (com.android.tools.idea.npw.assetstudio.assets.VectorAsset)2 com.android.tools.idea.ui.properties.core (com.android.tools.idea.ui.properties.core)2 AsOptionalExpression (com.android.tools.idea.ui.properties.expressions.optional.AsOptionalExpression)2 EnabledProperty (com.android.tools.idea.ui.properties.swing.EnabledProperty)2 SelectedProperty (com.android.tools.idea.ui.properties.swing.SelectedProperty)2 TextProperty (com.android.tools.idea.ui.properties.swing.TextProperty)2 Disposer (com.intellij.openapi.util.Disposer)2 JBLabel (com.intellij.ui.components.JBLabel)2 JBScrollPane (com.intellij.ui.components.JBScrollPane)2 java.awt (java.awt)2 ActionListener (java.awt.event.ActionListener)2 javax.swing (javax.swing)2 NotNull (org.jetbrains.annotations.NotNull)2 ActionBarIconGenerator (com.android.assetstudiolib.ActionBarIconGenerator)1 GraphicGenerator (com.android.assetstudiolib.GraphicGenerator)1 BaseAsset (com.android.tools.idea.npw.assetstudio.assets.BaseAsset)1 AndroidActionBarIconGenerator (com.android.tools.idea.npw.assetstudio.icon.AndroidActionBarIconGenerator)1 AndroidIconGenerator (com.android.tools.idea.npw.assetstudio.icon.AndroidIconGenerator)1 AndroidIconType (com.android.tools.idea.npw.assetstudio.icon.AndroidIconType)1