Swt Gantt进入Griffon app

时间:2014-10-31 11:14:51

标签: griffon

我们正试图将Swt Gantt嵌入到我们的Griffon应用程序中,但由于任何我们不知道的原因,该应用程序无效。我们认为这可能与线程有关,但我们不确定。

这是Swt Gantt:http://hexapixel.com/software/ganttwidget

附上我们测试的源代码 https://github.com/ivanarrizabalaga/gantt

此致

1 个答案:

答案 0 :(得分:0)

附上我们测试的源代码



    // MODEL

    class GanttModel {
    	
    	def view
    	def controller
    	
       GanttSWTBridge ganttBridge
       
       void mvcGroupInit(Map args) {
    	   ganttBridge=new GanttSWTBridge()
       }
    }

    // VIEW

    application(title: 'gantt',
      preferredSize: [800,600],
      pack: true,
      //location: [50,50],
      locationByPlatform: true,
      iconImage:   imageIcon('/griffon-icon-48x48.png').image,
      iconImages: [imageIcon('/griffon-icon-48x48.png').image,
                   imageIcon('/griffon-icon-32x32.png').image,
                   imageIcon('/griffon-icon-16x16.png').image]) {
        borderLayout()
        panel(id:"campaignGanttPanel", cssClass:"campaignGanttPanel") {      
          migLayout(layoutConstraints: "fill")
          //button(text:"Load Gantt",actionPerformed:controller.clickAction,constraints:"wrap")
          widget(id:'ganttBridge',new GanttSWTBridge(model.controller))
          model.ganttBridge=ganttBridge
        }
    }


    // CONTROLLER

    class GanttController {
    	// these will be injected by Griffon
    	def model
    	def view
    	
    	void mvcGroupInit(Map args) {
    		log.debug "Init app"
    		GanttSWTBridge ganttSWTBridge=((GanttSWTBridge)model.ganttBridge)
    		ganttSWTBridge.connect()
    		ganttSWTBridge.init()
    	}

    	def clickAction={evt=null->
    		log.debug "Load Gantt"
    		GanttSWTBridge ganttSWTBridge=((GanttSWTBridge)model.ganttBridge)
    		doLater{
    			ganttSWTBridge.connect()
    			ganttSWTBridge.init()
    		}		
    	}

    	void mvcGroupDestroy(){
    		model.ganttBridge.disconnect()
    	}
    }

    // GANTTSWTBRIDGE

    package gantt

    import java.awt.Canvas

    import org.apache.log4j.Logger
    import org.eclipse.nebula.widgets.ganttchart.GanttChart
    import org.eclipse.nebula.widgets.ganttchart.GanttEvent
    import org.eclipse.swt.SWT
    import org.eclipse.swt.awt.SWT_AWT
    import org.eclipse.swt.layout.FillLayout
    import org.eclipse.swt.widgets.Display
    import org.eclipse.swt.widgets.Shell

    class GanttSWTBridge extends Canvas {
    	
    	private static final Logger log = Logger.getLogger(this.class)
    	
    	Thread swtThread
    	GanttChart ganttChart
    	
    	def controller
    	
    	GanttSWTBridge(def controller){
    		System.setProperty("sun.awt.xembedserver", "true");
    		this.controller=controller
    	}
    	
    	public void connect() {		
    		if (this.swtThread == null) {
    		  final Canvas canvas = this;
    		  this.swtThread = new Thread() {
    			@Override
    			public void run() {
    			  try {
    			  	log.debug "Creating display and  shell"
    				Display display = new Display();
    				Shell shell = SWT_AWT.new_Shell(display, canvas);
    				shell.setLayout(new FillLayout());
    	 
    				synchronized (this) {
    				  log.debug "Adding gantt into shell"
    				  ganttChart = new GanttChart(shell, SWT.NONE);
    				  this.notifyAll();
    				}
    	 
    	 			log.debug "Opening shell"
    				shell.open();
    				while (!shell.isDisposed()) {
    				  log.debug "SWT Loop"
    				  if (!display.readAndDispatch()) {
    					display.sleep();
    				  }
    				}
    				shell.dispose();
    				display.dispose();
    			  } catch (Exception e) {
    			  	log.debug "Exception: $e"
    				interrupt();
    			  }
    			}
    		  };
    		  this.swtThread.start();
    		}
    	 
    		// Wait for the Browser instance to become ready
    		synchronized (this.swtThread) {
    		  while (this.ganttChart == null) {
    			try {
    			  this.swtThread.wait(100);
    			} catch (InterruptedException e) {
    			  log.debug "InterruptedException: $e"
    			  this.ganttChart = null;
    			  this.swtThread = null;
    			  break;
    			}
    		  }
    		}
    	  }
    	
    	/**
    	 * Stops the swt background thread.
    	 */
    	public void disconnect() {
    	  if (swtThread != null) {
    		ganttChart = null;
    		swtThread.interrupt();
    		swtThread = null;
    	  }
    	}
    	
    	/**
    	 * Ensures that the SWT background thread
    	 * is stopped if this canvas is removed from
    	 * it's parent component (e.g. because the
    	 * frame has been disposed).
    	 */
    	@Override
    	public void removeNotify() {
    	  super.removeNotify();
    	  disconnect();
    	}
    	
    	void init(){		 
    		 this.ganttChart.getDisplay().asyncExec(new Runnable() {
    		   @Override
    		   public void run() {
    			  
    			 log.debug "Adding data to gantt"		
    			 // make a 10 day long event
    			 Calendar cStartDate = Calendar.getInstance(Locale.getDefault());
    			 Calendar cEndDate = Calendar.getInstance(Locale.getDefault());
    			 cEndDate.add(Calendar.DATE, 10);
    			  
    			 // make some revised dates
    			 Calendar rStartDate = Calendar.getInstance(Locale.getDefault());
    			 Calendar rEndDate = Calendar.getInstance(Locale.getDefault());
    			 // pretend dates were 2 days earlier than the start date and 5 days later than the end date
    			 rStartDate.add(Calendar.DATE, -2);
    			 rEndDate.add(Calendar.DATE, 15);

    			 GanttEvent revisedEvent = new GanttEvent(ganttChart, "Revised", cStartDate, cEndDate, rStartDate, rEndDate, 50);
    		   }
    		 });
    		
    	}
    	
    }