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.SelectionAdapter;
00008 import org.eclipse.swt.events.SelectionEvent;
00009 import org.eclipse.swt.graphics.Color;
00010 import org.eclipse.swt.graphics.RGB;
00011 import org.eclipse.swt.layout.GridData;
00012 import org.eclipse.swt.layout.GridLayout;
00013 import org.eclipse.swt.widgets.Combo;
00014 import org.eclipse.swt.widgets.Composite;
00015 import org.eclipse.swt.widgets.Display;
00016 import org.swtchart.IAxis;
00017 import org.swtchart.LineStyle;
00018 import org.swtchart.IAxis.Direction;
00019 import org.swtchart.ext.InteractiveChart;
00020
00024 public class GridPage extends AbstractSelectorPage {
00025
00027 private static final String GRID_FOREGROUND = "org.swtchart.grid.foreground";
00028
00030 private IAxis[] axes;
00031
00033 protected Combo styleCombo;
00034
00036 protected ColorSelector foregroundButton;
00037
00039 protected LineStyle[] styles;
00040
00042 protected RGB[] foregroundColors;
00043
00056 public GridPage(InteractiveChart chart,
00057 PropertiesResources resources, Direction direction,
00058 String title) {
00059 super(chart, resources, title, "Axes:");
00060 if (direction == Direction.X) {
00061 this.axes = chart.getAxisSet().getXAxes();
00062 } else if (direction == Direction.Y) {
00063 this.axes = chart.getAxisSet().getYAxes();
00064 }
00065 styles = new LineStyle[axes.length];
00066 foregroundColors = new RGB[axes.length];
00067 }
00068
00069
00070
00071
00072 @Override
00073 protected String[] getListItems() {
00074 String[] items = new String[axes.length];
00075 for (int i = 0; i < items.length; i++) {
00076 items[i] = String.valueOf(axes[i].getId());
00077 }
00078 return items;
00079 }
00080
00081
00082
00083
00084 @Override
00085 protected void selectInitialValues() {
00086 for (int i = 0; i < axes.length; i++) {
00087 styles[i] = axes[i].getGrid().getStyle();
00088 foregroundColors[i] = axes[i].getGrid().getForeground().getRGB();
00089 }
00090 }
00091
00092
00093
00094
00095 @Override
00096 protected void updateControlSelections() {
00097 styleCombo.setText(String.valueOf(styles[selectedIndex]));
00098 foregroundButton.setColorValue(foregroundColors[selectedIndex]);
00099 }
00100
00101
00102
00103
00104 @Override
00105 protected void addRightPanelContents(Composite parent) {
00106 addGridPanel(parent);
00107 }
00108
00115 private void addGridPanel(Composite parent) {
00116 Composite group = new Composite(parent, SWT.NONE);
00117 GridData gridData = new GridData();
00118 gridData.horizontalSpan = 2;
00119 group.setLayoutData(gridData);
00120 group.setLayout(new GridLayout(2, false));
00121
00122 createLabelControl(group, "Line style:");
00123 LineStyle[] values = LineStyle.values();
00124 String[] labels = new String[values.length];
00125 for (int i = 0; i < values.length; i++) {
00126 labels[i] = values[i].label;
00127 }
00128 styleCombo = createComboControl(group, labels);
00129 styleCombo.addSelectionListener(new SelectionAdapter() {
00130 @Override
00131 public void widgetSelected(SelectionEvent e) {
00132 String value = styleCombo.getText();
00133 LineStyle selectedStyle = LineStyle.NONE;
00134 for (LineStyle style : LineStyle.values()) {
00135 if (style.label.equals(value)) {
00136 selectedStyle = style;
00137 }
00138 }
00139 styles[selectedIndex] = selectedStyle;
00140 }
00141 });
00142
00143 createLabelControl(group, "Color:");
00144 foregroundButton = createColorButtonControl(group);
00145 foregroundButton.addListener(new IPropertyChangeListener() {
00146 public void propertyChange(PropertyChangeEvent event) {
00147 foregroundColors[selectedIndex] = foregroundButton
00148 .getColorValue();
00149 }
00150 });
00151 }
00152
00153
00154
00155
00156 @Override
00157 public void apply() {
00158 for (int i = 0; i < axes.length; i++) {
00159 axes[i].getGrid().setStyle(styles[i]);
00160 Color color = new Color(Display.getDefault(), foregroundColors[i]);
00161 axes[i].getGrid().setForeground(color);
00162 resources.put(GRID_FOREGROUND + axes[i].getDirection()
00163 + axes[i].getId(), color);
00164 }
00165 }
00166
00167
00168
00169
00170 @Override
00171 protected void performDefaults() {
00172 styles[selectedIndex] = LineStyle.DOT;
00173 foregroundColors[selectedIndex] = Display.getDefault().getSystemColor(
00174 SWT.COLOR_GRAY).getRGB();
00175
00176 updateControlSelections();
00177
00178 super.performDefaults();
00179 }
00180 }