Toolbar

From Documentation



Stop.png This article is out of date, please refer to http://books.zkoss.org/wiki/ZK_Spreadsheet_Essentials for more up to date information.


Purpose

ZK Spreadsheet supports various toolbar buttons to perform actions.

Show toolbar

Use Spreadsheet.setShowToolbar to show toolbar.

Action Handler

ZK Spreadsheet uses a default ActionHandler to perform toolbar button's actions. Developers are able to set customized ActionHandler by API Spreadsheet.setActionHandler or by library property org.zkoss.zss.ui.ActionHandler.class, please refer to sample zk.xml

Each spreadsheet instance uses it own ActionHandler which cannot be shared across different spreadsheet instances.


Each toolbar's button represents an action, the ActionHandler processes the action when clicked by a user.
For example, the 'paste' toolbarbutton and menuitem are represented by Action.PASTE, developers are able to override default behavior by using ActionHandler.doPaste
ZKSsEss Spreadsheet Toolbar paste.png

New Book

ZKSsEss Spreadsheet Toolbar newBook.png
The default ActionHandler does nothing when users clicks the button, developers are able to override ActionHandler.doNewBook

	@Override
	public void doNewBook() {
		getSpreadsheet().setSrc("/WEB-INF/excel/config/blank.xlsx");
	}

Refer to sample code CustomizedActionHandler.java

Save Book

ZKSsEss Spreadsheet Toolbar saveBook.png
The default ActionHandler does nothing when users clicks the button, developers are able to override ActionHandler.doSaveBook

	@Override
	public void doSaveBook() {
		Spreadsheet spreadsheet = getSpreadsheet();
		if (spreadsheet.getBook() != null) {
			final String filePath = Executions.getCurrent().getDesktop().getWebApp().getRealPath(spreadsheet.getSrc());
			
			Exporter exporter = Exporters.getExporter("excel");
			FileOutputStream outputStream = null;
			try {
				outputStream = new FileOutputStream(new File(filePath));
				exporter.export(spreadsheet.getBook(), outputStream);
				try {
					Messagebox.show("Saved");
				} catch (InterruptedException e) {
				}
			} catch (FileNotFoundException e) {
				try {
					Messagebox.show("Save excel failed");
				} catch (InterruptedException e1) {
				}
			} finally {
				if (outputStream != null) {
					try {
						outputStream.close();
					} catch (IOException e) {
					}
				}
			}
		}			
	}

Refer to Export to Excel sample code CustomizedActionHandler.java

Export PDF

ZKSsEss Spreadsheet Toolbar exportPDF.png
The default ActionHandler does nothing when users clicks the button, developers are able to override ActionHandler.doExportPDF

	@Override
	public void doExportPDF(Rect selection) {
		Spreadsheet spreadsheet = getSpreadsheet();
		if (spreadsheet.getBook() != null && isValidSelection(selection)) {
			ExportPDFDialog dialog = new ExportPDFDialog(selection);
			//omit
		}
	}

Refer to Export to PDF and sample code exportPDFDialog.zulCustomizedActionHandler.java

Paste Special

ZKSsEss Spreadsheet Toolbar pasteSpecial.png
By default, ActionHandler does nothing when users clicks the button, developers are able to override ActionHandler.doPasteSpecial

	@Override
	public void doPasteSpecial(Rect selection) {
		Spreadsheet spreadsheet = getSpreadsheet();
		if (spreadsheet.getBook() != null && isValidSelection(selection)) {
			PasteSpecialDialog dialog = new PasteSpecialDialog(selection);
			//omit
		}
	}

Refer to sample code pasteSpecialDialog.zul and CustomizedActionHandler.java

Custom Sort

ZKSsEss Spreadsheet Toolbar customSort.png
By default, ActionHandler does nothing when users clicks the button, developers are able to override ActionHandler.doCustomSort

	@Override
	public void doCustomSort(Rect selection) {
		Spreadsheet spreadsheet = getSpreadsheet();
		if (spreadsheet.getBook() != null && isValidSelection(selection)) {
			CustomSortDialog dialog = new CustomSortDialog(selection);
			//omit	
		}
	}

