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 / DefaultJDynObjectSetComponent.java @ 270

History | View | Annotate | Download (9.99 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 java.awt.BorderLayout;
25
import java.awt.event.ActionEvent;
26

    
27
import javax.swing.AbstractAction;
28
import javax.swing.Action;
29
import javax.swing.Box;
30
import javax.swing.Icon;
31
import javax.swing.JComponent;
32
import javax.swing.JLabel;
33
import javax.swing.JPanel;
34
import javax.swing.JToolBar;
35

    
36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38

    
39
import org.gvsig.tools.dynobject.DynObject;
40
import org.gvsig.tools.dynobject.DynObjectSet;
41
import org.gvsig.tools.exception.BaseException;
42
import org.gvsig.tools.observer.Observable;
43
import org.gvsig.tools.observer.Observer;
44
import org.gvsig.tools.service.Manager;
45
import org.gvsig.tools.swing.api.ToolsSwingLocator;
46
import org.gvsig.tools.swing.api.dynobject.DynObjectSwingManager;
47
import org.gvsig.tools.swing.api.dynobject.JDynObjectComponent;
48
import org.gvsig.tools.swing.api.dynobject.set.JDynObjectSetComponent;
49

    
50
/**
51
 * Default {@link JDynObjectSetComponent} implementation using a
52
 * {@link DynObjectSetModel}.
53
 * 
54
 * @author gvSIG Team
55
 * @version $Id$
56
 */
57
public class DefaultJDynObjectSetComponent extends JPanel implements
58
    JDynObjectSetComponent, Observer {
59

    
60
    private static final long serialVersionUID = -741238766386344850L;
61

    
62
    private static final Logger LOG =
63
        LoggerFactory.getLogger(DefaultJDynObjectSetComponent.class);
64

    
65
    private final DynObjectSetModel model;
66
    private final DynObjectSwingManager manager;
67

    
68
    private AbstractEnableAction firstAction;
69
    private AbstractEnableAction previousAction;
70

    
71
    private JLabel pageCountLabel;
72

    
73
    private AbstractEnableAction nextAction;
74

    
75
    private AbstractEnableAction lastAction;
76

    
77
    private JDynObjectComponent component;
78

    
79
    /**
80
     * Creates a new {@link DefaultJDynObjectSetComponent}.
81
     * 
82
     * @param set
83
     *            to render
84
     * @param manager
85
     *            which manages this component
86
     * @throws BaseException
87
     *             if there is an error creating the component
88
     */
89
    public DefaultJDynObjectSetComponent(DynObjectSet set,
90
        DynObjectSwingManager manager) throws BaseException {
91
        this(set, manager, true);
92
    }
93

    
94
    /**
95
     * Creates a new {@link DefaultJDynObjectSetComponent}.
96
     * 
97
     * @param set
98
     *            to render
99
     * @param manager
100
     *            which manages this component
101
     * @param isDoubleBuffered
102
     *            a boolean, true for double-buffering, which
103
     *            uses additional memory space to achieve fast, flicker-free
104
     *            updates
105
     * @throws BaseException
106
     *             if there is an error creating the component
107
     */
108
    public DefaultJDynObjectSetComponent(DynObjectSet set,
109
        DynObjectSwingManager manager, boolean isDoubleBuffered)
110
        throws BaseException {
111
        super(new BorderLayout(), isDoubleBuffered);
112
        this.model = new DynObjectSetModel(set);
113
        model.addObserver(this);
114
        this.manager = manager;
115
        initializeGUI();
116
    }
117

    
118
    /**
119
     * Initializes the GUI.
120
     */
121
    private void initializeGUI() {
122
        addDynObjectViewer();
123
        addDynObjectPager();
124
    }
125

    
126
    private void addDynObjectPager() {
127
        JToolBar toolBar = new JToolBar();
128
        toolBar.setRollover(true);
129
        toolBar.setFloatable(false);
130

    
131
        // Add horizontal glues to center the buttons
132
        toolBar.add(Box.createHorizontalGlue());
133
        toolBar.add(getFirstAction());
134
        toolBar.add(getPreviousAction());
135
        toolBar.addSeparator();
136
        toolBar.add(getPageCount());
137
        toolBar.addSeparator();
138
        toolBar.add(getNextAction());
139
        toolBar.add(getLastAction());
140
        toolBar.add(Box.createHorizontalGlue());
141

    
142
        add(toolBar, BorderLayout.SOUTH);
143
    }
144

    
145
    @SuppressWarnings("serial")
146
    private Action getFirstAction() {
147
        if (firstAction == null) {
148
            // TODO: replace the button text with an Icon
149
            firstAction = new AbstractEnableAction("<<") {
150

    
151
                public void actionPerformed(ActionEvent e) {
152
                    model.first();
153
                }
154

    
155
                public boolean isEnabled() {
156
                    return model.hasPrevious();
157
                };
158
            };
159
            firstAction.putValue(Action.SHORT_DESCRIPTION, "Go to first value");
160
            firstAction.checkEnabled();
161
        }
162
        return firstAction;
163
    }
164

    
165
    @SuppressWarnings("serial")
166
    private Action getPreviousAction() {
167
        if (previousAction == null) {
168
            // TODO: replace the button text with an Icon
169
            previousAction = new AbstractEnableAction("<") {
170

    
171
                public void actionPerformed(ActionEvent e) {
172
                    model.previous();
173
                }
174

    
175
                public boolean isEnabled() {
176
                    return model.hasPrevious();
177
                };
178
            };
179
            previousAction.checkEnabled();
180
            previousAction.putValue(Action.SHORT_DESCRIPTION,
181
                "Go to previous value");
182
        }
183
        return previousAction;
184
    }
185

    
186
    private JLabel getPageCount() {
187
        if (pageCountLabel == null) {
188
            pageCountLabel = new JLabel();
189
            updatePageCountLabel();
190
        }
191
        return pageCountLabel;
192
    }
193

    
194
    /**
195
     * 
196
     */
197
    private void updatePageCountLabel() {
198
        StringBuffer buffer = new StringBuffer();
199
        buffer.append(model.getCurrentPosition() + 1).append('/').append(
200
            model.getSize());
201
        pageCountLabel.setText(buffer.toString());
202
    }
203

    
204
    @SuppressWarnings("serial")
205
    private Action getNextAction() {
206
        if (nextAction == null) {
207
            // TODO: replace the button text with an Icon
208
            nextAction = new AbstractEnableAction(">") {
209

    
210
                public void actionPerformed(ActionEvent e) {
211
                    model.next();
212
                }
213

    
214
                public boolean isEnabled() {
215
                    return model.hasNext();
216
                };
217
            };
218
            nextAction.checkEnabled();
219
            nextAction.putValue(Action.SHORT_DESCRIPTION,
220
                "Go to following value");
221
        }
222
        return nextAction;
223
    }
224

    
225
    @SuppressWarnings("serial")
226
    private Action getLastAction() {
227
        if (lastAction == null) {
228
            // TODO: replace the button text with an Icon
229
            lastAction = new AbstractEnableAction(">>") {
230

    
231
                public void actionPerformed(ActionEvent e) {
232
                    model.last();
233
                }
234

    
235
                public boolean isEnabled() {
236
                    return model.hasNext();
237
                };
238

    
239
            };
240
            lastAction.checkEnabled();
241
            lastAction.putValue(Action.SHORT_DESCRIPTION, "Go to last value");
242
        }
243
        return lastAction;
244
    }
245

    
246
    private void addDynObjectViewer() {
247
        try {
248
            component =
249
                ToolsSwingLocator.getDynObjectSwingManager()
250
                    .createJDynObjectComponent(model.getCurrentDynObject());
251

    
252
            add(component.asJComponent(), BorderLayout.CENTER);
253
        } catch (BaseException e) {
254
            LOG.error(
255
                "Error creating the component to view the current DynObject in"
256
                    + " position: " + model.getCurrentPosition(), e);
257
        }
258

    
259
    }
260

    
261
    public Manager getManager() {
262
        return manager;
263
    }
264

    
265
    public JComponent asJComponent() {
266
        return this;
267
    }
268

    
269
    public DynObjectSet getDynObjectSet() {
270
        return model.getDynObjectSet();
271
    }
272

    
273
    public void update(Observable observable, Object notification) {
274
        // When the model changes the current DynObject, update the contained
275
        // components status
276
        updateDynObjectPager();
277
        updateDynObjectViewer();
278
    }
279

    
280
    /**
281
     * Updates the status of the {@link DynObject} viewer when the model changes
282
     * the current DynObject.
283
     */
284
    private void updateDynObjectViewer() {
285
        remove(component.asJComponent());
286
        addDynObjectViewer();
287

    
288
        repaint();
289
    }
290

    
291
    /**
292
     * Updates the status of the tool bar when the model changes the
293
     * current DynObject.
294
     */
295
    private void updateDynObjectPager() {
296
        firstAction.checkEnabled();
297
        previousAction.checkEnabled();
298
        nextAction.checkEnabled();
299
        lastAction.checkEnabled();
300

    
301
        StringBuffer buffer = new StringBuffer();
302
        buffer.append(model.getCurrentPosition() + 1).append('/').append(
303
            model.getSize());
304
        pageCountLabel.setText(buffer.toString());
305

    
306
        revalidate();
307
        repaint();
308
    }
309

    
310
    /**
311
     * Utility Action extension which adds a new checkEnabled() method to
312
     * set enabled or not depending on the isEnabled() method implementation.
313
     * That will only work as expected if the isEnabled() method implementation
314
     * is changed.
315
     * 
316
     * @author gvSIG Team
317
     * @version $Id$
318
     */
319
    @SuppressWarnings("serial")
320
    private static abstract class AbstractEnableAction extends AbstractAction {
321

    
322
        public AbstractEnableAction(Icon icon) {
323
            super(null, icon);
324
        }
325

    
326
        public AbstractEnableAction(String name) {
327
            super(name);
328
        }
329

    
330
        public void checkEnabled() {
331
            setEnabled(isEnabled());
332
        }
333

    
334
    }
335

    
336
}