Revision 298 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

View differences:

DefaultJDynObjectSetComponent.java
57 57
public class DefaultJDynObjectSetComponent extends JPanel implements
58 58
    JDynObjectSetComponent, Observer {
59 59

  
60
    /**
61
     * Utility Action extension which adds a new checkEnabled() method to
62
     * set enabled or not depending on the isEnabled() method implementation.
63
     * That will only work as expected if the isEnabled() method implementation
64
     * is changed.
65
     * 
66
     * @author gvSIG Team
67
     * @version $Id$
68
     */
69
    @SuppressWarnings("serial")
70
    private static abstract class AbstractEnableAction extends AbstractAction {
71

  
72
        public AbstractEnableAction(Icon icon) {
73
            super(null, icon);
74
        }
75

  
76
        public AbstractEnableAction(String name) {
77
            super(name);
78
        }
79

  
80
        public void checkEnabled() {
81
            setEnabled(isEnabled());
82
        }
83

  
84
    }
85

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

  
62 88
    private static final Logger LOG =
63 89
        LoggerFactory.getLogger(DefaultJDynObjectSetComponent.class);
90
    private final DynObjectSetModel model;
64 91

  
65
    private final DynObjectSetModel model;
66 92
    private final DynObjectSwingManager manager;
93
    private AbstractEnableAction firstAction;
67 94

  
68
    private AbstractEnableAction firstAction;
69 95
    private AbstractEnableAction previousAction;
70 96

  
71 97
    private JLabel pageCountLabel;
......
115 141
        initializeGUI();
116 142
    }
117 143

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

  
126 144
    private void addDynObjectPager() {
127 145
        JToolBar toolBar = new JToolBar();
128 146
        toolBar.setRollover(true);
......
142 160
        add(toolBar, BorderLayout.SOUTH);
143 161
    }
144 162

  
163
    private void addDynObjectViewer() {
164
        try {
165
            component =
166
                ToolsSwingLocator.getDynObjectSwingManager()
167
                    .createJDynObjectComponent(model.getCurrentDynObject());
168

  
169
            add(component.asJComponent(), BorderLayout.CENTER);
170
        } catch (BaseException e) {
171
            LOG.error(
172
                "Error creating the component to view the current DynObject in"
173
                    + " position: " + model.getCurrentPosition(), e);
174
        }
175

  
176
    }
177

  
178
    public JComponent asJComponent() {
179
        return this;
180
    }
181

  
182
    public DynObjectSet getDynObjectSet() {
183
        return model.getDynObjectSet();
184
    }
185

  
145 186
    @SuppressWarnings("serial")
146 187
    private Action getFirstAction() {
147 188
        if (firstAction == null) {
......
152 193
                    model.first();
153 194
                }
154 195

  
196
                @Override
155 197
                public boolean isEnabled() {
156 198
                    return model.hasPrevious();
157 199
                };
......
163 205
    }
164 206

  
165 207
    @SuppressWarnings("serial")
166
    private Action getPreviousAction() {
167
        if (previousAction == null) {
208
    private Action getLastAction() {
209
        if (lastAction == null) {
168 210
            // TODO: replace the button text with an Icon
169
            previousAction = new AbstractEnableAction("<") {
211
            lastAction = new AbstractEnableAction(">>") {
170 212

  
171 213
                public void actionPerformed(ActionEvent e) {
172
                    model.previous();
214
                    model.last();
173 215
                }
174 216

  
217
                @Override
175 218
                public boolean isEnabled() {
176
                    return model.hasPrevious();
219
                    return model.hasNext();
177 220
                };
221

  
178 222
            };
179
            previousAction.checkEnabled();
180
            previousAction.putValue(Action.SHORT_DESCRIPTION,
181
                "Go to previous value");
223
            lastAction.checkEnabled();
224
            lastAction.putValue(Action.SHORT_DESCRIPTION, "Go to last value");
182 225
        }
183
        return previousAction;
226
        return lastAction;
184 227
    }
185 228

  
186
    private JLabel getPageCount() {
187
        if (pageCountLabel == null) {
188
            pageCountLabel = new JLabel();
189
            updatePageCountLabel();
190
        }
191
        return pageCountLabel;
229
    public Manager getManager() {
230
        return manager;
192 231
    }
193 232

  
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 233
    @SuppressWarnings("serial")
205 234
    private Action getNextAction() {
206 235
        if (nextAction == null) {
......
211 240
                    model.next();
212 241
                }
213 242

  
243
                @Override
214 244
                public boolean isEnabled() {
215 245
                    return model.hasNext();
216 246
                };
......
222 252
        return nextAction;
223 253
    }
224 254

  
255
    private JLabel getPageCount() {
256
        if (pageCountLabel == null) {
257
            pageCountLabel = new JLabel();
258
            updatePageCountLabel();
259
        }
260
        return pageCountLabel;
261
    }
262

  
225 263
    @SuppressWarnings("serial")
226
    private Action getLastAction() {
227
        if (lastAction == null) {
264
    private Action getPreviousAction() {
265
        if (previousAction == null) {
228 266
            // TODO: replace the button text with an Icon
229
            lastAction = new AbstractEnableAction(">>") {
267
            previousAction = new AbstractEnableAction("<") {
230 268

  
231 269
                public void actionPerformed(ActionEvent e) {
232
                    model.last();
270
                    model.previous();
233 271
                }
234 272

  
273
                @Override
235 274
                public boolean isEnabled() {
236
                    return model.hasNext();
275
                    return model.hasPrevious();
237 276
                };
238

  
239 277
            };
240
            lastAction.checkEnabled();
241
            lastAction.putValue(Action.SHORT_DESCRIPTION, "Go to last value");
278
            previousAction.checkEnabled();
279
            previousAction.putValue(Action.SHORT_DESCRIPTION,
280
                "Go to previous value");
242 281
        }
243
        return lastAction;
282
        return previousAction;
244 283
    }
245 284

  
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

  
285
    /**
286
     * Initializes the GUI.
287
     */
288
    private void initializeGUI() {
289
        addDynObjectViewer();
290
        addDynObjectPager();
259 291
    }
260 292

  
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 293
    public void update(Observable observable, Object notification) {
274 294
        // When the model changes the current DynObject, update the contained
275 295
        // components status
......
278 298
    }
279 299

  
280 300
    /**
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 301
     * Updates the status of the tool bar when the model changes the
293 302
     * current DynObject.
294 303
     */
......
308 317
    }
309 318

  
310 319
    /**
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$
320
     * Updates the status of the {@link DynObject} viewer when the model changes
321
     * the current DynObject.
318 322
     */
319
    @SuppressWarnings("serial")
320
    private static abstract class AbstractEnableAction extends AbstractAction {
323
    private void updateDynObjectViewer() {
324
        remove(component.asJComponent());
325
        addDynObjectViewer();
321 326

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

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

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

  
330
    /**
331
     * 
332
     */
333
    private void updatePageCountLabel() {
334
        StringBuffer buffer = new StringBuffer();
335
        buffer.append(model.getCurrentPosition() + 1).append('/').append(
336
            model.getSize());
337
        pageCountLabel.setText(buffer.toString());
334 338
    }
335 339

  
336 340
}

Also available in: Unified diff