Cut,Copy,Paste"

From Documentation
m (Created page with '{{ZKSpreadsheetEssentialsPageHeader}} CONTENT GOES HERE {{ZKSpreadsheetEssentialsPageFooter}}')
 
m
Line 1: Line 1:
 
{{ZKSpreadsheetEssentialsPageHeader}}
 
{{ZKSpreadsheetEssentialsPageHeader}}
  
CONTENT GOES HERE
+
=Cut, Copy and Paste in ZK Spreadsheet=
 +
ZK Spreadsheet use Range to represents a cell, a row, a column, or selection of cells. We can use Range to get information or execute command. In here, we use Range to demonstrate how to implement cut, copy, and paste function by menu and control keys.
 +
 
 +
=Zul example=
 +
<source lang="xml" >
 +
<zk>
 +
<div height="100%" width="100%" apply="demo.CopyPasteComposer">
 +
<spreadsheet id="spreadsheet"
 +
src="/demo_sample.xls"
 +
maxrows="200"
 +
maxcolumns="40"
 +
vflex="1"
 +
width="100%"
 +
ctrlKeys="^c^v^x">
 +
</spreadsheet>
 +
<menupopup id="cellMenu">
 +
<menuitem id="cutMenu" label="Cut" />
 +
<menuitem id="copyMenu" label="Copy" />
 +
<menuitem id="pasteMenu" label="Paste" />
 +
</menupopup>
 +
</div>
 +
</zk>
 +
</source>
 +
 
 +
=Composer=
 +
 
 +
=Copy function=
 +
1. We can construct Range object by Ranges various functions, for example
 +
<source lang="xml" >
 +
Ranges.range(sheet, "A1:D4");
 +
</source>
 +
 
 +
2. We can use <javadoc>org.zkoss.zul.Component.setAttribute</javadoc> to save user's current selection range
 +
<source lang="xml" >
 +
private void setSource() {
 +
Rect selectedRect = spreadsheet.getSelection();
 +
Range range = Ranges.range(spreadsheet.getSelectedSheet(),
 +
selectedRect.getTop(),
 +
selectedRect.getLeft(),
 +
selectedRect.getBottom(),
 +
selectedRect.getRight());
 +
 
 +
        //save user selection range
 +
spreadsheet.setAttribute(KEY_SOURCE_RANGE, range);
 +
spreadsheet.setAttribute(KEY_SOURCE_RECT, selectedRect);
 +
}
 +
</source>
 +
 
 +
=Cut function=
 +
 
 +
We can save a boolean object to distinguish between cut and paset command
 +
<source lang="xml" >
 +
private void onCut() {
 +
      spreadsheet.setAttribute(KEY_IS_CUT, Boolean.valueOf(true));
 +
...
 +
}
 +
</source>
 +
 
 +
==Paste function==
 +
We can use Range.copy(Range destination) to copy user's selection
 +
 
 +
1. Retrieve user's previous selection, implement by Copy, Cut<br/>
 +
We can retrieve by <javadoc>org.zkoss.zul.Component.getAttribute</javadoc>, for example
 +
<source lang="xml" >
 +
private void onPaste() {
 +
Range srcRange = (Range)spreadsheet.getAttribute(KEY_SOURCE_RANGE);
 +
Rect srcRect = (Rect)spreadsheet.getAttribute(KEY_SOURCE_RECT);
 +
....
 +
</source>
 +
 
 +
2. Construct destination Range object<br/>
 +
After we get previous selection range and current position, we can construct destination Range, and execute copy
 +
<source lang="xml" >
 +
int columnCount = srcRect.getRight() - srcRect.getLeft();
 +
int rowCount = srcRect.getBottom() - srcRect.getTop();
 +
 
 +
Rect dstRect = spreadsheet.getSelection();
 +
int rowIndex = dstRect.getTop();
 +
int columnIndex = dstRect.getLeft();
 +
 
 +
Range dst = Ranges.range(spreadsheet.getSelectedSheet(),
 +
rowIndex,
 +
columnIndex,
 +
rowIndex + rowCount,
 +
columnIndex + columnCount);
 +
srcRange.copy(dst);
 +
</source>
 +
 
 +
 
  
 
{{ZKSpreadsheetEssentialsPageFooter}}
 
{{ZKSpreadsheetEssentialsPageFooter}}

Revision as of 07:26, 29 October 2010


Cut, Copy and Paste in ZK Spreadsheet

ZK Spreadsheet use Range to represents a cell, a row, a column, or selection of cells. We can use Range to get information or execute command. In here, we use Range to demonstrate how to implement cut, copy, and paste function by menu and control keys.

Zul example

<zk>
	<div height="100%" width="100%" apply="demo.CopyPasteComposer">
		<spreadsheet id="spreadsheet" 
			src="/demo_sample.xls"
			maxrows="200" 
			maxcolumns="40" 
			vflex="1" 
			width="100%"
			ctrlKeys="^c^v^x">
		</spreadsheet>
		<menupopup id="cellMenu">
			<menuitem id="cutMenu" label="Cut" />
			<menuitem id="copyMenu" label="Copy" />
			<menuitem id="pasteMenu" label="Paste" />
		</menupopup>
	</div>
</zk>

Composer

Copy function

1. We can construct Range object by Ranges various functions, for example

Ranges.range(sheet, "A1:D4");

2. We can use Component.setAttribute to save user's current selection range

private void setSource() {
	Rect selectedRect = spreadsheet.getSelection();
	Range range = Ranges.range(spreadsheet.getSelectedSheet(), 
				selectedRect.getTop(), 
				selectedRect.getLeft(), 
				selectedRect.getBottom(),
				selectedRect.getRight());

        //save user selection range
	spreadsheet.setAttribute(KEY_SOURCE_RANGE, range);
	spreadsheet.setAttribute(KEY_SOURCE_RECT, selectedRect);
	}

Cut function

We can save a boolean object to distinguish between cut and paset command

private void onCut() {
       spreadsheet.setAttribute(KEY_IS_CUT, Boolean.valueOf(true));
...
}

Paste function

We can use Range.copy(Range destination) to copy user's selection

1. Retrieve user's previous selection, implement by Copy, Cut
We can retrieve by Component.getAttribute, for example

private void onPaste() {
		Range srcRange = (Range)spreadsheet.getAttribute(KEY_SOURCE_RANGE);
		Rect srcRect = (Rect)spreadsheet.getAttribute(KEY_SOURCE_RECT);
....

2. Construct destination Range object
After we get previous selection range and current position, we can construct destination Range, and execute copy

int columnCount = srcRect.getRight() - srcRect.getLeft();
int rowCount = srcRect.getBottom() - srcRect.getTop();

Rect dstRect = spreadsheet.getSelection();
int rowIndex = dstRect.getTop();
int columnIndex = dstRect.getLeft();

Range dst = Ranges.range(spreadsheet.getSelectedSheet(),
					rowIndex, 
					columnIndex, 
					rowIndex + rowCount, 
					columnIndex + columnCount);
srcRange.copy(dst);



All source code listed in this book is at Github.


Last Update : 2010/10/29

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.