1 48 49 package org.jfree.chart.ui; 50 51 import java.awt.BasicStroke ; 52 import java.awt.Color ; 53 import java.awt.event.ActionEvent ; 54 import java.awt.event.FocusEvent ; 55 import java.awt.event.FocusListener ; 56 import java.util.ResourceBundle ; 57 58 import javax.swing.BorderFactory ; 59 import javax.swing.JCheckBox ; 60 import javax.swing.JColorChooser ; 61 import javax.swing.JLabel ; 62 import javax.swing.JOptionPane ; 63 import javax.swing.JPanel ; 64 import javax.swing.JTabbedPane ; 65 import javax.swing.JTextField ; 66 67 import org.jfree.chart.axis.Axis; 68 import org.jfree.chart.axis.NumberAxis; 69 import org.jfree.layout.LCBLayout; 70 import org.jfree.ui.PaintSample; 71 import org.jfree.ui.StrokeChooserPanel; 72 import org.jfree.ui.StrokeSample; 73 74 77 class NumberAxisPropertyEditPanel extends AxisPropertyEditPanel 78 implements FocusListener { 79 80 83 private boolean autoRange; 84 85 86 private double minimumValue; 87 88 89 private double maximumValue; 90 91 94 private JCheckBox autoRangeCheckBox; 95 96 97 private JTextField minimumRangeValue; 98 99 100 private JTextField maximumRangeValue; 101 102 105 107 108 private PaintSample gridPaintSample; 109 110 111 private StrokeSample gridStrokeSample; 112 113 116 private StrokeSample[] availableStrokeSamples; 117 118 119 protected static ResourceBundle localizationResources = 120 ResourceBundle.getBundle("org.jfree.chart.ui.LocalizationBundle"); 121 122 123 128 public NumberAxisPropertyEditPanel(NumberAxis axis) { 129 130 super(axis); 131 132 this.autoRange = axis.isAutoRange(); 133 this.minimumValue = axis.getLowerBound(); 134 this.maximumValue = axis.getUpperBound(); 135 136 this.gridPaintSample = new PaintSample(Color.blue); 137 this.gridStrokeSample = new StrokeSample(new BasicStroke (1.0f)); 138 139 this.availableStrokeSamples = new StrokeSample[3]; 140 this.availableStrokeSamples[0] 141 = new StrokeSample(new BasicStroke (1.0f)); 142 this.availableStrokeSamples[1] 143 = new StrokeSample(new BasicStroke (2.0f)); 144 this.availableStrokeSamples[2] 145 = new StrokeSample(new BasicStroke (3.0f)); 146 147 JTabbedPane other = getOtherTabs(); 148 149 JPanel range = new JPanel (new LCBLayout(3)); 150 range.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); 151 152 range.add(new JPanel ()); 153 this.autoRangeCheckBox = new JCheckBox ( 154 localizationResources.getString("Auto-adjust_range"), this.autoRange 155 ); 156 this.autoRangeCheckBox.setActionCommand("AutoRangeOnOff"); 157 this.autoRangeCheckBox.addActionListener(this); 158 range.add(this.autoRangeCheckBox); 159 range.add(new JPanel ()); 160 161 range.add( 162 new JLabel (localizationResources.getString("Minimum_range_value")) 163 ); 164 this.minimumRangeValue = new JTextField ( 165 Double.toString(this.minimumValue) 166 ); 167 this.minimumRangeValue.setEnabled(!this.autoRange); 168 this.minimumRangeValue.setActionCommand("MinimumRange"); 169 this.minimumRangeValue.addActionListener(this); 170 this.minimumRangeValue.addFocusListener(this); 171 range.add(this.minimumRangeValue); 172 range.add(new JPanel ()); 173 174 range.add( 175 new JLabel (localizationResources.getString("Maximum_range_value")) 176 ); 177 this.maximumRangeValue = new JTextField ( 178 Double.toString(this.maximumValue) 179 ); 180 this.maximumRangeValue.setEnabled(!this.autoRange); 181 this.maximumRangeValue.setActionCommand("MaximumRange"); 182 this.maximumRangeValue.addActionListener(this); 183 this.maximumRangeValue.addFocusListener(this); 184 range.add(this.maximumRangeValue); 185 range.add(new JPanel ()); 186 187 other.add(localizationResources.getString("Range"), range); 188 189 } 190 191 196 public boolean isAutoRange() { 197 return this.autoRange; 198 } 199 200 205 public double getMinimumValue() { 206 return this.minimumValue; 207 } 208 209 214 public double getMaximumValue() { 215 return this.maximumValue; 216 } 217 218 222 public void actionPerformed(ActionEvent event) { 223 String command = event.getActionCommand(); 224 if (command.equals("GridStroke")) { 225 attemptGridStrokeSelection(); 226 } 227 else if (command.equals("GridPaint")) { 228 attemptGridPaintSelection(); 229 } 230 else if (command.equals("AutoRangeOnOff")) { 231 toggleAutoRange(); 232 } 233 else if (command.equals("MinimumRange")) { 234 validateMinimum(); 235 } 236 else if (command.equals("MaximumRange")) { 237 validateMaximum(); 238 } 239 else { 240 super.actionPerformed(event); 242 } 243 } 244 245 248 private void attemptGridStrokeSelection() { 249 StrokeChooserPanel panel = new StrokeChooserPanel( 250 null, this.availableStrokeSamples 251 ); 252 int result = JOptionPane.showConfirmDialog( 253 this, panel, localizationResources.getString("Stroke_Selection"), 254 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE 255 ); 256 257 if (result == JOptionPane.OK_OPTION) { 258 this.gridStrokeSample.setStroke(panel.getSelectedStroke()); 259 } 260 } 261 262 265 private void attemptGridPaintSelection() { 266 Color c; 267 c = JColorChooser.showDialog( 268 this, localizationResources.getString("Grid_Color"), Color.blue 269 ); 270 if (c != null) { 271 this.gridPaintSample.setPaint(c); 272 } 273 } 274 275 280 public void focusGained(FocusEvent event) { 281 } 283 284 289 public void focusLost(FocusEvent event) { 290 if (event.getSource() == this.minimumRangeValue) { 291 validateMinimum(); 292 } 293 else if (event.getSource() == this.maximumRangeValue) { 294 validateMaximum(); 295 } 296 } 297 298 301 public void toggleAutoRange() { 302 this.autoRange = this.autoRangeCheckBox.isSelected(); 303 if (this.autoRange) { 304 this.minimumRangeValue.setText(Double.toString(this.minimumValue)); 305 this.minimumRangeValue.setEnabled(false); 306 this.maximumRangeValue.setText(Double.toString(this.maximumValue)); 307 this.maximumRangeValue.setEnabled(false); 308 } 309 else { 310 this.minimumRangeValue.setEnabled(true); 311 this.maximumRangeValue.setEnabled(true); 312 } 313 } 314 315 318 public void validateMinimum() { 319 double newMin; 320 try { 321 newMin = Double.parseDouble(this.minimumRangeValue.getText()); 322 if (newMin >= this.maximumValue) { 323 newMin = this.minimumValue; 324 } 325 } 326 catch (NumberFormatException e) { 327 newMin = this.minimumValue; 328 } 329 330 this.minimumValue = newMin; 331 this.minimumRangeValue.setText(Double.toString(this.minimumValue)); 332 } 333 334 337 public void validateMaximum() { 338 double newMax; 339 try { 340 newMax = Double.parseDouble(this.maximumRangeValue.getText()); 341 if (newMax <= this.minimumValue) { 342 newMax = this.maximumValue; 343 } 344 } 345 catch (NumberFormatException e) { 346 newMax = this.maximumValue; 347 } 348 349 this.maximumValue = newMax; 350 this.maximumRangeValue.setText(Double.toString(this.maximumValue)); 351 } 352 353 359 public void setAxisProperties(Axis axis) { 360 super.setAxisProperties(axis); 361 NumberAxis numberAxis = (NumberAxis) axis; 362 numberAxis.setAutoRange(this.autoRange); 363 if (!this.autoRange) { 364 numberAxis.setRange(this.minimumValue, this.maximumValue); 365 } 366 } 367 368 } 369 | Popular Tags |