KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > SelectionAndScrollPositionManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.explorer.propertysheet;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24
25 /**
26  * Keeps a global list of scroll positions and selected tabs for the property
27  * sheet.
28  */

29 class SelectionAndScrollPositionManager {
30     private static Map JavaDoc<String JavaDoc, String JavaDoc> groupsToNodes = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
31     private static Map JavaDoc<String JavaDoc, Integer JavaDoc> namesToPositions = new HashMap JavaDoc<String JavaDoc, Integer JavaDoc>();
32     private static final Integer JavaDoc zero = new Integer JavaDoc(0);
33     private String JavaDoc lastSelectedGroup = "";
34     private String JavaDoc nodeName = null;
35
36     /**
37      * Stores the current node name.
38      *
39      * @param name
40      */

41     public void setCurrentNodeName(String JavaDoc name) {
42         nodeName = name;
43     }
44
45     public String JavaDoc getCurrentNodeName() {
46         return nodeName;
47     }
48
49     public String JavaDoc getLastSelectedGroupName() {
50         return lastSelectedGroup;
51     }
52
53     /**
54      * Store the current scroll position.
55      *
56      * @param pos A scroll position. Will only be stored if >= 0.
57      * @param name A node or tab name
58      */

59     public void storeScrollPosition(int pos, String JavaDoc name) {
60         if (pos >= 0) {
61             synchronized (namesToPositions) {
62                 namesToPositions.put(name, Integer.valueOf(pos));
63             }
64         }
65     }
66
67     /**
68      * Stores the last selected group
69      * @param group
70      */

71     public void storeLastSelectedGroup(String JavaDoc group) {
72         if (nodeName != null) {
73             synchronized (groupsToNodes) {
74                 lastSelectedGroup = group;
75                 groupsToNodes.put(nodeName, group);
76             }
77         }
78     }
79
80     /**
81      * Fetch the remembered group selection name. The returned value
82      * may or may not actually be present in the list of tab names for
83      * a given node.
84      * <p>
85      * If no value is stored for this node, will return the last selected
86      * group name for a node that does have groups, such that if a given
87      * tab is selected and the selected node changes to another node which
88      * is unknown but has the same list of tab names, the selected tab
89      * will not suddenly change.
90      * <p>
91      * If the name is not found, the caller should use PropUtils.basicPropsTabName()
92      * as the third fallback for selection, setting the selection thus to the
93      * Properties tab.
94      *
95      * @param name The name of a node
96      * @return A name of a group.
97      */

98     public String JavaDoc getGroupNameForNodeName(String JavaDoc name) {
99         String JavaDoc result = null;
100
101         synchronized (groupsToNodes) {
102             result = groupsToNodes.get(name);
103         }
104
105         if (result == null) {
106             result = lastSelectedGroup;
107         }
108
109         return result;
110     }
111
112     public int getScrollPositionForNodeName(String JavaDoc name) {
113         Integer JavaDoc result = zero;
114         Integer JavaDoc found = namesToPositions.get(name);
115
116         if (found != null) {
117             result = found;
118         }
119
120         return result.intValue();
121     }
122 }
123
Popular Tags