00001 package org.math.plot.components;
00002
00003 import java.awt.event.*;
00004 import java.io.*;
00005 import java.security.*;
00006
00007 import javax.swing.*;
00008 import javax.swing.filechooser.FileFilter;
00009
00010 import org.math.plot.*;
00011 import org.math.plot.canvas.*;
00012
00019 public class PlotToolBar extends JToolBar {
00020
00021
00022
00023 private static final long serialVersionUID = 1L;
00024
00025 protected ButtonGroup buttonGroup;
00026
00027 protected JToggleButton buttonCenter;
00028
00029
00030
00031 protected JToggleButton buttonZoom;
00032
00033 protected JToggleButton buttonRotate;
00034
00035
00036
00037 protected JButton buttonSetScales;
00038
00039 protected JButton buttonDatas;
00040
00041 protected JButton buttonSavePNGFile;
00042
00043 protected JButton buttonReset;
00044
00045 private boolean denySaveSecurity;
00046
00047 private JFileChooser pngFileChooser;
00048
00050 private PlotCanvas plotCanvas;
00051
00052 private PlotPanel plotPanel;
00053
00054 public PlotToolBar(PlotPanel pp) {
00055 plotPanel = pp;
00056 plotCanvas = pp.plotCanvas;
00057
00058 try {
00059 pngFileChooser = new JFileChooser();
00060 pngFileChooser.setFileFilter(new FileFilter() {
00061 public boolean accept(File f) {
00062 return f.isDirectory() || f.getName().endsWith(".png");
00063 }
00064
00065 public String getDescription() {
00066 return "Portable Network Graphic file";
00067 }
00068 });
00069 } catch (AccessControlException ace) {
00070 denySaveSecurity = true;
00071 }
00072
00073 buttonGroup = new ButtonGroup();
00074
00075 buttonCenter = new JToggleButton(new ImageIcon(PlotPanel.class.getResource("icons/center.png")));
00076 buttonCenter.setToolTipText("Center axes");
00077 buttonCenter.setSelected(plotCanvas.ActionMode == PlotCanvas.TRANSLATION);
00078
00079 buttonZoom = new JToggleButton(new ImageIcon(PlotPanel.class.getResource("icons/zoom.png")));
00080 buttonZoom.setToolTipText("Zoom");
00081 buttonZoom.setSelected(plotCanvas.ActionMode == PlotCanvas.ZOOM);
00082
00083
00084
00085
00086
00087
00088
00089 buttonSetScales = new JButton(new ImageIcon(PlotPanel.class.getResource("icons/scale.png")));
00090 buttonSetScales.setToolTipText("Set scales");
00091
00092 buttonDatas = new JButton(new ImageIcon(PlotPanel.class.getResource("icons/data.png")));
00093 buttonDatas.setToolTipText("Get datas");
00094
00095 buttonSavePNGFile = new JButton(new ImageIcon(PlotPanel.class.getResource("icons/topngfile.png")));
00096 buttonSavePNGFile.setToolTipText("Save graphics in a .PNG File");
00097
00098 buttonReset = new JButton(new ImageIcon(PlotPanel.class.getResource("icons/back.png")));
00099 buttonReset.setToolTipText("Reset zoom & axes");
00100
00101
00102
00103
00104
00105
00106
00107 buttonZoom.setSelected(true);
00108 buttonZoom.addActionListener(new ActionListener() {
00109 public void actionPerformed(ActionEvent e) {
00110 plotCanvas.ActionMode = PlotCanvas.ZOOM;
00111 }
00112 });
00113
00114 buttonCenter.addActionListener(new ActionListener() {
00115 public void actionPerformed(ActionEvent e) {
00116 plotCanvas.ActionMode = PlotCanvas.TRANSLATION;
00117 }
00118 });
00119
00120
00121
00122
00123
00124
00125
00126 buttonSetScales.addActionListener(new ActionListener() {
00127 public void actionPerformed(ActionEvent e) {
00128 plotCanvas.displaySetScalesFrame();
00129 }
00130 });
00131
00132 buttonDatas.addActionListener(new ActionListener() {
00133 public void actionPerformed(ActionEvent e) {
00134 plotCanvas.displayDatasFrame();
00135 }
00136 });
00137
00138 buttonSavePNGFile.addActionListener(new ActionListener() {
00139 public void actionPerformed(ActionEvent e) {
00140 choosePNGFile();
00141 }
00142 });
00143
00144 buttonReset.addActionListener(new ActionListener() {
00145 public void actionPerformed(ActionEvent e) {
00146 plotCanvas.resetBase();
00147 }
00148 });
00149
00150 buttonGroup.add(buttonCenter);
00151 buttonGroup.add(buttonZoom);
00152
00153
00154 add(buttonCenter, null);
00155 add(buttonZoom, null);
00156 add(buttonReset, null);
00157
00158 add(buttonSetScales, null);
00159
00160 add(buttonSavePNGFile, null);
00161 add(buttonDatas, null);
00162
00163 if (!denySaveSecurity) {
00164 pngFileChooser.addActionListener(new ActionListener() {
00165 public void actionPerformed(ActionEvent e) {
00166 saveGraphicFile();
00167 }
00168 });
00169 } else {
00170 buttonSavePNGFile.setEnabled(false);
00171 }
00172
00173
00174
00175
00176
00177
00178 if (plotCanvas instanceof Plot3DCanvas) {
00179 if (buttonRotate == null) {
00180 buttonRotate = new JToggleButton(new ImageIcon(PlotPanel.class.getResource("icons/rotation.png")));
00181 buttonRotate.setToolTipText("Rotate axes");
00182
00183 buttonRotate.addActionListener(new ActionListener() {
00184 public void actionPerformed(ActionEvent e) {
00185 plotCanvas.ActionMode = Plot3DCanvas.ROTATION;
00186 }
00187 });
00188 buttonGroup.add(buttonRotate);
00189 add(buttonRotate, null, 2);
00190 buttonRotate.setSelected(plotCanvas.ActionMode == Plot3DCanvas.ROTATION);
00191 } else {
00192 buttonRotate.setEnabled(true);
00193 }
00194 } else {
00195 if (buttonRotate != null) {
00196
00197 if (plotCanvas.ActionMode == Plot3DCanvas.ROTATION) {
00198 plotCanvas.ActionMode = PlotCanvas.ZOOM;
00199 }
00200 buttonRotate.setEnabled(false);
00201 }
00202 }
00203 }
00204
00205 void choosePNGFile() {
00206 pngFileChooser.showSaveDialog(this);
00207 }
00208
00209 void saveGraphicFile() {
00210 java.io.File file = pngFileChooser.getSelectedFile();
00211 try {
00212 plotPanel.toGraphicFile(file);
00213 } catch (IOException e) {
00214 JOptionPane.showConfirmDialog(null, "Save failed : " + e.getMessage(), "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
00215 }
00216 }
00217 }