⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 editor.java

📁 对于可视的桌面开发系统的一些详细的设计资料
💻 JAVA
字号:
package gr.osmosis.rcpsamples.pojoeditor;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchPartConstants;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Hyperlink;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;
import org.eclipse.ui.part.EditorPart;

public class Editor extends EditorPart {

	public static String ID = "gr.osmosis.rcpsamples.pojoeditor.editor";

	private Contact contact;

	private FormToolkit toolkit;

	private Form form;

	private Text fnameText;

	private Text lnameText;

	private Text phoneText;

	private Text addressText;

	private Text cityText;

	private Text zipText;

	private TextKeyListener textKeyListener = new TextKeyListener();

	private boolean isDirty = false;

	public Editor() {
		super();
	}

	public void doSave(IProgressMonitor monitor) {

		MessageDialog.openInformation(this.getSite().getShell(), "Save",
				"Contact saved.");
		setDirty(false);
	}

	public void doSaveAs() {

	}

	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {

		if (!(input instanceof IEditorInput))
			throw new PartInitException(
					"Invalid Input: Must be IFileEditorInput");

		if (input.getAdapter(Contact.class) != null) {
			contact = (Contact) input.getAdapter(Contact.class);
		} else {
			contact = null;
		}

		setSite(site);
		setInput(input);
	}