Refer to sample code customSortDialog.zul and CustomizedActionHandler.java

Hyperlink

ZKSsEss Spreadsheet Toolbar hyperlink.png
By default, ActionHandler does nothing when users clicks the button, developers are able to override ActionHandler.doHyperlink

	@Override
	public void doHyperlink(Rect selection) {
		Spreadsheet spreadsheet = getSpreadsheet();
		if (spreadsheet.getBook() != null && isValidSelection(selection)) {
			InsertHyperlinkDialog dialog = new InsertHyperlinkDialog(selection);
			//omit	
		}
	}

Close Book

ZKSsEss Spreadsheet Toolbar closeBookButton.png
The default ActionHandler.doCloseBook will invoke ActionHandler.toggleActionOnBookClosed, developers can override it to disable or enable action by Spreadsheet.setActionDisabled.

ZKSsEss Spreadsheet Toolbar bookClosed.png

All those actions(toolbar buttons) are disabled when the book is closed and can be enabled by ActionHandler.doSheetSelect. It will invoke ActionHandler.toggleActionOnSheetSelected and developers can override the method to enable or disable those actions.

I18N

Each button/menuitem map to a key, developers can browse all I18 keys by Action.getLabelKeys

Here are partial keys for the toolbar

Action I18 Key
Paste zss.paste
Paste Formula zss.pasteFormula
Paste Value zss.pasteValue
Paste All Except Border zss.pasteAllExceptBorder
Paste Transpose zss.pasteTranspose
Paste Special zss.pasteSpecial
Merge And Center zss.mergeAndCenter
Merge Across zss.mergeAcross
Merge Cell zss.mergeCell
Unmerge Cell zss.unmergeCell
Insert Cell zss.insertCell
Shift Cell Right zss.shiftCellRight
Shift Cell Down zss.shiftCellDown
Insert Sheet Row zss.insertSheetRow
Insert Sheet Column zss.insertSheetColumn
Delete Cell zss.deleteCell
Shift Cell Left zss.shiftCellLeft
Shift Cell Up zss.shiftCellUp
Clear zss.clear
Clear Context zss.clearContent
Clear Style zss.clearStyle
Clear All zss.clearAll
sort zss.sort
Sort And Filter zss.sortAndFilter
Sort Ascending zss.sortAscending
Sort Descending zss.sortDescending
Custom Sort zss.customSort
Filter zss.filter
Clear Filter zss.clearFilter
Reapply Filter zss.reapplyFilter
Column Chart zss.columnChart
Column Chart 3D zss.columnChart3D
Line Chart zss.lineChart
Line Chart 3D zss.lineChart3D
Pie Chart zss.pieChart
Bar Chart zss.barChart
Bar Chart 3D zss.barChart3D
Scatter Chart zss.scatterChart
Doughnut Chart zss.doughnutChart
Hyperlink zss.hyperlink

Refer to sample i3-label.properties

ZUML

Here is a sample example ZUL file

<zk>
	<window vflex="1" width="100%" apply="org.zkoss.zssessentials.config.ToolbarComposer">
		<button id="toggleToolbar" label="Toggle toolbar"/>
		<spreadsheet showToolbar="true"
			id="spreadsheet" vflex="true" width="100%"
			src="/WEB-INF/excel/config/ZSS-demo_sample.xlsx" maxcolumns="40" maxrows="200"
			></spreadsheet>
	</window>
</zk>

Composer

The composer uses Spreadsheet.setShowToolbar to show or hide toolbar.

	public void onClick$toggleToolbar() {
		boolean isShowToolbar = spreadsheet.isShowToolbar();
		spreadsheet.setShowToolbar(!isShowToolbar);
	}

See the full source code for Composer here

Version History

Last Update : 2022/01/19


Version Date Content
2.3.0 April, 2012 Toolbar
     


All source code listed in this book is at Github.

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