001: /*
002: * SwingML Copyright (C) 2002 Robert Morris.
003: *
004: * This library is free software; you can redistribute it and/or modify it under
005: * the terms of the GNU Lesser General Public License as published by the Free
006: * Software Foundation; either version 2 of the License, or (at your option) any
007: * later version.
008: *
009: * This library is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012: * details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with this library; if not, write to the Free Software Foundation, Inc.,
016: * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: * Authors: Robert Morris <robertj@morris.net>
019: *
020: */
021: package org.swingml.component;
022:
023: import java.awt.*;
024:
025: import javax.swing.*;
026:
027: import org.swingml.*;
028: import org.swingml.model.*;
029: import org.swingml.system.*;
030:
031: public class JSplitPaneComponent extends JSplitPane implements
032: XMLStateTranslatable {
033:
034: private boolean m_isLeft = false;
035:
036: /**
037: * Class constructor
038: *
039: * @param model
040: * A reference to a valid JSplitPaneModel instance. This
041: * reference is used to properly instantiate the JSplitPane
042: * component with information provided by the JSplitPaneModel
043: * instance.
044: */
045: public JSplitPaneComponent(JSplitPaneModel aModel) {
046: super (
047: (aModel.getType().equalsIgnoreCase(
048: Constants.SPLIT_HORIZONTAL) ? JSplitPane.HORIZONTAL_SPLIT
049: : JSplitPane.VERTICAL_SPLIT));
050: super .setDividerSize(aModel.getSize());
051: super .setName(aModel.getName());
052: super .setToolTipText(aModel.getTooltip());
053:
054: if (aModel.getLocation() instanceof Integer) {
055: super .setDividerLocation(((Integer) aModel.getLocation())
056: .intValue());
057: } else if (aModel.getLocation() instanceof Double) {
058: double theNewLocation = ((Double) aModel.getLocation())
059: .doubleValue();
060: if (theNewLocation < 1 || theNewLocation > 100) {
061: SwingMLLogger.getInstance().log(
062: "Syntax Error: LOCATION % for SPLITTER ["
063: + aModel.getName()
064: + "] can only be between 1 and 100");
065: // Set a default value of 50%.
066: theNewLocation = 0.5;
067: } else {
068: theNewLocation *= 0.01;
069: super .setResizeWeight(theNewLocation);
070: }
071:
072: super .setDividerLocation(theNewLocation);
073: }
074: super .setOneTouchExpandable(aModel.isExpandable());
075:
076: }
077:
078: /**
079: * Performs the necessary checking to make sure that a SwingML component is
080: * properly added to the JSplitPane. Forces Left vs. Right additions. The
081: * designation of "left" component and "right" component alternates with
082: * each additional add. Therefore, the first component is the left
083: * component, the second component added is the right component. If a third
084: * were added, it it would be the left component, and the fourth would
085: * become the right component.
086: *
087: * @param component
088: * The component to add to the JSplitPaneComponent.
089: */
090: public Component add(Component aComponent) {
091: this .m_isLeft = !this .m_isLeft;
092: if (this .m_isLeft) {
093: super .add(aComponent, JSplitPane.LEFT);
094: } else {
095: super .add(aComponent, JSplitPane.RIGHT);
096: }
097: return aComponent;
098: }
099:
100: public String getXMLState() {
101: StringBuffer buffer = new StringBuffer();
102: buffer.append("<SPLITPANE ");
103: buffer.append(Constants.NAME);
104: buffer.append("=\"");
105: buffer.append(getName());
106: buffer.append("\" ");
107: buffer.append(Constants.SIZE);
108: buffer.append("=\"");
109: buffer.append(getSize());
110: buffer.append("\" ");
111: buffer.append(Constants.LOCATION);
112: buffer.append("=\"");
113: buffer.append(getDividerLocation());
114: buffer.append("\" ");
115: buffer.append(Constants.EXPANDABLE);
116: buffer.append("=\"");
117: buffer.append(isOneTouchExpandable());
118: buffer.append("\" />");
119: return buffer.toString();
120: }
121:
122: public boolean hasState() {
123: return true;
124: }
125: }
|