Java tutorial
/******************************************************************************* * Copyright (c) 2010 - 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation * Lars Vogel <lars.Vogel@gmail.com> - Bug 419770 *******************************************************************************/ package c5_context.parts; import javax.annotation.PostConstruct; import javax.inject.Inject; import org.eclipse.e4.core.di.annotations.Optional; import org.eclipse.e4.ui.di.Focus; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.SWT; 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.Shell; import org.eclipse.swt.widgets.Text; import c5_context.parts.model.Person; public class ShowDataInjectedDirectlyPart { private Label firstNameLabel, lastNameLabel, emailLabel; private Text firstNameInput, lastNameInput, emailInput; @Inject @Optional private Person person; @PostConstruct public void createComposite(Composite parent, Shell shell) { parent.setLayout(new GridLayout(2, false)); /* first name */ firstNameLabel = new Label(parent, SWT.NONE); firstNameLabel.setText("First name:"); GridData gridData = new GridData(); gridData.horizontalAlignment = GridData.BEGINNING; gridData.grabExcessHorizontalSpace = false; firstNameLabel.setLayoutData(gridData); firstNameInput = new Text(parent, SWT.BORDER); firstNameInput.setEditable(false); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; firstNameInput.setLayoutData(gridData); /* last name */ lastNameLabel = new Label(parent, SWT.NONE); lastNameLabel.setText("Last name:"); gridData = new GridData(); gridData.horizontalAlignment = GridData.BEGINNING; gridData.grabExcessHorizontalSpace = false; lastNameLabel.setLayoutData(gridData); lastNameInput = new Text(parent, SWT.BORDER); lastNameInput.setEditable(false); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; lastNameInput.setLayoutData(gridData); /* email name */ emailLabel = new Label(parent, SWT.NONE); emailLabel.setText("Email:"); gridData = new GridData(); gridData.horizontalAlignment = GridData.BEGINNING; gridData.grabExcessHorizontalSpace = false; emailLabel.setLayoutData(gridData); emailInput = new Text(parent, SWT.BORDER); emailInput.setEditable(false); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; emailInput.setLayoutData(gridData); if (person != null) { reloadData(person, shell); } } @Focus public void setFocus() { firstNameInput.setFocus(); } @Inject public void reloadData(@Optional Person p, Shell shell) { if (p == null) { MessageDialog.openWarning(shell, "No person", "No person initialized"); return; } if (firstNameInput == null) { return; } firstNameInput.setText(p.getFirstName()); lastNameInput.setText(p.getLastName()); emailInput.setText(p.getEmail()); } }