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
Text areas expand on focus. Closes
#3
.
1 parent
1e8fd1d
commit
1e4f7d03fc851cd2930a5f018a44b13291d05d7b
Mark George
authored
on 3 Nov 2017
Patch
Showing
2 changed files
src/ui/CriterionPanel.java
src/ui/MarkingFrame.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.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; } }
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 javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import model.Criterion; /** * * @author mark */ public class CriterionPanel extends JPanel implements CriterionEditor { private final RadioGroup radioGroup; private final JTextArea txtComment; private final Criterion criterion; 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)); 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/MarkingFrame.java
package ui; import dao.MarkingProperties; import dao.ResultDAO; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import model.Criterion; import model.Student; import report.FeedbackReportGenerator; import report.SendReport; /** * * @author mark */ public class MarkingFrame extends JFrame { private String id; private Student student; private final JPanel schedulePane; private final ResultDAO result = new ResultDAO(); private final JLabel lblStudent; private final JLabel lblAssessment; private final SubmissionFrame parent; private final String assessment; public MarkingFrame(SubmissionFrame parent, String assessment) { this.parent = parent; // super(parent); // setModal(true); this.assessment = assessment; // add scroll pane to the root JScrollPane scrollPane = new JScrollPane(); JPanel contentPane = new JPanel(); JPanel titlePane = new JPanel(); scrollPane.setViewportView(contentPane); scrollPane.getVerticalScrollBar().setUnitIncrement(16); getContentPane().add(titlePane, java.awt.BorderLayout.NORTH); getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER); titlePane.setLayout(new BorderLayout()); lblStudent = new JLabel("Student"); Font newFont = lblStudent.getFont().deriveFont(Font.BOLD, 15); lblStudent.setFont(newFont); lblAssessment = new JLabel("Assessment"); lblAssessment.setFont(newFont); titlePane.add(BorderLayout.WEST, lblStudent); titlePane.add(BorderLayout.EAST, lblAssessment); // add other panes schedulePane = new JPanel(); JPanel buttonPane = new JPanel(); contentPane.setLayout(new BorderLayout()); contentPane.add(schedulePane, BorderLayout.CENTER); contentPane.add(buttonPane, BorderLayout.SOUTH); schedulePane.setLayout(new BoxLayout(schedulePane, BoxLayout.Y_AXIS)); // add buttons JButton btnSave = new JButton("Save"); btnSave.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { save(); } }); // add buttons JButton btnCancel = new JButton("Cancel"); btnCancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { dispose(); } }); JButton btnMin = new JButton("Min"); btnMin.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { min(); } }); JButton btnMax = new JButton("Max"); btnMax.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { max(); } }); JButton btnEmail = new JButton("Save & Email"); btnEmail.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { email(); } }); buttonPane.add(btnMin); buttonPane.add(btnMax); buttonPane.add(btnCancel); if (MarkingProperties.isEmailEnabled()) { buttonPane.add(btnEmail); } buttonPane.add(btnSave); } public void addCategory(String category, List<Criterion> criteria) { schedulePane.add(new CategoryPanel(category, criteria)); } private void save() { if (!result.isComplete()) { int res = JOptionPane.showConfirmDialog(MarkingFrame.this, "You haven't entered values for all criteria. Are you sure you want save?", "Incomplete", JOptionPane.OK_CANCEL_OPTION); if (JOptionPane.OK_OPTION == res) { result.save(id); parent.refresh(); dispose(); } } else { result.save(id); parent.refresh(); dispose(); } } private void min() { Component[] components = schedulePane.getComponents(); for (Component component : components) { if (component instanceof CategoryPanel) { CategoryPanel cp = (CategoryPanel) component; Component[] categories = cp.getComponents(); for (Component criterion : categories) { if (criterion instanceof CriterionPanel) { CriterionPanel c = (CriterionPanel) criterion; c.setZero(); } } } } } private void max() { Component[] components = schedulePane.getComponents(); for (Component component : components) { if (component instanceof CategoryPanel) { CategoryPanel cp = (CategoryPanel) component; Component[] categories = cp.getComponents(); for (Component criterion : categories) { if (criterion instanceof CriterionPanel) { CriterionPanel c = (CriterionPanel) criterion; if (!c.getCriterion().isIsSpecial()) { c.setMax(); } else { c.setZero(); } } } } } } public void load(String id) { this.id = id; String idType = MarkingProperties.idType(); student = idType.equals("id") ? new ResultDAO().findStudentById(id) : new ResultDAO().findStudentByUserCode(id); lblStudent.setText(student.getName() + " (" + student.getId()+")"); lblAssessment.setText(assessment); result.load(id); } public void display() { this.setPreferredSize(new Dimension(600, 980)); this.pack(); this.setVisible(true); } public void createReport() { try { new FeedbackReportGenerator(new ResultDAO().getResult(id), MarkingProperties.reportPath(), MarkingProperties.attachmentFilePrefix()).generate(student); } catch (Exception ex) { Logger.getLogger(MarkingFrame.class.getName()).log(Level.SEVERE, null, ex); } } public void emailReport() { String report = MarkingProperties.reportPath() + "/" + MarkingProperties.attachmentFilePrefix() + "-" + student.getId() + ".pdf"; new SendReport().sendReport(student, report); } private void email() { save(); new Thread(new Runnable() { @Override public void run() { createReport(); emailReport(); } }).start(); } }
package ui; import dao.MarkingProperties; import dao.ResultDAO; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import model.Criterion; import model.Student; import report.FeedbackReportGenerator; import report.SendReport; /** * * @author mark */ public class MarkingFrame extends JFrame { private String id; private Student student; private final JPanel schedulePane; private final ResultDAO result = new ResultDAO(); private final JLabel lblStudent; private final JLabel lblAssessment; private final SubmissionFrame parent; private final String assessment; public MarkingFrame(SubmissionFrame parent, String assessment) { this.parent = parent; // super(parent); // setModal(true); this.assessment = assessment; // add scroll pane to the root JScrollPane scrollPane = new JScrollPane(); JPanel contentPane = new JPanel(); JPanel titlePane = new JPanel(); scrollPane.setViewportView(contentPane); scrollPane.getVerticalScrollBar().setUnitIncrement(16); getContentPane().add(titlePane, java.awt.BorderLayout.NORTH); getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER); titlePane.setLayout(new BorderLayout()); lblStudent = new JLabel("Student"); Font newFont = lblStudent.getFont().deriveFont(Font.BOLD, 15); lblStudent.setFont(newFont); lblAssessment = new JLabel("Assessment"); lblAssessment.setFont(newFont); titlePane.add(BorderLayout.WEST, lblStudent); titlePane.add(BorderLayout.EAST, lblAssessment); // add other panes schedulePane = new JPanel(); JPanel buttonPane = new JPanel(); contentPane.setLayout(new BorderLayout()); contentPane.add(schedulePane, BorderLayout.CENTER); contentPane.add(buttonPane, BorderLayout.SOUTH); schedulePane.setLayout(new BoxLayout(schedulePane, BoxLayout.Y_AXIS)); // add buttons JButton btnSave = new JButton("Save"); btnSave.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { save(); } }); // add buttons JButton btnCancel = new JButton("Cancel"); btnCancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { dispose(); } }); JButton btnMin = new JButton("Min"); btnMin.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { min(); } }); JButton btnMax = new JButton("Max"); btnMax.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { max(); } }); JButton btnEmail = new JButton("Save & Email"); btnEmail.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { email(); } }); buttonPane.add(btnMin); buttonPane.add(btnMax); buttonPane.add(btnCancel); if (MarkingProperties.isEmailEnabled()) { buttonPane.add(btnEmail); } buttonPane.add(btnSave); } public void addCategory(String category, List<Criterion> criteria) { schedulePane.add(new CategoryPanel(category, criteria)); } private void save() { if (!result.isComplete()) { int res = JOptionPane.showConfirmDialog(MarkingFrame.this, "You haven't entered values for all criteria. Are you sure you want save?", "Incomplete", JOptionPane.OK_CANCEL_OPTION); if (JOptionPane.OK_OPTION == res) { result.save(id); parent.refresh(id); dispose(); } } else { result.save(id); parent.refresh(id); dispose(); } } private void min() { Component[] components = schedulePane.getComponents(); for (Component component : components) { if (component instanceof CategoryPanel) { CategoryPanel cp = (CategoryPanel) component; Component[] categories = cp.getComponents(); for (Component criterion : categories) { if (criterion instanceof CriterionPanel) { CriterionPanel c = (CriterionPanel) criterion; c.setZero(); } } } } } private void max() { Component[] components = schedulePane.getComponents(); for (Component component : components) { if (component instanceof CategoryPanel) { CategoryPanel cp = (CategoryPanel) component; Component[] categories = cp.getComponents(); for (Component criterion : categories) { if (criterion instanceof CriterionPanel) { CriterionPanel c = (CriterionPanel) criterion; if (!c.getCriterion().isIsSpecial()) { c.setMax(); } else { c.setZero(); } } } } } } public void load(String id) { this.id = id; String idType = MarkingProperties.idType(); student = idType.equals("id") ? new ResultDAO().findStudentById(new Integer(id)) : new ResultDAO().findStudentByUserCode(id); lblStudent.setText(student.getName()); lblAssessment.setText(assessment); result.load(id); } public void display() { this.setPreferredSize(new Dimension(600, 980)); this.pack(); this.setVisible(true); } public void createReport() { try { new FeedbackReportGenerator(new ResultDAO().getResult(id), MarkingProperties.reportPath(), MarkingProperties.attachmentFilePrefix()).generate(student); } catch (Exception ex) { Logger.getLogger(MarkingFrame.class.getName()).log(Level.SEVERE, null, ex); } } public void emailReport() { String report = MarkingProperties.reportPath() + "/" + MarkingProperties.attachmentFilePrefix() + "-" + student.getId() + ".pdf"; new SendReport().sendReport(student, report); } private void email() { save(); new Thread(new Runnable() { @Override public void run() { createReport(); emailReport(); } }).start(); } }
Show line notes below