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.types.Alignment;
019import com.smartgwt.client.types.VerticalAlignment;
020import com.smartgwt.client.widgets.Label;
021import com.smartgwt.client.widgets.layout.HLayout;
022import com.smartgwt.client.widgets.layout.VLayout;
023import java.util.Collection;
024
025/**
026 * Pattern for grid view.
027 * 
028 * @author kulikov
029 * @param <R>
030 * @param <V>
031 */
032public abstract class Grid<R extends HLayout, V> extends VLayout {
033    private R[] records;
034    private final VLayout viewPort = new VLayout();
035    private final HLayout bottomPanel = new HLayout();
036    
037    public Grid(String caption) {
038        super();
039        init(caption);
040    }
041    
042    private void init(String caption) {
043        this.records = records();
044        this.setStyleName("grid-bg");
045        this.setLayoutMargin(1);
046        
047        VLayout background = new VLayout();
048        background.setWidth100();
049        background.setHeight100();
050        background.setStyleName("grid-panel");
051        addMember(background);
052        
053
054        HLayout topPanel = new HLayout();
055        topPanel.setHeight(30);
056        topPanel.setAlign(VerticalAlignment.CENTER);
057        topPanel.setAlign(Alignment.LEFT);
058        
059        topPanel.setLayoutMargin(3);
060        topPanel.setBackgroundColor("#e6f1f6");
061        
062        Label label = new Label(caption);
063        label.setWidth100();
064        label.setStyleName("caption");
065        topPanel.addMember(label);
066
067        background.addMember(topPanel);
068        
069        HLayout toolBar = this.toolBar();
070        if (toolBar != null) {
071            background.addMember(toolBar);
072        }
073        
074        HLayout header = this.tableHeader();
075        if (header != null) {
076            background.addMember(header);
077        }
078
079        setAutoHeight();
080
081        bottomPanel.setHeight(30);
082        bottomPanel.setBackgroundColor("#e6f1f6");
083        bottomPanel.setLayoutAlign(VerticalAlignment.CENTER);
084        bottomPanel.setDefaultLayoutAlign(VerticalAlignment.CENTER);
085        
086        viewPort.setAutoHeight();
087        
088        background.addMember(viewPort);
089        background.addMember(bottomPanel);
090    }
091    
092    protected abstract R[] records();
093    protected abstract HLayout tableHeader();
094    protected abstract HLayout toolBar();
095    
096    protected void setValues(Collection<V> values) {
097        try {
098            for (int i = 0; i < records.length; i++) {
099                records[i].setVisible(false);
100                viewPort.removeMember(records[i]);
101            }
102        } catch (Exception e) {
103        }
104
105        if (values.isEmpty()) {
106            viewPort.addMember(records[0]);
107            updateRecord(-1, records[0], null);
108            records[0].show();
109            return;
110        }
111
112        viewPort.addMember(records[0]);
113        records[0].show();
114        updateRecord(0, records[0], null);
115        
116        int i = 1;
117        for (V value : values) {
118            viewPort.addMember(records[i]);
119            records[i].show();
120            updateRecord(i, records[i], value);
121            i++;
122        }
123    }
124    
125    protected abstract void updateRecord(int pos, R record, V value);
126    
127    public void setFooterContent(HLayout component) {
128        component.setWidth(100);
129        bottomPanel.addMember(component);
130    }
131    
132    protected class Strut extends HLayout {
133        public Strut(int size) {
134            super();
135            setWidth(size);                    
136        }
137    }
138    
139}