2014-10-31 33 views

回答

0

的源代码

// MODEL 
 

 
    class GanttModel { 
 
    \t 
 
    \t def view 
 
    \t def controller 
 
    \t 
 
     GanttSWTBridge ganttBridge 
 
     
 
     void mvcGroupInit(Map args) { 
 
    \t 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 { 
 
    \t // these will be injected by Griffon 
 
    \t def model 
 
    \t def view 
 
    \t 
 
    \t void mvcGroupInit(Map args) { 
 
    \t \t log.debug "Init app" 
 
    \t \t GanttSWTBridge ganttSWTBridge=((GanttSWTBridge)model.ganttBridge) 
 
    \t \t ganttSWTBridge.connect() 
 
    \t \t ganttSWTBridge.init() 
 
    \t } 
 

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

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

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

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