1 41 42 package org.jfree.chart.editor; 43 44 import java.awt.BasicStroke ; 45 import java.awt.Color ; 46 import java.awt.event.ActionEvent ; 47 import java.awt.event.FocusEvent ; 48 import java.awt.event.FocusListener ; 49 import java.util.ResourceBundle ; 50 51 import javax.swing.BorderFactory ; 52 import javax.swing.JCheckBox ; 53 import javax.swing.JColorChooser ; 54 import javax.swing.JLabel ; 55 import javax.swing.JOptionPane ; 56 import javax.swing.JPanel ; 57 import javax.swing.JTabbedPane ; 58 import javax.swing.JTextField ; 59 60 import org.jfree.chart.axis.Axis; 61 import org.jfree.chart.axis.NumberAxis; 62 import org.jfree.layout.LCBLayout; 63 import org.jfree.ui.PaintSample; 64 import org.jfree.ui.StrokeChooserPanel; 65 import org.jfree.ui.StrokeSample; 66 67 70 class DefaultNumberAxisEditor extends DefaultAxisEditor 71 implements FocusListener { 72 73 76 private boolean autoRange; 77 78 79 private double minimumValue; 80 81 82 private double maximumValue; 83 84 87 private JCheckBox autoRangeCheckBox; 88 89 90 private JTextField minimumRangeValue; 91 92 93 private JTextField maximumRangeValue; 94 95 96 private PaintSample gridPaintSample; 97 98 99 private StrokeSample gridStrokeSample; 100 101 104 private StrokeSample[] availableStrokeSamples; 105 106 107 protected static ResourceBundle localizationResources = 108 ResourceBundle.getBundle("org.jfree.chart.editor.LocalizationBundle"); 109 110 115 public DefaultNumberAxisEditor(NumberAxis axis) { 116 117 super(axis); 118 119 this.autoRange = axis.isAutoRange(); 120 this.minimumValue = axis.getLowerBound(); 121 this.maximumValue = axis.getUpperBound(); 122 123 this.gridPaintSample = new PaintSample(Color.blue); 124 this.gridStrokeSample = new StrokeSample(new BasicStroke (1.0f)); 125 126 this.availableStrokeSamples = new StrokeSample[3]; 127 this.availableStrokeSamples[0] 128 = new StrokeSample(new BasicStroke (1.0f)); 129 this.availableStrokeSamples[1] 130 = new StrokeSample(new BasicStroke (2.0f)); 131 this.availableStrokeSamples[2] 132 = new StrokeSample(new BasicStroke (3.0f)); 133 134 JTabbedPane other = getOtherTabs(); 135 136 JPanel range = new JPanel (new LCBLayout(3)); 137 range.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); 138 139 range.add(new JPanel ()); 140 this.autoRangeCheckBox = new JCheckBox ( 141 localizationResources.getString("Auto-adjust_range"), this.autoRange 142 ); 143 this.autoRangeCheckBox.setActionCommand("AutoRangeOnOff"); 144 this.autoRangeCheckBox.addActionListener(this); 145 range.add(this.autoRangeCheckBox); 146 range.add(new JPanel ()); 147 148 range.add( 149 new JLabel (localizationResources.getString("Minimum_range_value")) 150 ); 151 this.minimumRangeValue = new JTextField ( 152 Double.toString(this.minimumValue) 153 ); 154 this.minimumRangeValue.setEnabled(!this.autoRange); 155 this.minimumRangeValue.setActionCommand("MinimumRange"); 156 this.minimumRangeValue.addActionListener(this); 157 this.minimumRangeValue.addFocusListener(this); 158 range.add(this.minimumRangeValue); 159 range.add(new JPanel ()); 160 161 range.add( 162 new JLabel (localizationResources.getString("Maximum_range_value")) 163 ); 164 this.maximumRangeValue = new JTextField ( 165 Double.toString(this.maximumValue) 166 ); 167 this.maximumRangeValue.setEnabled(!this.autoRange); 168 this.maximumRangeValue.setActionCommand("MaximumRange"); 169 this.maximumRangeValue.addActionListener(this); 170 this.maximumRangeValue.addFocusListener(this); 171 range.add(this.maximumRangeValue); 172 range.add(new JPanel ()); 173 174 other.add(localizationResources.getString("Range"), range); 175 176 } 177 178 183 public boolean isAutoRange() { 184 return this.autoRange; 185 } 186 187 192 public double getMinimumValue() { 193 return this.minimumValue; 194 } 195 196 201 public double getMaximumValue() { 202 return this.maximumValue; 203 } 204 205 209 public void actionPerformed(ActionEvent event) { 210 String command = event.getActionCommand(); 211 if (command.equals("GridStroke")) { 212 attemptGridStrokeSelection(); 213 } 214 else if (command.equals("GridPaint")) { 215 attemptGridPaintSelection(); 216 } 217 else if (command.equals("AutoRangeOnOff")) { 218 toggleAutoRange(); 219 } 220 else if (command.equals("MinimumRange")) { 221 validateMinimum(); 222 } 223 else if (command.equals("MaximumRange")) { 224 validateMaximum(); 225 } 226 else { 227 super.actionPerformed(event); 229 } 230 } 231 232 235 private void attemptGridStrokeSelection() { 236 StrokeChooserPanel panel = new StrokeChooserPanel( 237 null, this.availableStrokeSamples 238 ); 239 int result = JOptionPane.showConfirmDialog( 240 this, panel, localizationResources.getString("Stroke_Selection"), 241 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE 242 ); 243 244 if (result == JOptionPane.OK_OPTION) { 245 this.gridStrokeSample.setStroke(panel.getSelectedStroke()); 246 } 247 } 248 249 252 private void attemptGridPaintSelection() { 253 Color c; 254 c = JColorChooser.showDialog( 255 this, localizationResources.getString("Grid_Color"), Color.blue 256 ); 257 if (c != null) { 258 this.gridPaintSample.setPaint(c); 259 } 260 } 261 262 267 public void focusGained(FocusEvent event) { 268 } 270 271 276 public void focusLost(FocusEvent event) { 277 if (event.getSource() == this.minimumRangeValue) { 278 validateMinimum(); 279 } 280 else if (event.getSource() == this.maximumRangeValue) { 281 validateMaximum(); 282 } 283 } 284 285 288 public void toggleAutoRange() { 289 this.autoRange = this.autoRangeCheckBox.isSelected(); 290 if (this.autoRange) { 291 this.minimumRangeValue.setText(Double.toString(this.minimumValue)); 292 this.minimumRangeValue.setEnabled(false); 293 this.maximumRangeValue.setText(Double.toString(this.maximumValue)); 294 this.maximumRangeValue.setEnabled(false); 295 } 296 else { 297 this.minimumRangeValue.setEnabled(true); 298 this.maximumRangeValue.setEnabled(true); 299 } 300 } 301 302 305 public void validateMinimum() { 306 double newMin; 307 try { 308 newMin = Double.parseDouble(this.minimumRangeValue.getText()); 309 if (newMin >= this.maximumValue) { 310 newMin = this.minimumValue; 311 } 312 } 313 catch (NumberFormatException e) { 314 newMin = this.minimumValue; 315 } 316 317 this.minimumValue = newMin; 318 this.minimumRangeValue.setText(Double.toString(this.minimumValue)); 319 } 320 321 324 public void validateMaximum() { 325 double newMax; 326 try { 327 newMax = Double.parseDouble(this.maximumRangeValue.getText()); 328 if (newMax <= this.minimumValue) { 329 newMax = this.maximumValue; 330 } 331 } 332 catch (NumberFormatException e) { 333 newMax = this.maximumValue; 334 } 335 336 this.maximumValue = newMax; 337 this.maximumRangeValue.setText(Double.toString(this.maximumValue)); 338 } 339 340 346 public void setAxisProperties(Axis axis) { 347 super.setAxisProperties(axis); 348 NumberAxis numberAxis = (NumberAxis) axis; 349 numberAxis.setAutoRange(this.autoRange); 350 if (!this.autoRange) { 351 numberAxis.setRange(this.minimumValue, this.maximumValue); 352 } 353 } 354 355 } 356 | Popular Tags |