private static void addButons(final MultiChoice<?> mc) { final Button buttonShowSelection = new Button(mc.getParent(), SWT.PUSH); buttonShowSelection.setLayoutData( new GridData(GridData.BEGINNING, GridData.BEGINNING, false, false)); buttonShowSelection.setText("Show selection"); buttonShowSelection.addSelectionListener( new SimpleSelectionAdapter() { @Override public void handle(final SelectionEvent e) { final Iterator<?> it = mc.getSelection().iterator(); final StringBuilder sb = new StringBuilder(); while (it.hasNext()) { sb.append(it.next().toString()); if (it.hasNext()) { sb.append(", "); } } final MessageBox mb = new MessageBox(mc.getShell(), SWT.OK); mb.setMessage(sb.toString()); mb.open(); } }); final Button buttonShowSelectedIndex = new Button(mc.getParent(), SWT.PUSH); buttonShowSelectedIndex.setLayoutData( new GridData(GridData.BEGINNING, GridData.BEGINNING, false, false)); buttonShowSelectedIndex.setText("Show selected index"); buttonShowSelectedIndex.addSelectionListener( new SimpleSelectionAdapter() { @Override public void handle(final SelectionEvent e) { final StringBuilder sb = new StringBuilder(); final int[] selectedIndex = mc.getSelectedIndex(); if (selectedIndex.length > 0) { sb.append(selectedIndex[0]); for (int i = 1; i < selectedIndex.length; i++) { sb.append(","); sb.append(selectedIndex[i]); } } else { sb.append("Empty"); } final MessageBox mb = new MessageBox(mc.getShell(), SWT.OK); mb.setMessage(sb.toString()); mb.open(); } }); }
/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) * <p>Creates the question and registers it within the database then depending on what the * user clicks, sends them to the finish quiz page or to create another question */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); Quiz qz = (Quiz) session.getAttribute("newQuiz"); DBConnection dbCon = (DBConnection) session.getAttribute("connection"); int qzID = qz.getID(); Question newQn; String question = request.getParameter("question"); String answer = request.getParameter("answer"); int type = Integer.parseInt(request.getParameter("type")); String MC = null; switch (type) { case 1: newQn = new QResponse(question, answer); newQn.setType(type); break; case 2: newQn = new FillIn(question, answer); newQn.setType(type); break; case 3: newQn = new MultiChoice(question, answer); MC = request.getParameter("choices"); ((MultiChoice) newQn).addMCOptions(MC); newQn.setType(type); break; case 4: newQn = new PictureResponse(question, answer); newQn.setType(type); break; default: newQn = null; break; } newQn.printString(); if (!newQn.equals(null)) qz.addQuestion(newQn); Question.registerQuestion(qzID, type, question, answer, MC, dbCon); // send the user on forward through the quiz creation String action = request.getParameter("action"); if (action.equals("Create & Continue")) { RequestDispatcher dispatch = request.getRequestDispatcher("chooseQuestionType.jsp"); dispatch.forward(request, response); } else if (action.equals("Create & Finish Quiz")) { RequestDispatcher dispatch = request.getRequestDispatcher("finishCreationQuiz.jsp"); dispatch.forward(request, response); } }
public static void main(final String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new GridLayout(4, false)); shell.setText("MultiChoice Example"); // Data final String[] euroZone = new String[] { "Austria", "Belgium", "Cyprus", "Estonia", "Finland", "France", "Germany", "Greece", "Ireland", "Italy", "Luxembourg", "Malta", "Netherlands", "Portugal", "Slovakia", "Slovenia", "Spain" }; final List<Country> membersOfEuropeanUnion = new ArrayList<Country>(); membersOfEuropeanUnion.add(new Country("Austria", 8372930)); membersOfEuropeanUnion.add(new Country("Belgium", 10827519)); membersOfEuropeanUnion.add(new Country("Bulgaria", 7576751)); membersOfEuropeanUnion.add(new Country("Cyprus", 801851)); membersOfEuropeanUnion.add(new Country("Czech Republic", 10512397)); membersOfEuropeanUnion.add(new Country("Denmark", 5547088)); membersOfEuropeanUnion.add(new Country("Estonia", 1340274)); membersOfEuropeanUnion.add(new Country("Finland", 5530575)); membersOfEuropeanUnion.add(new Country("France", 64709480)); membersOfEuropeanUnion.add(new Country("Germany", 81757595)); membersOfEuropeanUnion.add(new Country("Greece", 11125179)); membersOfEuropeanUnion.add(new Country("Hungary", 10013628)); membersOfEuropeanUnion.add(new Country("Ireland", 4450878)); membersOfEuropeanUnion.add(new Country("Italy", 60397353)); membersOfEuropeanUnion.add(new Country("Latvia", 2248961)); membersOfEuropeanUnion.add(new Country("Lithuania", 3329227)); membersOfEuropeanUnion.add(new Country("Luxembourg", 502207)); membersOfEuropeanUnion.add(new Country("Malta", 416333)); membersOfEuropeanUnion.add(new Country("Netherlands", 16576800)); membersOfEuropeanUnion.add(new Country("Poland", 38163895)); membersOfEuropeanUnion.add(new Country("Portugal", 11317192)); membersOfEuropeanUnion.add(new Country("Romania", 21466174)); membersOfEuropeanUnion.add(new Country("Slovakia", 5424057)); membersOfEuropeanUnion.add(new Country("Slovenia", 2054119)); membersOfEuropeanUnion.add(new Country("Spain", 46087170)); membersOfEuropeanUnion.add(new Country("Sweden", 9347899)); membersOfEuropeanUnion.add(new Country("United Kingdom", 62041708)); final List<Country> membersOfEUSelectAll = new ArrayList<Country>(); membersOfEUSelectAll.addAll(membersOfEuropeanUnion); membersOfEUSelectAll.add(new Country("Select All", -1)); final List<Country> countryCodes = new ArrayList<Country>(); countryCodes.add(new Country("France", "FR")); countryCodes.add(new Country("United states", "US")); countryCodes.add(new Country("United Kingdom", "UK")); countryCodes.add(new Country("Germany", "DE")); countryCodes.add(new Country("Belgium", "BE")); countryCodes.add(new Country("Netherland", "NL")); countryCodes.add(new Country("Italy", "IT")); countryCodes.add(new Country("Spain", "ES")); countryCodes.add(new Country("Portugal", "PT")); // Draw the window drawLabel(shell, "Simple Multichoice :"); final MultiChoice<String> mcSimple = new MultiChoice<String>(shell, SWT.READ_ONLY); final GridData gridData = new GridData(GridData.FILL, GridData.BEGINNING, true, true); gridData.widthHint = 200; mcSimple.setLayoutData(gridData); mcSimple.addAll(euroZone); addButons(mcSimple); drawLabel(shell, "Multichoice with beans :"); final MultiChoice<Country> mcBeans = new MultiChoice<Country>(shell, SWT.READ_ONLY); mcBeans.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, true)); mcBeans.addAll(membersOfEuropeanUnion); addButons(mcBeans); drawLabel(shell, "Selection listener :"); final MultiChoice<Country> mcSL = new MultiChoice<Country>(shell, SWT.READ_ONLY); mcSL.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, true)); mcSL.setSelectionListener( new MultiChoiceSelectionListener<Country>(mcSL) { @Override public void handle( final MultiChoice<Country> parent, final Country receiver, final boolean selection, final Shell popup) { if ("Select All".equals(receiver.toString())) { if (selection) { parent.deselectAll(); parent.selectAll(); } else { parent.deselectAll(); } popup.setVisible(false); } } }); mcSL.addAll(membersOfEUSelectAll); addButons(mcSL); drawLabel(shell, "3 columns :"); final MultiChoice<String> mc3Columns = new MultiChoice<String>(shell, SWT.READ_ONLY); mc3Columns.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, true)); mc3Columns.addAll(euroZone); mc3Columns.setNumberOfColumns(3); addButons(mc3Columns); drawLabel(shell, "Other separator :"); final MultiChoice<String> mcOtherSeparator = new MultiChoice<String>(shell, SWT.READ_ONLY); mcOtherSeparator.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, true)); mcOtherSeparator.addAll(euroZone); mcOtherSeparator.setSeparator(" - "); addButons(mcOtherSeparator); drawLabel(shell, "Modifiable combo :"); final MultiChoice<Country> mcModify = new MultiChoice<Country>(shell, SWT.NONE); mcModify.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, true)); mcModify.setLabelProvider( new MultiChoiceLabelProvider() { @Override public String getText(final Object element) { if (element == null || !(element instanceof Country)) { return ""; } return ((Country) element).getCode(); } }); mcModify.addAll(countryCodes); addButons(mcModify); drawLabel(shell, "Lot of data :"); final List<String> data = new ArrayList<String>(); for (int i = 0; i < 1000; i++) { data.add("Data #" + i); } final MultiChoice<String> mcLotOfData = new MultiChoice<String>(shell, SWT.NONE); mcLotOfData.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, true)); mcLotOfData.setLabelProvider( new MultiChoiceLabelProvider() { @Override public String getText(final Object element) { if (element == null) { return ""; } return (String) element; } }); mcLotOfData.addAll(data); addButons(mcLotOfData); // display the shell... shell.open(); shell.pack(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }