001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.web.client.grid;
017
018import com.smartgwt.client.util.SC;
019import com.smartgwt.client.widgets.layout.HLayout;
020import com.smartgwt.client.widgets.layout.VLayout;
021import java.util.Collection;
022
023/**
024 * Pattern for grid view.
025 * 
026 * @author kulikov
027 * @param <R>
028 * @param <V>
029 */
030public abstract class TabGrid<R extends HLayout, V> extends VLayout {
031    private R[] records;
032    private final VLayout viewPort = new VLayout();
033
034    public TabGrid(String caption) {
035        super();
036        init(caption);
037    }
038    
039    private void init(String caption) {
040        this.records = records();
041        this.setStyleName("grid-bg");
042//        this.setLayoutMargin(1);
043        
044
045        HLayout toolBar = this.toolBar();
046        if (toolBar != null) {
047            addMember(toolBar);
048        }
049        
050        HLayout header = this.tableHeader();
051        if (header != null) {
052            addMember(header);
053        }
054
055        setAutoHeight();
056
057        HLayout bottomPanel = new HLayout();
058        bottomPanel.setHeight(30);
059        bottomPanel.setBackgroundColor("#e6f1f6");
060
061        viewPort.setAutoHeight();
062        addMember(viewPort);
063//        addMember(bottomPanel);
064    }
065    
066    protected abstract R[] records();
067    protected abstract HLayout tableHeader();
068    protected abstract HLayout toolBar();
069    
070    protected void setValues(Collection<V> values) {
071        try {
072            for (int i = 0; i < records.length; i++) {
073                records[i].setVisible(false);
074                viewPort.removeMember(records[i]);
075            }
076        } catch (Exception e) {
077        }
078
079        if (values.isEmpty()) {
080            viewPort.addMember(records[0]);
081            updateRecord(-1, records[0], null);
082            records[0].show();
083            return;
084        }
085        
086        int i = 0;
087        for (V value : values) {
088            try {
089                viewPort.addMember(records[i]);
090            updateRecord(i, records[i], value);
091            records[i].show();
092            i++;
093            } catch (Exception e) {
094                SC.say(e.getMessage() + ":::;" + Integer.toString(i));
095            }
096        }
097    }
098    
099    protected abstract void updateRecord(int pos, R record, V value);
100    
101    protected class Strut extends HLayout {
102        public Strut(int size) {
103            super();
104            setWidth(size);                    
105        }
106    }
107    
108}