001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.support.swing;
014:
015: import java.awt.Component;
016: import java.awt.Container;
017: import java.awt.Cursor;
018: import java.awt.Dimension;
019: import java.awt.Graphics;
020: import java.awt.LayoutManager;
021:
022: import javax.swing.ImageIcon;
023: import javax.swing.JButton;
024: import javax.swing.JComponent;
025: import javax.swing.JSplitPane;
026: import javax.swing.plaf.basic.BasicSplitPaneDivider;
027: import javax.swing.plaf.basic.BasicSplitPaneUI;
028:
029: import com.eviware.soapui.support.UISupport;
030:
031: /**
032: * SplitPaneUI that draws nicer buttons and enables/disables them appropriately
033: *
034: * @author Ole.Matzura
035: */
036:
037: public class SoapUISplitPaneUI extends BasicSplitPaneUI {
038: private boolean hasBeenDragged;
039: private final static ImageIcon upArrow = UISupport
040: .createImageIcon("/up_arrow.gif");
041: private final static ImageIcon leftArrow = UISupport
042: .createImageIcon("/left_arrow.gif");
043: private final static ImageIcon rightArrow = UISupport
044: .createImageIcon("/right_arrow.gif");
045: private final static ImageIcon downArrow = UISupport
046: .createImageIcon("/down_arrow.gif");
047:
048: public SoapUISplitPaneUI() {
049: super ();
050: }
051:
052: protected void finishDraggingTo(int location) {
053: super .finishDraggingTo(location);
054:
055: hasBeenDragged = true;
056: }
057:
058: public void resetToPreferredSizes(JSplitPane jc) {
059: super .resetToPreferredSizes(jc);
060: hasBeenDragged = false;
061: }
062:
063: public boolean hasBeenDragged() {
064: return hasBeenDragged;
065: }
066:
067: public void setHasBeenDragged(boolean hasBeenDragged) {
068: this .hasBeenDragged = hasBeenDragged;
069: }
070:
071: public BasicSplitPaneDivider createDefaultDivider() {
072: return new SoapUIDivider(this );
073: }
074:
075: public class SoapUIDivider extends BasicSplitPaneDivider {
076: public SoapUIDivider(BasicSplitPaneUI ui) {
077: super (ui);
078:
079: setLayout(new SoapUIDividerLayout());
080: }
081:
082: protected JButton createLeftOneTouchButton() {
083: if (getSplitPane().getOrientation() == JSplitPane.VERTICAL_SPLIT) {
084: JButton b = new JButton(upArrow);
085:
086: b.setMinimumSize(new Dimension(8, 6));
087: b.setFocusPainted(false);
088: b.setBorderPainted(false);
089: b.setRequestFocusEnabled(false);
090: b.setBorder(null);
091: b.setCursor(Cursor
092: .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
093:
094: return b;
095: } else {
096: JButton b = new JButton(leftArrow);
097:
098: b.setMinimumSize(new Dimension(6, 8));
099: b.setFocusPainted(false);
100: b.setBorderPainted(false);
101: b.setRequestFocusEnabled(false);
102: b.setBorder(null);
103: b.setCursor(Cursor
104: .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
105:
106: return b;
107: }
108: }
109:
110: protected JButton createRightOneTouchButton() {
111: if (getSplitPane().getOrientation() == JSplitPane.VERTICAL_SPLIT) {
112: JButton b = new JButton(downArrow);
113:
114: b.setMinimumSize(new Dimension(8, 6));
115: b.setFocusPainted(false);
116: b.setBorderPainted(false);
117: b.setRequestFocusEnabled(false);
118: b.setBorder(null);
119: b.setCursor(Cursor
120: .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
121:
122: return b;
123: } else {
124: JButton b = new JButton(rightArrow);
125:
126: b.setMinimumSize(new Dimension(6, 8));
127: b.setFocusPainted(false);
128: b.setBorderPainted(false);
129: b.setRequestFocusEnabled(false);
130: b.setBorder(null);
131: b.setCursor(Cursor
132: .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
133:
134: return b;
135: }
136: }
137:
138: protected class SoapUIDividerLayout implements LayoutManager {
139: private int lastOrientation;
140:
141: public SoapUIDividerLayout() {
142: lastOrientation = getOrientation();
143: }
144:
145: public void layoutContainer(Container c) {
146: if (lastOrientation != getOrientation()) {
147: if (leftButton != null) {
148: leftButton
149: .setIcon(getOrientation() == JSplitPane.VERTICAL_SPLIT ? upArrow
150: : leftArrow);
151: leftButton
152: .setMinimumSize(getOrientation() == JSplitPane.VERTICAL_SPLIT ? new Dimension(
153: 8, 6)
154: : new Dimension(6, 8));
155: }
156:
157: if (rightButton != null) {
158: rightButton
159: .setIcon(getOrientation() == JSplitPane.VERTICAL_SPLIT ? downArrow
160: : rightArrow);
161: rightButton
162: .setMinimumSize(getOrientation() == JSplitPane.VERTICAL_SPLIT ? new Dimension(
163: 8, 6)
164: : new Dimension(6, 8));
165: }
166:
167: lastOrientation = getOrientation();
168: }
169:
170: if (getOrientation() == JSplitPane.VERTICAL_SPLIT) {
171: if (leftButton != null)
172: leftButton.setBounds(2, 2, 8, 6);
173:
174: if (rightButton != null)
175: rightButton.setBounds(12, 2, 8, 6);
176: } else {
177: if (leftButton != null)
178: leftButton.setBounds(2, 2, 6, 8);
179:
180: if (rightButton != null)
181: rightButton.setBounds(2, 12, 6, 8);
182: }
183: }
184:
185: public Dimension preferredLayoutSize(Container c) {
186: return minimumLayoutSize(c);
187: }
188:
189: public void removeLayoutComponent(Component c) {
190: }
191:
192: public void addLayoutComponent(String string, Component c) {
193: }
194:
195: public Dimension minimumLayoutSize(Container parent) {
196: return new Dimension(10, 10);
197: }
198: }
199:
200: public JButton getLeftButton() {
201: return leftButton;
202: }
203:
204: public JButton getRightButton() {
205: return rightButton;
206: }
207: }
208:
209: public void setDividerLocation(JSplitPane jc, int location) {
210: super .setDividerLocation(jc, location);
211: enableOneTouchButtons(jc, location);
212: }
213:
214: public void update(Graphics g, JComponent c) {
215: super .update(g, c);
216: enableOneTouchButtons(getSplitPane(), getSplitPane()
217: .getDividerLocation());
218: }
219:
220: private void enableOneTouchButtons(JSplitPane jc, int location) {
221: JButton leftButton = ((SoapUIDivider) getDivider())
222: .getLeftButton();
223: JButton rightButton = ((SoapUIDivider) getDivider())
224: .getRightButton();
225:
226: if (leftButton != null)
227: leftButton.setEnabled(location > jc
228: .getMinimumDividerLocation()
229: && jc.getRightComponent() != null
230: && jc.getRightComponent().isVisible());
231:
232: if (rightButton != null)
233: rightButton.setEnabled(location < jc
234: .getMaximumDividerLocation()
235: && jc.getLeftComponent() != null
236: && jc.getLeftComponent().isVisible());
237: }
238: }
|