package ie.dcu.apps.ist.dialogs; import java.util.ArrayList; import ie.dcu.swt.SwtUtils; import ie.dcu.apps.ist.labelling.*; import ie.dcu.swt.layout.LayoutFactory; import org.apache.commons.lang3.StringEscapeUtils; import org.eclipse.swt.SWT; import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.events.KeyListener; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Dialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class PopUpLabelDialog extends Dialog { private Labels labels = new Labels(); public class Result { public final String curator; public final String comboText; Result() { curator = curatorText.getText().trim(); comboText = labelCombo.getText(); } }; // Result private Result result; // Top level components private Shell shell; private Composite content; private Composite widgets; private Composite buttons; // Widgets private Combo labelCombo; private Text curatorText; // Dialog buttons private Button cancelButton; private Button assignButton; // Default title private static final String TITLE = "Label the segmented Pieces"; public PopUpLabelDialog(Shell shell) { super(shell); setText(TITLE); } public PopUpLabelDialog(Shell shell, int style) { super(shell, style); setText(TITLE); } public Result open() { Shell parent = getParent(); shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.SHEET); shell.setText(getText()); shell.setLayout(new FillLayout()); createUI(); shell.pack(); SwtUtils.center(parent, shell); shell.open(); Display display = parent.getDisplay(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } return result; } private void createUI() { content = new Composite(shell, 0); content.setLayout(LayoutFactory.createGridLayout(0, 0, 1, false)); widgets = new Composite(content, 0); widgets.setLayout(LayoutFactory.createGridLayout(10, 5, 2, false)); widgets.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); hline(content); buttons = new Composite(content, 0); buttons.setLayout(LayoutFactory.createGridLayout(10, 5, 3, false)); buttons.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); // Form content label(widgets, "Annotate"); labelCombo = combo(widgets); labelCombo.addKeyListener(new KeyListener() { @Override public void keyReleased(KeyEvent e) { ArrayList terms = new ArrayList(); //For the down arrow functionality if(e.keyCode == 16777218) { labelCombo.setListVisible(true); } // If key pressed is only a number of character or space. else if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 97 && e.keyCode <= 122) || e.keyCode == 32) { //For removing all previously assigned labels labelCombo.remove(0,labelCombo.getItemCount()-1); terms = labels.getOntologyTerms(labelCombo.getText()); } int i = 0; for (OntologyTerm term : terms) { // set text for term label labelCombo.add(term.getFormattedTerm(),i); i++; } } @Override public void keyPressed(KeyEvent arg0) { // TODO Auto-generated method stub } }); label(widgets, "Curator"); curatorText = text(widgets,"Enter your name"); // Button bar expander(buttons); cancelButton = new Button(buttons, SWT.PUSH); cancelButton.setLayoutData(layout4()); cancelButton.setText("Cancel"); assignButton = new Button(buttons, SWT.PUSH); assignButton.setLayoutData(layout4()); assignButton.setText("Export"); // Set the default button shell.setDefaultButton(assignButton); // Add listeners addListeners(); } private void addListeners() { cancelButton.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { result = null; shell.dispose(); } }); assignButton.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { result = new Result(); shell.dispose(); } }); } private Label hline(Composite parent) { Label label = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL); GridData data = new GridData(SWT.FILL, SWT.CENTER, false, false); data.horizontalSpan = 3; data.heightHint = 10; label.setLayoutData(data); return label; } private Label spacer(Composite parent) { Label label = new Label(parent, SWT.NONE); label.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false)); return label; } private Label expander(Composite parent) { Label label = new Label(parent, SWT.NONE); label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); return label; } private Label label(Composite parent, String text) { Label label = new Label(parent, SWT.NONE); label.setText(text); label.setLayoutData(layout1()); return label; } private Text text(Composite parent, String value) { Text text = new Text(parent, SWT.BORDER | SWT.SINGLE); text.setText(value); text.setLayoutData(layout2()); return text; } private Combo combo(Composite parent) { Combo combo = new Combo(parent, SWT.BORDER | SWT.DROP_DOWN); combo.setLayoutData(layout2()); return combo; } private GridData layout1() { return new GridData(SWT.LEFT, SWT.CENTER, false, false); } private GridData layout2() { return new GridData(SWT.FILL, SWT.CENTER, true, false); } private GridData layout4() { return new GridData(SWT.RIGHT, SWT.BOTTOM, false, false); } }