00001 package org.swtchart.ext.internal.properties;
00002
00003 import org.eclipse.jface.preference.ColorSelector;
00004 import org.eclipse.swt.SWT;
00005 import org.eclipse.swt.events.SelectionAdapter;
00006 import org.eclipse.swt.events.SelectionEvent;
00007 import org.eclipse.swt.graphics.Color;
00008 import org.eclipse.swt.graphics.Font;
00009 import org.eclipse.swt.graphics.FontData;
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.Button;
00014 import org.eclipse.swt.widgets.Composite;
00015 import org.eclipse.swt.widgets.Control;
00016 import org.eclipse.swt.widgets.Display;
00017 import org.eclipse.swt.widgets.Label;
00018 import org.eclipse.swt.widgets.Spinner;
00019 import org.swtchart.Constants;
00020 import org.swtchart.ILegend;
00021 import org.swtchart.ext.InteractiveChart;
00022
00026 public class LegendPage extends AbstractPage {
00027
00029 private static final String LEGEND_FONT = "org.swtchart.legend.font";
00030
00032 private static final String LEGEND_FOREGROUND = "org.swtchart.legend.foreground";
00033
00035 private static final String LEGEND_GACKGROUND = "org.swtchart.legend.background";
00036
00038 protected Button showLegendButton;
00039
00041 private Label backgroundLabel;
00042
00044 private ColorSelector backgroundButton;
00045
00047 private Label foregroundLabel;
00048
00050 private ColorSelector foregroundButton;
00051
00053 private Label fontSizeLabel;
00054
00056 private Spinner fontSizeSpinner;
00057
00059 private ILegend legend;
00060
00071 public LegendPage(InteractiveChart chart,
00072 PropertiesResources resources, String title) {
00073 super(chart, resources, title);
00074 legend = chart.getLegend();
00075 }
00076
00077
00078
00079
00080 @Override
00081 protected Control createContents(Composite parent) {
00082 Composite composite = new Composite(parent, SWT.NONE);
00083 GridLayout layout = new GridLayout();
00084 layout.marginHeight = 0;
00085 layout.marginWidth = 0;
00086 composite.setLayout(layout);
00087
00088 addLegendPanel(composite);
00089
00090 selectValues();
00091 return composite;
00092 }
00093
00100 private void addLegendPanel(Composite parent) {
00101
00102 Composite group = new Composite(parent, SWT.NONE);
00103 GridData gridData = new GridData();
00104 gridData.horizontalSpan = 2;
00105 group.setLayoutData(gridData);
00106 group.setLayout(new GridLayout(2, false));
00107
00108 showLegendButton = createCheckBoxControl(group, "Show legend");
00109 showLegendButton.addSelectionListener(new SelectionAdapter() {
00110 @Override
00111 public void widgetSelected(SelectionEvent e) {
00112 boolean visible = showLegendButton.getSelection();
00113 setControlsEnable(visible);
00114 }
00115 });
00116
00117 backgroundLabel = createLabelControl(group, "Background:");
00118 backgroundButton = createColorButtonControl(group);
00119
00120 foregroundLabel = createLabelControl(group, "Foreground:");
00121 foregroundButton = createColorButtonControl(group);
00122
00123 fontSizeLabel = createLabelControl(group, "Font size:");
00124 fontSizeSpinner = createSpinnerControl(group, 8, 30);
00125 }
00126
00130 private void selectValues() {
00131 showLegendButton.setSelection(legend.isVisible());
00132 setControlsEnable(legend.isVisible());
00133 backgroundButton.setColorValue(legend.getBackground().getRGB());
00134 foregroundButton.setColorValue(legend.getForeground().getRGB());
00135 fontSizeSpinner.setSelection(legend.getFont().getFontData()[0]
00136 .getHeight());
00137 }
00138
00145 protected void setControlsEnable(boolean enabled) {
00146 backgroundLabel.setEnabled(enabled);
00147 backgroundButton.setEnabled(enabled);
00148 foregroundLabel.setEnabled(enabled);
00149 foregroundButton.setEnabled(enabled);
00150 fontSizeLabel.setEnabled(enabled);
00151 fontSizeSpinner.setEnabled(enabled);
00152 }
00153
00154
00155
00156
00157 @Override
00158 public void apply() {
00159 legend.setVisible(showLegendButton.getSelection());
00160
00161 Color color = new Color(Display.getDefault(), backgroundButton
00162 .getColorValue());
00163 legend.setBackground(color);
00164 resources.put(LEGEND_GACKGROUND, color);
00165
00166 color = new Color(Display.getDefault(), foregroundButton
00167 .getColorValue());
00168 legend.setForeground(color);
00169 resources.put(LEGEND_FOREGROUND, color);
00170
00171 FontData fontData = legend.getFont().getFontData()[0];
00172 Font font = new Font(legend.getFont().getDevice(), fontData.getName(),
00173 fontSizeSpinner.getSelection(), fontData.getStyle());
00174 legend.setFont(font);
00175 resources.put(LEGEND_FONT, font);
00176 }
00177
00178
00179
00180
00181 @Override
00182 protected void performDefaults() {
00183 showLegendButton.setSelection(true);
00184 setControlsEnable(true);
00185
00186 backgroundButton.setColorValue(new RGB(255, 255, 255));
00187 foregroundButton.setColorValue(new RGB(0, 0, 0));
00188 fontSizeSpinner.setSelection(Constants.SMALL_FONT_SIZE);
00189
00190 super.performDefaults();
00191 }
00192 }