FileDocCategorySizeDatePackage
MultiPageEditor.javaAPI DocExample5779Wed Mar 24 10:15:36 GMT 2004org.cookbook.ch13.EditorPlugIn.editors

MultiPageEditor

public class MultiPageEditor extends org.eclipse.ui.part.MultiPageEditorPart
An example showing how to create a multi-page editor. This example has 3 pages:
  • page 0 contains a nested text editor.
  • page 1 allows you to change the font used in page 2
  • page 2 shows the words in page 0 in sorted order

Fields Summary
private org.eclipse.ui.editors.text.TextEditor
editor
The text editor used in page 0.
private org.eclipse.swt.graphics.Font
font
The font chosen in page 1.
private org.eclipse.swt.custom.StyledText
text
The text widget used in page 2.
Constructors Summary
public MultiPageEditor()
Creates a multi-page editor example.

		super();
	
Methods Summary
voidcreatePage0()
Creates page 0 of the multi-page editor, which contains a text editor.

		try {
			editor = new TextEditor();
			int index = addPage(editor, getEditorInput());
			setPageText(index, editor.getTitle());
		} catch (PartInitException e) {
			ErrorDialog.openError(
				getSite().getShell(),
				"Error creating nested text editor",
				null,
				e.getStatus());
		}
	
voidcreatePage1()
Creates page 1 of the multi-page editor, which allows you to change the font used in page 2.


		Composite composite = new Composite(getContainer(), SWT.NONE);
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		layout.numColumns = 2;

		Button fontButton = new Button(composite, SWT.NONE);
		GridData gd = new GridData(GridData.BEGINNING);
		gd.horizontalSpan = 2;
		fontButton.setLayoutData(gd);
		fontButton.setText("Change Font...");
		
		fontButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				setFont();
			}
		});

		int index = addPage(composite);
		setPageText(index, "Properties");
	
voidcreatePage2()
Creates page 2 of the multi-page editor, which shows the sorted text.

		Composite composite = new Composite(getContainer(), SWT.NONE);
		FillLayout layout = new FillLayout();
		composite.setLayout(layout);
		text = new StyledText(composite, SWT.H_SCROLL | SWT.V_SCROLL);
		text.setEditable(false);

		int index = addPage(composite);
		setPageText(index, "Preview");
	
protected voidcreatePages()
Creates the pages of the multi-page editor.

		createPage0();
//		createPage1();
//		createPage2();
	
public voiddoSave(org.eclipse.core.runtime.IProgressMonitor monitor)
Saves the multi-page editor's document.

		getEditor(0).doSave(monitor);
	
public voiddoSaveAs()
Saves the multi-page editor's document as another file. Also updates the text for page 0's tab, and updates this multi-page editor's input to correspond to the nested editor's.

		IEditorPart editor = getEditor(0);
		editor.doSaveAs();
		setPageText(0, editor.getTitle());
		setInput(editor.getEditorInput());
	
public voidgotoMarker(org.eclipse.core.resources.IMarker marker)

		setActivePage(0);
		getEditor(0).gotoMarker(marker);
	
public voidinit(IEditorSite site, IEditorInput editorInput)
The MultiPageEditorExample implementation of this method checks that the input is an instance of IFileEditorInput.

		if (!(editorInput instanceof IFileEditorInput))
			throw new PartInitException("Invalid Input: Must be IFileEditorInput");
		super.init(site, editorInput);
	
public booleanisSaveAsAllowed()

		return true;
	
protected voidpageChange(int newPageIndex)
Calculates the contents of page 2 when the it is activated.

		super.pageChange(newPageIndex);
		if (newPageIndex == 2) {
			sortWords();
		}
	
voidsetFont()
Sets the font related data to be applied to the text in page 2.

		FontDialog fontDialog = new FontDialog(getSite().getShell());
		fontDialog.setFontList(text.getFont().getFontData());
		FontData fontData = fontDialog.open();
		if (fontData != null) {
			if (font != null)
				font.dispose();
			font = new Font(text.getDisplay(), fontData);
			text.setFont(font);
		}
	
voidsortWords()
Sorts the words in page 0, and shows them in page 2.


		String editorText =
			editor.getDocumentProvider().getDocument(editor.getEditorInput()).get();

		StringTokenizer tokenizer =
			new StringTokenizer(editorText, " \t\n\r\f!@#\u0024%^&*()-_=+`~[]{};:'\",.<>/?|\\");
		ArrayList editorWords = new ArrayList();
		while (tokenizer.hasMoreTokens()) {
			editorWords.add(tokenizer.nextToken());
		}

		Collections.sort(editorWords, Collator.getInstance());
		StringWriter displayText = new StringWriter();
		for (int i = 0; i < editorWords.size(); i++) {
			displayText.write(((String) editorWords.get(i)));
			displayText.write(System.getProperty("line.separator"));
		}
		text.setText(displayText.toString());