private void goDraw(String description, int decomp_strategy, int recomp_strategy) { ConcreteDiagram cd = null; String failureMessage = null; try { AbstractDescription adr = AbstractDescription.makeForTesting(description); DEB.out(1, "draw " + adr.debug()); DiagramCreator dc = new DiagramCreator( adr, DecompositionStrategy.getStrategy(decomp_strategy), RecompositionStrategy.getStrategy(recomp_strategy)); cd = dc.createDiagram(SIZE); } catch (CannotDrawException x) { failureMessage = x.message; } resultPanel.show(description, failureMessage, cd, SIZE, useColors); }
class SettingsPanel { private int test_num = 0; String[] decompStrings = DecompositionStrategy.getDecompStrings(); String[] recompStrings = RecompositionStrategy.getRecompStrings(); final JComboBox decompList = new JComboBox(decompStrings); final JComboBox recompList = new JComboBox(recompStrings); final JTextField testJTF = new JTextField(""); static final String ENTER_ACTION = "go-draw-test"; final JPanel p = new JPanel(); SettingsPanel() { // Construct a GridLayout with 1 columns and an unspecified number of rows. // p.setLayout(new GridLayout(0,1)); p.setLayout(new BorderLayout()); JPanel topPanel = new JPanel(); topPanel.setBorder(BorderFactory.createTitledBorder("settings")); topPanel.setLayout(new GridLayout(0, 1)); p.add(topPanel, BorderLayout.NORTH); final JCheckBox jcb = new JCheckBox("Show contours with colours", true); jcb.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { useColors = jcb.isSelected(); redraw(); } }); topPanel.add(jcb); decompList.setSelectedIndex(decompStrings.length - 1); decompList.addActionListener(new RedrawListener()); topPanel.add(decompList); recompList.setSelectedIndex(recompStrings.length - 1); recompList.addActionListener(new RedrawListener()); topPanel.add(recompList); JPanel examplePanel = new JPanel(); examplePanel.setBorder(BorderFactory.createTitledBorder("examples")); examplePanel.setLayout(new GridLayout(0, 1)); p.add(examplePanel, BorderLayout.CENTER); JButton v3 = new JButton("draw Venn 3"); v3.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { draw("a b c ab ac bc abc"); } }); examplePanel.add(v3); final JLabel testLabel = new JLabel("draw test index:"); examplePanel.add(testLabel); InputMap im = testJTF.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); ActionMap am = testJTF.getActionMap(); im.put(KeyStroke.getKeyStroke("ENTER"), ENTER_ACTION); am.put(ENTER_ACTION, new TestListener()); examplePanel.add(testJTF); JButton next = new JButton("draw next test case"); next.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (testJTF.getText().length() == 0) { test_num = 0; } else { test_num += 1; if (test_num > TestData.test_data.length - 1) { test_num = 0; } } testJTF.setText("" + (test_num + 1)); drawTest(test_num + 1); } }); examplePanel.add(next); JButton prev = new JButton("draw previous test case"); prev.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (testJTF.getText().length() == 0) { test_num = 0; } else { test_num -= 1; if (test_num < 0) { test_num = TestData.test_data.length - 1; } } testJTF.setText("" + (test_num + 1)); drawTest(test_num + 1); } }); examplePanel.add(prev); } void setDecompStrategy(int i) { decompList.setSelectedIndex(i); } int getDecompStrategy() { return decompList.getSelectedIndex(); } String getDecompString() { return decompStrings[getDecompStrategy()]; } void setRecompStrategy(int i) { recompList.setSelectedIndex(i); } int getRecompStrategy() { return recompList.getSelectedIndex(); } String getRecompString() { return recompStrings[getRecompStrategy()]; } JPanel getPanel() { return p; } class TestListener extends AbstractAction { private static final long serialVersionUID = 1L; public void actionPerformed(ActionEvent ev) { try { int i = Integer.parseInt(testJTF.getText()); if (i < 1 || i > TestData.test_data.length) { JOptionPane.showMessageDialog( null, "test number should be between 1 and " + TestData.test_data.length); return; } test_num = i - 1; drawTest(test_num + 1); } catch (NumberFormatException x) { JOptionPane.showMessageDialog( null, "type an integer between 1 and " + TestData.test_data.length); return; } } } }