Cut,Copy,Paste"

From Documentation
Line 62: Line 62:
  
 
2. Construct destination Range object<br/>
 
2. Construct destination Range object<br/>
 +
 
After we get previous selection range and current position, we can construct destination Range, and execute copy
 
After we get previous selection range and current position, we can construct destination Range, and execute copy
 
<source lang="java" >
 
<source lang="java" >
Line 77: Line 78:
 
columnIndex + columnCount);
 
columnIndex + columnCount);
 
srcRange.copy(dst);
 
srcRange.copy(dst);
 +
</source>
 +
 +
3. If command is cut, clear highlight area of spreadsheet and clear previous selection's content.
 +
<source lang="java" >
 +
private void clearCutRangeIfNeeded() {
 +
if (!isCut())
 +
return;
 +
 +
Range srcRange = (Range)spreadsheet.getAttribute(KEY_SOURCE_RANGE);
 +
srcRange.clearContents();
 +
/* clear cell style*/
 +
CellStyle defaultCellStyle = spreadsheet.getBook().getCellStyleAt((short)0);
 +
srcRange.setStyle(defaultCellStyle);
 +
}
 +
 +
 +
private void clearHighlightIfNeeded() {
 +
if (isCut())
 +
spreadsheet.setHighlight(null);
 +
}
 
</source>
 
</source>
  

Revision as of 09:09, 29 October 2010


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 command.

Purpose

Use menu or control key to execute cut, copy or paste command.
For control key, use Ctrl + 'C' for copy, Ctrl + 'X' for cut, and Ctrl + 'V' for paste.

Copy function

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

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

2. We can use AbstractComponent.getAttribute(String) 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.
The reason why we need save this is variable is because paste function needs to know whether is cut or paste.
If cut, we need to clear previous selection's content; if copy, we can keep previous selection.

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

private boolean isCut() {
	return Boolean.valueOf( 
			(Boolean)spreadsheet.getAttribute(KEY_IS_CUT));
}

Paste function

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

1. Retrieve user's previous selection. Refer to Copy, Cut
We can retrieve by AbstractComponent.getAttribute(String, Object), 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);

3. If command is cut, clear highlight area of spreadsheet and clear previous selection's content.

private void clearCutRangeIfNeeded() {
	if (!isCut())
		return;
		
	Range srcRange = (Range)spreadsheet.getAttribute(KEY_SOURCE_RANGE);
	srcRange.clearContents();
	/* clear cell style*/
	CellStyle defaultCellStyle = spreadsheet.getBook().getCellStyleAt((short)0);
	srcRange.setStyle(defaultCellStyle);
}


private void clearHighlightIfNeeded() {
	if (isCut())
		spreadsheet.setHighlight(null);
}

Zul example

In zul, we declare a composer for event handling.

<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

In composer, we need to handle events, including onCellRightClick, Menuitem's onClick event and onCtrlKey event

In here, we use GenericForwardComposer, it allow us to write intuitive onXxx$myid event handler methods and it can auto-wired variable.

onCellRightClick

onCellRightClick is spreadsheet's event, this event occurs when user right click on a cell. We use this event to popup menu, allow user to select cut, copy or paste function.

Also, we can retrieve mouse position from CellMouseEvent and set menupopup's popup position.

public void onCellRightClick$spreadsheet(CellMouseEvent event) {
	cellMenu.open(event.getClientx(),  event.getClienty());
}

onClick

We can register menuitem's onClick event to execute corresponding command.

public void onClick$cutMenu() {
	onCut();
}
	
public void onClick$copyMenu(Event evt) {
	onCopy();
}

public void onClick$pasteMenu(Event evt) {
	onPaste();
}

onCtrlKey

We can also register control key event to execute corresponding command.

public void onCtrlKey$spreadsheet(KeyEvent evt) {
	if (evt.isCtrlKey()) {
		switch (evt.getKeyCode()) {
		case 'C':
			onCopy();
			break;
		case 'V':
			onPaste();
			break;
		case 'X':
			onCut();
			break;
		}
			
		}
	}


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.