20 package net.sf.gridarta.gui.utils;
22 import java.awt.Component;
23 import java.awt.Container;
24 import java.awt.Dimension;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.awt.event.HierarchyEvent;
28 import java.awt.event.HierarchyListener;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.prefs.Preferences;
32 import javax.swing.AbstractButton;
33 import javax.swing.JSplitPane;
34 import javax.swing.border.EmptyBorder;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
56 private static final String
MIN =
"min";
62 private static final String
MAX =
"max";
105 public void hierarchyChanged(
final HierarchyEvent e) {
106 if ((e.getChangeFlags() & (long) HierarchyEvent.SHOWING_CHANGED) != 0L && isShowing()) {
107 removeHierarchyListener(hierarchyListener);
108 if (defaultDividerLocation == null) {
110 }
else if (defaultDividerLocation.equals(MIN)) {
111 if (!actionListeners.isEmpty()) {
112 actionListeners.get(0).actionPerformed(
new ActionEvent(
GSplitPane.this, 0,
""));
114 }
else if (defaultDividerLocation.equals(MAX)) {
115 if (!actionListeners.isEmpty()) {
116 actionListeners.get(1).actionPerformed(
new ActionEvent(
GSplitPane.this, 0,
""));
121 }
catch (
final NumberFormatException ignored) {
143 public GSplitPane(
final int newOrientation, @NotNull
final Component newLeftComponent, @NotNull
final Component newRightComponent, @NotNull
final String preferencesKey,
final int defaultDividerLocation) {
144 super(newOrientation, newLeftComponent, newRightComponent);
146 setOneTouchExpandable(
true);
147 this.defaultDividerLocation = PREFERENCES.get(preferencesKey, defaultDividerLocation == -1 ? null : Integer.toString(defaultDividerLocation));
158 setBorder(
new EmptyBorder(0, 0, 0, 0));
159 for (
final Component component : getComponents()) {
160 if (component != newLeftComponent && component != newRightComponent) {
161 if (component instanceof Container) {
162 final Container container = (Container) component;
163 for (
final Component component2 : container.getComponents()) {
164 if (component2 instanceof AbstractButton) {
165 final AbstractButton button = (AbstractButton) component2;
166 for (
final ActionListener actionListener : button.getActionListeners()) {
167 if (actionListener.getClass().getName().endsWith(
"$OneTouchActionHandler")) {
168 actionListeners.add(actionListener);
169 button.removeActionListener(actionListener);
170 if (actionListeners.size() == 1) {
171 button.addActionListener(
new ActionListener() {
174 public void actionPerformed(@NotNull
final ActionEvent e) {
175 getRightComponent().setVisible(
true);
176 actionListener.actionPerformed(e);
181 button.addActionListener(
new ActionListener() {
184 public void actionPerformed(@NotNull
final ActionEvent e) {
185 getLeftComponent().setVisible(
true);
186 actionListener.actionPerformed(e);
199 if (actionListeners.size() != 2) {
200 actionListeners.clear();
203 if (this.defaultDividerLocation != null) {
204 addHierarchyListener(hierarchyListener);
212 final int dividerLocation = getDividerLocation();
213 if (dividerLocation != -1) {
215 switch (
getState(dividerLocation)) {
221 value = Integer.toString(dividerLocation);
228 PREFERENCES.put(preferencesKey, value);
234 super.setDividerLocation(location);
235 final int state =
getState(location);
236 getLeftComponent().setVisible(state != -1);
237 getRightComponent().setVisible(state != +1);
247 final Dimension leftSize = leftComponent.getMinimumSize();
248 final Dimension rightSize = rightComponent.getMinimumSize();
249 final int minLimit = getOrientation() == HORIZONTAL_SPLIT ? leftSize.width : leftSize.height;
250 final int maxLimit = getOrientation() == HORIZONTAL_SPLIT ? getWidth() - rightSize.width - getDividerSize() : getHeight() - rightSize.height - getDividerSize();
251 if (dividerLocation < minLimit) {
253 }
else if (dividerLocation > maxLimit) {
void setDividerLocation(final int location)
static final Preferences PREFERENCES
The preferences.
final List< ActionListener > actionListeners
The ActionListeners attached to the "oneTouchExpandable" buttons of the JSplitPane.
A JSplitPane that keeps its size even upon ancestor layout changes and is restored upon editor restar...
Base package of all Gridarta classes.
final String preferencesKey
The preferences key for restoring the divider location.
Interface used as preferences location.
final String defaultDividerLocation
The default divider location.
static final long serialVersionUID
Serial Version UID.
static final String MAX
The preferences value for a maximized divider.
void saveLocation()
Saves the current divider location into the preferences.
static final String MIN
The preferences value for a minimized divider.
GSplitPane(final int newOrientation, @NotNull final Component newLeftComponent, @NotNull final Component newRightComponent, @NotNull final String preferencesKey, final int defaultDividerLocation)
Create a new GSplitPane.
final HierarchyListener hierarchyListener
The HierarchyListener for updating the initial divider location.
int getState(final int dividerLocation)
Returns the minimized/maximized state for a given divider location.