Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / dynobject / set / DynObjectSetModel.java @ 298

History | View | Annotate | Download (4.87 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.tools.swing.impl.dynobject.set;
23

    
24
import org.gvsig.tools.ToolsLocator;
25
import org.gvsig.tools.dynobject.DynObject;
26
import org.gvsig.tools.dynobject.DynObjectPagingHelper;
27
import org.gvsig.tools.dynobject.DynObjectSet;
28
import org.gvsig.tools.exception.BaseException;
29
import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable;
30
import org.gvsig.tools.swing.api.dynobject.set.JDynObjectSetComponent;
31

    
32
/**
33
 * Model for a {@link JDynObjectSetComponent}.
34
 * 
35
 * @author gvSIG Team
36
 * @version $Id$
37
 */
38
public class DynObjectSetModel extends BaseWeakReferencingObservable {
39

    
40
    public static final int DEFAULT_PAGE_SIZE = 10;
41

    
42
    private final DynObjectPagingHelper helper;
43

    
44
    private long currentPosition = 0;
45

    
46
    /**
47
     * Creates a new {@link DynObjectSetModel} with a {@link DynObjectSet} to
48
     * render.
49
     * 
50
     * @param set
51
     *            the data set to show
52
     * @throws BaseException
53
     *             if there is an error creating the model
54
     */
55
    public DynObjectSetModel(DynObjectSet set) throws BaseException {
56
        this(set, DEFAULT_PAGE_SIZE);
57
    }
58

    
59
    /**
60
     * Creates a new {@link DynObjectSetModel} with a {@link DynObjectSet} to
61
     * render.
62
     * 
63
     * @param set
64
     *            the data set to show
65
     * @param pageSize
66
     *            the size of the pages of data to preload
67
     * @throws BaseException
68
     *             if there is an error creating the model
69
     */
70
    public DynObjectSetModel(DynObjectSet set, int pageSize)
71
        throws BaseException {
72
        helper =
73
            ToolsLocator.getDynObjectManager().createDynObjectPagingHelper(set);
74
        helper.setMaxPageSize(pageSize);
75
    }
76

    
77
    /**
78
     * Moves the current position to the first one, if it is not already there.
79
     */
80
    public void first() {
81
        if (currentPosition != 0) {
82
            currentPosition = 0;
83
            notifyObservers();
84
        }
85
    }
86

    
87
    /**
88
     * Returns the {@link DynObject} at the current position.
89
     * 
90
     * @return the current DynObject
91
     * @throws BaseException
92
     *             if there is an error getting the DynObject
93
     */
94
    public DynObject getCurrentDynObject() throws BaseException {
95
        return helper.getDynObjectAt(currentPosition);
96
    }
97

    
98
    /**
99
     * Returns the position of the current element.
100
     * 
101
     * @return the current position
102
     */
103
    public long getCurrentPosition() {
104
        return currentPosition;
105
    }
106

    
107
    /**
108
     * Returns the {@link DynObjectSet} used by this model.
109
     * 
110
     * @return the DynObjectSet used by this model
111
     */
112
    public DynObjectSet getDynObjectSet() {
113
        return helper.getDynObjectSet();
114
    }
115

    
116
    /**
117
     * Returns the number of DynObjects.
118
     * 
119
     * @return the number of DynObjects
120
     */
121
    public long getSize() {
122
        return helper.getTotalSize();
123
    }
124

    
125
    /**
126
     * Returns if there are more DynObjects after the current one.
127
     * 
128
     * @return if there are following DynObjects
129
     */
130
    public boolean hasNext() {
131
        return currentPosition < helper.getTotalSize() - 1;
132
    }
133

    
134
    /**
135
     * Returns if there are more DynObjects before the current one.
136
     * 
137
     * @return if there are preceding DynObjects
138
     */
139
    public boolean hasPrevious() {
140
        return currentPosition > 0;
141
    }
142

    
143
    /**
144
     * Moves the current position to the last one, if it is not already there.
145
     */
146
    public void last() {
147
        long lastPosition = helper.getTotalSize() - 1;
148
        if (currentPosition != lastPosition) {
149
            currentPosition = lastPosition;
150
            notifyObservers();
151
        }
152
    }
153

    
154
    /**
155
     * Moves the current position to the next one, if it exists.
156
     */
157
    public void next() {
158
        if (hasNext()) {
159
            currentPosition++;
160
            notifyObservers();
161
        }
162
    }
163

    
164
    /**
165
     * Moves the current position to the previous one, if it exists.
166
     */
167
    public void previous() {
168
        if (hasPrevious()) {
169
            currentPosition--;
170
            notifyObservers();
171
        }
172
    }
173

    
174
}