00001 package org.swtchart.ext.internal.properties;
00002
00003 import org.eclipse.jface.preference.ColorSelector;
00004 import org.eclipse.jface.util.IPropertyChangeListener;
00005 import org.eclipse.jface.util.PropertyChangeEvent;
00006 import org.eclipse.swt.SWT;
00007 import org.eclipse.swt.events.FocusAdapter;
00008 import org.eclipse.swt.events.FocusEvent;
00009 import org.eclipse.swt.events.ModifyEvent;
00010 import org.eclipse.swt.events.ModifyListener;
00011 import org.eclipse.swt.events.SelectionAdapter;
00012 import org.eclipse.swt.events.SelectionEvent;
00013 import org.eclipse.swt.graphics.Color;
00014 import org.eclipse.swt.graphics.Font;
00015 import org.eclipse.swt.graphics.FontData;
00016 import org.eclipse.swt.graphics.RGB;
00017 import org.eclipse.swt.layout.GridLayout;
00018 import org.eclipse.swt.widgets.Button;
00019 import org.eclipse.swt.widgets.Combo;
00020 import org.eclipse.swt.widgets.Composite;
00021 import org.eclipse.swt.widgets.Display;
00022 import org.eclipse.swt.widgets.Event;
00023 import org.eclipse.swt.widgets.Group;
00024 import org.eclipse.swt.widgets.Label;
00025 import org.eclipse.swt.widgets.Spinner;
00026 import org.eclipse.swt.widgets.Text;
00027 import org.swtchart.Constants;
00028 import org.swtchart.IAxis;
00029 import org.swtchart.IDisposeListener;
00030 import org.swtchart.Range;
00031 import org.swtchart.IAxis.Direction;
00032 import org.swtchart.IAxis.Position;
00033 import org.swtchart.ext.InteractiveChart;
00034
00038 public class AxisPage extends AbstractSelectorPage {
00039
00041 private static final String AXIS_TITLE_FONT = "org.swtchart.axis.title.font";
00042
00044 private static final String AXIS_TITLE_FOREGROUND = "org.swtchart.axis.title.foreground";
00045
00047 private IAxis[] axes;
00048
00050 private Direction direction;
00051
00053 protected Button showTitleButton;
00054
00056 private Label titleLabel;
00057
00059 protected Text titleText;
00060
00062 private Label fontSizeLabel;
00063
00065 protected Spinner fontSizeSpinner;
00066
00068 private Label titleColorLabel;
00069
00071 protected ColorSelector titleColorButton;
00072
00074 protected Text minRangeText;
00075
00077 protected Text maxRangeText;
00078
00080 protected Combo positionCombo;
00081
00083 protected Button categoryButton;
00084
00086 protected Button logScaleButton;
00087
00089 protected boolean[] titleVisibleStates;
00090
00092 protected String[] titleTexts;
00093
00095 protected int[] titleFontSizes;
00096
00098 protected RGB[] titleColors;
00099
00101 protected double[] minRanges;
00102
00104 protected double[] maxRanges;
00105
00107 protected Position[] positions;
00108
00110 protected boolean[] categoryStates;
00111
00113 protected boolean[] logScaleStates;
00114
00127 public AxisPage(InteractiveChart chart, PropertiesResources resources,
00128 Direction direction, String title) {
00129 super(chart, resources, title, "Axes:");
00130 this.direction = direction;
00131 if (direction == Direction.X) {
00132 this.axes = chart.getAxisSet().getXAxes();
00133 } else if (direction == Direction.Y) {
00134 this.axes = chart.getAxisSet().getYAxes();
00135 }
00136
00137 titleVisibleStates = new boolean[axes.length];
00138 titleTexts = new String[axes.length];
00139 titleFontSizes = new int[axes.length];
00140 titleColors = new RGB[axes.length];
00141 minRanges = new double[axes.length];
00142 maxRanges = new double[axes.length];
00143 positions = new Position[axes.length];
00144 if (direction == Direction.X) {
00145 categoryStates = new boolean[axes.length];
00146 }
00147 logScaleStates = new boolean[axes.length];
00148 }
00149
00150
00151
00152
00153 @Override
00154 protected String[] getListItems() {
00155 String[] items = new String[axes.length];
00156 for (int i = 0; i < items.length; i++) {
00157 items[i] = String.valueOf(axes[i].getId());
00158 }
00159 return items;
00160 }
00161
00162
00163
00164
00165 @Override
00166 protected void selectInitialValues() {
00167 for (int i = 0; i < axes.length; i++) {
00168 titleVisibleStates[i] = axes[i].getTitle().isVisible();
00169 titleTexts[i] = axes[i].getTitle().getText();
00170 titleFontSizes[i] = axes[i].getTitle().getFont().getFontData()[0]
00171 .getHeight();
00172 titleColors[i] = axes[i].getTitle().getForeground().getRGB();
00173 minRanges[i] = axes[i].getRange().lower;
00174 maxRanges[i] = axes[i].getRange().upper;
00175 positions[i] = axes[i].getPosition();
00176 if (direction == Direction.X) {
00177 categoryStates[i] = axes[i].isCategoryEnabled();
00178 }
00179 logScaleStates[i] = axes[i].isLogScaleEnabled();
00180 }
00181 }
00182
00183
00184
00185
00186 @Override
00187 protected void updateControlSelections() {
00188 showTitleButton.setSelection(titleVisibleStates[selectedIndex]);
00189 setControlsEnable(titleVisibleStates[selectedIndex]);
00190 titleText.setText(titleTexts[selectedIndex]);
00191 fontSizeSpinner.setSelection(titleFontSizes[selectedIndex]);
00192 titleColorButton.setColorValue(titleColors[selectedIndex]);
00193
00194 minRangeText.setText(String.valueOf(minRanges[selectedIndex]));
00195 maxRangeText.setText(String.valueOf(maxRanges[selectedIndex]));
00196 positionCombo.setText(String.valueOf(positions[selectedIndex]));
00197 logScaleButton.setSelection(logScaleStates[selectedIndex]);
00198 if (direction == Direction.X) {
00199 categoryButton.setSelection(categoryStates[selectedIndex]);
00200 }
00201 }
00202
00203
00204
00205
00206 @Override
00207 protected void addRightPanelContents(Composite parent) {
00208 addAxisPanel(parent);
00209 addTitleGroup(parent);
00210 }
00211
00218 private void addAxisPanel(Composite parent) {
00219 Composite group = new Composite(parent, SWT.NONE);
00220 group.setLayout(new GridLayout(2, true));
00221
00222 createLabelControl(group, "Minimum range value:");
00223 minRangeText = createTextControl(group);
00224 minRangeText.addFocusListener(new FocusAdapter() {
00225 @Override
00226 public void focusLost(FocusEvent e) {
00227 minRanges[selectedIndex] = Double.valueOf(minRangeText
00228 .getText());
00229 }
00230 });
00231
00232 createLabelControl(group, "Maximum range value:");
00233 maxRangeText = createTextControl(group);
00234 maxRangeText.addFocusListener(new FocusAdapter() {
00235 @Override
00236 public void focusLost(FocusEvent e) {
00237 maxRanges[selectedIndex] = Double.valueOf(maxRangeText
00238 .getText());
00239 }
00240 });
00241
00242 createLabelControl(group, "Position:");
00243 String[] items = new String[] { Position.Primary.name(),
00244 Position.Secondary.name() };
00245
00246 positionCombo = createComboControl(group, items);
00247 positionCombo.addSelectionListener(new SelectionAdapter() {
00248 @Override
00249 public void widgetSelected(SelectionEvent e) {
00250 positions[selectedIndex] = Position.valueOf(positionCombo
00251 .getText());
00252 }
00253 });
00254
00255 logScaleButton = createCheckBoxControl(group, "Enable log scale");
00256 logScaleButton.addSelectionListener(new SelectionAdapter() {
00257 @Override
00258 public void widgetSelected(SelectionEvent e) {
00259 logScaleStates[selectedIndex] = logScaleButton.getSelection();
00260 }
00261 });
00262
00263 if (direction == Direction.X) {
00264 categoryButton = createCheckBoxControl(group, "Enable category");
00265 categoryButton.addSelectionListener(new SelectionAdapter() {
00266 @Override
00267 public void widgetSelected(SelectionEvent e) {
00268 categoryStates[selectedIndex] = categoryButton
00269 .getSelection();
00270 }
00271 });
00272 }
00273 }
00274
00281 private void addTitleGroup(Composite parent) {
00282
00283 Group group = createGroupControl(parent, "Title:", false);
00284
00285 showTitleButton = createCheckBoxControl(group, "Show title");
00286 showTitleButton.addSelectionListener(new SelectionAdapter() {
00287 @Override
00288 public void widgetSelected(SelectionEvent e) {
00289 boolean visible = showTitleButton.getSelection();
00290 titleVisibleStates[selectedIndex] = visible;
00291 setControlsEnable(visible);
00292 }
00293 });
00294
00295 titleLabel = createLabelControl(group, "Text:");
00296 titleText = createTextControl(group);
00297 titleText.addModifyListener(new ModifyListener() {
00298 public void modifyText(ModifyEvent e) {
00299 titleTexts[selectedIndex] = titleText.getText();
00300 }
00301 });
00302
00303 fontSizeLabel = createLabelControl(group, "Font size:");
00304 fontSizeSpinner = createSpinnerControl(group, 8, 30);
00305 fontSizeSpinner.addSelectionListener(new SelectionAdapter() {
00306 @Override
00307 public void widgetSelected(SelectionEvent e) {
00308 titleFontSizes[selectedIndex] = fontSizeSpinner.getSelection();
00309 }
00310 });
00311
00312 titleColorLabel = createLabelControl(group, "Color:");
00313 titleColorButton = createColorButtonControl(group);
00314 titleColorButton.addListener(new IPropertyChangeListener() {
00315 public void propertyChange(PropertyChangeEvent event) {
00316 titleColors[selectedIndex] = titleColorButton.getColorValue();
00317 }
00318 });
00319 }
00320
00327 protected void setControlsEnable(boolean enabled) {
00328 titleLabel.setEnabled(enabled);
00329 titleText.setEnabled(enabled);
00330 fontSizeLabel.setEnabled(enabled);
00331 fontSizeSpinner.setEnabled(enabled);
00332 titleColorLabel.setEnabled(enabled);
00333 titleColorButton.setEnabled(enabled);
00334 }
00335
00336
00337
00338
00339 @Override
00340 public void apply() {
00341 for (int i = 0; i < axes.length; i++) {
00342 axes[i].getTitle().setVisible(titleVisibleStates[i]);
00343 axes[i].getTitle().setText(titleTexts[i]);
00344
00345 FontData fontData = axes[i].getTitle().getFont().getFontData()[0];
00346 fontData.setHeight(titleFontSizes[i]);
00347 Font font = new Font(Display.getDefault(), fontData);
00348 axes[i].getTitle().setFont(font);
00349 final String fontKey = AXIS_TITLE_FONT + axes[i].getDirection()
00350 + axes[i].getId();
00351 if (resources.getFont(fontKey) == null) {
00352 axes[i].addDisposeListener(new IDisposeListener() {
00353 public void disposed(Event e) {
00354 resources.removeFont(fontKey);
00355 }
00356 });
00357 }
00358 resources.put(fontKey, font);
00359
00360 Color color = new Color(Display.getDefault(), titleColors[i]);
00361 axes[i].getTitle().setForeground(color);
00362 final String colorKey = AXIS_TITLE_FOREGROUND
00363 + axes[i].getDirection() + axes[i].getId();
00364 if (resources.getColor(colorKey) == null) {
00365 axes[i].addDisposeListener(new IDisposeListener() {
00366 public void disposed(Event e) {
00367 resources.removeColor(colorKey);
00368 }
00369 });
00370 }
00371 resources.put(colorKey, color);
00372
00373 axes[i].setRange(new Range(minRanges[i], maxRanges[i]));
00374 axes[i].setPosition(positions[i]);
00375 try {
00376 axes[i].enableLogScale(logScaleStates[i]);
00377 } catch (IllegalStateException e) {
00378 axes[i].enableLogScale(false);
00379 logScaleButton.setSelection(false);
00380 }
00381 if (direction == Direction.X) {
00382 axes[i].enableCategory(categoryStates[i]);
00383 }
00384 }
00385 }
00386
00387
00388
00389
00390 @Override
00391 protected void performDefaults() {
00392 titleVisibleStates[selectedIndex] = true;
00393 if (direction == Direction.X) {
00394 titleTexts[selectedIndex] = "X Axis";
00395 categoryStates[selectedIndex] = false;
00396 } else if (direction == Direction.Y) {
00397 titleTexts[selectedIndex] = "Y Axis";
00398 }
00399 positions[selectedIndex] = Position.Primary;
00400 titleFontSizes[selectedIndex] = Constants.MEDIUM_FONT_SIZE;
00401 titleColors[selectedIndex] = Display.getDefault().getSystemColor(
00402 SWT.COLOR_BLUE).getRGB();
00403
00404 minRanges[selectedIndex] = 0.0;
00405 maxRanges[selectedIndex] = 1.0;
00406 logScaleStates[selectedIndex] = false;
00407
00408 updateControlSelections();
00409
00410 super.performDefaults();
00411 }
00412 }