JFace TableViewer get multiple selected rows - jface

I want to add a listener to the select button which gets the multiple rows selected in the checklist tableviewer. It then proccedes to check those boxes.
My question is how do I get the list of rows selected in the tableviewer?
Here is the code for the table:
private void createCheckViewer(Composite parent){
tableViewer = CheckboxTableViewer.newCheckList(parent, SWT.MULTI| SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
if ( (GanttFrame.getListOfFunctionTasks()!= null)){
if (!(GanttFrame.getListOfFunctionTasks().isEmpty())){
// Data Rows
for (int i = 0; i < GanttFrame.getListOfFunctionTasks().size(); i++) {
tableViewer.add(GanttFrame.getListOfFunctionTasks().get(i));
GanttFrame.getListOfFunctionTasks().get(i).setCheckId(i);
}
}
}
// flow trace or function trace
String columnHeader;
if (TraceData.getFlowTraceFlag()){
columnHeader = "Flow Traces";
}else{
columnHeader = "Function Traces";
}
// define layout for the viewer
gridData = new GridData(SWT.FILL, SWT.TOP, true, false, 3, 1);
gridData.heightHint = 400;
tableViewer.getControl().setLayoutData(gridData);
tableViewer.addCheckStateListener(this.getCheckListListener());
final Table table = tableViewer.getTable();
TableLayout layout = new TableLayout();
TableViewerColumn col = new TableViewerColumn(tableViewer, SWT.LEAD);
col.getColumn().setText(columnHeader);
layout.addColumnData(new ColumnWeightData(500));
table.setLayout(layout);
table.setHeaderVisible(true);
table.setLinesVisible(true);
}

Use:
IStructuredSelection selection = (IStructuredSelection)tableViewer.getSelection();
You then have various choices for processing the selection:
Object [] selections = selection.toArray();
List<?> selections = selection.toList();
Iterator<?> iterator = selection.iterator();

Related

Is it possible to create dynamic charts in epplus?

I am wondering if it is possible to configure epplus in such a way that when the excel file is opened the table data can be clicked on and a graph will be displayed based on the row of the table clicked. (I realize this is super easy to do in excel I would just rather have everything taken care of before it gets to certain people)
Currently I just have one data table and a graph for each row but it would be better if there was just one graph that changed based on what row is clicked in excel. I also tried a pivot table but that doesn't help me with the dynamic chart.
For anyone trying to figure this out as well. I ended up using a data validation drop down list and making a dynamic table row (outside the base table) that the dynamic chart is based on. The dynamic table row changes based on the value of the data validation dropdown list (you kinda need a feel for excel to be able to do this which I didn't have):
class Program
{
static void Main(string[] args)
{
// Creating an instance
// of ExcelPackage
ExcelPackage excel = new ExcelPackage();
// name of the sheet
var workSheet = excel.Workbook.Worksheets.Add("testSheet");
//init table
var randTable = new DataTable();
randTable.TableName = "randTable";
//init columns
var countColumn = new DataColumn()
{
DataType = typeof(int),
ColumnName = "Count",
ReadOnly = true,
Unique = true
};
var randomColumn0 = new DataColumn()
{
DataType = typeof(int),
ColumnName = "Random-0",
ReadOnly = true,
Unique = false
};
var randomColumn1 = new DataColumn()
{
DataType = typeof(int),
ColumnName = "Random-1",
ReadOnly = true,
Unique = false
};
//add columns to table
randTable.Columns.AddRange(new DataColumn[] { countColumn, randomColumn0, randomColumn1 });
//init data validation
ExcelRange dropdownRange = workSheet.Cells[12, 1, 12, 3];
var dropdownValidation = workSheet.DataValidations.AddListValidation(dropdownRange.Address);
workSheet.Names.Add("count", dropdownRange);
//style data validation
dropdownRange.Merge = true;
workSheet.Cells[dropdownRange.Address].Style.Fill.PatternType = ExcelFillStyle.Solid;
workSheet.Cells[dropdownRange.Address].Style.Fill.BackgroundColor.SetColor(Color.Yellow);
workSheet.Cells[dropdownRange.Address].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
var rand = new Random();
for (var i = 0; i < 10; i++)
{
//add table first column values to validation list
dropdownValidation.Formula.Values.Add(i.ToString());
var row = randTable.NewRow();
row[countColumn] = i;
row[randomColumn0] = rand.Next(0, 100);
row[randomColumn1] = rand.Next(0, 100);
randTable.Rows.Add(row);
}
//make the tableIndexer cell. This cell will be used to get the
//table indices for the selected table row cells
ExcelRange randTableIndexer = workSheet.Cells[12, 4, 12, 4];
randTableIndexer.Formula = "MATCH(INDEX(count,1,1), randTable[Count], 0)";
workSheet.Cells[randTableIndexer.Address].Style.Fill.PatternType = ExcelFillStyle.Solid;
workSheet.Cells[randTableIndexer.Address].Style.Fill.BackgroundColor.SetColor(Color.LightGreen);
workSheet.Cells[randTableIndexer.Address].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
workSheet.Names.Add("tableIndex", randTableIndexer);
//make the cells based off the table at row(randTableIndexer.value)
ExcelRange graphCells = workSheet.Cells[13, 1, 13, 3];
graphCells.CreateArrayFormula("INDEX(randTable[], tableIndex, 0)"); //need [] on table names in epplus formulas
workSheet.Cells[graphCells.Address].Style.Fill.PatternType = ExcelFillStyle.Solid;
workSheet.Cells[graphCells.Address].Style.Fill.BackgroundColor.SetColor(Color.LightBlue);
workSheet.Cells[graphCells.Address].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
graphCells.Calculate();
//add table to workSheet
workSheet.Cells[1, 1].LoadFromDataTable(randTable, true, OfficeOpenXml.Table.TableStyles.Medium15);
workSheet.Cells.AutoFitColumns();
//add dynamic chart
var chart = workSheet.Drawings.AddChart("rands", eChartType.Pie) as ExcelPieChart;
chart.Title.Text = "rands";
chart.Series.Add(graphCells.Address, ExcelRange.GetAddress(1, 1, 3, 1));
chart.Legend.Position = eLegendPosition.Bottom;
chart.SetSize(500, 400);
chart.SetPosition(60, 500);
WriteToFile(excel);
}
public static void WriteToFile(ExcelPackage package)
{
// file name with .xlsx extension
string p_strPath = "C:\\your\\file\\path";
if (File.Exists(p_strPath))
File.Delete(p_strPath);
// Create excel file on physical disk
FileStream objFileStrm = File.Create(p_strPath);
objFileStrm.Close();
// Write content to excel file
File.WriteAllBytes(p_strPath, package.GetAsByteArray());
}
}

AS2 inventory script

I'm trying to modify the below script so that it will automatically drop the first instance of "_item" and snap it to the nearest "slot"
The code works great except I cannot find a way to make it drop the item without the user clicking on it first. This is not my script. You can see it in action here- http://www.freeactionscript.com/2008/11/drag-drop-snap-inventory-system-v2/
Any help greatly appreciated, thanks!
/**
* Drag, Drop and Snap Inventory System
*
* Version: 2.0
* Author: Philip Radvan
* URL: http://www.freeactionscript.com
*/
var slots_array:Array = new Array(slot0, slot1, slot2, slot3, slot4, slot5, slot6, slot7, slot8, slot9, slot10, slot11, slot12, slot13, slot14, slot15);
var items_array:Array;
var uniqueItemId:Number = 0;
//
//start dragging
function dragItem(_item:Object):Void
{
//save position before dragging
_item.nowX = _item._x;
_item.nowY = _item._y;
//drag
_item.startDrag(true);
_item.onMouseMove = updateDrag;
}
//stop dragging
function dropItem(_item:Object):Void
{
//stop dragging
_item.stopDrag();
//delete mouse event
delete _item.onMouseMove;
//run loop on slots array
for (i=0; i<slots_array.length; i++)
{
//set temp slot var
currentSlot = eval(slots_array[i]);
//check slot hittest and slot itemID; if item is over slot and slot is empty, drop
if ((eval(_item._droptarget) == currentSlot) && (currentSlot.itemID == "empty"))
{
//place item in slot
_item._x = currentSlot._x;
_item._y = currentSlot._y;
//update current slot itemID
currentSlot.itemID = this;
//update previous slot itemID
temp = eval(_item.slotID);
temp.itemID = "empty";
//update current item slotID
_item.slotID = currentSlot.myName;
//item moved, end loop
return;
}
else
{
//return item to previous position if: item is NOT over slot or slot is NOT empty
_item._x = _item.nowX;
_item._y = _item.nowY;
}
}
}
/*
* updateAfterEven re-renders the screen
*/
function updateDrag():Void
{
updateAfterEvent();
}
/*
* Create Slots
*/
for (i=0; i<slots_array.length; i++)
{
var _currentSlot = slots_array[i];
_currentSlot.itemID = "empty";
_currentSlot.num = i;
_currentSlot.myName = "slot"+i;
}
/*
* Create Item
*/
function createInventoryItem()
{
//attach item to stage
var _item = attachMovie("item", "item"+uniqueItemId, _root.getNextHighestDepth());
//set item position
_item._x = 280;
_item._y = 320;
//set item settings
_item.myName = "item"+uniqueItemId;
_item.slotID = "empty";
//make item a button
_item.onPress = function()
{
dragItem(this)
}
_item.onRelease = _item.onReleaseOutside = function()
{
dropItem(this);
}
//add item to array
items_array.push(_item);
//update unique Item Id
uniqueItemId++;
}
//button to create a new inventory item
create_item_btn.onRelease = function()
{
createInventoryItem();
}
Fixed it. Rather than modifying this one I wrote my own. Thanks everyone : )
By placing the hit test code on the movie clip "_item" itself, a small array loop sorts it's position on clip event load, and snaps it to the slot. Super easy.

EXTJS Grid row colour change dynamically with getting colour code from database

I am working on an EXTJS grid whose row-color will be set according to a field(status field) value from the table.
User can edit the fields of the row and after clicking update, the color of the row will change according to status field value set for that row.
I need the row background color should be set fetching from a table in db.
Currently I am setting different css class with checking the status field value using following code.
getRowClass: function(record, rowIndex, rp, ds)
{
if( record.get('status') == 'xxxxx' )
{
return 'status-xxxxx';
}
else if( record.get('status') == 'yyyyy' )
{
return 'status-yyyyy';
}
else
{
return 'status-zzzzzz';
}
}
I have the color in the store with the status value for each row.
But I need the color should be fetched from db and set as row background.
Can any one help me to achieve this.
Thanks
If you want to use as row background-color color from row record you will have to set background color of each row td elements after row is rendered.
You can do this in refresh event of gridView. So in grid config you should define something like this:
viewConfig: {
listeners: {
refresh: function(view) {
// get all grid view nodes
var nodes = view.getNodes();
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
// get node record
var record = view.getRecord(node);
// get color from record data
var color = record.get('color');
// get all td elements
var cells = Ext.get(node).query('td');
// set bacground color to all row td elements
for(var j = 0; j < cells.length; j++) {
console.log(cells[j]);
Ext.fly(cells[j]).setStyle('background-color', color);
}
}
}
}
}
Fiddle with live example: https://fiddle.sencha.com/#fiddle/2m8

Dynamically Create multiple combobox Codebehind & bind the value through wcf in silverlight

This is the code i have written to dynamically generate Controls:
private void GenCtrl(string type, int typeid,string sql)
{
string id = Convert.ToString(typeid);
if (type == "TextBox")
{// created for TextBox
}
else if (type == "ComboBox")
{
ComboBoxEdit cb = new ComboBoxEdit();
cb.Name = "cb" + id;
// this is to set unique id if more than 1 combobox created.
cb.Height = 20;
cb.Width = 100;
cb.Margin = new Thickness(2, 2, 0, 0);
cb.HorizontalAlignment = HorizontalAlignment.Left;
cb.Text = "Show All";
stctrl.Children.Add(cb);
ComboBoxEdit cbx = (ComboBoxEdit)cb.FindName(cb.Name);
cb.DisplayMember = "Name";
if(filsql !=null)
ServRef.GetComboBoxlistAsync(sql); //this would retrieve the list and attach to the combobox.
//ctlst.Add(cb.Name, type);
}
}
//WCF Service reference
ServRef.GetComboBoxlistCompleted += new EventHandler<GetComboBoxlistCompletedEventArgs>(ServRef_GetComboBoxlistCompleted);
void ServRef_GetComboBoxlistCompleted(object sender, GetComboBoxlistCompletedEventArgs e)
{
cb.ItemsSource = e.Result;
cb.SelectedIndex = 0;
}
The problem i am facing while binding two combobox is created ex : cb1,cb2( dynamically created)only the control cb2 combox which is the latest get activated at ServRef_GetComboBoxlistCompleted method. i am unable to bind the value for the first control(combobox).
if my question is not clear let me know.
i was able to find using FindName
ComboBoxEdit cbx = (ComboBoxEdit)stackctrl.FindName(cb.Name);

Disable specific rows in datagrid/enhancedgrid

I want to disable one specific row in datagrid in following manner:
1) Highlight one row with a different color
2) Disable checkbox/radio button selection of that row
3) Disable inline editing of cells present in that row but allow inline editing for other rows.
Pls. help if you have any ideas.
You can use a combination of the following functions to extract stuff
// as example, one of youre items uses identifier:'id' and 'id:10'
var identifier = '10';
var item = store._arrayOfTopLevelItems[10]; // you probably have this allready
var index = grid.getItemIndex(item); // find which index it has in grid
var rowNode = grid.getRowNode(index); // find a DOM element at that index
You will have the <div> as rowNode, it contains a table with cells (as many as you got columns). Set its background-color
The checkbox thing, you will prly know which cell-index it has
var cellNode = dojo.query('td[idx='+cellIndex+']', rowNode)[0];
// with cellType Bool, td contains an input
var checkbox = cellNode.firstChild;
Editing is another store really.. works in focus handlers. To override it, you must keep like an array of rows which you dont want editable (allthough the cell.editable == true).
function inarray(arr, testVal) {
return dojo.some(arr, function(val) { return val == testVal }).length > 0
}
grid.setNonEditable = function (rowIndex) {
if(! inarray(this.nonEditable,rowIndex) )
this.nonEditable.push(rowIndex);
}
grid.setEditable = function (rowIndex) {
this.nonEditable = dojo.filter(this.nonEditable, function(val) { return val != rowIndex; });
}
var originalApply = grid.onApplyEdit
grid.onApplyEdit = function(inValue, inRowIndex) {
if(! inarray(this.nonEditable,inRowIndex) )
originalApply.apply(this, arguments);
}
If you are using dojox.grid.DataGrid you can use canEdit function to disable row editing or cell editing :
grid = new dojox.grid.DataGrid({
canEdit: function(inCell, inRowIndex) {
var item = this.getItem(inRowIndex);
var value = this.store.getValue(item, "name");
return value == null; // allow edit if value is null
}
}