/**
* This file is part of HIDB2.
*
* HIDB2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PoJamas is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with HIDB2. If not, see <http://www.gnu.org/licenses/>.
*/
package hidb2.gui.util;
import static hidb2.kern.AttrType.T_Integer;
import static hidb2.kern.AttrType.T_Object;
import static hidb2.kern.AttrType.T_String;
import static hidb2.kern.AttrType.T_TypeEnum;
import static hidb2.kern.StatusCycle.CREATED;
import hidb2.gui.editor.DlgAttrChecker;
import hidb2.kern.AttrChecker;
import hidb2.kern.Attribut;
import hidb2.kern.AttributedDescription;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.DialogCellEditor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.forms.widgets.TableWrapData;
/**
* Table viewer dedicated to Attributes edition.
*
*/
public class AttributTableViewer extends PoserTableViewer
{
public final static String COLNAME_TYPE = "Type";
public final static String COLNAME_CHECKERDEF = "Checker";
/** Target edited description */
AttributedDescription _descr;
public AttributTableViewer(Composite parent)
{
super(parent, SWT.SINGLE | SWT.FULL_SELECTION);
TableWrapData data_tabv = new TableWrapData(TableWrapData.FILL_GRAB);
data_tabv.grabVertical = true;
data_tabv.heightHint = 30 + 100 + 100 + T_TypeEnum.defaultColumnWidth + 40;
getTable().setLayoutData(data_tabv);
addROAttribut("DisplayOrder", T_Integer, "No", 30);
addAttribut("Name", T_String, "Name", 100);
addAttribut("Comment", T_String, "Comment", 100);
addAttribut(COLNAME_TYPE, T_TypeEnum, "Type", T_TypeEnum.defaultColumnWidth);
addROAttribut("ColName", T_String, "Column", 40);
addAttribut("ColumnWidth", T_Integer, "Column Width", 30);
addAttribut(new PojoColumnDescr(COLNAME_CHECKERDEF, T_Object, "Def", 40)
{
public CellEditor createCellEditor(final Table table)
{
if ((_cellEdt == null) && editable)
{
_cellEdt = new DialogCellEditor(table)
{
@Override
protected Object openDialogBox(Control cellEditorWindow)
{
// Use Checker atttibuts editor
DlgAttrChecker dlg = new DlgAttrChecker(cellEditorWindow.getShell(), "Checking Values", null,
"Give range:", MessageDialog.INFORMATION, new String[]
{
"Ok", "Cancel"
}, 0);
AttrChecker curAck = (AttrChecker) getValue();
int rmsg = dlg.open(curAck);
return (rmsg == 0) ? curAck : null;
}
};
}
return _cellEdt;
}
});
}
public void setDescr(AttributedDescription d)
{
_descr = d;
setInput(_descr.getAttributList());
}
@Override
public boolean isModifiable(Object el, String prop)
{
if (prop.equals(COLNAME_TYPE))
{
return ((Attribut) el).getStatus() == CREATED;
}
else
{
if (prop.equals(COLNAME_CHECKERDEF))
{
return ((Attribut) el).getChecker() != null;
}
else
{
return super.isModifiable(el, prop);
}
}
}
public void addAfterSelection()
{
ISelection selection = getSelection();
int addidx = 0;
if (selection == null)
{
addidx = _descr.getAttributList().size();
}
else
{
Object obj = ((IStructuredSelection) selection).getFirstElement();
addidx = _descr.getAttributList().indexOf(obj) + 1;
}
Attribut at0 = new Attribut(_descr.getID(), T_String);
_descr.addAttribut(at0, addidx);
refresh();
}
public void addBeforeSelection()
{
ISelection selection = getSelection();
int addidx = 0;
if (selection != null)
{
Object obj = ((IStructuredSelection) selection).getFirstElement();
addidx = Math.max(0, _descr.getAttributList().indexOf(obj) - 1);
}
Attribut at0 = new Attribut(_descr.getID(), T_String);
_descr.addAttribut(at0, addidx);
refresh();
}
}