Search in sources :

Example 1 with CompositeConfigurable

use of com.intellij.openapi.options.CompositeConfigurable in project ballerina by ballerina-lang.

the class BallerinaLibrariesConfigurableProvider method createConfigurable.

@Nullable
private Configurable createConfigurable(boolean dialogMode) {
    return new CompositeConfigurable<UnnamedConfigurable>() {

        @Nullable
        @Override
        public JComponent createComponent() {
            List<UnnamedConfigurable> configurables = getConfigurables();
            Collection<HideableDecorator> hideableDecorators = ContainerUtil.newHashSet();
            GridLayoutManager layoutManager = new GridLayoutManager(configurables.size() + 1, 1, new Insets(0, 0, 0, 0), -1, -1);
            JPanel rootPanel = new JPanel(layoutManager);
            Spacer spacer = new Spacer();
            rootPanel.add(spacer, new GridConstraints(configurables.size(), 0, 1, 1, GridConstraints.ANCHOR_SOUTH, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null));
            for (int i = 0; i < configurables.size(); i++) {
                UnnamedConfigurable configurable = configurables.get(i);
                JComponent configurableComponent = configurable.createComponent();
                assert configurableComponent != null;
                JPanel hideablePanel = new JPanel(new BorderLayout());
                rootPanel.add(hideablePanel, configurableConstrains(i));
                if (configurable instanceof Configurable) {
                    String displayName = ((Configurable) configurable).getDisplayName();
                    ListenableHideableDecorator decorator = new ListenableHideableDecorator(hideablePanel, displayName, configurableComponent);
                    decorator.addListener(new MyHideableDecoratorListener(layoutManager, hideablePanel, spacer, hideableDecorators, configurableExpandedPropertyKey((Configurable) configurable)));
                    hideableDecorators.add(decorator);
                    decorator.setOn(isConfigurableExpanded(i, (Configurable) configurable));
                }
            }
            if (dialogMode) {
                rootPanel.setPreferredSize(new Dimension(400, 600));
            }
            rootPanel.revalidate();
            return rootPanel;
        }

        @NotNull
        @Override
        protected List<UnnamedConfigurable> createConfigurables() {
            List<UnnamedConfigurable> result = ContainerUtil.newArrayList();
            String[] urlsFromEnv = ContainerUtil.map2Array(BallerinaSdkUtil.getBallerinaPathsRootsFromEnvironment(), String.class, VirtualFile::getUrl);
            result.add(new BallerinaLibrariesConfigurable("Global libraries", BallerinaApplicationLibrariesService.getInstance(), urlsFromEnv));
            if (!myProject.isDefault()) {
                result.add(new BallerinaLibrariesConfigurable("Project libraries", BallerinaProjectLibrariesService.getInstance(myProject)));
            }
            return result;
        }

        @NotNull
        @Nls
        @Override
        public String getDisplayName() {
            return "Ballerina Libraries";
        }

        @Nullable
        @Override
        public String getHelpTopic() {
            return null;
        }

        @NotNull
        private GridConstraints configurableConstrains(int i) {
            return new GridConstraints(i, 0, 1, 1, GridConstraints.ANCHOR_NORTHEAST, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_WANT_GROW | GridConstraints.SIZEPOLICY_CAN_SHRINK, GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_CAN_SHRINK, null, null, null);
        }

        private boolean isConfigurableExpanded(int index, @NotNull Configurable configurable) {
            return PropertiesComponent.getInstance(myProject).getBoolean(configurableExpandedPropertyKey(configurable), index < 2);
        }

        private void storeConfigurableExpandedProperty(@NotNull String storeKey, @NotNull Boolean value) {
            PropertiesComponent.getInstance(myProject).setValue(storeKey, value.toString());
        }

        private String configurableExpandedPropertyKey(@NotNull Configurable configurable) {
            String keyName = "configurable " + configurable.getDisplayName() + " is expanded".toLowerCase(Locale.US);
            return StringUtil.replaceChar(keyName, ' ', '.');
        }

        class MyHideableDecoratorListener extends ListenableHideableDecorator.MyListener {

            private final GridLayoutManager myLayoutManager;

            private final JPanel myHideablePanel;

            @NotNull
            private final String myStoreKey;

            private final Spacer mySpacer;

            private final Collection<HideableDecorator> myHideableDecorators;

            public MyHideableDecoratorListener(@NotNull GridLayoutManager layoutManager, @NotNull JPanel hideablePanel, @NotNull Spacer spacer, @NotNull Collection<HideableDecorator> hideableDecorators, @NotNull String storeKey) {
                myLayoutManager = layoutManager;
                myHideablePanel = hideablePanel;
                myStoreKey = storeKey;
                mySpacer = spacer;
                myHideableDecorators = hideableDecorators;
            }

            @Override
            public void on() {
                GridConstraints c = myLayoutManager.getConstraintsForComponent(myHideablePanel);
                c.setVSizePolicy(c.getVSizePolicy() | GridConstraints.SIZEPOLICY_WANT_GROW);
                GridConstraints spacerConstraints = myLayoutManager.getConstraintsForComponent(mySpacer);
                spacerConstraints.setVSizePolicy(spacerConstraints.getVSizePolicy() & ~GridConstraints.SIZEPOLICY_WANT_GROW);
                storeConfigurableExpandedProperty(myStoreKey, Boolean.TRUE);
            }

            @Override
            public void beforeOff() {
                GridConstraints c = myLayoutManager.getConstraintsForComponent(myHideablePanel);
                c.setVSizePolicy(c.getVSizePolicy() & ~GridConstraints.SIZEPOLICY_WANT_GROW);
            }

            @Override
            public void afterOff() {
                if (isAllDecoratorsCollapsed()) {
                    GridConstraints c = myLayoutManager.getConstraintsForComponent(mySpacer);
                    c.setVSizePolicy(c.getVSizePolicy() | GridConstraints.SIZEPOLICY_WANT_GROW);
                }
                storeConfigurableExpandedProperty(myStoreKey, Boolean.FALSE);
            }

            private boolean isAllDecoratorsCollapsed() {
                for (HideableDecorator hideableDecorator : myHideableDecorators) {
                    if (hideableDecorator.isExpanded()) {
                        return false;
                    }
                }
                return true;
            }
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) JPanel(javax.swing.JPanel) CompositeConfigurable(com.intellij.openapi.options.CompositeConfigurable) Insets(java.awt.Insets) HideableDecorator(com.intellij.ui.HideableDecorator) JComponent(javax.swing.JComponent) UnnamedConfigurable(com.intellij.openapi.options.UnnamedConfigurable) CompositeConfigurable(com.intellij.openapi.options.CompositeConfigurable) UnnamedConfigurable(com.intellij.openapi.options.UnnamedConfigurable) Configurable(com.intellij.openapi.options.Configurable) Dimension(java.awt.Dimension) NotNull(org.jetbrains.annotations.NotNull) GridLayoutManager(com.intellij.uiDesigner.core.GridLayoutManager) BorderLayout(java.awt.BorderLayout) GridConstraints(com.intellij.uiDesigner.core.GridConstraints) Spacer(com.intellij.uiDesigner.core.Spacer) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

CompositeConfigurable (com.intellij.openapi.options.CompositeConfigurable)1 Configurable (com.intellij.openapi.options.Configurable)1 UnnamedConfigurable (com.intellij.openapi.options.UnnamedConfigurable)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 HideableDecorator (com.intellij.ui.HideableDecorator)1 GridConstraints (com.intellij.uiDesigner.core.GridConstraints)1 GridLayoutManager (com.intellij.uiDesigner.core.GridLayoutManager)1 Spacer (com.intellij.uiDesigner.core.Spacer)1 BorderLayout (java.awt.BorderLayout)1 Dimension (java.awt.Dimension)1 Insets (java.awt.Insets)1 JComponent (javax.swing.JComponent)1 JPanel (javax.swing.JPanel)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1