View Javadoc
1 /* 2 * Created on Nov 13, 2003 3 */ 4 package portaview.view; 5 6 import java.awt.BorderLayout; 7 import java.awt.Color; 8 import java.awt.Component; 9 import java.awt.Dimension; 10 import java.awt.FlowLayout; 11 import java.awt.GridBagConstraints; 12 import java.awt.GridBagLayout; 13 import java.awt.Insets; 14 import java.awt.Point; 15 import java.awt.event.ActionEvent; 16 import java.awt.event.ActionListener; 17 import java.util.Dictionary; 18 import java.util.Enumeration; 19 import java.util.Hashtable; 20 21 import javax.swing.JButton; 22 import javax.swing.JColorChooser; 23 import javax.swing.JDialog; 24 import javax.swing.JFrame; 25 import javax.swing.JLabel; 26 import javax.swing.JPanel; 27 import javax.swing.JSlider; 28 import javax.swing.border.TitledBorder; 29 import javax.swing.event.ChangeEvent; 30 import javax.swing.event.ChangeListener; 31 32 import portaview.Registry; 33 import portaview.model.PortaViewModel; 34 import portaview.model.SlideSettings; 35 36 /*** 37 * Represents a dialog that one can set the slide settings. 38 * @author William Lee 39 */ 40 public class SettingsDialog extends JDialog 41 { 42 private PortaViewModel pvm = null; 43 private JSlider speedSlider = null; 44 private JSlider borderSlider = null; 45 private JSlider transSlider = null; 46 private JPanel transPanel = null; 47 private JButton colorButton = null; 48 private JPanel colorButtonPanel = null; 49 private SettingsPreview previewPanel = null; 50 51 public SettingsDialog(JFrame parent, PortaViewModel m) 52 { 53 super(parent, "Border and Play Settings for " + m.getName(), true); 54 pvm = m; 55 // calculate where to put the dialog. The guideline says one 56 // should place the dialog at approx. 1/5 below main application. 57 Point p = new Point(Registry.getMainApplication().getLocation()); 58 Dimension d = new Dimension(Registry.getMainApplication().getSize()); 59 p.x = p.x + (d.width / 3) + 50; 60 p.y = p.y + (d.height / 5); 61 setLocation(p); 62 setResizable(false); 63 getContentPane().setLayout(new BorderLayout()); 64 initSliders(); 65 initButtons(); 66 } 67 68 private void initButtons() 69 { 70 final JDialog diag = this; 71 final JSlider sslider = speedSlider; 72 final JSlider bslider = borderSlider; 73 JButton okButton = new JButton("OK"); 74 okButton.addActionListener(new ActionListener() 75 { 76 public void actionPerformed(ActionEvent e) 77 { 78 pvm.getSlideSettings().setSpeed(sslider.getValue()); 79 pvm.getSlideSettings().setBorder(bslider.getValue()); 80 pvm.getSlideSettings().setBorderColor( 81 colorButton.getBackground()); 82 pvm.getSlideSettings().setTransparency(transSlider.getValue()); 83 pvm.forceNotifyObservers(); 84 diag.dispose(); 85 } 86 }); 87 JButton cButton = new JButton("Cancel"); 88 cButton.addActionListener(new ActionListener() 89 { 90 public void actionPerformed(ActionEvent e) 91 { 92 diag.dispose(); 93 } 94 }); 95 JPanel p = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 96 p.add(okButton); 97 p.add(cButton); 98 getContentPane().add(p, BorderLayout.SOUTH); 99 } 100 101 /*** 102 * Initialize the sliders. 103 * 104 */ 105 private void initSliders() 106 { 107 SlideSettings ss = pvm.getSlideSettings(); 108 JPanel speedPanel = initSpeedPanel(ss); 109 JPanel borderPanel = initMainBorderPanel(pvm); 110 JPanel content = new JPanel(new GridBagLayout()); 111 GridBagConstraints c = new GridBagConstraints(); 112 c.gridx = 0; 113 c.gridy = 0; 114 c.fill = GridBagConstraints.BOTH; 115 content.add(speedPanel, c); 116 c.gridx = 0; 117 c.gridy = 1; 118 c.fill = GridBagConstraints.BOTH; 119 content.add(borderPanel, c); 120 getContentPane().add(content, BorderLayout.CENTER); 121 } 122 123 private JPanel initMainBorderPanel(PortaViewModel pvm) 124 { 125 SlideSettings ss = pvm.getSlideSettings(); 126 JPanel borderPanel = new JPanel(new GridBagLayout()); 127 borderPanel.setBorder(new TitledBorder("Picture Border")); 128 // note that we need to initialize the transPanel and getColorPanel first 129 // for we need to use the sliders in the borderSliderPanel 130 JPanel transPanel = initTransPanel(ss); 131 JPanel getColorPanel = initColorPanel(ss); 132 JPanel borderSliderPanel = initBorderPanel(ss); 133 JPanel ppanel = initPreviewPanel(pvm); 134 135 GridBagConstraints c = new GridBagConstraints(); 136 // put the border panel 137 c.gridx = 0; 138 c.gridy = 0; 139 c.fill = GridBagConstraints.BOTH; 140 borderPanel.add(borderSliderPanel, c); 141 // put the preview panel 142 c.gridx = 1; 143 c.gridy = 0; 144 c.gridheight = 3; 145 c.gridwidth = 1; 146 c.fill = GridBagConstraints.NONE; 147 c.anchor = GridBagConstraints.CENTER; 148 c.insets = new Insets(10, 10, 10, 10); 149 borderPanel.add(ppanel, c); 150 // put the other two 151 c.gridx = 0; 152 c.gridy = 1; 153 c.gridheight = 1; 154 c.gridwidth = 1; 155 c.fill = GridBagConstraints.BOTH; 156 c.insets = new Insets(0, 0, 0, 0); 157 borderPanel.add(getColorPanel, c); 158 c.gridx = 0; 159 c.gridy = 2; 160 c.gridheight = 1; 161 c.gridwidth = 1; 162 c.fill = GridBagConstraints.BOTH; 163 c.ipadx = 0; 164 c.ipady = 0; 165 borderPanel.add(transPanel, c); 166 return borderPanel; 167 } 168 169 private JPanel initPreviewPanel(PortaViewModel pvm) 170 { 171 previewPanel = 172 new SettingsPreview( 173 pvm.getImageModel().getImage(), 174 borderSlider, 175 transSlider, 176 colorButton); 177 borderSlider.addChangeListener(previewPanel); 178 transSlider.addChangeListener(previewPanel); 179 return previewPanel; 180 } 181 182 private JPanel initTransPanel(SlideSettings ss) 183 { 184 // panel for getting transparency. 185 transPanel = new JPanel(); 186 transPanel.setBorder(new TitledBorder("Border Transparency")); 187 transSlider = new JSlider(1, 10, ss.getTransparency()); 188 transSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE); 189 transSlider.setPaintTicks(true); 190 transSlider.setMinorTickSpacing(1); 191 transSlider.setPaintLabels(true); 192 transSlider.setSnapToTicks(true); 193 194 // make the custom label 195 Dictionary tlabels = new Hashtable(); 196 tlabels.put(new Integer(1), new JLabel("Transparent")); 197 tlabels.put(new Integer(10), new JLabel("Solid")); 198 transSlider.setLabelTable(tlabels); 199 transPanel.add(transSlider); 200 return transPanel; 201 } 202 203 private JPanel initColorPanel(SlideSettings ss) 204 { 205 colorButtonPanel = new JPanel(new GridBagLayout()); 206 colorButtonPanel.setBorder(new TitledBorder("Border Color")); 207 GridBagConstraints cc = new GridBagConstraints(); 208 cc.fill = GridBagConstraints.BOTH; 209 cc.gridx = 0; 210 cc.gridy = 0; 211 cc.weightx = 1; 212 cc.weighty = 1; 213 colorButton = new JButton(" "); 214 colorButton.setBackground(ss.getBorderColor()); 215 colorButton.setFocusable(false); 216 colorButtonPanel.add(colorButton, cc); 217 final JButton getColor = colorButton; 218 getColor.addActionListener(new ActionListener() 219 { 220 221 public void actionPerformed(ActionEvent e) 222 { 223 Color c; 224 c = 225 JColorChooser.showDialog( 226 ((Component) e.getSource()).getParent(), 227 "Border Color", 228 getColor.getBackground()); 229 getColor.setBackground(c); 230 // repaint the preview panel. 231 previewPanel.repaint(); 232 233 } 234 }); 235 return colorButtonPanel; 236 } 237 238 private JPanel initBorderPanel(SlideSettings ss) 239 { 240 JPanel borderSliderPanel = new JPanel(); 241 borderSliderPanel.setBorder(new TitledBorder("Border Width")); 242 borderSlider = new JSlider(0, 16, ss.getBorder()); 243 borderSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE); 244 borderSlider.setPaintTicks(true); 245 borderSlider.setMinorTickSpacing(2); 246 borderSlider.setPaintLabels(true); 247 borderSlider.setSnapToTicks(true); 248 249 // make the custom label 250 Dictionary labels = new Hashtable(); 251 labels.put(new Integer(0), new JLabel("None")); 252 labels.put(new Integer(8), new JLabel("Medium")); 253 labels.put(new Integer(16), new JLabel("Thick")); 254 borderSlider.setLabelTable(labels); 255 borderSliderPanel.add(borderSlider); 256 // add the listener so we can disable the color button and the 257 // transparency slider if border is set to none 258 borderSlider.addChangeListener(new ChangeListener() 259 { 260 public void stateChanged(ChangeEvent e) 261 { 262 updateBorderSldiers(); 263 } 264 }); 265 updateBorderSldiers(); 266 return borderSliderPanel; 267 } 268 269 /*** 270 * Disables or enables the color button and transparency slider 271 * according to the value of the border slider. 272 * 273 */ 274 private void updateBorderSldiers() 275 { 276 if (borderSlider.getValue() == 0) 277 { 278 // disable the buttons and the sliders for transparency 279 colorButton.setEnabled(false); 280 colorButton.setBackground(Color.GRAY); 281 colorButtonPanel.setEnabled(false); 282 ((TitledBorder) colorButtonPanel.getBorder()).setTitleColor( 283 Color.GRAY); 284 transSlider.setEnabled(false); 285 transPanel.setEnabled(false); 286 ((TitledBorder) transPanel.getBorder()).setTitleColor(Color.GRAY); 287 disableLabels(transSlider); 288 } 289 else 290 { 291 // enable the buttons and the sliders for transparency 292 colorButton.setEnabled(true); 293 colorButtonPanel.setEnabled(true); 294 ((TitledBorder) colorButtonPanel.getBorder()).setTitleColor( 295 Color.BLACK); 296 transSlider.setEnabled(true); 297 transPanel.setEnabled(true); 298 ((TitledBorder) transPanel.getBorder()).setTitleColor(Color.BLACK); 299 enableLabels(transSlider); 300 } 301 } 302 303 /*** 304 * Disables the labels here 305 * @param slider 306 */ 307 private void disableLabels(JSlider slider) 308 { 309 changeLabelsColor(slider, Color.GRAY); 310 } 311 312 /*** 313 * Enables the labels for the slider. 314 * @param slider 315 */ 316 private void enableLabels(JSlider slider) 317 { 318 changeLabelsColor(slider, Color.BLACK); 319 } 320 321 private void changeLabelsColor(JSlider slider, Color c) 322 { 323 Dictionary labels = slider.getLabelTable(); 324 for (Enumeration en = labels.keys(); en.hasMoreElements();) 325 { 326 JLabel l = (JLabel) labels.get(en.nextElement()); 327 l.setForeground(c); 328 } 329 } 330 331 private JPanel initSpeedPanel(SlideSettings ss) 332 { 333 JPanel speedPanel = new JPanel(new BorderLayout()); 334 speedPanel.setBorder(new TitledBorder("Album Play Speed (seconds)")); 335 speedSlider = new JSlider(10, 30, ss.getSpeed()); 336 speedSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE); 337 speedSlider.setPaintTicks(true); 338 speedSlider.setMinorTickSpacing(5); 339 340 Dictionary slabels = new Hashtable(); 341 slabels.put(new Integer(5), new JLabel("5")); 342 slabels.put(new Integer(10), new JLabel("10")); 343 slabels.put(new Integer(15), new JLabel("15")); 344 slabels.put(new Integer(20), new JLabel("20")); 345 slabels.put(new Integer(25), new JLabel("25")); 346 slabels.put(new Integer(30), new JLabel("30")); 347 speedSlider.setLabelTable(slabels); 348 349 speedSlider.setPaintLabels(true); 350 speedSlider.setSnapToTicks(true); 351 speedPanel.add(speedSlider); 352 return speedPanel; 353 } 354 355 }

This page was automatically generated by Maven