source: git/Singular/LIB/surfex/Project.java @ 2ab830

spielwiese
Last change on this file since 2ab830 was 3de2ca, checked in by Hans Schönemann <hannes@…>, 16 years ago
*hannes: surfex_0_90_00 git-svn-id: file:///usr/local/Singular/svn/trunk@11070 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 36.7 KB
Line 
1////////////////////////////////////////////////////////////////////////
2//
3// This file Project.java is part of SURFEX.
4//
5////////////////////////////////////////////////////////////////////////
6
7////////////////////////////////////////////////////////////////////////
8//
9// SURFEX version 0.90.00
10// =================
11//
12// Saarland University at Saarbruecken, Germany
13// Department of Mathematics and Computer Science
14//
15// SURFEX on the web: www.surfex.AlgebraicSurface.net
16//
17// Authors: Oliver Labs (2001-2008), Stephan Holzer (2004-2005)
18//
19// Copyright (C) 2001-2008
20//
21//
22// *NOTICE*
23// ========
24// 
25// This program is free software; you can redistribute it and/or modify it
26// under the terms of the GNU General Public License as published by the
27// Free Software Foundation ( version 3 or later of the License ).
28//
29// See LICENCE.TXT for details.
30//
31/////////////////////////////////////////////////////////////////////////
32
33import java.awt.BorderLayout;
34import java.awt.Color;
35import java.awt.Container;
36import java.awt.FlowLayout;
37import java.awt.GridLayout;
38import java.awt.Panel;
39import java.awt.event.ActionEvent;
40import java.awt.event.ActionListener;
41import java.io.BufferedReader;
42import java.io.File;
43import java.io.FileInputStream;
44import java.io.FileOutputStream;
45import java.io.IOException;
46import java.io.InputStreamReader;
47import java.io.PrintWriter;
48import java.net.MalformedURLException;
49import java.net.URL;
50import java.net.URLConnection;
51import java.net.URLEncoder;
52import java.util.Hashtable;
53import java.util.Random;
54import java.util.Vector;
55
56import javax.swing.ImageIcon;
57import javax.swing.JButton;
58import javax.swing.JCheckBox;
59import javax.swing.JComboBox;
60import javax.swing.JColorChooser;
61import javax.swing.JFileChooser;
62import javax.swing.JFrame;
63import javax.swing.JLabel;
64import javax.swing.JOptionPane;
65import javax.swing.JPanel;
66import javax.swing.JScrollPane;
67import javax.swing.JSlider;
68import javax.swing.JSplitPane;
69import javax.swing.JTextField;
70import javax.swing.SwingUtilities;
71import javax.swing.event.ChangeEvent;
72import javax.swing.event.ChangeListener;
73import javax.swing.filechooser.FileFilter;
74
75//////////////////////////////////////////////////////////////
76//
77//class Project
78//
79//////////////////////////////////////////////////////////////
80public class Project extends JPanel implements ActionListener {
81  surfex surfex_;
82
83  public jv4surfex jv4sx;
84
85  public int ProjectNumber;
86
87  public String projectName;
88
89  String old_tmpsubdir = "";
90 
91  String old_surfCodePart1 = "", old_surfCodePart2 = "";
92 
93  public AppearanceScheme appearanceScheme = new AppearanceScheme();
94
95  public boolean saveDone = false;
96
97  JTextField tf;
98
99  LampAdmin lampAdmin;
100  public String filename;
101
102  SavePicDialog savePicDialog;
103 
104  SaveMovieDialog saveMovieDialog;
105
106  RayFrame rayFrame;
107
108  Vector tbuttons = new Vector();
109
110  JButton bt1 = new JButton();
111
112  JButton bt2 = new JButton();
113
114  JButton bt3 = new JButton();
115
116  JButton bt4 = new JButton();
117
118  JButton bt5 = new JButton();
119
120  JButton bt6 = new JButton();
121
122  JButton bt7 = new JButton();
123 
124  int rand;
125
126  JCheckBox raytraceAlways = new JCheckBox("permanently", true);
127
128  JCheckBox antialiasing = new JCheckBox("antialiasing", false);
129
130  JButton bgColorButton = null;
131
132  JComboBox clipMode = new JComboBox();
133
134  JTextField clipRadius = new JTextField("9.0");
135
136    double scale = 1.0;
137   
138    String rootFinder = "bezier_all_roots";
139    String epsilon = "0.00000000000001";
140
141    int interpolConst = 2;
142    JComboBox interpolConstBox = null; 
143
144//  JSlider scaleSlider = null;
145
146//  JLabel scaleLabel = null;
147   
148    JPanel controlpanel = new JPanel();
149   
150    EquationAdmin eqAdm;
151   
152    CurveAdmin cuAdm;
153   
154    ParameterAdmin parAdmin;
155   
156    public SolitaryPointsAdmin solPtsAdm;
157   
158    String tmp_surfex_file = "tmp_surfex.jpg";
159   
160    Project(surfex su, jv4surfex jv4sx, int proNr, String proName) {   
161        surfex_ = su;
162        rayFrame = surfex_.rayFrame;
163        this.jv4sx = jv4sx;
164        ProjectNumber = proNr;
165        projectName = proName;
166       
167        // create a new temporary filename: tmp_surfex_file
168        Random myRNG = new Random();
169        int myRN = myRNG.nextInt(99999999);
170        //System.out.println("rn:"+myRN);
171        tmp_surfex_file = "tmp_surfex_" + myRN + "_" + proNr + ".jpg";
172       
173        savePicDialog = new SavePicDialog(projectName, rayFrame, this, surfex_);
174       
175       
176        // int i, String filename, RayFrame ray, surfex su,
177        // jv4surfex jv4sx
178        eqAdm = new EquationAdmin(proNr, "", surfex_.rayFrame, surfex_,
179                                  surfex_.jv4sx, this);
180        cuAdm = new CurveAdmin(proNr, "", surfex_, this);
181       
182        setLayout(new BorderLayout());//new GridLayout(3,1));
183        JPanel toplinePanel = new JPanel(new BorderLayout());
184        JPanel toplineGridPanel = new JPanel(new GridLayout(2, 1));
185        JPanel controlpanel = new JPanel(new FlowLayout());
186        JPanel globalConfigPanel = new JPanel(new FlowLayout());
187        JPanel leftFlushGCPanel = new JPanel(new BorderLayout());
188       
189       
190        toplinePanel.add(toplineGridPanel, BorderLayout.WEST);
191        toplineGridPanel.add(controlpanel);
192        leftFlushGCPanel.add(globalConfigPanel, BorderLayout.WEST);
193        toplineGridPanel.add(leftFlushGCPanel);
194       
195       
196        add(toplinePanel, BorderLayout.NORTH);
197       
198        parAdmin = new ParameterAdmin(surfex_, this);
199        JSplitPane tempPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
200                                              new JScrollPane(eqAdm), new JScrollPane(cuAdm));
201        tempPanel.setDividerLocation(160);
202        JSplitPane tempPanel1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
203                                               new JScrollPane(parAdmin), tempPanel);
204        tempPanel1.setDividerLocation(60);
205        //add(tempPanel1,BorderLayout.CENTER);
206        //tempPanel.add(new JScrollPane(eqAdm));
207        //tempPanel.add(new JScrollPane(cuAdm));
208       
209        solPtsAdm = new SolitaryPointsAdmin(surfex_, this);
210        JSplitPane tempPanel2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
211                                               tempPanel1, new JScrollPane(solPtsAdm));
212        tempPanel2.setDividerLocation(300);
213        add(tempPanel2, BorderLayout.CENTER);
214       
215        //JPanel controlpanel=new JPanel();
216       
217        //this.add(controlpanel, BorderLayout.NORTH);
218       
219        JButton closeProject;
220        try {
221            if ((surfex_.filePrefix).equals("")) {
222                closeProject = new JButton(new ImageIcon("images/closeProject.gif"));
223            } else {
224                closeProject = new JButton(new ImageIcon(new URL(
225                                                             surfex_.filePrefix + 
226                                                             "images/closeProject.gif")));
227            }
228        } catch (MalformedURLException e) {
229            System.out.println(e.toString());
230            closeProject = new JButton();
231        }
232        closeProject.setToolTipText("close this project");
233        closeProject.addActionListener(this);
234        closeProject.setActionCommand("closeProject");
235        if (!surfex_.inAnApplet) {
236//          controlpanel.add(closeProject);
237        }
238       
239        // some space:
240        controlpanel.add(new JLabel("   "));
241       
242        JButton save;
243        try {
244            if ((surfex_.filePrefix).equals("")) {
245                save = new JButton(new ImageIcon("images/save.gif"));
246            } else {
247                save = new JButton(new ImageIcon(new URL(surfex_.filePrefix
248                                                         + "images/save.gif")));
249            }
250        } catch (MalformedURLException e) {
251            System.out.println(e.toString());
252            save = new JButton();
253        }
254        save.setToolTipText("save this project");
255        save.addActionListener(this);
256        save.setActionCommand("save");
257        if (!surfex_.inAnApplet) {
258            //controlpanel.add(save);
259        }
260       
261        JButton saveas;
262        try {
263            if ((surfex_.filePrefix).equals("")) {
264                saveas = new JButton(new ImageIcon("images/saveas.gif"));
265            } else {
266                saveas = new JButton(new ImageIcon(new URL(surfex_.filePrefix
267                                                           + "images/saveas.gif")));
268            }
269        } catch (MalformedURLException e) {
270            System.out.println(e.toString());
271            saveas = new JButton();
272        }
273        saveas.setToolTipText("save this Project as...");   
274       
275        saveas.addActionListener(this);
276        saveas.setActionCommand("saveas");
277        //if(!surfex_.inAnApplet) {
278        controlpanel.add(saveas);
279        //}
280       
281        // button1:
282        bt3.setText("your files");
283        bt3.setToolTipText("show your files in a new browser window");
284        //    getContentPane().add(bt2);
285        bt3.addActionListener(new ActionListener() {
286                public void actionPerformed(ActionEvent evt) {
287                    bt3ActionPerformed(evt);
288                }
289            });
290        if (surfex_.inAnApplet) {
291            controlpanel.add(bt3);
292        }
293       
294        JButton savePic;
295        try {
296            if ((surfex_.filePrefix).equals("")) {
297                savePic = new JButton(new ImageIcon("images/savePic.gif"));
298            } else {
299                savePic = new JButton(new ImageIcon(new URL(surfex_.filePrefix
300                                                            + "images/savePic.gif")));
301            }
302        } catch (MalformedURLException e) {
303            System.out.println(e.toString());
304            savePic = new JButton();
305        }
306        savePic.setToolTipText("save this raytraced picture as...");
307        savePic.addActionListener(this);
308        savePic.setActionCommand("savePic");
309        controlpanel.add(savePic);
310       
311        JButton saveMovie;
312        try {
313            if ((surfex_.filePrefix).equals("")) {
314                saveMovie = new JButton(new ImageIcon("images/saveMovie.gif"));
315            } else {
316                saveMovie = new JButton(new ImageIcon(new URL(surfex_.filePrefix
317                                                              + "images/saveMovie.mov")));
318            }
319        } catch (MalformedURLException e) {
320            System.out.println(e.toString());
321            saveMovie = new JButton();
322        }
323        saveMovie.setToolTipText("create and save a movie of this surface as...");
324        saveMovie.addActionListener(this);
325        saveMovie.setActionCommand("saveMovie");
326        controlpanel.add(saveMovie);
327       
328        JButton lampButt;
329        try {
330            if ((surfex_.filePrefix).equals("")) {
331                lampButt = new JButton(new ImageIcon("images/lamps.gif"));
332            } else {
333                lampButt = new JButton(new ImageIcon(new URL(surfex_.filePrefix
334                                                             + "images/lamps.gif")));
335            }
336        } catch (MalformedURLException e) {
337            System.out.println(e.toString());
338            lampButt = new JButton();
339        }
340        lampButt.setToolTipText("configure project's lamps");
341        lampButt.addActionListener(this);
342        lampButt.setActionCommand("configure lamps");
343//    controlpanel.add(lampButt);
344       
345        // some space:
346        controlpanel.add(new JLabel("   "));
347       
348        //
349        bt6.setText("add param.");
350        bt6.setToolTipText("add a parameter");
351        //    getContentPane().add(bt1);
352        bt6.addActionListener(new ActionListener() {
353                public void actionPerformed(ActionEvent evt) {
354                    bt6ActionPerformed(evt);
355                }
356            });
357        controlpanel.add(bt6);
358       
359        //
360        bt1.setText("add eqn.");
361        bt1.setToolTipText("add an equation");
362        //    getContentPane().add(bt1);
363        bt1.addActionListener(new ActionListener() {
364                public void actionPerformed(ActionEvent evt) {
365                    bt1ActionPerformed(evt);
366                }
367            });
368        controlpanel.add(bt1);
369       
370        //
371        bt5.setText("add curve");
372        bt5.setToolTipText("add a curve");
373        //   getContentPane().add(bt1);
374        bt5.addActionListener(new ActionListener() {
375                public void actionPerformed(ActionEvent evt) {
376                    bt5ActionPerformed(evt);
377                }
378            });
379        controlpanel.add(bt5);
380       
381    //
382        bt7.setText("add pt.");
383        bt7.setToolTipText("add a point");
384        bt7.addActionListener(new ActionListener() {
385                public void actionPerformed(ActionEvent evt) {
386                    bt7ActionPerformed(evt);
387                }
388            });
389        controlpanel.add(bt7);
390       
391        // some space:
392        controlpanel.add(new JLabel("   "));
393       
394        //
395        bt2.setText("raytrace");
396        bt2.setEnabled(false);
397        bt2.setToolTipText("raytrace");
398        //    getContentPane().add(bt2);
399        bt2.addActionListener(new ActionListener() {
400                public void actionPerformed(ActionEvent evt) {
401                    bt2ActionPerformed(evt);
402                }
403            });
404//      controlpanel.add(bt2);
405       
406        //
407        //controlpanel.add(new JLabel("permanently"));
408        //    getContentPane().add(bt4);
409        raytraceAlways.addChangeListener(new ChangeListener() {
410                public void stateChanged(ChangeEvent evt) {
411                    raytraceAlwaysChanged(evt);
412                }
413            });
414        raytraceAlways.setEnabled(false);
415//      controlpanel.add(raytraceAlways);
416       
417        // some space:
418        //controlpanel.add(new JLabel(" "));
419       
420        // a checkbox for activating antialiasing
421        globalConfigPanel.add(antialiasing);
422       
423        Vector temp = new Vector();
424        temp.add(new Integer(1));
425        temp.add(new Integer(2));
426        temp.add(new Integer(4));
427        temp.add(new Integer(8));
428        interpolConstBox = new JComboBox(temp);
429        interpolConstBox.setSelectedIndex(1);
430        interpolConstBox.addActionListener(new ActionListener() {
431                public void actionPerformed(ActionEvent evt) {
432                    setInterpolConst(((Integer)(interpolConstBox.getSelectedItem())).intValue());
433                }
434            });
435        globalConfigPanel.add(new JLabel(" preview quality:"));
436        globalConfigPanel.add(interpolConstBox);
437       
438        // a button for choosing the background color:
439        bgColorButton = new JButton();
440        bgColorButton.setBackground(Color.white);
441        bgColorButton.setToolTipText("select the background color");
442        bgColorButton.addActionListener(new ActionListener() {
443                public void actionPerformed(ActionEvent evt) {
444                    bgColorButton.setBackground(JColorChooser.showDialog(null,
445                                                                         "change the background color", bgColorButton
446                                                                         .getBackground()));
447                }
448            });
449        globalConfigPanel.add(new JLabel(" background-color:"));
450        globalConfigPanel.add(bgColorButton);
451
452        clipMode.insertItemAt("sphere", 0);
453        clipMode.insertItemAt("tetrahedron", 1);
454        clipMode.insertItemAt("cube", 2);
455        clipMode.insertItemAt("octahedron", 3);
456        clipMode.insertItemAt("dodecahedron", 4);
457        clipMode.insertItemAt("icosahedron", 5);
458        clipMode.insertItemAt("cylinder", 6);
459        clipMode.insertItemAt("user-defined", 7);
460        clipMode.insertItemAt("none", 8);
461        clipMode.setSelectedIndex(0);
462
463        globalConfigPanel.add(new JLabel(" clip-mode:"));
464        globalConfigPanel.add(clipMode);
465        globalConfigPanel.add(new JLabel(" clip-radius:"));
466        globalConfigPanel.add(clipRadius);
467       
468        /*   // the Slider for scaling:
469             scaleSlider = new JSlider(1, 1000);
470             scaleLabel = new JLabel("1.0");
471             scaleSlider.setValue(100);
472             scaleSlider.setMinorTickSpacing(10);
473             scaleSlider.setMajorTickSpacing(100);
474             scaleSlider.setPaintTicks(true);
475             Hashtable labelTable = new Hashtable();
476             labelTable.put(new Integer(10), new JLabel("1"));
477             labelTable.put(new Integer(500), new JLabel("5"));
478             labelTable.put(new Integer(1000), new JLabel("10"));
479             scaleSlider.setLabelTable(labelTable);
480             scaleSlider.addChangeListener(new ChangeListener() {
481             public void stateChanged(ChangeEvent evt) {
482             String str = "" + scaleSlider.getValue() / 100.0;
483             int end = 6;
484             if (str.length() <= 6) {
485          end = str.length();
486        }
487        scaleLabel.setText(str.substring(0, end));
488        scaleSliderUpdated();
489      }
490    });
491    // we do not show the scaleSlider for the moment,
492    // because it does not work yet.
493    globalConfigPanel.add(new JLabel("scale:"));
494    globalConfigPanel.add(scaleSlider);
495    globalConfigPanel.add(scaleLabel);
496*/
497    // button4:
498    //bt4.setText("raytrace always");
499    //bt4.setToolTipText("raytrace always");
500    //  controlpanel.add(bt4);
501
502
503       
504        rand=(int)(Math.random()*2000000000);
505       
506        // muss nach pqrameterAdmin instanziert werden, da auf paramComboBox zurueckgegriffen wird
507        saveMovieDialog = new SaveMovieDialog(projectName, rayFrame, this, surfex_);
508       
509        lampAdmin=new LampAdmin(surfex_,this);
510       
511       
512//    jv4sx.disp.getCamera().setScale(1.0);   
513        jv4sx.setScale(1.0);   
514//    System.out.println("Scale:" + jv4sx.getScale());
515       
516    }
517
518    public void clear() {
519        eqAdm.clear();
520        cuAdm.clear();
521        parAdmin.clear();
522        solPtsAdm.clear();
523        newEquation();
524    }
525
526    public void setRootFinder(String s) {
527        rootFinder = s;
528    }
529
530    public String getRootFinder() {
531        return(rootFinder);
532    }
533   
534    public void setEpsilon(String s) {
535        epsilon = s;
536    }
537
538    public String getEpsilon() {
539        return(epsilon);
540    }
541   
542    public double getScale() {
543        return(scale);
544    }
545
546    public int getClipMode() {
547        return(clipMode.getSelectedIndex());
548    }
549
550    public String getClipRadius() {
551        return(clipRadius.getText());
552    }
553   
554    public void setScale(double s) {
555//      System.out.println("setScale P:"+s);
556        scale = s;
557//      jv4sx.setScale(s);
558    }
559   
560    public void setInterpolConst(int c) {
561        interpolConst = c;
562    }
563
564    public int getInterpolConst() {
565        return(interpolConst);
566    }
567 
568  public Vector getAllLamps(){
569        return lampAdmin.getAllLamps();
570  }
571
572  public void bt1ActionPerformed(ActionEvent evt) {
573    newEquation();
574  }
575
576  public void newEquation() {
577    eqAdm.newEquation();
578    cuAdm.equationListChanged_add(new Integer(((Equation) eqAdm.eqnList
579        .lastElement()).eqnr).toString());
580    solPtsAdm.equationListChanged_add(new Integer(((Equation) eqAdm.eqnList
581        .lastElement()).eqnr).toString());
582   }
583
584  public void bt6ActionPerformed(ActionEvent evt) {
585    newParameter();
586  }
587 
588  public boolean eqauls(Project p){
589        if(p!=null){
590                return (this.rand==p.rand);
591               
592        }else{
593                System.out.println("error in project.equals(project p):Nullpinter");
594        }
595        return false;
596       
597  }
598
599  public void newParameter() {
600    parAdmin.newParameter();
601  }
602
603  public void newCurve() {
604    cuAdm.newCurve();
605  }
606
607  public void bt2ActionPerformed(ActionEvent evt) {
608    // raytracen
609    String fn = surfex_.tmpDir + "test.png";
610    if (surfex_.configFrame.surf.isSelected()) {
611      fn = surfex_.tmpDir + tmp_surfex_file;
612    } else {
613      fn = surfex_.tmpDir + "test.png";
614    }
615    SavePic saveThread = new SavePic(fn, false, antialiasing.isSelected(),
616        rayFrame.getContentPane().getSize().height
617            / getInterpolConst(), rayFrame.getContentPane()
618            .getSize().width
619            / getInterpolConst(), 72, surfex_, this, this.jv4sx.getCamPos(),this.jv4sx.getViewDir(),this.jv4sx.getUpVector(),this.jv4sx.getRightVector(),parAdmin.getAllParams(),parAdmin.getAllParamValues(),this.jv4sx,this.getAllLamps());
620    try {
621      saveThread.start();
622      saveThread.join();
623    } catch (InterruptedException e) {
624
625    }
626    // thread ist fertig
627    //System.out.println("finally");
628    if (surfex_.OS == surfex_.osWINDOWS) {
629      Runtime r = Runtime.getRuntime();
630      Process p;
631      try {
632        if (getInterpolConst() != 1) {
633//          System.out.println("resample");
634          p = r.exec(surfex_.configFrame.iviewPath.getText() + " "
635              + surfex_.tmpDir + "test.png /resample=(0,"
636              + rayFrame.getContentPane().getSize().height
637              + ") /convert=" + surfex_.tmpDir + "test.png");
638        } else {
639
640          p = r.exec("iview\\i_view32.exe " + surfex_.tmpDir
641              + "test.png /convert=" + surfex_.tmpDir
642              + "test.png");
643
644        }
645        p.waitFor();
646        eqAdm.img_filename = surfex_.tmpDir + "test.png";
647      } catch (Exception er) {
648        System.out.println(er);
649      }
650    }
651    if (!rayFrame.isVisible()) {
652      //System.out.println("setVisible rayFrame");
653      //rayFrame.setVisible(true);
654    }
655    rayFrame.changeBackground(eqAdm.img_filename);
656
657    SwingUtilities.updateComponentTreeUI(bt4);
658  } // end of bt2ActionPerformed()
659
660  public void bt3ActionPerformed(ActionEvent evt) {
661    // open the temporary directory for the current project
662    // in a new browser window
663    try {
664      surfex_.theApplet.getAppletContext().showDocument(
665          new URL(surfex_.theApplet.getCodeBase() + "/"
666              + old_tmpsubdir), "_blank");
667    } catch (Exception e) {
668      System.out.println(e.toString());
669    }
670  } // end of bt3ActionPerformed()
671
672  public void bt4ActionPerformed(ActionEvent evt) {
673  }
674
675  public void startRaytraceAlways() {
676    surfex_.configFrame.last[4] = true;
677    double ang[] = this.eqAdm.getAngles();
678    double scale[] = this.eqAdm.getScales();
679    surfex_.newUpdateRayframeImmediatlyThread();
680    surfex_.updateRayframeImmediatlyThread.start();
681  }
682
683  public void raytraceAlwaysChanged(ChangeEvent evt) {
684    if (raytraceAlways.isSelected() && !surfex_.configFrame.last[4]
685        && surfex_.pane.getSelectedIndex() >= 0) {
686      startRaytraceAlways();
687    }
688    if (!raytraceAlways.isSelected() && surfex_.configFrame.last[4]) {
689      surfex_.configFrame.last[4] = false;
690      surfex_.updateRayframeImmediatlyThread.stop();
691    }
692  } // end of bt4ActionPerformed()
693
694  public void bt5ActionPerformed(ActionEvent evt) {
695    cuAdm.newCurve();
696  } // end of bt5ActionPerformed()
697
698  public Lamp getSelectedLamp(){
699        if(lampAdmin==null){
700                System.out.println("lampAdmin null");
701        }
702        return lampAdmin.getSelectedLamp();
703  }
704  public void bt7ActionPerformed(ActionEvent evt) {
705    solPtsAdm.newSolitaryPoint();
706  } // end of bt7ActionPerformed()
707
708  public void closeProject() {
709    surfex_.closeProject(ProjectNumber);
710  }
711
712  private void save_as_and_close_Project() {
713    String internalname = projectName; // interneer name des aktuell foku
714    // creating new Frame
715    final JFrame frame2 = new JFrame("save" + internalname + " as");
716
717    // adding container
718    Container contentPane = frame2.getContentPane();
719
720    // creating Filechooser
721    JFileChooser fileChooser = new JFileChooser(surfex_.currentDirectory);
722
723    // adding filechooser
724    contentPane.add(fileChooser, BorderLayout.CENTER);
725
726    // Create ActionListener
727    ActionListener actionListener = new ActionListener() {
728      public void actionPerformed(ActionEvent actionEvent) {
729        JFileChooser theFileChooser = (JFileChooser) actionEvent.getSource();
730
731        // get command
732        String command = actionEvent.getActionCommand();
733
734        // check if doubleclickt or if "open" was pressed
735        if (command.equals(JFileChooser.APPROVE_SELECTION)) {
736
737          File selectedFile = theFileChooser.getSelectedFile();
738
739          // making savepopup invisible/removing it
740          frame2.dispose();
741
742          // creating complete filepath
743          String filelocation = new String(selectedFile.getParent()
744              + "\\" + selectedFile.getName());
745
746          // ausgabe fehler bein z.B.: d:\ -> d:\\ bzw \ -> \\
747//          System.out.println("filelocation : " + filelocation + "\n");
748          save(filelocation);
749          // this is not the cancel-button
750          // } else if (command.equals(JFileChooser.CANCEL_SELECTION))
751          // {
752        }
753        closeProject();
754      }
755    };
756
757    // Adding filefilters
758    // how to get "all common files"&"common image files" in current
759    // language?
760    // no global array! -> you have to carry the readmenuentries[] with you
761    // through
762    // all maethods as argument @aufruf (?selection?)
763    FileFilter acfFilter = new ExtensionFileFilter("all common files",
764        new String[] { "JPG", "JPEG", "jpg", "jpeg",
765        //"gif", "tif",
766            //"spc",
767//            "sun"
768        });
769    fileChooser.addChoosableFileFilter(acfFilter);
770
771    FileFilter figFilter = new ExtensionFileFilter("*.fig",
772        new String[] { "fig" });
773    //fileChooser.addChoosableFileFilter(figFilter);
774
775    FileFilter suxFilter = new ExtensionFileFilter("*.spc (spicy-file)",
776        new String[] { "spc" });
777    //fileChooser.addChoosableFileFilter(suxFilter);
778
779    // adding actionListener
780    fileChooser.addActionListener(actionListener);
781    frame2.pack();
782    frame2.setVisible(true);
783  }
784
785  public void showSavePicDialog() {
786    if (antialiasing.isSelected()) {
787      savePicDialog.colorType.setSelectedIndex(2);//.setSelected(true);
788    } else {
789      savePicDialog.colorType.setSelectedIndex(0);//antialiasing.setSelected(false);
790    }
791    savePicDialog.setVisible(true);
792  }
793 
794  public void showSaveMovieDialog() {
795      if (antialiasing.isSelected()) {
796          saveMovieDialog.antialiasingQuality.setSelectedIndex(5);
797      } else {
798          saveMovieDialog.antialiasingQuality.setSelectedIndex(0);
799      }
800      surfex_.showMoviePreview = true;
801      saveMovieDialog.setVisible(true);
802  }
803   
804    public String getSuxFile() {
805        String str = "";
806    str += "this is surfex v" + surfex_.strVersion + "\n";
807    str += "///////// TYPE: //////" + "\n";
808    str += "complete\n";
809    str += "///////// GENERAL DATA: //////" + "\n";
810    str += jv4sx.disp.getCamera().getUpVector().getEntry(0) + "\n";
811    str += jv4sx.disp.getCamera().getUpVector().getEntry(1) + "\n";
812    str += jv4sx.disp.getCamera().getUpVector().getEntry(2) + "\n";
813    str += jv4sx.disp.getCamera().getViewDir().getEntry(0) + "\n";
814    str += jv4sx.disp.getCamera().getViewDir().getEntry(1) + "\n";
815    str += jv4sx.disp.getCamera().getViewDir().getEntry(2) + "\n";
816    str += jv4sx.disp.getCamera().getPosition().getEntry(0) + "\n";
817    str += jv4sx.disp.getCamera().getPosition().getEntry(1) + "\n";
818    str += jv4sx.disp.getCamera().getPosition().getEntry(2) + "\n";
819    str += jv4sx.disp.getCamera().getInterest().getEntry(0) + "\n";
820    str += jv4sx.disp.getCamera().getInterest().getEntry(1) + "\n";
821    str += jv4sx.disp.getCamera().getInterest().getEntry(2) + "\n";
822    str += jv4sx.getScale() + "\n";
823    str += jv4sx.scaleMin + "\n";
824    str += jv4sx.scaleMax + "\n";
825    str += bgColorButton.getBackground().getRed() + "\n";
826    str += bgColorButton.getBackground().getGreen() + "\n";
827    str += bgColorButton.getBackground().getBlue() + "\n";
828    str += antialiasing.isSelected() + "\n";
829    str += getClipMode() + "\n";
830    str += getClipRadius() + "\n";
831
832    str += eqAdm.save();
833    str += cuAdm.save();
834    str += parAdmin.save();
835    str += solPtsAdm.save();
836    return (str);
837  }
838
839    public void save(String filelocation) {
840        if (surfex_.inAnApplet) {
841            try {
842                // ??? this should be done for all open projects!!!
843                URL url = new URL(surfex_.webPrgs.surfPath.getText()
844                                  + "surfex_comp.php");
845                URLConnection connection = url.openConnection();
846                connection.setUseCaches(false);
847                connection.setDoOutput(true);
848               
849                PrintWriter out = new PrintWriter(connection.getOutputStream());
850                //System.out.println("save
851                // tmpdir:"+surfex_.getCurrentProject().old_tmpsubdir);
852                out
853                    .print("tmpsubdir="
854                           + URLEncoder
855                           .encode(this.old_tmpsubdir)
856                           + "&prg=save" + "&filelocation="
857                           + URLEncoder.encode(filelocation)
858                           + "&prg_code_part1="
859                           + URLEncoder.encode(getSuxFile()));
860                out.close();
861               
862                BufferedReader in = new BufferedReader(new InputStreamReader(
863                                                           connection.getInputStream()));
864                String inputLine = in.readLine();
865                if (inputLine.equals("okay")) {
866                    // it worked
867                    inputLine = in.readLine();
868                    this.old_tmpsubdir = in.readLine();
869                }
870            } catch (Exception e) {
871                System.out.println("closeSurfex:" + e.toString());
872            }
873                   
874            return;
875        }
876       
877        // Project speichern
878        // lege datei an oder ueberschreibe
879        if (filename == null) {
880            System.out.println("null file");
881        } else {
882            try {
883                String proName = ((new File(filelocation)).getCanonicalPath()).substring
884                    (((new File(filelocation)).getCanonicalPath()).lastIndexOf(File.separator)+1);
885                projectName = proName;
886                surfex_.pane.setTitleAt(surfex_.pane.getSelectedIndex(), projectName);
887            } catch(Exception ex) {
888                System.out.println("Exception in loadProject:"+ex.toString());
889            }
890            //System.out.println("filenem");
891        }
892        try {
893            FileOutputStream fo = new FileOutputStream(filename);
894            /*
895             * JDK-1.0-Version:
896             *
897             * PrintStream ps = new PrintStream(fo); ps.println(dStr);
898             */
899            PrintWriter pw = new PrintWriter(fo, true);
900            pw.println(getSuxFile());
901            pw.close();
902        } catch (IOException er) {
903            System.out.println(er);
904        }
905    }
906   
907    // Ereignismethoden
908    public void actionPerformed(ActionEvent e) {
909        String command = e.getActionCommand();
910        //  System.out.println(command );
911       
912        actionCommand(command);
913  }
914
915  public void actionCommand(String command) {
916
917    if (command == "save") {
918      if (filename == null) {
919        command = "saveas";
920      } else {
921        save(filename);
922      }
923    }
924
925    if (command == "saveas") {
926      // creating new Frame
927      final JFrame frame2 = new JFrame("save " + projectName + " as");
928
929      // adding container
930      Container contentPane = frame2.getContentPane();
931
932      if (surfex_.inAnApplet) {
933        ActionListener aL = new ActionListener() {
934          public void actionPerformed(ActionEvent actionEvent) {
935            String command2 = actionEvent.getActionCommand();
936            //System.out.println("com:"+command2);
937            // check if doubleclickt or if "open" was pressed
938            if (command2.equals("  save  ")) {
939              // creating complete filepath
940              String filelocation = tf.getText();
941              //System.out.println("file:"+filelocation);
942              // ausgabe fehler bein z.B.: d:\ -> d:\\ bzw \ -> \\
943              //          System.out.println("filelocation : "+
944              // filelocation + "\n");
945              save(filelocation);
946
947              frame2.dispose();
948            } else {
949              //if(command.equals(JFileChooser.CANCEL_SELECTION)){
950              frame2.dispose();
951              //}
952            }
953          }
954        };
955        contentPane.setLayout(new BorderLayout());
956        contentPane.add(new JLabel("Give filename: "),
957            BorderLayout.WEST);
958        tf = new JTextField("test.sux");
959        contentPane.add(tf, BorderLayout.CENTER);
960        JPanel tmpPanel = new JPanel();
961        tmpPanel.setLayout(new FlowLayout());
962        //String strFormats[] = { "sux" };
963        JButton jbsave = new JButton("  save  ");
964        jbsave.addActionListener(aL);
965        tmpPanel.add(jbsave);
966        JButton jbcancel = new JButton("  cancel  ");
967        jbcancel.addActionListener(aL);
968        tmpPanel.add(jbcancel);
969        contentPane.add(tmpPanel, BorderLayout.EAST);
970        frame2.setSize(500, 75);
971      } else {
972        // creating Filechooser
973        JFileChooser fileChooser = new JFileChooser(
974            surfex_.currentDirectory);
975
976        // adding filechooser
977        contentPane.add(fileChooser, BorderLayout.CENTER);
978
979        // Create ActionListener
980        ActionListener actionListener = new ActionListener() {
981          public void actionPerformed(ActionEvent actionEvent) {
982            JFileChooser theFileChooser = (JFileChooser) actionEvent
983                .getSource();
984
985            // get command2
986            String command2 = actionEvent.getActionCommand();
987
988            // check if doubleclickt or if "open" was pressed
989            if (command2.equals(JFileChooser.APPROVE_SELECTION)) {
990
991              File selectedFile = theFileChooser
992                  .getSelectedFile();
993
994              // making savepopup invisible/removing it
995              frame2.dispose();
996
997              // creating complete filepath
998              String filelocation = new String(selectedFile
999                  .getParent()
1000                  + File.separator + selectedFile.getName());
1001
1002              // ausgabe fehler bein z.B.: d:\ -> d:\\ bzw \ -> \\
1003              //          System.out.println("filelocation: "+
1004              // filelocation);
1005
1006              //                   speicher den Kram unter filename;
1007              //                     filename=filelocation+"_"+projectName+".sux";
1008              if (filelocation.indexOf(".sux") == -1) {
1009                filename = filelocation + ".sux";
1010              } else {
1011                filename = filelocation;
1012              }
1013              save(filename);
1014            } else {
1015              //if(command.equals(JFileChooser.CANCEL_SELECTION)){
1016              frame2.setVisible(false);
1017              //}
1018            }
1019          }
1020        };
1021
1022        FileFilter acfFilter = new ExtensionFileFilter(
1023            "all common files", new String[] { "sux" });
1024//        fileChooser.addChoosableFileFilter(acfFilter);
1025
1026        FileFilter suxFilter = new ExtensionFileFilter(
1027            "*.sux (sux-file)", new String[] { "sux" });
1028        fileChooser.addChoosableFileFilter(suxFilter);
1029        fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
1030        String theName = "";
1031        try {
1032            File lastFile = new File(filename);
1033            theName = lastFile.getName();
1034            fileChooser.setSelectedFile(lastFile);
1035//          System.out.println("name:"+theName);
1036        } catch(Exception fileEx) {
1037            System.out.println("fileEx:"+fileEx.toString());
1038        }
1039
1040
1041        // adding actionListener
1042        fileChooser.addActionListener(actionListener);
1043      }
1044      frame2.pack();
1045      frame2.setVisible(true);
1046    }
1047
1048    if (command == "closeProject") {
1049      // schliesse dieses Projekt
1050      // popup with questions, if you want to save before leavin
1051      Object[] butts = { "Ja", "Nein", "Abbrechen" };
1052      String initbutt = "Ja";
1053      int optReturn = JOptionPane.showConfirmDialog(new Panel(),
1054          "Do you want to save \"" + projectName
1055              + "\" before leaving?\n", " close?",
1056          JOptionPane.YES_NO_CANCEL_OPTION,
1057          JOptionPane.QUESTION_MESSAGE);
1058      //System.out.println(optReturn);
1059      switch (optReturn) {
1060      case 0:
1061        // ja, speichern
1062        save_as_and_close_Project();
1063        break;
1064      case 1:
1065        //nein , ggf. sicherungskopie anlegen
1066        // Projekt schliessen
1067        closeProject();
1068        break;
1069      case 2: // abbrechen, also nic machen ...
1070        break;
1071      }
1072    }
1073
1074    if (command == "savePic") {
1075      // tollen Bildspeicherdialog anzeigen ....
1076      showSavePicDialog();
1077    }
1078   
1079    if (command == "saveMovie") {
1080      // tollen Bildspeicherdialog anzeigen ....
1081      showSaveMovieDialog();
1082    }
1083   
1084    if(command =="configure lamps"){
1085        lampAdmin.showall();
1086    }
1087  }
1088
1089  public void saveFile(String filelocation, int savetype, boolean dithered,
1090      boolean antialiasing, int h, int w, int d,jv4surfex jv4sx) {
1091    //System.out.println("saveFile..."+antialiasing+",
1092    // savetype:"+savetype);
1093    if (savetype == 0) {
1094        eqAdm.saveSurfCode(filelocation, filelocation + ".jpg",
1095                           antialiasing,jv4sx);
1096    }
1097    if (savetype == 1) {
1098/*
1099  eqAdm.savePovCode(filelocation);
1100*/
1101    }
1102    if (savetype == 2) {
1103        eqAdm.saveSurfCode(filelocation, filelocation + ".jpg",
1104                           antialiasing,jv4sx);
1105    }
1106    if (savetype == 3) {
1107        SavePic saveThread = new SavePic(filelocation, dithered,
1108                                         antialiasing, h, w, d, surfex_, this,this.jv4sx.getCamPos(),
1109                                         this.jv4sx.getViewDir(),this.jv4sx.getUpVector(),this.jv4sx.getRightVector(),
1110                                         parAdmin.getAllParams(),parAdmin.getAllParamValues(),
1111                                         this.jv4sx,this.getAllLamps()); 
1112       
1113        saveThread.start();
1114    }
1115  } // end of saveFile()
1116 
1117 
1118   
1119    public void loadProject(String filelocation) {
1120        try {
1121            filename = filelocation;
1122            String proName = ((new File(filelocation)).getCanonicalPath()).substring
1123                (((new File(filelocation)).getCanonicalPath()).lastIndexOf(File.separator)+1);
1124            projectName = proName;
1125            surfex_.pane.setTitleAt(surfex_.pane.getSelectedIndex(), projectName);
1126        } catch(Exception ex) {
1127            System.out.println("Exception in loadProject:"+ex.toString());
1128        }
1129//      System.out.println("file:"+filelocation);
1130        File fi = new File(filelocation);
1131        try {
1132            FileInputStream fs = new FileInputStream(fi);
1133//          System.out.println("input.");
1134            BufferedReader bs = new BufferedReader(new InputStreamReader(fs));
1135//          System.out.println("stream");
1136//          System.out.println("loadProject bs...");
1137            loadProject(bs);
1138//          System.out.println("loaded.");
1139        } catch (Exception e) {
1140            System.out.println(e.toString());
1141        }
1142    }
1143   
1144    public void loadProject(URL url) {
1145        try {
1146            BufferedReader bs = new BufferedReader(
1147                new InputStreamReader(url.openStream()));
1148            loadProject(bs);
1149        } catch (Exception e) {
1150            System.out.println(e.toString());
1151        }
1152    }
1153   
1154    public void loadProject(BufferedReader bs) {
1155        try {
1156            String version = bs.readLine().substring(16);
1157            String version_num = version.substring(0, version.indexOf(".")) 
1158                + version.substring(version.indexOf(".")+1, version.indexOf(".")+3)
1159                + version.substring(version.indexOf(".")+4);
1160            int iVersion = Integer.parseInt(version_num);
1161           
1162//          System.out.println("ver:"+iVersion);
1163            String nix = bs.readLine();
1164            String datatype = bs.readLine(); // one out of: "complete", "specify",
1165            // "onlyeqns", "onlystyle", "eqnsvis"
1166            //System.out.println("datatype:"+datatype);
1167            if (datatype.equals("specify")) {
1168                try {
1169                    String whichinfo;
1170                    bs.mark(255);
1171                    whichinfo = bs.readLine();
1172                    //System.out.println("whichinfo:"+whichinfo);
1173                    while (whichinfo.equals("GENERAL DATA:")
1174                           || whichinfo.equals("EQUATIONS:")
1175                           || whichinfo.equals("CURVES:")
1176                           || whichinfo.equals("PARAMETERS:")
1177                           || whichinfo.equals("SOLITARY POINTS:")) {
1178                        bs.reset();
1179//                      System.out.println("general data");
1180                        if (whichinfo.equals("GENERAL DATA:")) {
1181                            eqAdm.loadGeneralData(bs, datatype, iVersion);
1182                        }
1183//                      System.out.println("equations");
1184                        if (whichinfo.equals("EQUATIONS:")) {
1185                            eqAdm.loadEquations(bs, datatype, iVersion);
1186                        }
1187//                      System.out.println("curves");
1188                        if (whichinfo.equals("CURVES:")) {
1189                            cuAdm.loadCurves(bs, datatype, iVersion);
1190                        }
1191//                      System.out.println("parameters");
1192                        if (whichinfo.equals("PARAMETERS:")) {
1193                            parAdmin.loadParameters(bs, datatype, iVersion);
1194                        }
1195//                      System.out.println("solitary points");
1196                        if (whichinfo.equals("SOLITARY POINTS:")) {
1197                            solPtsAdm.loadSolitaryPoints(bs, datatype, iVersion);
1198                        }
1199                        bs.mark(255);
1200                        whichinfo = bs.readLine();
1201                        //System.out.println("whichinfo:"+whichinfo);
1202                    }
1203                } catch (Exception ex) {
1204                    bs.reset();
1205                }
1206            } else {
1207                // i.e. "complete":
1208                eqAdm.loadGeneralData(bs, datatype, iVersion);
1209                eqAdm.loadEquations(bs, datatype, iVersion);
1210                cuAdm.loadCurves(bs, datatype, iVersion);
1211                parAdmin.loadParameters(bs, datatype, iVersion);
1212                solPtsAdm.loadSolitaryPoints(bs, datatype, iVersion);
1213            } // end of else
1214           
1215        } catch (IOException e) {
1216            System.out.println("loadProject-Error:" + e);
1217        }
1218    }
1219   
1220    public void scaleSliderUpdated() {
1221        //System.out.println("ssUpd:"+Double.parseDouble(scaleLabel.getText()));
1222        //  eqAdm.lastScale = Double.parseDouble(scaleLabel.getText());
1223        //System.out.println("scale:"+scaleLabel.getText());
1224//    System.out.println("setscale:"+eqAdm.lastScale);
1225//    eqAdm.jv4sx.disp.getCamera().setScale(eqAdm.lastScale);
1226        jv4sx.setScale(eqAdm.lastScale);
1227        eqAdm.jv4sx.updateDisp();
1228        //surfex_.raytrace();
1229        //eqAdm.updateJV4SXandReconstructLastView();
1230       
1231    }
1232} // end class Project
1233
Note: See TracBrowser for help on using the repository browser.