001: package net.sf.jmoney.charts;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.FlowLayout;
006: import java.awt.GridBagConstraints;
007: import java.awt.GridBagLayout;
008: import java.awt.Panel;
009: import java.awt.event.ActionEvent;
010: import java.awt.event.ActionListener;
011: import java.util.Calendar;
012: import java.util.Date;
013:
014: import javax.swing.JSpinner;
015: import javax.swing.JTextField;
016: import javax.swing.SpinnerDateModel;
017: import javax.swing.event.ChangeEvent;
018: import javax.swing.event.ChangeListener;
019:
020: import net.sf.jmoney.IBookkeepingPage;
021: import net.sf.jmoney.IBookkeepingPageFactory;
022: import net.sf.jmoney.JMoneyPlugin;
023: import net.sf.jmoney.model2.Session;
024: import net.sf.jmoney.views.NodeEditor;
025: import net.sf.jmoney.views.SectionlessPage;
026:
027: import org.eclipse.swt.SWT;
028: import org.eclipse.swt.awt.SWT_AWT;
029: import org.eclipse.swt.widgets.Composite;
030: import org.eclipse.ui.IMemento;
031: import org.eclipse.ui.PartInitException;
032: import org.eclipse.ui.forms.widgets.FormToolkit;
033: import org.jfree.chart.ChartPanel;
034:
035: /**
036: * @author Faucheux
037: */
038: public class PieChartPage implements IBookkeepingPageFactory {
039:
040: private static final String PAGE_ID = "net.sf.jmoney.charts.pieChart";
041:
042: protected ExpensePieChart chart;
043: protected JSpinner fromDate;
044: protected JSpinner toDate;
045: protected JTextField maxLevel;
046:
047: /* (non-Javadoc)
048: * @see net.sf.jmoney.IBookkeepingPageFactory#createPages(java.lang.Object, org.eclipse.swt.widgets.Composite)
049: */
050: public Composite createContent(Session session, Composite parent) {
051:
052: // The chart
053: Composite swingComposite = new Composite(parent, SWT.EMBEDDED);
054: final java.awt.Panel pagePanel;
055: {
056: // Create the pagePanel and register it as display of the plugin
057: java.awt.Frame accountEntriesFrame = SWT_AWT
058: .new_Frame(swingComposite);
059: pagePanel = new java.awt.Panel(new BorderLayout());
060: pagePanel.setBackground(Color.pink);
061: accountEntriesFrame.add(pagePanel);
062:
063: // Create the optionsPanel and add it to pagePanel
064: Panel optionsPanel = new Panel(new FlowLayout());
065: pagePanel.add(optionsPanel, BorderLayout.NORTH);
066: optionsPanel.setBackground(Color.magenta);
067:
068: // Create the graphPanel and add it to pagePanel
069: GridBagLayout gbLayout = new GridBagLayout();
070: Panel graphPanel = new Panel(gbLayout);
071: pagePanel.add(graphPanel, BorderLayout.CENTER);
072: GridBagConstraints gbConstraints = new GridBagConstraints();
073: graphPanel.setBackground(Color.GREEN);
074:
075: // Create the (main) graph and add it to graphPanel
076: chart = new ExpensePieChart("myChart", session, 1);
077: chart.run();
078: ChartPanel chartPanel = chart.getChartPanel();
079: gbConstraints.fill = GridBagConstraints.BOTH;
080: gbConstraints.anchor = GridBagConstraints.NORTHWEST;
081: gbConstraints.weightx = 1;
082: gbConstraints.weighty = 1;
083: gbConstraints.gridwidth = 3;
084: gbLayout.setConstraints(chartPanel, gbConstraints);
085: graphPanel.add(chartPanel);
086: chartPanel.addChartMouseListener(chart);
087: chart.setContainerForSeveralGraphics(graphPanel);
088:
089: // final TextField fromDate = new TextField("from"); optionsPanel.add(fromDate);
090: fromDate = new JSpinner(new SpinnerDateModel(new Date(0),
091: null, null, Calendar.DATE));
092: optionsPanel.add(fromDate);
093: fromDate.addChangeListener(new redrawGraph());
094:
095: toDate = new JSpinner(new SpinnerDateModel(new Date(),
096: null, null, Calendar.MONTH));
097: optionsPanel.add(toDate);
098: toDate.addChangeListener(new redrawGraph());
099:
100: maxLevel = new JTextField("2");
101: optionsPanel.add(maxLevel);
102: maxLevel.addActionListener(new redrawGraph());
103:
104: }
105:
106: return swingComposite;
107:
108: }
109:
110: /* (non-Javadoc)
111: * @see net.sf.jmoney.IBookkeepingPageListener#createPages(java.lang.Object, org.eclipse.swt.widgets.Composite)
112: */
113: public IBookkeepingPage createFormPage(NodeEditor editor,
114: IMemento memento) {
115: SectionlessPage formPage = new SectionlessPage(editor, PAGE_ID,
116: "Chart", "Pie Chart") {
117:
118: public Composite createControl(Object nodeObject,
119: Composite parent, FormToolkit toolkit,
120: IMemento memento) {
121: Session session = JMoneyPlugin.getDefault()
122: .getSession();
123: return createContent(session, parent);
124: }
125:
126: public void saveState(IMemento memento) {
127: // TODO Save the chart options selected by the user
128: }
129: };
130:
131: try {
132: editor.addPage(formPage);
133: } catch (PartInitException e) {
134: JMoneyPlugin.log(e);
135: // TODO: cleanly leave out this page.
136: }
137:
138: return formPage;
139: }
140:
141: public class redrawGraph implements ChangeListener, ActionListener {
142: public void stateChanged(ChangeEvent e) {
143: redraw();
144: }
145:
146: public void actionPerformed(ActionEvent e) {
147: redraw();
148: }
149:
150: private void redraw() {
151: // chart.setAccount("Dépenses exceptionelles");
152: chart.setDates((Date) fromDate.getValue(), (Date) toDate
153: .getValue());
154: chart.setMaxLevel(maxLevel.getText());
155: chart.createValues();
156: }
157: }
158:
159: }
|