001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.settings.support;
017    
018    import javax.swing.JSplitPane;
019    
020    import org.springframework.richclient.settings.Settings;
021    import org.springframework.util.Assert;
022    import org.springframework.util.StringUtils;
023    
024    /**
025     * @author Peter De Bruycker
026     */
027    public class SplitPaneMemento implements Memento {
028    
029            private JSplitPane splitPane;
030    
031            private String key;
032    
033            public SplitPaneMemento(JSplitPane splitPane) {
034                    this(splitPane, null);
035            }
036    
037            public SplitPaneMemento(JSplitPane splitPane, String key) {
038                    Assert.notNull(splitPane, "SplitPane cannot be null");
039                    Assert.isTrue(StringUtils.hasText(key) || StringUtils.hasText(splitPane.getName()),
040                                    "Key is empty or splitpane has no name");
041    
042                    if (!StringUtils.hasText(key)) {
043                            key = splitPane.getName();
044                    }
045    
046                    this.splitPane = splitPane;
047                    this.key = key;
048            }
049    
050            public void saveState(Settings settings) {
051                    settings.setInt(key + ".dividerLocation", splitPane.getDividerLocation());
052            }
053    
054            public void restoreState(Settings settings) {
055                    if (settings.contains(key + ".dividerLocation")) {
056                            splitPane.setDividerLocation(settings.getInt(key + ".dividerLocation"));
057                    }
058            }
059    
060            public JSplitPane getSplitPane() {
061                    return splitPane;
062            }
063    
064            public String getKey() {
065                    return key;
066            }
067    }