	public boolean isDirty() {
		return this.isDirty;
	}

	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	public void createPartControl(Composite parent) {

		toolkit = new FormToolkit(parent.getDisplay());

		form = toolkit.createForm(parent);
		form.setText("Contact form");

		/*
		 * 4 columns TableWrapLayout
		 */
		TableWrapLayout layout = new TableWrapLayout();
		layout.numColumns = 4;

		form.getBody().setLayout(layout);

		TableWrapData td;

		/*
		 * First Name label colspan = 2
		 */
		td = new TableWrapData();
		td.colspan = 2;
		Label label = toolkit.createLabel(form.getBody(), "First name:");
		label.setLayoutData(td);

		/*
		 * Last Name label colspan = 2
		 */
		td = new TableWrapData();
		td.colspan = 2;
		label = toolkit.createLabel(form.getBody(), "Last name:");
		label.setLayoutData(td);

		/*
		 * First Name text
		 * 
		 * style: FILL_GRAB colspan = 2
		 */
		td = new TableWrapData(TableWrapData.FILL_GRAB);
		td.colspan = 2;
		fnameText = toolkit.createText(form.getBody(), "Enter fname..",
				SWT.BORDER);
		fnameText.setLayoutData(td);
		fnameText.addKeyListener(textKeyListener);

		/*
		 * Last Name text
		 * 
		 * style: FILL_GRAB colspan = 2
		 */
		td = new TableWrapData(TableWrapData.FILL_GRAB);
		td.colspan = 2;
		lnameText = toolkit.createText(form.getBody(), "Enter lname..",
				SWT.BORDER);
		lnameText.setLayoutData(td);
		lnameText.addKeyListener(textKeyListener);

		/*
		 * Phones Section Expandable / Collapsible section
		 * 
		 * style : TITLE_BAR layout style: FILL colspan = 2
		 * 
		 */
		Section section = toolkit.createSection(form.getBody(),
				Section.DESCRIPTION | Section.TITLE_BAR | Section.TWISTIE
						| Section.EXPANDED);

		td = new TableWrapData(TableWrapData.FILL);
		td.colspan = 2;
		section.setLayoutData(td);

		section.setText("Phones");
		section.setDescription("Phone numbers.");

		Composite sectionClient = toolkit.createComposite(section);
		sectionClient.setLayout(new GridLayout(2, false));

		/*
		 * Phone label
		 */
		GridData gd = new GridData(GridData.GRAB_HORIZONTAL
				| GridData.FILL_HORIZONTAL);
		label = toolkit.createLabel(sectionClient, "Phone number:");
		/*
		 * Phone text
		 */
		phoneText = toolkit.createText(sectionClient, "Enter phone..",
				SWT.BORDER);
		phoneText.setLayoutData(gd);
		phoneText.addKeyListener(textKeyListener);

		section.setClient(sectionClient);

		/*
		 * Address Section Expandable / Collapsible section
		 * 
		 * style : TITLE_BAR layout style: FILL colspan = 2
		 * 
		 */
		section = toolkit.createSection(form.getBody(), Section.DESCRIPTION
				| Section.TWISTIE | Section.TITLE_BAR | Section.EXPANDED);
		td = new TableWrapData(TableWrapData.FILL);
		td.colspan = 2;
		section.setLayoutData(td);

		section.setText("Address");
		section.setDescription("Address information.");

		sectionClient = toolkit.createComposite(section);
		sectionClient.setLayout(new GridLayout(2, false));

		/*
		 * Address label
		 */
		gd = new GridData(GridData.FILL_HORIZONTAL);
		label = toolkit.createLabel(sectionClient, "Address:");
		/*
		 * Address text
		 */
		addressText = toolkit.createText(sectionClient, "Enter address..",
				SWT.BORDER);
		addressText.setLayoutData(gd);
		addressText.addKeyListener(textKeyListener);

		/*
		 * City label
		 */
		gd = new GridData(GridData.FILL_HORIZONTAL);
		label = toolkit.createLabel(sectionClient, "City:");
		/*
		 * City text
		 */
		cityText = toolkit.createText(sectionClient, "Enter city..", SWT.BORDER
				| SWT.FILL);
		cityText.setLayoutData(gd);
		cityText.addKeyListener(textKeyListener);

		/*
		 * ZIP label
		 */
		gd = new GridData(GridData.FILL_HORIZONTAL);
		label = toolkit.createLabel(sectionClient, "Zip Code:");
		/*
		 * ZIP text
		 */
		zipText = toolkit.createText(sectionClient, "Enter zip..", SWT.BORDER);
		zipText.setLayoutData(gd);
		zipText.addKeyListener(textKeyListener);

		section.setClient(sectionClient);

		/*
		 * Actions Section NO Expandable / Collapsible section
		 * 
		 * layout style: FILL colspan = 4
		 * 
		 */
		section = toolkit.createSection(form.getBody(), Section.COMPACT);
		td = new TableWrapData(TableWrapData.FILL);
		td.colspan = 4;
		section.setLayoutData(td);

		section.setText("Actions");
		toolkit.createCompositeSeparator(section);
		section.setDescription("Save changes.");

		sectionClient = toolkit.createComposite(section);
		sectionClient.setLayout(new GridLayout(2, false));

		/*
		 * Save changes link
		 */
		Hyperlink link = toolkit.createHyperlink(sectionClient, "Click here.",
				SWT.WRAP);
		link.addHyperlinkListener(new HyperlinkAdapter() {
			public void linkActivated(HyperlinkEvent e) {
				System.out.println("Link activated!");
				doSave(null);
			}
		});

		link.setText("Save changes...");

		section.setClient(sectionClient);

		/*
		 * we have create the form, so its time to populate it with data from
		 * our POJO (Contact).
		 */
		populateForm();

	}

	public void setFocus() {

	}

	protected void setPartName(String partName) {
		super.setPartName(partName);
	}

	private void populateForm() {

		this.fnameText.setText(contact.getFname());
		this.lnameText.setText(contact.getLname());
		this.phoneText.setText(contact.getPhone());
		this.addressText.setText(contact.getAddress());
		this.cityText.setText(contact.getCity());
		this.zipText.setText(contact.getZip());

		/*
		 * Change editor's TAB Caption to show fname lname.
		 */
		setPartName(contact.toString());
	}

	private void setDirty(boolean dirty) {
		this.isDirty = dirty;

		/*
		 * force platform to re-check if editor is dirty or not to remove (*)
		 * from editors TAB
		 */
		firePropertyChange(IWorkbenchPartConstants.PROP_DIRTY);
	}

	private class TextKeyListener implements KeyListener {
		public void keyPressed(KeyEvent e) {

		}

		public void keyReleased(KeyEvent e) {
			System.out.println(e.widget.toString() + " character: "
					+ e.character);

			/*
			 * a key has pressed (and released) in one of Texts in our form. we
			 * have to change editor's state to dirty and force platform to
			 * re-check if editor is dirty or not to add an (*) in editors TAB
			 */
			setDirty(true);

		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -