2
0
Fork 0
imxmi/src/com/idcanet/vasc/impl/swt/SwtVascViewRenderer.java

236 lines
8.5 KiB
Java
Raw Normal View History

2007-03-22 00:01:07 +00:00
/*
* Copyright 2004-2007 IDCA. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and
* the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY IDCA AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IDCA OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the authors and
* should not be interpreted as representing official policies, either expressed or implied, of IDCA.
*/
package com.idcanet.vasc.impl.swt;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import com.idcanet.vasc.core.VascTable;
import com.idcanet.vasc.core.VascUserOption;
import com.idcanet.vasc.core.VascViewRenderer;
import com.idcanet.vasc.core.actions.RowVascAction;
import com.idcanet.vasc.core.column.VascTableColumn;
import fr.jayasoft.binding.jface.adapter.JFaceComponentFactory;
import fr.jayasoft.binding.jface.utils.table.AbstractColumn;
import fr.jayasoft.binding.jface.utils.table.PropertyColumn;
/**
*
* @author Willem Cazander
* @version 1.0 Mar 21, 2007
*/
public class SwtVascViewRenderer implements VascViewRenderer {
private Logger logger = null;
private Composite parent = null;
private VascTable table = null;
public SwtVascViewRenderer(Composite parent) {
logger = Logger.getLogger(SwtVascViewRenderer.class.getName());
this.parent=parent;
}
public void renderView(VascTable table) {
this.table=table;
GridLayout layout = new GridLayout();
layout.marginHeight = 0;
layout.marginWidth = 0;
parent.setLayout(layout);
Composite header = new Composite(parent, SWT.NONE);
GridLayout headerLayout = new GridLayout();
headerLayout.numColumns = 6;
header.setLayout(headerLayout);
header.setLayoutData(new GridData(SWT.NONE));
Composite body = new Composite(parent, SWT.NONE);
GridLayout bodyLayout = new GridLayout();
bodyLayout.numColumns = 1;
body.setLayout(bodyLayout);
body.setLayoutData(new GridData(GridData.FILL_BOTH));
Composite footer = new Composite(parent, SWT.NONE);
GridLayout footerLayout = new GridLayout();
footerLayout.numColumns = 6;
footer.setLayout(footerLayout);
footer.setLayoutData(new GridData(SWT.NONE));
createHeader(header);
createBody(body);
createFooter(footer);
// should be last
partCreated();
}
public void createHeader(Composite header) {
logger.finest("Creating header");
header.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Color c = new Color(header.getDisplay(),255,255,255);
header.setBackground(c);
if(table.getHeaderName()!=null) {
Font headerFont = new Font(header.getDisplay(), "verdana", 16, SWT.BOLD);
Label l = new Label(header, SWT.CENTER);
l.setFont(headerFont);
l.setText(table.getVascTextValue().getTextValue(table.getHeaderName()));
l.setBackground(c);
}
// create options
for(VascUserOption option:table.getUserOptions()) {
/*
if(option.getLabelText()!=null) {
Label l = new Label(header,SWT.WRAP);
l.setText(crudTable.i18n(cpo.getLabelText()));
l.setBackground(c);
}
// create ValueHolder
cpo.setValueHolder(new ValueHolder(null));
// create control
cpo.setTempObjectPropertyControl(cpo.getPropertyEditor().createPropertyEditor(header,cpo.getValueHolder()));
cpo.getValueHolder().addPropertyChangeListener(this);
*/
}
}
public void createBody(Composite body) {
logger.finer("Creating body");
final TableViewer tableViewer = JFaceComponentFactory.createTableViewer(body,table.getSelectionInList(),getTableColumns(crudTable),false);
final Table table2 = tableViewer.getTable();
table2.setLayoutData(new GridData(GridData.FILL_BOTH));
table2.setHeaderVisible(true);
table2.setLinesVisible(true);
//Add sort indicator and sort data when column selected
Listener sortListener = new Listener() {
public void handleEvent(Event e) {
// determine new sort column and direction
TableColumn sortColumn = table2.getSortColumn();
TableColumn currentColumn = (TableColumn) e.widget;
int dir = table2.getSortDirection();
if (sortColumn == currentColumn) {
dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
} else {
table2.setSortColumn(currentColumn);
dir = SWT.UP;
}
// sort the data based on column and direction
String prop = (String)currentColumn.getData("PROP");
List l = new ArrayList(10);
/*
for(int i=0;i<crudTable.getSelectionInList().getSize();i++) {
l.add(crudTable.getSelectionInList().getElementAt(i));
}
BeanPropertyComparator c = new BeanPropertyComparator();
c.setAscending(dir!=SWT.DOWN);
c.setProperty(prop);
Collections.sort(l,c);
crudTable.getSelectionInList().setList(l);
*/
}
};
Integer totalSize = VascTable.getTotalColumnSize(table);
logger.finer("Total size: "+totalSize);
TableColumn[] columns = table2.getColumns();
for (int i = 0; i < columns.length; i++) {
columns[i].pack();
columns[i].setMoveable(true);
columns[i].setResizable(true);
columns[i].addListener(SWT.Selection, sortListener);
//columns[i].setData("PROP",table.getTableColumns().get(i)....);
if(totalSize!=null) {
Integer cWidth = table.getTableColumns().get(i).getWidth();
columns[i].setWidth(cWidth);
/*
columns[i].setWidth((int)((double)tableWidth/(double)totalSize)*cWidth);
logger.finest("Setting column width: "+(int)((double)tableWidth/(double)totalSize)*cWidth+" total: "+totalSize+" c: "+cWidth+" of column: "+i);
*/
}
}
logger.fine("Table with columns created: "+table2.getColumnCount());
}
private static AbstractColumn[] getTableColumns(VascTable crudTable) {
AbstractColumn[] cols = new AbstractColumn[crudTable.getCrudTableColumns().size()];
int i=0;
for(VascTableColumn c:crudTable.getTableColumns()) {
AbstractColumn ac = new PropertyColumn(crudTable.i18n(c.getName()),c.getObjectProperty());
ac.setEditable(false);
ac.setReorderable(true);
ac.setSortable(true);
cols[i]=ac;
i++;
}
return cols;
}
public void createFooter(Composite footer) {
logger.finest("Creating footer");
for(RowVascAction action:table.getRowActions()) {
//action.createActionObject(table);
//Button ton = new Button(footer, SWT.NONE);
//addButton.setText(crudTable.i18n("generic.add"));
//addButton.setImage(crudTable.getImageDescriptor("icons/fam/table_add.png").createImage());
//addButton.addSelectionListener(new AddEvent(crudTable));
//action.
}
}
/**
* Is called when all createPartControl is done with creating Parts
*/
public void partCreated() {
}
}