private List<Integer> getUnstableIndices(Ling.StoredGraphs storedGraphs) { List<Integer> indices = new ArrayList<>(); for (int i = 0; i < storedGraphs.getNumGraphs(); i++) { if (!storedGraphs.isStable(i)) { indices.add(i); } } return indices; }
private List<Integer> getAllIndices(Ling.StoredGraphs storedGraphs) { List<Integer> indices = new ArrayList<>(); for (int i = 0; i < storedGraphs.getNumGraphs(); i++) { indices.add(i); } return indices; }
public LingDisplay(final Ling.StoredGraphs storedGraphs) { this.storedGraphs = storedGraphs; if (storedGraphs.getNumGraphs() == 0) { workbench = new GraphWorkbench(); } else { workbench = new GraphWorkbench(storedGraphs.getGraph(0)); } subsetIndices = getStableIndices(storedGraphs); final SpinnerNumberModel model = new SpinnerNumberModel(subsetIndices.size() == 0 ? 0 : 1, 0, subsetIndices.size(), 1); model.addChangeListener( new ChangeListener() { public void stateChanged(ChangeEvent e) { int index = model.getNumber().intValue(); workbench.setGraph(storedGraphs.getGraph(subsetIndices.get(index - 1))); } }); spinner = new JSpinner(); subsetCombo = new JComboBox(new String[] {"Show Stable", "Show Unstable", "Show All"}); subsetCombo.setSelectedItem("Show Stable"); spinner.setModel(model); totalLabel = new JLabel(" of " + subsetIndices.size()); subsetCombo.setMaximumSize(subsetCombo.getPreferredSize()); subsetCombo.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { resetDisplay(); } }); spinner.setPreferredSize(new Dimension(50, 20)); spinner.setMaximumSize(spinner.getPreferredSize()); Box b = Box.createVerticalBox(); Box b1 = Box.createHorizontalBox(); // b1.add(Box.createHorizontalGlue()); // b1.add(Box.createHorizontalStrut(10)); b1.add(subsetCombo); b1.add(Box.createHorizontalGlue()); b1.add(new JLabel("DAG ")); b1.add(spinner); b1.add(totalLabel); b.add(b1); Box b2 = Box.createHorizontalBox(); JPanel graphPanel = new JPanel(); graphPanel.setLayout(new BorderLayout()); JScrollPane jScrollPane = new JScrollPane(workbench); // jScrollPane.setPreferredSize(new Dimension(400, 400)); graphPanel.add(jScrollPane); // graphPanel.setBorder(new TitledBorder("DAG")); b2.add(graphPanel); b.add(b2); setLayout(new BorderLayout()); // add(menuBar(), BorderLayout.NORTH); add(b, BorderLayout.CENTER); }
private void resetDisplay() { String option = (String) subsetCombo.getSelectedItem(); if ("Show All".equals(option)) { final List<Integer> _subsetIndices = getAllIndices(getStoredGraphs()); subsetIndices.clear(); subsetIndices.addAll(_subsetIndices); int min = subsetIndices.size() == 0 ? 0 : 1; final SpinnerNumberModel model = new SpinnerNumberModel(min, min, subsetIndices.size(), 1); model.addChangeListener( new ChangeListener() { public void stateChanged(ChangeEvent e) { int index = model.getNumber().intValue(); workbench.setGraph(storedGraphs.getGraph(subsetIndices.get(index - 1))); } }); spinner.setModel(model); totalLabel.setText(" of " + _subsetIndices.size()); if (subsetIndices.isEmpty()) { workbench.setGraph(new EdgeListGraph()); } else { workbench.setGraph(storedGraphs.getGraph(subsetIndices.get(0))); } } else if ("Show Stable".equals(option)) { final List<Integer> _subsetIndices = getStableIndices(getStoredGraphs()); subsetIndices.clear(); subsetIndices.addAll(_subsetIndices); int min = subsetIndices.size() == 0 ? 0 : 1; final SpinnerNumberModel model = new SpinnerNumberModel(min, min, subsetIndices.size(), 1); model.addChangeListener( new ChangeListener() { public void stateChanged(ChangeEvent e) { int index = model.getNumber().intValue(); workbench.setGraph(storedGraphs.getGraph(subsetIndices.get(index - 1))); } }); spinner.setModel(model); totalLabel.setText(" of " + _subsetIndices.size()); if (subsetIndices.isEmpty()) { workbench.setGraph(new EdgeListGraph()); } else { workbench.setGraph(storedGraphs.getGraph(subsetIndices.get(0))); } } else if ("Show Unstable".equals(option)) { final List<Integer> _subsetIndices = getUnstableIndices(getStoredGraphs()); subsetIndices.clear(); subsetIndices.addAll(_subsetIndices); int min = subsetIndices.size() == 0 ? 0 : 1; final SpinnerNumberModel model = new SpinnerNumberModel(min, min, subsetIndices.size(), 1); model.addChangeListener( new ChangeListener() { public void stateChanged(ChangeEvent e) { int index = model.getNumber().intValue(); workbench.setGraph(storedGraphs.getGraph(subsetIndices.get(index - 1))); } }); spinner.setModel(model); totalLabel.setText(" of " + _subsetIndices.size()); if (subsetIndices.isEmpty()) { workbench.setGraph(new EdgeListGraph()); } else { workbench.setGraph(storedGraphs.getGraph(subsetIndices.get(0))); } } }