001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.chameleon;
028:
029: /**
030: * The class represents bi-directional ordered list of UI layers with
031: * possibility to add, remove and iterate over list elements
032: */
033: class CLayerList {
034: protected CLayerElement top;
035: protected CLayerElement bottom;
036: protected int count;
037:
038: /** Construct empty layers list */
039: CLayerList() {
040: top = null;
041: bottom = null;
042: count = 0;
043: }
044:
045: /**
046: * Find UI layer instance in the list
047: * @param layer the instance to search in the list for
048: * @return list element with searched layer if success, null otherwise
049: */
050: CLayerElement find(CLayer layer) {
051: for (CLayerElement l = top; l != null; l = l.lower)
052: if (l.layer == layer)
053: return l;
054: return null;
055: }
056:
057: /**
058: * Add new layer to the top of list with no check for other
059: * occurrences of the layer in the list
060: *
061: * @param layer CLayer instance to be added
062: * @return list element of the newly added layer
063: */
064: CLayerElement addLayer(CLayer layer) {
065: CLayerElement le = new CLayerElement(layer, top, null);
066: if (top != null)
067: top.upper = le;
068: top = le;
069: if (bottom == null)
070: bottom = le;
071: count++;
072: return le;
073: }
074:
075: /**
076: * Remove layer from the list
077: *
078: * @param layer CLayer instance to be removed
079: * @return true if the layer was found and removed, false otherwise
080: */
081: boolean removeLayer(CLayer layer) {
082: CLayerElement le = find(layer);
083: if (le != null) {
084: removeLayerElement(le);
085: le.layer = null;
086: return true;
087: } else {
088: return false;
089: }
090: };
091:
092: /**
093: * Remove layer element from the list with no extra checks.
094: * It's caller's responsibility to apply the method on
095: * list elements only.
096: *
097: * @param le list element to be removed
098: */
099: void removeLayerElement(CLayerElement le) {
100: CLayerElement upper = le.upper;
101: CLayerElement lower = le.lower;
102:
103: if (upper != null) {
104: upper.lower = lower;
105: } else if (top == le) {
106: top = lower;
107: }
108:
109: if (lower != null) {
110: lower.upper = upper;
111: } else if (bottom == le) {
112: bottom = upper;
113: }
114:
115: // Clear links to neighbour layers
116: le.upper = le.lower = null;
117: count--;
118: }
119:
120: /**
121: * Get the most top list element
122: * @return return the element with top most layer
123: */
124: CLayerElement getTop() {
125: return top;
126: }
127:
128: /**
129: * Get the most bottom list element
130: * @return return the element with top most layer
131: */
132: CLayerElement getBottom() {
133: return bottom;
134: }
135:
136: /**
137: * Get number of layers in the list
138: * @return number of list elements
139: */
140: int size() {
141: return count;
142: }
143: }
|