Switch back to new palette
[maximus:sft.git] / SuperFractalThing / source / SuperFractalThing.java
1 //      SuperFractalThing
2 //
3 //
4 //    Copyright 2013 Kevin Martin
5 //
6 //    This file is part of SuperFractalThing.
7 //
8 //    SuperFractalThing is free software: you can redistribute it and/or modify
9 //    it under the terms of the GNU General Public License as published by
10 //    the Free Software Foundation, either version 3 of the License, or
11 //    any later version.
12 //
13 //    SuperFractalThing is distributed in the hope that it will be useful,
14 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //    GNU General Public License for more details.
17 //
18 //    You should have received a copy of the GNU General Public License
19 //    along with SuperFractalThing.  If not, see <http://www.gnu.org/licenses/>.
20 //
21 import javax.swing.JApplet;
22 import javax.swing.JFrame;
23 import javax.swing.JPanel;
24 //import javax.swing.JTextField;
25 import javax.swing.JButton;
26 import javax.swing.JFormattedTextField;
27 import javax.swing.JLabel;
28 import javax.swing.JMenuBar;
29 import javax.swing.JMenu;
30 import javax.swing.JMenuItem;
31 import javax.swing.JFileChooser;
32 import javax.swing.JOptionPane;
33 import javax.swing.JProgressBar;
34 import javax.swing.filechooser.FileNameExtensionFilter;
35 import javax.swing.text.InternationalFormatter;
36 import javax.swing.text.NumberFormatter;
37
38 import java.awt.event.ActionListener;
39 import java.awt.event.ActionEvent;
40 import java.awt.image.*;
41 import java.awt.*;
42 import java.io.BufferedWriter;
43 import java.io.ByteArrayOutputStream;
44 import java.io.File;
45 import java.io.FileNotFoundException;
46 import java.io.FileOutputStream;
47 import java.io.FileReader;
48 import java.io.FileWriter;
49
50 import javax.imageio.ImageIO;
51
52 import java.io.BufferedReader;
53 import java.io.IOException;
54 import java.math.BigDecimal;
55 import java.text.DecimalFormatSymbols;
56 import java.text.FieldPosition;
57 import java.text.NumberFormat;
58 import java.text.Format;
59 import java.text.DecimalFormat;
60 import java.text.AttributedCharacterIterator;
61 import java.text.ParsePosition;
62
63 interface IPaletteDialog extends ActionListener
64 {
65         public boolean Run();
66         public IPalette GetPalette();
67         public void MakePaletteLibrary(JMenuBar aMenuBar);
68 }
69
70
71 public class SuperFractalThing  extends JApplet implements SFTGui, ActionListener, LibraryLoader, PaletteIO
72 {
73
74         /**
75          * 
76          */
77         private static final long serialVersionUID = 0;//get rid of warning
78         static SftComponent mComp;
79         static JFormattedTextField mSize_box;
80         static JFormattedTextField mPos_x_box;
81         static JFormattedTextField mPos_y_box;
82         static JFormattedTextField mIterations_box;
83         static BigDecimal mPos_x;
84         static BigDecimal mPos_y;
85         static BigDecimal mSize;
86         JLabel mSize_label;
87         JProgressBar mProgress_bar;
88         JButton mCancel_button;
89         JLabel mIterations_label;
90         PositionLibrary mLibrary;
91         PaletteLibrary pLibrary;
92         JLabel mTime_label;
93         JMenuBar mMenu_bar;
94         ExportDialog mDialog;
95         IPaletteDialog mPalette_dialog;
96         OptionsDialog mOptions_dialog;
97         IPalette mPalette;
98         UndoBuffer mUndo_buffer;
99         JMenuItem mRedo_item;
100         JMenuItem mUndo_item;
101         
102         static JFrame mFrame;
103         /**
104          * @param args
105          */
106
107         public static void main(String[] args)
108         {
109                 
110                 mFrame = new JFrame("SuperFractalThing");
111                 mFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
112
113                 SuperFractalThing ap = new SuperFractalThing();
114                 ap.init();
115             ap.start();
116
117     
118             mFrame.add("Center", ap);
119             mFrame.pack();
120             mFrame.setVisible(true);
121
122  
123         }
124
125         public void start()
126         {
127            // init();
128             
129
130                 mUndo_buffer = new UndoBuffer();
131             initComponents();   
132                 mPalette_dialog.Run();
133             
134         }
135         
136         public void SetProgress(int aProgress, int pMax)
137         {
138                 mProgress_bar.setMaximum(pMax);
139                 mProgress_bar.setValue(aProgress);
140         }
141
142     public void AddToUndoBuffer()
143     {
144         BigDecimal coords[] = GetCoords();
145                 mUndo_buffer.Push(coords[0],coords[1],GetTheSize(),GetIterations());
146                 if (mUndo_item!=null)
147                         mUndo_item.setEnabled(mUndo_buffer.CanUndo());
148                 if (mRedo_item!=null)
149                         mRedo_item.setEnabled(mUndo_buffer.CanRedo());
150     }
151     
152     public void OutOfMemory()
153     {
154                 JOptionPane.showMessageDialog(mFrame,
155                             "Out of Memory!\n",
156                             "Error",
157                             JOptionPane.WARNING_MESSAGE);
158     }   
159     
160         public void SetCalculationTime(long aTime_ms)
161         {
162                 String text;
163                 if (aTime_ms>=0)
164                         text = "  Last calculation time: " + Double.toString((double)aTime_ms / 1000 )+" seconds";
165                 else
166                         text="";
167                 mTime_label.setText(text);
168         }
169         
170         public void actionPerformed(ActionEvent event)
171         {
172                 String command = event.getActionCommand();
173                 
174                 if (command=="Cancel")
175                 {
176                         mComp.Cancel();
177                 }
178                 if (command=="Open")
179                 {
180                         OpenFile();
181                 }
182                 else if (command=="Save")
183                 {
184                         String str;
185                         BigDecimal half = new BigDecimal(0.5);
186                         BigDecimal half_size =  GetTheSize().multiply(half);
187                         //size*=0.5f;
188                         
189                         str="s="+half_size.toString()+"\n";
190                         str+="r="+mPos_x_box.getText()+"\n";
191                         str+="i="+mPos_y_box.getText()+"\n";
192                         str+="iteration_limit="+mIterations_box.getText().replaceAll(",", "")+"\n";
193
194                         
195                         SaveFile(str);
196                         
197
198                 }
199                 else if (command=="Reset")
200                 {
201                         mPos_x_box.setValue(new BigDecimal(-0.75));
202                         mPos_y_box.setValue(new BigDecimal(0.0));
203                         mSize_box.setValue(new BigDecimal(3.0));
204                         mIterations_box.setValue(new Integer(1024));
205                         mSize = new BigDecimal(3.0);
206                         AddToUndoBuffer();
207                         mComp.Refresh();
208                         mComp.repaint();                                                
209                 }
210                 else if (command=="Export PNG")
211                 {
212                         
213                         //ExportDialog dialog = new ExportDialog(mFrame, mComp);
214                         boolean res = mDialog.Run();
215                         if (!res)
216                                 return;
217                         
218                         if (mDialog.GetWidth()>50000 || mDialog.GetHeight()>50000 || mDialog.GetWidth()<32 || mDialog.GetHeight()<32)
219                         {
220                                 JOptionPane.showMessageDialog(mFrame,
221                                             "Invalid Image Size",
222                                             "Error",
223                                             JOptionPane.WARNING_MESSAGE);
224                                 return;
225                 
226                         }
227                         try
228                         {
229                                 mComp.ExportCalculation(mDialog.GetWidth(), mDialog.GetHeight(), mDialog.GetSuperSample());
230                         } catch (OutOfMemoryError e) {
231                                 // TODO Auto-generated catch block
232                                 JOptionPane.showMessageDialog(mFrame,
233                                             "Out of Memory!\n Try using a 64 bit browser.",
234                                             "Error",
235                                             JOptionPane.WARNING_MESSAGE);
236                                 EndProcessing();
237                                 return;
238                         };
239                         
240 /*                      ByteArrayOutputStream bos = new ByteArrayOutputStream();
241                         try {
242                                 ImageIO.write(mComp.GetImage(),"PNG",bos);
243                         } catch (IOException e1) {
244                                 // TODO Auto-generated catch block
245                                 e1.printStackTrace();
246                                 return;
247                         }
248
249                         FileSaveService fss; 
250                     try { 
251                         fss = (FileSaveService)ServiceManager.lookup("javax.jnlp.FileSaveService"); 
252                     } catch (UnavailableServiceException e) { 
253                         fss = null; 
254                         return;
255                     } 
256                     
257                     ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
258                     String[] exts={"png"};
259                         try {
260                                 fss.saveFileDialog(null,exts,bis,"sft_exp.png");
261                         } catch (IOException e) {
262                                 // TODO Auto-generated catch block
263                                 e.printStackTrace();
264                         }
265 */              }
266                 else if (command=="Refresh")
267                 {
268                         mComp.Refresh();
269                         mComp.repaint();                        
270                 }                   
271                 else if (command=="About")
272                 {
273                         //JOptionPane.showMessageDialog(mFrame, "Abitrary(ish) precision Mandelbrot set rendering in Java.\n\nVersion 0.1\n\n(c) Kevin Martin","SuperFractalThing Java",JOptionPane.PLAIN_MESSAGE);
274                         AboutDialog ad = new AboutDialog(mFrame,mComp);
275                         ad.Run();
276                 }
277                 else if (command=="Palette")
278                 {
279                         mPalette_dialog.Run();
280                 }
281                 else if (command=="Options")
282                 {
283                         mOptions_dialog.Run();
284                         mComp.SetSuperSampleType(mOptions_dialog.GetSuperSampleType());
285                         mComp.SetNumThreads(mOptions_dialog.GetNumThreads());
286                 }
287                 else if (command == "Undo")
288                 {
289                         if (mUndo_buffer.CanUndo())
290                         {
291                                 mUndo_buffer.Undo();
292                                 mPos_x_box.setValue(mUndo_buffer.GetX());
293                                 mPos_y_box.setValue(mUndo_buffer.GetY());
294                                 mSize_box.setValue(mUndo_buffer.GetSize());
295                                 mIterations_box.setValue(mUndo_buffer.GetIterations());
296                                 mComp.Refresh();
297                                 mComp.repaint();                        
298                                 mUndo_item.setEnabled(mUndo_buffer.CanUndo());
299                                 mRedo_item.setEnabled(mUndo_buffer.CanRedo());
300                         }
301                 }
302                 else if (command == "Redo")
303                 {
304                         if (mUndo_buffer.CanRedo())
305                         {
306                                 mUndo_buffer.Redo();
307                                 mPos_x_box.setValue(mUndo_buffer.GetX());
308                                 mPos_y_box.setValue(mUndo_buffer.GetY());
309                                 mSize_box.setValue(mUndo_buffer.GetSize());
310                                 mIterations_box.setValue(mUndo_buffer.GetIterations());
311                                 mComp.Refresh();
312                                 mComp.repaint();                        
313                                 mUndo_item.setEnabled(mUndo_buffer.CanUndo());
314                                 mRedo_item.setEnabled(mUndo_buffer.CanRedo());
315                         }
316                 }
317         }
318         
319         void OpenFile()
320         {
321                 JFileChooser chooser = new JFileChooser();
322                  
323             FileNameExtensionFilter filter = new FileNameExtensionFilter("SuperFractalThingFile",  "txt");
324             chooser.setFileFilter(filter);
325             int returnVal = chooser.showOpenDialog(this);
326             if(returnVal == JFileChooser.APPROVE_OPTION)
327             {
328                System.out.println("You chose to open this file: " +
329                     chooser.getSelectedFile().getName());
330                
331                File f = chooser.getSelectedFile();
332                
333                LoadTheFile(f);
334                
335             } 
336             return;
337         }
338         
339         void SaveFile(String str)
340         {
341                 JFileChooser chooser = new JFileChooser();
342                  
343             FileNameExtensionFilter filter = new FileNameExtensionFilter("SuperFractalThingFile",  "txt");
344             chooser.setFileFilter(filter);
345             int returnVal = chooser.showSaveDialog(this);
346             if(returnVal == JFileChooser.APPROVE_OPTION)
347             {
348                System.out.println("You chose to open this file: " +
349                     chooser.getSelectedFile().getName());
350                
351                File f = chooser.getSelectedFile();
352                
353                BufferedWriter file;
354                         try {
355                                 file = new BufferedWriter(new FileWriter(f));
356                                 file.write(str);
357                                 file.close();
358                         } catch (IOException e) {
359                                 // TODO Auto-generated catch block
360                                 e.printStackTrace();
361                         };
362             } 
363         }
364
365         public void ExportImage(BufferedImage aImage)
366         {
367                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
368                 try {
369                         ImageIO.write(aImage,"PNG",bos);
370                 } catch (IOException e1) {
371                         // TODO Auto-generated catch block
372                         e1.printStackTrace();
373                         return;
374                 }
375
376                 SaveByteArrayOutputStream(bos);
377         }
378         
379         
380         void SaveByteArrayOutputStream(ByteArrayOutputStream bos)
381         {       
382             JFileChooser chooser = new JFileChooser();
383                  
384             FileNameExtensionFilter filter = new FileNameExtensionFilter("PNG",  "png");
385             chooser.setFileFilter(filter);
386             int returnVal = chooser.showSaveDialog(this);
387             if(returnVal == JFileChooser.APPROVE_OPTION)
388             {
389                
390                File f = chooser.getSelectedFile();
391                
392                FileOutputStream fs;
393                         try {
394                                 fs = new FileOutputStream(f);
395                        fs.write(bos.toByteArray());
396                        fs.close();
397                         } catch (FileNotFoundException e) {
398                                 // TODO Auto-generated catch block
399                                 e.printStackTrace();
400                         } catch (IOException e) {
401                                 // TODO Auto-generated catch block
402                                 e.printStackTrace();
403                         }
404             }
405             return;             
406         }
407
408         public void SetHoverIndex(int index)
409         {
410                 String str = Integer.toString(index);
411                 mIterations_label.setText(str);
412         }
413         
414         public void LoadTheFile(File f)
415     {
416         try
417         {
418                         FileReader fr = new FileReader(f);
419                         BufferedReader br = new BufferedReader(fr);
420                         LoadTheFile(br);
421                 } catch (FileNotFoundException e) {
422                         // TODO Auto-generated catch block
423                         e.printStackTrace();
424                 }
425         }
426
427     public void LoadTheFile(BufferedReader br)
428     {
429         try
430         {
431                         String line1 = br.readLine();
432                         String line2 = br.readLine();
433                         String line3 = br.readLine();
434                         String line4 = br.readLine();
435                         
436                         
437                         if (line1.startsWith("s=") && line2.startsWith("r=") && line3.startsWith("i=") && line4.startsWith("iteration_limit="))
438                         {
439                                 //double size = Double.parseDouble(line1.substring(2));
440                                 //mSize_box.setText(Double.toString(size*2));
441                                 BigDecimal size = new BigDecimal(line1.substring(2));
442                                 size = size.add(size);
443                                 mSize_box.setText(size.toString());
444                                 mPos_x_box.setText(line2.substring(2));
445                                 mPos_y_box.setText(line3.substring(2));
446                                 mIterations_box.setValue(Integer.parseInt(line4.substring(16)));
447                                 mComp.repaint();
448                                 AddToUndoBuffer();
449                                 mComp.DoCalculation();
450                                 mComp.repaint();
451                                 
452                         }
453                 
454
455                 } catch (FileNotFoundException e) {
456                         // TODO Auto-generated catch block
457                         e.printStackTrace();
458                 } catch (IOException e) {
459                         // TODO Auto-generated catch block
460                         e.printStackTrace();
461                 }
462         }
463     
464         public void SetCoords( BigDecimal aSize, BigDecimal x, BigDecimal y, int iterations)
465         {
466                 mSize_box.setValue(aSize);
467                 mPos_x_box.setValue(x);
468                 mPos_y_box.setValue(y);
469                 mIterations_box.setValue(iterations);
470         }
471         
472         public BigDecimal GetTheSize()
473         {
474                 return new BigDecimal(mSize_box.getText());
475         }
476         public int GetIterations()
477         {
478                 String str = mIterations_box.getText().replaceAll(",", "");
479                 str = str.replaceAll(" ", "");
480                 str = str.replaceAll("\\.", "");
481                 return Integer.parseInt(str);
482         }
483         public void SetIterations(int aValue)
484         {
485 //              mIterations_box.setText(Integer.toString(aValue));
486                 mIterations_box.setValue(aValue);
487         }
488         public BigDecimal[] GetCoords()
489         {
490                 BigDecimal[] x=new BigDecimal[2];
491                 x[0]=new BigDecimal(mPos_x_box.getText());
492                 x[1]=new BigDecimal(mPos_y_box.getText());
493                 return x;
494         }
495     public void initComponents()
496     {     
497         //setLayout(new BorderLayout());
498         JPanel p = new JPanel();
499         p.setLayout(new GridBagLayout());
500         GridBagConstraints gbc = new GridBagConstraints();
501         gbc.gridx=0;
502         gbc.gridy=0;
503         gbc.gridwidth=8;
504         //p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
505         mComp = new SftComponent(this);
506         
507         p.add(mComp,gbc);
508
509         p.addMouseListener(mComp);
510         p.addMouseMotionListener(mComp);
511         add("North", p);
512         
513         gbc.gridy+=1;
514         gbc.gridwidth=7;
515         gbc.fill = GridBagConstraints.HORIZONTAL;
516         mProgress_bar = new JProgressBar(0, 1024*768);
517         mProgress_bar.setSize(new Dimension(896,20));
518         mProgress_bar.setPreferredSize(new Dimension(896,20));
519         p.add(mProgress_bar,gbc);
520         mProgress_bar.setVisible(false);
521         
522         gbc.gridx=7;
523         gbc.fill = GridBagConstraints.HORIZONTAL;
524         gbc.gridwidth=1;
525         mCancel_button = new JButton("Cancel");
526         p.add(mCancel_button,gbc);
527         mCancel_button.setVisible(false);
528         mCancel_button.addActionListener(this);
529         
530         gbc.ipady=(mProgress_bar.getHeight()-mCancel_button.getHeight())/2;
531         gbc.gridx=0;
532         gbc.gridy+=1;
533         gbc.gridwidth=1;
534         gbc.fill = GridBagConstraints.HORIZONTAL;
535         mSize_label=new JLabel("Horizontal size", null, JLabel.LEFT);
536         p.add(mSize_label,gbc);
537        
538         InternationalFormatter f2 = new InternationalFormatter();
539         f2.setFormat(new BigDecimalFormat());
540         f2.setAllowsInvalid(false);
541  
542         InternationalFormatter size_format = new InternationalFormatter();
543         size_format.setFormat(new BigDecimalFormat());
544         size_format.setAllowsInvalid(false);
545
546         gbc.gridx=1;
547         gbc.gridwidth=2; 
548         gbc.fill = GridBagConstraints.HORIZONTAL;
549         //DecimalFormat format = new DecimalFormat("#.#####E0");
550         mSize = new BigDecimal(1.5);
551         mSize_box = new JFormattedTextField(size_format);
552         mSize_box.setPreferredSize(new Dimension(400,20));
553         //mSize_box.setAlignmentY(1);
554         p.add(mSize_box, gbc);
555  
556         gbc.gridx=6;
557         gbc.gridwidth=2;
558         gbc.fill = GridBagConstraints.HORIZONTAL;
559         mIterations_label = new JLabel("Iterations:", null, JLabel.CENTER);
560         p.add(mIterations_label,gbc);
561         
562         gbc.ipady=0;
563         gbc.gridx=0;
564         gbc.gridy+=1;
565         gbc.gridwidth=1;
566         gbc.fill = GridBagConstraints.HORIZONTAL;
567         p.add(new JLabel("Real position", null, JLabel.LEFT),gbc);
568         
569         gbc.gridx=1;
570         gbc.gridwidth=7;
571        
572         //f2.setMaximumFractionDigits(1000);
573         mPos_x = new BigDecimal(-0.5);
574         mPos_x_box = new JFormattedTextField(f2);
575         mPos_x_box.setPreferredSize(new Dimension(200,20));
576         //mPos_x_box.setAlignmentY(1);
577         p.add(mPos_x_box, gbc);
578         
579         gbc.gridx=0;
580         gbc.gridy+=1;
581         gbc.gridwidth=1;
582         gbc.fill = GridBagConstraints.HORIZONTAL;
583         p.add(new JLabel("Imaginary position", null, JLabel.LEFT),gbc);
584
585         gbc.gridx=1;
586         gbc.gridwidth=7;
587         mPos_y = new BigDecimal(0);
588         mPos_y_box = new JFormattedTextField(f2);
589         mPos_y_box.setPreferredSize(new Dimension(200,20));
590         //mPos_y_box.setAlignmentY(1);
591         p.add(mPos_y_box, gbc);   
592  
593         
594         gbc.gridx=0;
595         gbc.gridy+=1;
596         gbc.gridwidth=1;
597         gbc.fill = GridBagConstraints.HORIZONTAL;
598         p.add(new JLabel("Iteration Limit", null, JLabel.LEFT),gbc);
599
600         gbc.gridx=1;
601         gbc.gridwidth=4;
602         NumberFormat iformat = NumberFormat.getInstance();// new DecimalFormat("#################");
603         mIterations_box = new JFormattedTextField(iformat);
604         mIterations_box.setPreferredSize(new Dimension(400,20));
605         p.add(mIterations_box, gbc);   
606         
607         gbc.gridx=5;
608         gbc.gridwidth=2;
609         gbc.fill = GridBagConstraints.HORIZONTAL;
610         mTime_label =new JLabel("", null, JLabel.CENTER);
611         p.add(mTime_label, gbc);   
612
613
614                 mDialog = new ExportDialog(mFrame, mComp);
615                 //mPalette_dialog = new PaletteDialogOld(mFrame, mComp, mComp, this);
616                 mPalette_dialog = new PaletteDialog(mFrame, mComp, mComp, this);
617                 mPalette = mPalette_dialog.GetPalette();
618                 mComp.SetPalette(mPalette);
619                 mOptions_dialog = new OptionsDialog(mFrame, mComp);
620
621                 mComp.CreateImage();
622         
623            //Menu bar
624             JMenuBar menuBar = new JMenuBar();
625             JMenu menu = new JMenu("SuperFractalThing");
626             JMenu navigate = new JMenu("Controls");
627  
628             JMenuItem menuItem = new JMenuItem("Refresh");
629         menuItem.addActionListener(this);
630         navigate.add(menuItem);
631  
632             menuItem = new JMenuItem("Undo");
633         menuItem.addActionListener(this);
634         navigate.add(menuItem);
635         mUndo_item = menuItem;
636         mUndo_item.setEnabled(false);
637        
638             menuItem = new JMenuItem("Redo");
639         menuItem.addActionListener(this);
640         navigate.add(menuItem);
641         mRedo_item = menuItem;
642         mRedo_item.setEnabled(false);
643         
644         menuItem = new JMenuItem("Reset");
645         menuItem.addActionListener(this);
646         navigate.add(menuItem);
647         
648         
649         menuItem = new JMenuItem("Open");
650         menuItem.addActionListener(this);
651         menu.add(menuItem);
652         
653         menuItem = new JMenuItem("Save");
654         menuItem.addActionListener(this);
655         menu.add(menuItem);
656
657         menuItem = new JMenuItem("Export PNG");
658         menuItem.addActionListener(this);
659         menu.add(menuItem);
660
661         menuItem = new JMenuItem("Palette");
662         menuItem.addActionListener(this);
663         menu.add(menuItem);
664
665         menuItem = new JMenuItem("Options");
666         menuItem.addActionListener(this);
667         menu.add(menuItem);
668
669
670         menuItem = new JMenuItem("About");
671         menuItem.addActionListener(this);
672         menu.add(menuItem);
673       
674         menuBar.add(menu);
675         menuBar.add(navigate);
676         
677         mLibrary = new PositionLibrary(menuBar, this);
678         mPalette_dialog.MakePaletteLibrary(menuBar);
679         //pLibrary = new PaletteLibrary(menuBar, mPalette_dialog);
680
681         setJMenuBar(menuBar);
682             mMenu_bar = menuBar;
683             
684
685                 mComp.SetSuperSampleType(mOptions_dialog.GetSuperSampleType());
686                 mComp.SetNumThreads(mOptions_dialog.GetNumThreads());
687
688     }
689     
690     public void StartProcessing()
691     {
692         mMenu_bar.getComponent(0).setEnabled(false);
693         mMenu_bar.getComponent(1).setEnabled(false);
694         mMenu_bar.getComponent(2).setEnabled(false);
695                 SetProgress(0,1024);
696         mProgress_bar.setVisible(true);
697         mCancel_button.setVisible(true);
698         mSize_label.setVisible(false);
699         mIterations_label.setVisible(false);
700         mSize_box.setVisible(false);
701     }
702     
703     public void EndProcessing()
704     {
705         mMenu_bar.getComponent(0).setEnabled(true);
706         mMenu_bar.getComponent(1).setEnabled(true);
707         mMenu_bar.getComponent(2).setEnabled(true);
708         mProgress_bar.setVisible(false);
709         mCancel_button.setVisible(false);
710         mSize_label.setVisible(true);
711         mIterations_label.setVisible(true);
712         mSize_box.setVisible(true);     
713    }
714
715         @Override
716         public void SavePalette(String str)
717         {
718                 JFileChooser chooser = new JFileChooser();
719                  
720             FileNameExtensionFilter filter = new FileNameExtensionFilter("SuperFractalThingFile",  "txt");
721             chooser.setFileFilter(filter);
722             int returnVal = chooser.showSaveDialog(mComp);
723             if(returnVal == JFileChooser.APPROVE_OPTION)
724             {
725                System.out.println("You chose to open this file: " +
726                     chooser.getSelectedFile().getName());
727                
728                File f = chooser.getSelectedFile();
729                
730                BufferedWriter file;
731                         try {
732                                 file = new BufferedWriter(new FileWriter(f));
733                                 file.write(str);
734                                 file.close();
735                         } catch (IOException e) {
736                                 // TODO Auto-generated catch block
737                                 e.printStackTrace();
738                                 return;
739                         };
740             } 
741             return;
742         }
743
744         @Override
745         public String LoadPalette()
746         {
747                 JFileChooser chooser = new JFileChooser();
748                  
749             FileNameExtensionFilter filter = new FileNameExtensionFilter("SuperFractalThingFile",  "txt");
750             chooser.setFileFilter(filter);
751             int returnVal = chooser.showOpenDialog(mComp);
752             if(returnVal == JFileChooser.APPROVE_OPTION)
753             {
754                System.out.println("You chose to open this file: " +
755                     chooser.getSelectedFile().getName());
756                
757                File f = chooser.getSelectedFile();
758                
759                         try
760                         {
761                                 FileReader fr = new FileReader(f);
762                                 BufferedReader br = new BufferedReader(fr);
763                                 char arr[]=new char[2048];
764                                 br.read(arr, 0,2048);
765                                 String str = String.copyValueOf(arr);
766                                 br.close();
767                                 return str;
768                                 
769                         }
770                         catch (FileNotFoundException e)
771                         {
772                                 // TODO Auto-generated catch block
773                                 e.printStackTrace();
774                                 return null;
775                         }
776                         catch (IOException e)
777                         {
778                                 return null;                    
779                         }
780
781                 }  
782             return null;
783         }
784
785
786 }
787
788
789 class BigDecimalFormatter extends NumberFormatter
790 {
791         private static final long serialVersionUID = 0;
792
793         BigDecimalFormatter()
794         {
795                 //setAllowsInvalid(false);
796         }
797         
798         public Object stringToValue(String text) //throws ParseException
799         {
800                 if("".equals(text.trim()))
801                 {
802                 return null;
803                 }
804                 char ds = getDefaultLocaleDecimalSeparator();
805
806  
807                 try
808                 {
809                         String val = text;
810                         if(ds != '.')
811                         {
812                                 val = val.replace(".", "").replace(ds, '.');
813                         }
814                         return new BigDecimal(val);
815                 } catch(NumberFormatException e)
816                 {
817                         return null;
818                 }
819         }
820         
821         public String valueToString(Object value) //throws ParseException
822         {
823                 if (value!=null)
824                         return value.toString();
825                 else
826                         return null;
827         }
828          
829         private char getDefaultLocaleDecimalSeparator()
830         {
831                 DecimalFormatSymbols symbols = new DecimalFormat("0").getDecimalFormatSymbols();
832                 char ds = symbols.getDecimalSeparator();
833                 return ds;
834         }
835
836 }
837
838
839 class BigDecimalFormat extends Format
840 {
841         private static final long serialVersionUID = 0;//get rid of warning
842         BigDecimal mOld_value;
843         String mOld_string;
844
845         String Format(Object number)
846         {
847                 BigDecimal x=(BigDecimal)number;
848                 return x.toString();
849         }
850         
851         public AttributedCharacterIterator formatToCharacterIterator(Object obj)
852         {
853                 return null;
854         }
855
856         public Object   parseObject(String source)
857         {
858                 mOld_string = null;
859                 try
860                 {
861                         BigDecimal x= new BigDecimal(source);
862                         mOld_value = x;
863                         if (source.endsWith(".") || source.contentEquals("-0") || source.contains("E") || source.contains("e"))
864                                 mOld_string = source;
865                         return x;
866                 }
867                 catch (NumberFormatException e)
868                 {
869                         if (source.length()==0)
870                         {
871                                 mOld_value=null;
872                                 mOld_string=null;
873                                 return null;
874                         }
875                         if (source.equals("-") || source.endsWith("E") || source.endsWith("e") || source.endsWith("-"))
876                         {
877                                 mOld_string = source;
878                                 mOld_value = new BigDecimal(0);
879                                 return mOld_value;
880                         }
881                         return mOld_value;
882                 }
883         }
884
885         @Override
886         public StringBuffer format(Object arg0, StringBuffer arg1,
887                         FieldPosition arg2)
888         {
889                 if (mOld_string!=null && mOld_value.equals(arg0))
890                 {
891                         arg1.append(mOld_string);
892                         return arg1;
893                 }
894                 
895                 BigDecimal x=(BigDecimal)arg0;
896                 String str = x.toString();
897                 arg1.append(str);
898                 return arg1;
899         }
900
901         @Override
902         public Object parseObject(String arg0, ParsePosition arg1)
903         {
904                 return parseObject(arg0);
905         }
906 }