00001 package gui.Main;
00002
00003 import gui.Factories.EmptyJFrame;
00004 import gui.InputOutput.ObjectReaderWriter;
00005 import gui.OpenGL.JoglNativeTest;
00006 import gui.Toolbars.FileChangeHandler;
00007 import gui.Toolbars.FileChangeListener;
00008 import gui.Toolbars.FileChangePublisher;
00009 import gui.Widgets.MyImageDisplay;
00010 import gui.Widgets.MyImageDisplayListerner;
00011
00012 import java.io.FileNotFoundException;
00013 import java.lang.reflect.InvocationTargetException;
00014
00015 import javax.swing.SwingUtilities;
00016
00017 import commandline.Program;
00018 import commandline.ProgramControlListener;
00019 import commandline.Decoder.NormalMeshDecoder;
00020 import commandline.Encoder.NormalMeshEncoder;
00021 import commandline.Encoder.PruneListener;
00022 import commandline.Encoder.RecodingListener;
00023 import commandline.Encoder.Events.RecodingEvent;
00024 import commandline.ImageStuff.GeometricObjects.factories.EdgeConstructorTriangleMakerFactory;
00025 import commandline.ImageStuff.GeometricObjects.factories.GenericEdgeConstructorTriangleMakerFactory;
00026 import commandline.NormalMeshAlgorithm.Configuration;
00027 import commandline.debuggingCode.Debug;
00028
00035 public class StartUp implements ProgramControlListener,MyImageDisplayListerner, FileChangeListener, RecodingListener{
00036
00037 public final static String configFileName="config.ser";
00038
00039 private Configuration configuration;
00040 private Program program;
00041 private volatile boolean stopRequested=false;
00042 EmptyJFrame frame;
00043 boolean hasperformed;
00044 public StartUp() {
00045 System.out.println("java.library.path = " +
00046 System.getProperty("java.library.path"));
00047 System.out.println("java.ext.dirs = " +
00048 System.getProperty("java.ext.dirs"));
00049 System.out.println("java.class.path = " +
00050 System.getProperty("java.class.path"));
00051 try {
00052 restoreConfiguration();
00053 } catch (FileNotFoundException e) {
00054 configuration=new Configuration();
00055 }
00056 frame=new EmptyJFrame(this,configuration);
00057 frame.setEnabled(true);
00058 frame.setVisible(true);
00059 init();
00060 hasperformed=false;
00061 JoglNativeTest.testIfAvailable();
00062 }
00063
00064
00065 private void init(){
00066 program=createNewProgram(configuration);
00067
00068
00069 registerNewProgram(program,frame);
00070 }
00071
00077 private void restoreConfiguration() throws FileNotFoundException{
00078 Object o=ObjectReaderWriter.read(StartUp.configFileName);
00079 if(o==null) throw new FileNotFoundException();
00080 try{
00081 configuration=(Configuration)o;
00082 }catch(ClassCastException castException){
00083
00084 Debug.print("Classcast exception");
00085 }
00086 }
00087
00088
00089
00090
00094 public void setIngelezenBeeld(MyImageDisplay imageDisplay){
00095
00096 imageDisplay.getImageInformation().fillInData(this.getConfiguration());
00097 }
00098
00102 public void updateFilePath(FileChangePublisher fileChangePublisher){
00103 getConfiguration().setPath(fileChangePublisher.getPathName());
00104 getConfiguration().setImageName(fileChangePublisher.getFileName());
00105 }
00106
00107
00111 public void requestStop(){
00112 stopRequested=true;
00113 program.requestStop();
00114 }
00115
00116
00117
00122 public boolean isStopRequested(){
00123 return stopRequested;
00124 }
00125
00127 public void perform(){
00128 if(hasperformed){
00129 init();
00130 }
00131 hasperformed=true;
00132
00133 Runnable r=new Runnable(){
00134 public void run(){
00135 program.perform();
00136
00137 if(stopRequested) {
00138 stopRequested=false;
00139 return;}
00140 }
00141 };
00142
00143 Thread workerTread=new Thread(r);
00144 workerTread.start();
00145 }
00146
00147
00151 private Program createNewProgram(Configuration configuration){
00152 Program program=null;
00153 try {
00154 if(configuration.getFileType()==Configuration.IMAGE_FILE)
00155 program=new NormalMeshEncoder(configuration);
00156 else{
00157 program=new NormalMeshDecoder(configuration.getPath(),new GenericEdgeConstructorTriangleMakerFactory());
00158 }
00159 program.registerRecodingListener(this);
00160 } catch (FileNotFoundException e) {
00161 e.printStackTrace();
00162 System.exit(-1);
00163 }
00164 Debug.print("new Program is created");
00165 return program;
00166 }
00167
00168 public void exit(){
00169 frame.dispose();
00170 ObjectReaderWriter.write(configuration,StartUp.configFileName);
00171 System.exit(0);
00172 }
00173
00178 public void update(RecodingEvent event){
00179 showResults(event);
00180 }
00181
00182 private void showResults(final RecodingEvent recodingEvent){
00183 Runnable r =new Runnable(){
00184 public void run(){
00185 frame.completeJFrame(recodingEvent.getOriginalGrayimage(),recodingEvent.getTestTriangleStorage(),recodingEvent.getTriangleStorage());
00186 frame.repaint();
00187 }
00188 };
00189 try {
00190 SwingUtilities.invokeAndWait(r);
00191 } catch (InterruptedException e) {
00192 e.printStackTrace();
00193 } catch (InvocationTargetException e) {
00194 e.printStackTrace();
00195 }
00196 }
00197
00198
00199 public Configuration getConfiguration(){
00200 return this.configuration;
00201 }
00202
00203
00204 public static void main (String args[]) throws Throwable{
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 StartUp startUp=new StartUp();
00216
00217
00218 if(Debug.JCONSOLE && !Debug.DISTIBUTION) {
00219 String command="cmd /c start jconsole";
00220 Debug.print("batcommand = " + command);
00221 Runtime rt= Runtime.getRuntime();
00222 Process proc = rt.exec(command);
00223 }
00224
00225 }
00226
00227
00228
00229 private void registerNewProgram(Program program,EmptyJFrame frame) {
00230 if(program==null){
00231 Debug.print("program is null");
00232 return;
00233 }
00234 if(Debug.DEBUG) Debug.print("other program set in " + this.getClass().getSimpleName());
00235 this.program = program;
00236 this.program.registerRecodingListener(this);
00237
00238
00239 frame.getMyFileSaver().setProgram(program);
00240 frame.connectTesselationViewerWithProgram(program);
00241
00242 frame.getEncodeSettingsPanel().setProgramListener(this);
00243 frame.getDecoderSettingsPanel().setProgramListener(this);
00244
00245 frame.getMenuExample().setFrame(frame);
00246 try{
00247 frame.getEncodeSettingsPanel().setPruneListener((PruneListener)program);
00248 }catch (ClassCastException castException) {
00249 System.out.println(program.getClass().getName()+" cannot be casted to Prunelistener");
00250 }
00251
00252 FileChangeHandler fileChangeHandler=new FileChangeHandler();
00253 fileChangeHandler.subscribeFileChangePublisher(frame.getFileChooser());
00254 fileChangeHandler.subscribeFileChangeListener(frame.getMyTabbedPane());
00255
00256 fileChangeHandler.subscribeFileChangeListener(this);
00257 fileChangeHandler.subscribeFileChangeListener(frame);
00258
00259 fileChangeHandler.subscribeFileChangeListener(frame.getImageDisplay());
00260 fileChangeHandler.subscribeFileChangeListener(frame.getInformatieTextBox());
00261
00262
00263
00264 try{
00265 program.addProgressListener(new ProgressBarProgressListener(frame.getProgressBar()));
00266 }catch(NullPointerException exception){
00267 exception.printStackTrace();
00268 }
00269 }
00270
00271 private void unregisterOldProgram(Program program){
00272 program.unRegisterRecodingsListener();
00273 program.removeProgressListener();
00274 frame.getMyFileSaver().setProgram(null);
00275 program.unregisterRefinementEventListeners();
00276 }
00277
00278 }