Project

General

Profile

« Previous | Next » 

Revision 65508125

Added by Andreas Kohlbecker about 7 years ago

PopupEditor with GridLayout support

View differences:

src/main/java/eu/etaxonomy/vaadin/mvp/AbstractPopupEditor.java
19 19
import com.vaadin.ui.CheckBox;
20 20
import com.vaadin.ui.Component;
21 21
import com.vaadin.ui.Field;
22
import com.vaadin.ui.GridLayout;
23
import com.vaadin.ui.GridLayout.OutOfBoundsException;
24
import com.vaadin.ui.GridLayout.OverlapsException;
22 25
import com.vaadin.ui.HorizontalLayout;
23 26
import com.vaadin.ui.Layout;
24 27
import com.vaadin.ui.Notification;
......
51 54

  
52 55
    private Button cancel;
53 56

  
57
    private GridLayout _gridLayoutCache;
58

  
54 59
    public AbstractPopupEditor(Layout layout, Class<DTO> dtoType) {
55 60

  
56 61
        setWidthUndefined();
......
93 98
        return mainLayout;
94 99
    }
95 100

  
101
    protected Layout getFieldLayout() {
102
        return fieldLayout;
103
    }
104

  
105
    /**
106
     * @return
107
     */
108
    private GridLayout gridLayout() {
109
        if(_gridLayoutCache == null){
110
            if(fieldLayout instanceof GridLayout){
111
                _gridLayoutCache = (GridLayout)fieldLayout;
112
            } else {
113
                throw new RuntimeException("The fieldlayout of this editor is not a GridLayout");
114
            }
115
        }
116
        return _gridLayoutCache;
117
    }
118

  
96 119
    @Override
97 120
    public void setReadOnly(boolean readOnly) {
98 121
        super.setReadOnly(readOnly);
......
147 170
        return addField(new TextField(caption), propertyId);
148 171
    }
149 172

  
173
    protected TextField addTextField(String caption, String propertyId, int column1, int row1,
174
            int column2, int row2)
175
            throws OverlapsException, OutOfBoundsException {
176
        return addField(new TextField(caption), propertyId, column1, row1, column2, row2);
177
    }
178

  
179
    protected TextField addTextField(String caption, String propertyId, int column, int row)
180
            throws OverlapsException, OutOfBoundsException {
181
        return addField(new TextField(caption), propertyId, column, row);
182
    }
183

  
150 184
    protected PopupDateField addDateField(String caption, String propertyId) {
151 185
        return addField(new PopupDateField(caption), propertyId);
152 186
    }
......
157 191

  
158 192
    protected <T extends Field> T addField(T field, String propertyId) {
159 193
        fieldGroup.bind(field, propertyId);
160
        fieldLayout.addComponent(field);
194
        addComponent(field);
195
        return field;
196
    }
197

  
198
    /**
199
     * Can only be used if the <code>fieldlayout</code> is a GridLayout.
200
     *
201
     * @param field
202
     *            the field to be added, not <code>null</code>.
203
     * @param propertyId
204
     * @param column
205
     *            the column index, starting from 0.
206
     * @param row
207
     *            the row index, starting from 0.
208
     * @throws OverlapsException
209
     *             if the new component overlaps with any of the components
210
     *             already in the grid.
211
     * @throws OutOfBoundsException
212
     *             if the cell is outside the grid area.
213
     */
214
    protected <T extends Field> T addField(T field, String propertyId, int column, int row)
215
            throws OverlapsException, OutOfBoundsException {
216
        fieldGroup.bind(field, propertyId);
217
        addComponent(field, column, row);
218
        return field;
219
    }
220

  
221
    /**
222
     * Can only be used if the <code>fieldlayout</code> is a GridLayout.
223
     *
224
     * @param field
225
     * @param propertyId
226
     * @param column1
227
     * @param row1
228
     * @param column2
229
     * @param row2
230
     * @return
231
     * @throws OverlapsException
232
     * @throws OutOfBoundsException
233
     */
234
    protected <T extends Field> T addField(T field, String propertyId, int column1, int row1,
235
            int column2, int row2)
236
            throws OverlapsException, OutOfBoundsException {
237
        fieldGroup.bind(field, propertyId);
238
        addComponent(field, column1, row1, column2, row2);
161 239
        return field;
162 240
    }
163 241

  
164 242
    protected void addComponent(Component component) {
243
        applyDefaultComponentStyles(component);
165 244
        fieldLayout.addComponent(component);
166 245
    }
167 246

  
247
    /**
248
     * @param component
249
     */
250
    public void applyDefaultComponentStyles(Component component) {
251
        component.setStyleName(getDefaultComponentStyles());
252
    }
253

  
254
    protected abstract String getDefaultComponentStyles();
255

  
256
    /**
257
     * Can only be used if the <code>fieldlayout</code> is a GridLayout.
258
     * <p>
259
     * Adds the component to the grid in cells column1,row1 (NortWest corner of
260
     * the area.) End coordinates (SouthEast corner of the area) are the same as
261
     * column1,row1. The coordinates are zero-based. Component width and height
262
     * is 1.
263
     *
264
     * @param component
265
     *            the component to be added, not <code>null</code>.
266
     * @param column
267
     *            the column index, starting from 0.
268
     * @param row
269
     *            the row index, starting from 0.
270
     * @throws OverlapsException
271
     *             if the new component overlaps with any of the components
272
     *             already in the grid.
273
     * @throws OutOfBoundsException
274
     *             if the cell is outside the grid area.
275
     */
276
    public void addComponent(Component component, int column, int row)
277
            throws OverlapsException, OutOfBoundsException {
278
        applyDefaultComponentStyles(component);
279
        gridLayout().addComponent(component, column, row, column, row);
280
    }
281

  
282
    /**
283
     * Can only be used if the <code>fieldlayout</code> is a GridLayout.
284
     * <p>
285
     * Adds a component to the grid in the specified area. The area is defined
286
     * by specifying the upper left corner (column1, row1) and the lower right
287
     * corner (column2, row2) of the area. The coordinates are zero-based.
288
     * </p>
289
     *
290
     * <p>
291
     * If the area overlaps with any of the existing components already present
292
     * in the grid, the operation will fail and an {@link OverlapsException} is
293
     * thrown.
294
     * </p>
295
     *
296
     * @param component
297
     *            the component to be added, not <code>null</code>.
298
     * @param column1
299
     *            the column of the upper left corner of the area <code>c</code>
300
     *            is supposed to occupy. The leftmost column has index 0.
301
     * @param row1
302
     *            the row of the upper left corner of the area <code>c</code> is
303
     *            supposed to occupy. The topmost row has index 0.
304
     * @param column2
305
     *            the column of the lower right corner of the area
306
     *            <code>c</code> is supposed to occupy.
307
     * @param row2
308
     *            the row of the lower right corner of the area <code>c</code>
309
     *            is supposed to occupy.
310
     * @throws OverlapsException
311
     *             if the new component overlaps with any of the components
312
     *             already in the grid.
313
     * @throws OutOfBoundsException
314
     *             if the cells are outside the grid area.
315
     */
316
    public void addComponent(Component component, int column1, int row1,
317
            int column2, int row2)
318
            throws OverlapsException, OutOfBoundsException {
319
        applyDefaultComponentStyles(component);
320
        gridLayout().addComponent(component, column1, row1, column2, row2);
321
    }
322

  
323

  
324

  
168 325
    // ------------------------ data binding ------------------------ //
169 326

  
170 327
    protected void bindDesign(Component component) {

Also available in: Unified diff