You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2018/04/04 20:10:41 UTC
[GitHub] geertjanw closed pull request #484: Allow action hiding in toolbars
geertjanw closed pull request #484: Allow action hiding in toolbars
URL: https://github.com/apache/incubator-netbeans/pull/484
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/editor/src/org/netbeans/modules/editor/NbEditorToolBar.java b/editor/src/org/netbeans/modules/editor/NbEditorToolBar.java
index c7ad16bea..4821a5286 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.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.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 @@ private JTextComponent getComponent() {
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 @@ private void addPresenters() {
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 @@ private void addPresenters() {
}
}
+ 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 @@ private void addPresenters() {
}
}
+ 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 @@ private void addPresenters() {
}
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 @@ private void addPresenters() {
}
}
}
+ 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 5ddc36ebd..8675c3a69 100644
--- a/openide.awt/src/org/openide/awt/Actions.java
+++ b/openide.awt/src/org/openide/awt/Actions.java
@@ -73,6 +73,13 @@
* @author Jaroslav Tulach
*/
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
*/
@@ -1127,7 +1134,7 @@ public void updateState(String changedProperty) {
);
}
}
-
+
if (
button instanceof javax.accessibility.Accessible &&
((changedProperty == null) || changedProperty.equals(Action.NAME))
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists