-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi,
I'm trying to set an event on a spreadsheet (ZK spreadsheet), that when I press Ctrl+C to copy the content of the cell and, when I press Ctrl+V to paste it in other cell. I'm doing this in Java.
if (event instanceof KeyEvent)
{
int code = ((KeyEvent)event).getKeyCode();
Position pos;
if(67 == code)
{//mark copy
pos = spreadSheet.getCellFocus();
copyrow = pos.getRow();
copycol = pos.getColumn();
}
if(86 == code)
{//do paste
pos = spreadSheet.getCellFocus();
int row = pos.getRow();
int col = pos.getColumn();
Cell cell = sheet.getCell(copyrow,copycol);
if(cell!=null)
{
sheet.copyCell(cell,row,col);
}
}
}
And on the spreadsheet I put this : spreadSheet.addEventListener(Events.ON_CTRL_KEY, this);
In ZK script I saw how to do it.. but in Java... I don't know how to put the right listener. If anyone can help me...
Thanks!
Best regards,
Cristina
This is the example in ZK:
<window>
<zscript>
int copyrow;
int copycol;
boolean copy = false;
</zscript>
<hbox>Key Event:<label id="lab2"/></hbox>
<spreadsheet id="ss1" src="/WEB-INF/xls/demo/travel.xls" maxrows="11" maxcolumns="20" height="246px" width="98%" >
<attribute name="ctrlKeys">@c^c^v</attribute>
<attribute name="onCtrlKey">
lab2.value = "Key Code:"+event.keyCode+" , "+"Ctrl:"+event.ctrlKey+" , "+"Alt:"+event.altKey;
if(event.keyCode==67){//mark copy
copy=true;
var pos = ss1.cellFocus;
copyrow = pos.row;
copycol = pos.column;
}
if(event.keyCode==86){//do paste
var pos = ss1.cellFocus;
int row = pos.row;
int col = pos.column;
var sheet = ss1.getSelectedSheet();
var cell = sheet.getCell(copyrow,copycol);
if(cell!=null){
sheet.copyCell(cell,row,col);
}
}
</attribute>
</spreadsheet>
</window>
And I am trying to do the same in java. The part with behavior it's ok, but I don't know How to put the event on spreadsheet
I use your example to "test" my composerknowledge.
You have 2 choices to implement javacode to you zul-file:
1.) extends a GUI-Component with the use-attribute (use="package.class")
2.) use a composer, i.e. install with apply="package.class" in the rootwindow
Each variant has advantages. When you override the GUI-Component you can create your own variant of the spreadsheet component (including copy/paste).
You can use it with: <spreadsheet use="package.yourclass..> or override do a fix override in the lang-addon.xml or create a macrocomponent...
On the other side: a central eventhandler like the composer, make the developers life easier (and reduces the references between the components)
/Robert
@ZK Team:
I'm not sure about the
public void onCtrlKey$ss1(ForwardEvent genericEvent) { KeyEvent event = (KeyEvent) genericEvent.getOrigin();
Here are the examples:
1.) extends the GUI-Component:
<spreadsheet id="ss1" src="/WEB-INF/xls/OD-IP-Adressen.xls" maxcolumns="20" height="600px" width="98%" use="Spread1" ctrlKeys="@c^c^v"/>
public class Spread1 extends Spreadsheet implements AfterCompose { int copyrow; int copycol; boolean copy = false; Label lab2; // autowired public void afterCompose() { Components.wireVariables(this, this); // wire components } public void onCtrlKey(KeyEvent event) { lab2.setValue("Key Code:"+ event.getKeyCode() + " , "+"Ctrl:"+ event.isCtrlKey() +" , "+"Alt:"+ event.isAltKey()); if(event.getKeyCode()==67){//mark copy copy=true; Position pos = this.getCellFocus(); copyrow = pos.getRow(); copycol = pos.getColumn(); } if(event.getKeyCode()==86){//do paste Position pos = this.getCellFocus(); int row = pos.getRow(); int col = pos.getColumn(); Sheet sheet = this.getSelectedSheet(); Cell cell = sheet.getCell(copyrow,copycol); if(cell!=null){ sheet.copyCell(cell,row,col); } } } }
<window apply="Spread2"> <zscript> int copyrow; int copycol; boolean copy = false; </zscript> <hbox>Key Event:<label id="lab2"/></hbox> <spreadsheet id="ss1" src="/WEB-INF/xls/OD-IP-Adressen.xls" maxcolumns="20" height="600px" width="98%" ctrlKeys="@c^c^v"/> </window>
public class Spread2 extends GenericForwardComposer { int copyrow; int copycol; boolean copy = false; Label lab2; // autowired Spreadsheet ss1; // autowired public void onCtrlKey$ss1(ForwardEvent genericEvent) { KeyEvent event = (KeyEvent) genericEvent.getOrigin(); lab2.setValue("Key Code:"+ event.getKeyCode() + " , "+"Ctrl:"+ event.isCtrlKey() +" , "+"Alt:"+ event.isAltKey()); if(event.getKeyCode()==67){//mark copy copy=true; Position pos = ss1.getCellFocus(); copyrow = pos.getRow(); copycol = pos.getColumn(); } if(event.getKeyCode()==86){//do paste Position pos = ss1.getCellFocus(); int row = pos.getRow(); int col = pos.getColumn(); Sheet sheet = ss1.getSelectedSheet(); Cell cell = sheet.getCell(copyrow,copycol); if(cell!=null){ sheet.copyCell(cell,row,col); } } } }
Robert,
I know what you mean. I will see if we can "unwrap" the ForwardEvent automatically.
I think I will only "unwarp" the ForwardEvent if and only if the developer say so by giving specific Event class in their event listener.
That is, only in such case:
onCtrlKey$ss1(KeyEvent event)
But not in such cases:
onCtrlKey$ss1(Event event)
or
onCtrlKey$ss1(ForwardEvent event)
What do you think?
Follow this feature tracker.
https://sourceforge.net/tracker/index.php?func=detail&aid=2092356&group_id=152762&atid=785194
Asked: 2008-09-03 13:35:34 +0800
Seen: 524 times
Last updated: Jan 28 '13