You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by ge...@apache.org on 2018/04/04 20:10:41 UTC

[incubator-netbeans] branch master updated: Allow action hiding in toolbars (#484)

This is an automated email from the ASF dual-hosted git repository.

geertjan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 7536abf  Allow action hiding in toolbars (#484)
7536abf is described below

commit 7536abf2e7f5ba384b4ed72666bec2386948d49d
Author: Svatopluk Dedic <sv...@oracle.com>
AuthorDate: Wed Apr 4 22:10:39 2018 +0200

    Allow action hiding in toolbars (#484)
---
 .../netbeans/modules/editor/NbEditorToolBar.java   | 76 +++++++++++++++++++++-
 openide.awt/src/org/openide/awt/Actions.java       |  9 ++-
 2 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/editor/src/org/netbeans/modules/editor/NbEditorToolBar.java b/editor/src/org/netbeans/modules/editor/NbEditorToolBar.java
index c7ad16b..4821a52 100644
--- a/editor/src/org/netbeans/modules/editor/NbEditorToolBar.java
+++ b/editor/src/org/netbeans/modules/editor/NbEditorToolBar.java
@@ -28,6 +28,7 @@ import java.awt.event.KeyEvent;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.awt.event.MouseMotionListener;
+import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.lang.ref.Reference;
 import java.lang.ref.WeakReference;
@@ -73,6 +74,7 @@ import org.netbeans.lib.editor.util.swing.DocumentUtilities;
 import org.netbeans.modules.editor.impl.ToolbarActionsProvider;
 import org.netbeans.modules.editor.lib2.EditorPreferencesDefaults;
 import org.netbeans.modules.editor.lib2.actions.EditorActionUtilities;
+import org.openide.awt.DynamicMenuContent;
 import org.openide.awt.ToolbarWithOverflow;
 import org.openide.filesystems.FileChangeAdapter;
 import org.openide.filesystems.FileChangeListener;
@@ -399,6 +401,63 @@ import org.openide.util.lookup.ProxyLookup;
 	return (JTextComponent)componentRef.get();
     }
     
+    private ToolbarItemHider hider;
+    
+    void attachHidingComponent(Component c) {
+        if (hider == null) {
+            hider = new ToolbarItemHider();
+        }
+        c.addPropertyChangeListener("enabled", hider); // NOI18N
+    }
+    
+    private class ToolbarItemHider implements PropertyChangeListener {
+        @Override
+        public void propertyChange(PropertyChangeEvent evt) {
+            if ("enabled".equals(evt.getPropertyName())) { // NOI18N
+                hideShowPresenter((JComponent)evt.getSource());
+            }
+        }
+    }
+    
+    private void hideShowPresenter(JComponent jc) {
+        boolean vis = jc.isEnabled();
+        jc.setVisible(vis);
+        // check surroundings and hide possible adjacent separators
+        Component[] comps = getComponents();
+        int idx = getComponentIndex(jc);
+        if (idx == -1) {
+            return;
+        }
+        int sepBefore;
+        for (sepBefore = idx - 1; sepBefore >= 0; sepBefore--) {
+            Component c = comps[sepBefore];
+            if (c.isVisible()) {
+                if (!(c instanceof JSeparator)) {
+                    break;
+                }
+            }
+        }
+        int sepAfter;
+        final int l = comps.length;
+        for (sepAfter = idx + 1; sepAfter < l; sepAfter++) {
+            Component c = comps[sepAfter];
+            if (c.isVisible()) {
+                if (!(c instanceof JSeparator)) {
+                    break;
+                }
+            }
+        }
+        boolean first = sepBefore >= 0;
+        // hide all JSeparators except one between sepBefore + 1 and sepAfter - 1
+        for (int i = sepBefore + 1; i < sepAfter; i++) {
+            Component c = comps[i];
+            if (c instanceof JSeparator) {
+                c.setVisible(first);
+                first = false;
+            }
+        }
+    }
+    
     /** Add the presenters (usually buttons) for the contents of the toolbar
      * contained in the base and mime folders.
      * @param baseFolder folder that corresponds to "text/base"
@@ -428,6 +487,7 @@ import org.openide.util.lookup.ProxyLookup;
             items.add(new JSeparator());
             items.addAll(oldTextBaseItems);
         }
+        List<JComponent>    processHiding = new ArrayList<>();
         
         for(Object item : items) {
             LOG.log(Level.FINE, "Adding item {0}", item); //NOI18N
@@ -473,6 +533,8 @@ import org.openide.util.lookup.ProxyLookup;
                 }
             }
             
+            Action ai = item instanceof Action ? (Action)item : null;
+            
             if (item instanceof Presenter.Toolbar) {
                 Component presenter = ((Presenter.Toolbar) item).getToolbarPresenter();
                 if (presenter != null) {
@@ -487,6 +549,9 @@ import org.openide.util.lookup.ProxyLookup;
                 }
             }
             
+            boolean hideWhenDisabled = ai != null && (ai.getValue(DynamicMenuContent.HIDE_WHEN_DISABLED) == Boolean.TRUE) &&
+                        !ai.isEnabled();
+            
             if (item instanceof Component) {
                 add((Component)item);
                 if (LOG.isLoggable(Level.FINE)) {
@@ -514,7 +579,13 @@ import org.openide.util.lookup.ProxyLookup;
                 }
                 continue;
             }
-
+            if (hideWhenDisabled) {
+                JComponent jc = (JComponent)item;
+                attachHidingComponent(jc);
+                if (!jc.isEnabled()) {
+                    processHiding.add(jc);
+                }
+            }
             if (item instanceof AbstractButton) {
                 AbstractButton button = (AbstractButton)item;
                 processButton(button);
@@ -529,6 +600,9 @@ import org.openide.util.lookup.ProxyLookup;
                 }
             }
         }
+        for (JComponent jc : processHiding) {
+            hideShowPresenter(jc);
+        }
     }
     
     // XXX: this is actually wierd, because it changes the action's properties
diff --git a/openide.awt/src/org/openide/awt/Actions.java b/openide.awt/src/org/openide/awt/Actions.java
index 5ddc36e..8675c3a 100644
--- a/openide.awt/src/org/openide/awt/Actions.java
+++ b/openide.awt/src/org/openide/awt/Actions.java
@@ -74,6 +74,13 @@ import org.openide.util.actions.SystemAction;
 */
 public class Actions {
     /**
+     * Action may {@link Action#putValue} this value to indicate that, if not enabled,
+     * it should not be visible at all. Presenters may honour the request by removing
+     * action's presenter from the UI.
+     */
+    public static final String ACTION_VALUE_VISIBLE = "openide.awt.actionVisible"; // NOI18N
+    
+    /**
      * @deprecated should not be used
      */
     @Deprecated
@@ -1127,7 +1134,7 @@ public class Actions {
                     );
                 }
             }
-
+            
             if (
                 button instanceof javax.accessibility.Accessible &&
                     ((changedProperty == null) || changedProperty.equals(Action.NAME))

-- 
To stop receiving notification emails like this one, please contact
geertjan@apache.org.

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists