GitBucket
4.21.2
Toggle navigation
Snippets
Sign in
Files
Branches
1
Releases
Issues
4
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
mark.george
/
marking
Browse code
Added a clear mark button. Closes
#2
.
1 parent
1e4f7d0
commit
b6b85e47aa075caafbce1bfc3581f646d9a50341
Mark George
authored
on 3 Nov 2017
Patch
Showing
2 changed files
src/ui/CriterionPanel.java
src/ui/RadioGroup.java
Ignore Space
Show notes
View
src/ui/CriterionPanel.java
package ui; import java.awt.Dimension; import java.awt.Font; import model.CriterionEditor; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingUtilities; import model.Criterion; /** * * @author mark */ public class CriterionPanel extends JPanel implements CriterionEditor { private final RadioGroup radioGroup; private final JTextArea txtComment; private final Criterion criterion; private final JButton clearBtn; private static Dimension selectedSize; public CriterionPanel(Criterion criterion) { this.criterion = criterion; this.setLayout(new GridBagLayout()); // label is left aligned and full width GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 3; gbc.weightx = 1; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets(0, 0, 5, 0); this.add(new JLabel(criterion.getDescription()), gbc); // clear mark buttonleft aligned, but doesn't grow gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; clearBtn = new JButton("X"); this.add(clearBtn, gbc); // radio group is left aligned, but doesn't grow gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 1; radioGroup = new RadioGroup(criterion.getMin(), criterion.getMax()); this.add(radioGroup, gbc); // text area is right aligned, but grows in both directions gbc = new GridBagConstraints(); gbc.gridx = 2; gbc.gridy = 1; gbc.weightx = 1; gbc.weighty = 1; gbc.fill = GridBagConstraints.BOTH; txtComment = new JTextArea(); txtComment.setWrapStyleWord(true); txtComment.setLineWrap(true); JScrollPane scrollPane = new JScrollPane(); scrollPane.setViewportView(txtComment); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); // Dimensions are necessary to prevent the semi-famous "text area grows infinitely" bug. // Unfortunately this also means we can't let the layout manager figure out the correct // size dynamically... txtComment.setMinimumSize(new Dimension(10, 20)); txtComment.setPreferredSize(new Dimension(10, 100)); // need to hardcode height which is annoying... scrollPane.setPreferredSize(new Dimension(10, 20)); txtComment.addFocusListener(new FocusAdapter() { @Override public void focusGained(FocusEvent fe) { super.focusGained(fe); CriterionPanel selectedPanel = CriterionPanel.this; if (selectedSize == null) { System.out.println(selectedSize); } selectedPanel.setPreferredSize(new Dimension(10, 150)); selectedPanel.invalidate(); SwingUtilities.getWindowAncestor(selectedPanel).pack(); } @Override public void focusLost(FocusEvent fe) { super.focusLost(fe); CriterionPanel selectedPanel = CriterionPanel.this; selectedPanel.setPreferredSize(selectedSize); selectedPanel.invalidate(); SwingUtilities.getWindowAncestor(selectedPanel).pack(); } }); clearBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { radioGroup.clearSelection(); } }); Font font = txtComment.getFont(); Font biggerFont = font.deriveFont(14f); txtComment.setFont(biggerFont); this.add(scrollPane, gbc); } @Override public String getComment() { return txtComment.getText().trim(); } @Override public Double getResult() { Integer result = radioGroup.getSelected(); return result != null ? new Double(result) : null; } @Override public boolean isComplete() { return getResult() != null; } @Override public void setComment(String comment) { txtComment.setText(comment); } @Override public void setResult(Double result) { radioGroup.setSelected(result.intValue()); } public void setZero() { setResult(0.0); } public void setMax() { setResult(new Double(criterion.getMax())); } public Criterion getCriterion() { return criterion; } }
package ui; import java.awt.Dimension; import java.awt.Font; import model.CriterionEditor; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingUtilities; import model.Criterion; /** * * @author mark */ public class CriterionPanel extends JPanel implements CriterionEditor { private final RadioGroup radioGroup; private final JTextArea txtComment; private final Criterion criterion; private static Dimension selectedSize; public CriterionPanel(Criterion criterion) { this.criterion = criterion; this.setLayout(new GridBagLayout()); // label is left aligned and full width GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2; gbc.weightx = 1; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets(0, 0, 5, 0); this.add(new JLabel(criterion.getDescription()), gbc); // radio group is left aligned, but doesn't grow gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; radioGroup = new RadioGroup(criterion.getMin(), criterion.getMax()); this.add(radioGroup, gbc); // text area is right aligned, but grows in both directions gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 1; gbc.weightx = 1; gbc.weighty = 1; gbc.fill = GridBagConstraints.BOTH; txtComment = new JTextArea(); txtComment.setWrapStyleWord(true); txtComment.setLineWrap(true); JScrollPane scrollPane = new JScrollPane(); scrollPane.setViewportView(txtComment); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); // Dimensions are necessary to prevent the semi-famous "text area grows infinitely" bug. // Unfortunately this also means we can't let the layout manager figure out the correct // size dynamically... txtComment.setMinimumSize(new Dimension(10, 20)); txtComment.setPreferredSize(new Dimension(10, 100)); // need to hardcode height which is annoying... scrollPane.setPreferredSize(new Dimension(10, 20)); txtComment.addFocusListener(new FocusAdapter() { @Override public void focusGained(FocusEvent fe) { super.focusGained(fe); CriterionPanel selectedPanel = CriterionPanel.this; if (selectedSize == null) { System.out.println(selectedSize); } selectedPanel.setPreferredSize(new Dimension(10, 150)); selectedPanel.invalidate(); SwingUtilities.getWindowAncestor(selectedPanel).pack(); } @Override public void focusLost(FocusEvent fe) { super.focusLost(fe); CriterionPanel selectedPanel = CriterionPanel.this; selectedPanel.setPreferredSize(selectedSize); selectedPanel.invalidate(); SwingUtilities.getWindowAncestor(selectedPanel).pack(); } }); Font font = txtComment.getFont(); Font biggerFont = font.deriveFont(14f); txtComment.setFont(biggerFont); this.add(scrollPane, gbc); } @Override public String getComment() { return txtComment.getText().trim(); } @Override public Double getResult() { Integer result = radioGroup.getSelected(); return result != null ? new Double(result) : null; } @Override public boolean isComplete() { return getResult() != null; } @Override public void setComment(String comment) { txtComment.setText(comment); } @Override public void setResult(Double result) { radioGroup.setSelected(result.intValue()); } public void setZero() { setResult(0.0); } public void setMax() { setResult(new Double(criterion.getMax())); } public Criterion getCriterion() { return criterion; } }
Ignore Space
Show notes
View
src/ui/RadioGroup.java
package ui; import java.awt.FlowLayout; import java.util.HashMap; import java.util.Map; import javax.swing.ButtonGroup; import javax.swing.ButtonModel; import javax.swing.JPanel; import javax.swing.JRadioButton; public class RadioGroup extends JPanel { private Integer min; private Integer max; private final Map<Integer, JRadioButton> radios = new HashMap<>(); private final Map<ButtonModel, Integer> models = new HashMap<>(); private ButtonGroup group = new ButtonGroup(); public RadioGroup(int min, int max) { this.min = min; this.max = max; this.setLayout(new FlowLayout()); for (int i = min; i <= max; i++) { JRadioButton rad = new JRadioButton(); rad.setText(String.valueOf(i)); radios.put(i, rad); group.add(rad); models.put(rad.getModel(), i); this.add(rad); } } public void setSelected(Integer idx) { JRadioButton rad = radios.get(idx); group.setSelected(rad.getModel(), true); } public void setMin(Integer min) { this.min = min; } public void setMax(Integer max) { this.max = max; } public void max() { setSelected(max); } public void zero() { setSelected(0); } public Integer getSelected() { return models.get(group.getSelection()); } public void clearSelection() { group.clearSelection(); } // public void setRange(int min, int max) { // this.min = min; // this.max = max; // // radios.clear(); // models.clear(); // group = new ButtonGroup(); // this.removeAll(); // // for (int i = min; i <= max; i++) { // JRadioButton rad = new JRadioButton(); // rad.setText(String.valueOf(i)); // radios.put(i, rad); // group.add(rad); // models.put(rad.getModel(), i); // this.add(rad); // } // } }
package ui; import java.awt.FlowLayout; import java.util.HashMap; import java.util.Map; import javax.swing.ButtonGroup; import javax.swing.ButtonModel; import javax.swing.JPanel; import javax.swing.JRadioButton; public class RadioGroup extends JPanel { private Integer min; private Integer max; private final Map<Integer, JRadioButton> radios = new HashMap<>(); private final Map<ButtonModel, Integer> models = new HashMap<>(); private ButtonGroup group = new ButtonGroup(); public RadioGroup(int min, int max) { this.min = min; this.max = max; this.setLayout(new FlowLayout()); for (int i = min; i <= max; i++) { JRadioButton rad = new JRadioButton(); rad.setText(String.valueOf(i)); radios.put(i, rad); group.add(rad); models.put(rad.getModel(), i); this.add(rad); } } public void setSelected(Integer idx) { JRadioButton rad = radios.get(idx); group.setSelected(rad.getModel(), true); } public void setMin(Integer min) { this.min = min; } public void setMax(Integer max) { this.max = max; } public void max() { setSelected(max); } public void zero() { setSelected(0); } public Integer getSelected() { return models.get(group.getSelection()); } // public void setRange(int min, int max) { // this.min = min; // this.max = max; // // radios.clear(); // models.clear(); // group = new ButtonGroup(); // this.removeAll(); // // for (int i = min; i <= max; i++) { // JRadioButton rad = new JRadioButton(); // rad.setText(String.valueOf(i)); // radios.put(i, rad); // group.add(rad); // models.put(rad.getModel(), i); // this.add(rad); // } // } }
Show line notes below