package quote; import it.condarelli.e4.resourcemanager.IResourceManager; import it.condarelli.osgi.resourcepool.IQuoteService; import java.util.Random; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.inject.Inject; import org.eclipse.e4.core.contexts.ContextInjectionFactory; import org.eclipse.e4.core.contexts.IEclipseContext; import org.eclipse.e4.ui.di.Focus; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Text; public class Quote { @Inject private IQuoteService quoteService; @Inject private IResourceManager resourceManager; private IconProvider iconProvider; private Text txtQuote; private Label lblNewLabel; private Button btnGetViaIp; @Inject public Quote(IEclipseContext context) { iconProvider = ContextInjectionFactory.make(IconProvider.class, context); } /** * Create contents of the view part. */ @PostConstruct public void createControls(Composite parent) { parent.setLayout(new GridLayout(2, false)); Button btnNext = new Button(parent, SWT.NONE); btnNext.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1)); btnNext.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { if (quoteService != null) { String s = quoteService.getQuote(); txtQuote.setText(s); } } }); btnNext.setText("Next"); txtQuote = new Text(parent, SWT.BORDER); txtQuote.setText("Quote"); txtQuote.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); Button btnNext_1 = new Button(parent, SWT.NONE); btnNext_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1)); btnNext_1.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { String name = getImageName(); Image image = iconProvider.getIcon(128, name); lblNewLabel.setImage(image); lblNewLabel.getParent().pack(true); } }); btnNext_1.setText("Get via IP"); lblNewLabel = new Label(parent, SWT.NONE); lblNewLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 2)); lblNewLabel.setText("Icon"); btnGetViaIp = new Button(parent, SWT.NONE); btnGetViaIp.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { String name = getImageName(); Image image = resourceManager.getImage(name, 128); lblNewLabel.setImage(image); lblNewLabel.getParent().pack(true); } }); btnGetViaIp.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1)); btnGetViaIp.setText("Get via RM"); } private static final String[] names = new String[] { "Actor", "ActorFolder", "Actors", "AlienWorld", "ArrowBook", "Assemble", "AxeBook", "Back", "BlueOff", "BlueRefresh", "Books", "BrushBook", "CleanDocuments", "CleanNewPaper", "ClosePage", "Compass", "CompassBook", "CompassFolder", "Cut", "Decapitated Thugs", "Delete", "DesktopPublishing", "Down Left", "Down Right Outrageous", "Eclipse", "EditPages", "Export", "Fiends ain't comin' fast enough.", "Forward", "GreenBook", "I shine you up a nice big pizza.", "Import", "Indeed, I do rock.", "Left Down", "Left Up", "MergeBook", "Minus", "Network", "NewBook", "NewChapter", "NewPage", "Next", "Open", "PaperTrail", "Pen", "PenBook", "Plus", "Preferences", "Previous", "Printer", "RedBook", "Review", "Right Up", "Save", "Shutdown", "Stop", "TextDocument", "ThingDrawer", "ThingFolder", "Things", "ThisBook", "Thuggergotchi", "Tick", "Trash Full", "Tree", "TreeBook", "Typeset", "Up Left", "Uploads", "View", "Warning", "World", "X", "YellowOff", "ZoomIn", "ZoomOut" }; private String getImageName() { Random random = new Random(); int nextInt = random.nextInt(names.length); return names[nextInt]; } @PreDestroy public void dispose() { } @Focus public void setFocus() { txtQuote.setFocus(); } }