001 package crisp.wbwiki.client; 002 003 import com.google.gwt.core.client.EntryPoint; 004 import com.google.gwt.core.client.GWT; 005 import com.google.gwt.user.client.Timer; 006 import com.google.gwt.user.client.rpc.ServiceDefTarget; 007 import com.google.gwt.user.client.ui.Button; 008 import com.google.gwt.user.client.ui.ClickListener; 009 import com.google.gwt.user.client.ui.RootPanel; 010 import com.google.gwt.user.client.ui.TextArea; 011 import com.google.gwt.user.client.ui.VerticalPanel; 012 import com.google.gwt.user.client.ui.Widget; 013 014 015 /** 016 * Entry point classes define <code>onModuleLoad()</code>. 017 */ 018 public class WikiClient implements EntryPoint, WikiGui { 019 /** How often we poll the server to save/load the wiki page */ 020 private static final int POLL_INTERVAL_MS = 1000; 021 022 final TextArea textArea = new TextArea(); 023 final TextArea log = new TextArea(); 024 boolean loggingEnabled = false; 025 WikiServiceAsync wikiService; 026 027 ClientStateManager stateManager; 028 029 boolean timerOn = true; 030 Button timerButton = new Button("Turn off timer"); 031 032 /** 033 * Init method called by GWT 034 */ 035 public void onModuleLoad() { 036 //Load and initialize the wikiService 037 wikiService = (WikiServiceAsync) GWT.create(WikiService.class); 038 ServiceDefTarget endPoint = (ServiceDefTarget) wikiService; 039 String url = GWT.getModuleBaseURL() + "wbwiki"; 040 endPoint.setServiceEntryPoint(url); 041 042 //Add the wiki textarea to the RootPanel 043 RootPanel.get("wikiSlot").add(textArea); 044 textArea.setText("(loading page contents...)"); 045 textArea.setEnabled(false); 046 textArea.setSize("100%", "100%"); 047 048 049 addDebugWidgets(); 050 051 stateManager = new ClientStateManager(wikiService); 052 053 //Load the wiki page from the server 054 stateManager.synchronizeWithServer(this); 055 056 //Start a timer that regularly saves & reloads the page 057 Timer timer; 058 timer = new Timer() { 059 public void run() { 060 if (timerOn) { 061 stateManager.synchronizeWithServer(WikiClient.this); 062 } 063 } 064 }; 065 timer.scheduleRepeating(POLL_INTERVAL_MS); 066 067 068 069 } 070 071 private void addDebugWidgets() { 072 RootPanel logSlot = RootPanel.get("logSlot"); 073 if (logSlot != null) { 074 log.setSize("100%", "100%"); 075 logSlot.add(log); 076 loggingEnabled = true; 077 } else { 078 loggingEnabled = false; 079 } 080 RootPanel timerButtonSlot = RootPanel.get("timerButtonSlot"); 081 if (timerButtonSlot != null) { 082 timerButtonSlot.add(timerButton); 083 timerButton.addClickListener( 084 new ClickListener() { 085 public void onClick(Widget sender) { 086 if (timerOn == true) { 087 timerOn = false; 088 timerButton.setText("Turn on timer"); 089 } else { 090 timerOn = true; 091 timerButton.setText("Turn off timer"); 092 } 093 } 094 } 095 ); 096 } 097 } 098 099 public String getCurrentContent() { 100 return textArea.getText(); 101 } 102 103 public void setCurrentContent(String content) { 104 int oldCursorPos = textArea.getCursorPos(); 105 int oldLength = textArea.getText().length(); 106 textArea.setText(content); 107 textArea.setEnabled(true); 108 int newLength = content.length(); 109 int newCursorPos; 110 if (oldCursorPos > newLength) { 111 newCursorPos = newLength; 112 } else { 113 newCursorPos = oldCursorPos; 114 } 115 textArea.setCursorPos(newCursorPos); 116 log("" + oldCursorPos + "/" + oldLength + " => " + newCursorPos + "/" + newLength); 117 118 119 } 120 121 public void log(String logEntry) { 122 if (loggingEnabled) { 123 log.setText(log.getText() + logEntry + "\n"); 124 log.setCursorPos(log.getText().length()); 125 } 126 } 127 }