001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006     *
007     * Project Info:  http://www.jfree.org/jfreechart/index.html
008     *
009     * This library is free software; you can redistribute it and/or modify it 
010     * under the terms of the GNU Lesser General Public License as published by 
011     * the Free Software Foundation; either version 2.1 of the License, or 
012     * (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but 
015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017     * License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this library; if not, write to the Free Software
021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022     * USA.  
023     *
024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025     * in the United States and other countries.]
026     *
027     * -------------------
028     * SWTChartEditor.java
029     * -------------------
030     * (C) Copyright 2006, 2007, by Henry Proudhon and Contributors.
031     *
032     * Original Author:  Henry Proudhon (henry.proudhon AT ensmp.fr);
033     * Contributor(s):   David Gilbert (for Object Refinery Limited);
034     *
035     * Changes
036     * -------
037     * 01-Aug-2006 : New class (HP);
038     * 
039     */
040    
041    package org.jfree.experimental.chart.swt.editor;
042    
043    import java.util.ResourceBundle;
044    
045    import org.eclipse.swt.SWT;
046    import org.eclipse.swt.events.SelectionAdapter;
047    import org.eclipse.swt.events.SelectionEvent;
048    import org.eclipse.swt.layout.FillLayout;
049    import org.eclipse.swt.layout.GridData;
050    import org.eclipse.swt.layout.GridLayout;
051    import org.eclipse.swt.widgets.Button;
052    import org.eclipse.swt.widgets.Composite;
053    import org.eclipse.swt.widgets.Display;
054    import org.eclipse.swt.widgets.Shell;
055    import org.eclipse.swt.widgets.TabFolder;
056    import org.eclipse.swt.widgets.TabItem;
057    import org.jfree.chart.JFreeChart;
058    import org.jfree.chart.editor.ChartEditor;
059    
060    /**
061     * An editor for chart properties.
062     */
063    public class SWTChartEditor implements ChartEditor {
064        
065        /** The shell */
066        private Shell shell;
067        
068        /** The chart which the properties have to be edited */
069        private JFreeChart chart;
070        
071        /** A composite for displaying/editing the properties of the title. */
072        private SWTTitleEditor titleEditor;
073    
074        /** A composite for displaying/editing the properties of the plot. */
075        private SWTPlotEditor plotEditor;
076        
077        /** A composite for displaying/editing the other properties of the chart. */
078        private SWTOtherEditor otherEditor;
079    
080        /** The resourceBundle for the localization. */
081        protected static ResourceBundle localizationResources 
082            = ResourceBundle.getBundle("org.jfree.chart.editor.LocalizationBundle");
083        
084        /**
085         * Creates a new editor.
086         * 
087         * @param display  the display.
088         * @param chart2edit  the chart to edit.
089         */
090        public SWTChartEditor(Display display, JFreeChart chart2edit) {
091            this.shell = new Shell(display, SWT.DIALOG_TRIM);
092            this.shell.setSize(400, 500);
093            this.chart = chart2edit;
094            this.shell.setText(ResourceBundle.getBundle(
095                    "org.jfree.chart.LocalizationBundle").getString(
096                            "Chart_Properties"));
097            GridLayout layout = new GridLayout(2, true);
098            layout.marginLeft = layout.marginTop = layout.marginRight 
099                    = layout.marginBottom = 5;
100            this.shell.setLayout(layout);
101            Composite main = new Composite(this.shell, SWT.NONE);
102            main.setLayout(new FillLayout());
103            main.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
104            
105            TabFolder tab = new TabFolder(main, SWT.BORDER);
106            // build first tab
107            TabItem item1 = new TabItem(tab, SWT.NONE);
108            item1.setText(" " + localizationResources.getString("Title") + " ");
109            this.titleEditor = new SWTTitleEditor(tab, SWT.NONE, 
110                    this.chart.getTitle());
111            item1.setControl(this.titleEditor);
112            // build second tab
113            TabItem item2 = new TabItem(tab, SWT.NONE);
114            item2.setText(" " + localizationResources.getString( "Plot" ) + " ");
115            this.plotEditor = new SWTPlotEditor(tab, SWT.NONE, 
116                    this.chart.getPlot());
117            item2.setControl(this.plotEditor);
118            // build the third tab
119            TabItem item3 = new TabItem(tab, SWT.NONE);
120            item3.setText(" " + localizationResources.getString("Other") + " ");
121            this.otherEditor = new SWTOtherEditor(tab, SWT.NONE, this.chart);
122            item3.setControl(this.otherEditor);
123            
124            // ok and cancel buttons
125            Button ok = new Button(this.shell, SWT.PUSH | SWT.OK);
126            ok.setText(" Ok ");
127            ok.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
128            ok.addSelectionListener(new SelectionAdapter() {
129                public void widgetSelected(SelectionEvent e) {            
130                    updateChart(SWTChartEditor.this.chart);
131                    SWTChartEditor.this.shell.dispose();
132                }
133            });
134            Button cancel = new Button(this.shell, SWT.PUSH);
135            cancel.setText(" Cancel ");
136            cancel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
137            cancel.addSelectionListener(new SelectionAdapter() {
138                public void widgetSelected(SelectionEvent e) {            
139                    SWTChartEditor.this.shell.dispose();
140                }
141            } );
142        }
143        
144        /**
145         * Opens the editor.
146         */
147        public void open() {
148            this.shell.open();
149            this.shell.layout();
150            while (!this.shell.isDisposed()) {
151                if (!this.shell.getDisplay().readAndDispatch()) {
152                    this.shell.getDisplay().sleep();
153                }
154            }
155        }
156    
157        /**
158         * Updates the chart properties.
159         * 
160         * @param chart  the chart.
161         */
162        public void updateChart(JFreeChart chart)
163        {
164            this.titleEditor.setTitleProperties(chart);
165            this.plotEditor.updatePlotProperties(chart.getPlot());
166            this.otherEditor.updateChartProperties(chart );
167        }
168    
169    }