1 33 34 package edu.rice.cs.drjava.ui.config; 35 36 import javax.swing.*; 37 import javax.swing.border.EmptyBorder ; 38 import java.awt.event.*; 39 import java.awt.*; 40 41 import java.util.Vector ; 43 44 48 public class ConfigPanel extends JPanel { 49 50 protected final String _title; 51 protected final Vector <OptionComponent> _components; 52 53 56 public ConfigPanel(String title) { 57 _title = title; 59 _components = new Vector <OptionComponent>(); 60 } 61 62 public String getTitle() { return _title; } 63 64 67 public void addComponent( OptionComponent oc) { _components.add(oc); } 68 69 public void displayComponents() { 70 this.setLayout(new BorderLayout()); 71 72 JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); 74 JPanel panel2 = new JPanel(); panel.add(panel2, BorderLayout.NORTH); 76 77 JScrollPane scroll = 78 new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 79 scroll.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), _title)); 80 81 JScrollBar bar = scroll.getVerticalScrollBar(); 83 bar.setUnitIncrement(25); 84 bar.setBlockIncrement(400); 85 86 GridBagLayout gridbag = new GridBagLayout(); 87 GridBagConstraints c = new GridBagConstraints(); 88 panel2.setLayout(gridbag); 89 c.fill = GridBagConstraints.HORIZONTAL; 90 Insets labelInsets = new Insets(0, 10, 0, 10); 91 Insets compInsets = new Insets(0, 0, 0, 0); 92 for (int i=0; i<_components.size(); i++) { 93 OptionComponent comp = _components.get(i); 94 95 c.weightx = 0.0; 96 c.gridwidth = 1; 97 c.insets = labelInsets; 98 99 JLabel label= comp.getLabel(); 100 gridbag.setConstraints(label, c); 101 panel2.add(label); 102 103 c.weightx = 1.0; 104 c.gridwidth = GridBagConstraints.REMAINDER; 105 c.insets = compInsets; 106 107 JComponent otherC = comp.getComponent(); 108 gridbag.setConstraints(otherC, c); 109 panel2.add(otherC); 110 } 111 115 116 JButton _resetToDefaultButton = new JButton("Reset to Defaults"); 118 _resetToDefaultButton.addActionListener(new ActionListener() { 119 public void actionPerformed(ActionEvent e) { resetToDefault(); } 120 }); 121 JPanel resetPanel = new JPanel(); 122 resetPanel.setLayout(new FlowLayout()); 123 resetPanel.setBorder(new EmptyBorder (5,5,5,5)); 124 resetPanel.add(_resetToDefaultButton); 125 panel.add(resetPanel, BorderLayout.SOUTH); 126 127 this.add(scroll, BorderLayout.CENTER); 128 } 129 130 133 public boolean update() { 134 135 for (int i = 0; i < _components.size();i++) { 136 boolean isValidUpdate = _components.get(i).updateConfig(); 137 if (! isValidUpdate) return false; 138 } 139 return true; 140 } 141 142 143 public void resetToCurrent() { 144 for (int i=0; i < _components.size(); i++) _components.get(i).resetToCurrent(); 145 } 146 147 148 public void resetToDefault() { 149 for (int i=0; i < _components.size(); i++) _components.get(i).resetToDefault(); 150 } 151 } 152 | Popular Tags |