View Javadoc

1   package crisp.wbwiki.client;
2   
3   import com.google.gwt.core.client.EntryPoint;
4   import com.google.gwt.core.client.GWT;
5   import com.google.gwt.user.client.Timer;
6   import com.google.gwt.user.client.rpc.ServiceDefTarget;
7   import com.google.gwt.user.client.ui.Button;
8   import com.google.gwt.user.client.ui.ClickListener;
9   import com.google.gwt.user.client.ui.RootPanel;
10  import com.google.gwt.user.client.ui.TextArea;
11  import com.google.gwt.user.client.ui.VerticalPanel;
12  import com.google.gwt.user.client.ui.Widget;
13  
14  
15  /**
16   * Entry point classes define <code>onModuleLoad()</code>.
17   */
18  public class WikiClient implements EntryPoint, WikiGui {
19  	/** How often we poll the server to save/load the wiki page */
20  	private static final int POLL_INTERVAL_MS = 1000;
21  	
22  	final TextArea textArea = new TextArea();
23  	final TextArea log = new TextArea();
24  	boolean loggingEnabled = false;
25  	WikiServiceAsync wikiService;
26  	
27  	ClientStateManager stateManager;
28  	
29  	boolean timerOn = true;
30  	Button timerButton = new Button("Turn off timer");
31  	
32  	/**
33  	 * Init method called by GWT
34  	 */
35  	public void onModuleLoad() {
36  		//Load and initialize the wikiService
37  		wikiService = (WikiServiceAsync) GWT.create(WikiService.class);		
38  		ServiceDefTarget endPoint = (ServiceDefTarget) wikiService;
39  		String url = GWT.getModuleBaseURL() + "wbwiki";
40  		endPoint.setServiceEntryPoint(url);
41  				
42  		//Add the wiki textarea to the RootPanel
43  		RootPanel.get("wikiSlot").add(textArea);
44  		textArea.setText("(loading page contents...)");
45  		textArea.setEnabled(false);
46  		textArea.setSize("100%", "100%");
47  		
48  		
49  		addDebugWidgets();
50  				
51  		stateManager = new ClientStateManager(wikiService);
52  		
53  		//Load the wiki page from the server
54  		stateManager.synchronizeWithServer(this);
55  		
56  		//Start a timer that regularly saves & reloads the page
57  		Timer timer;
58  		timer = new Timer() {
59  			public void run() {
60  				if (timerOn) {					
61  					stateManager.synchronizeWithServer(WikiClient.this);
62  				}
63  			}
64  		};
65  		timer.scheduleRepeating(POLL_INTERVAL_MS);
66  		
67  
68  		
69  	}
70  
71  	private void addDebugWidgets() {
72  		RootPanel logSlot = RootPanel.get("logSlot");
73  		if (logSlot != null) {
74  			log.setSize("100%", "100%");
75  			logSlot.add(log);
76  			loggingEnabled = true;
77  		} else {
78  			loggingEnabled = false;
79  		}
80  		RootPanel timerButtonSlot = RootPanel.get("timerButtonSlot");
81  		if (timerButtonSlot != null) {
82  			timerButtonSlot.add(timerButton);
83  			timerButton.addClickListener(
84  					new ClickListener() {
85  						public void onClick(Widget sender) {
86  							if (timerOn == true) {
87  								timerOn = false;
88  								timerButton.setText("Turn on timer");
89  							} else {
90  								timerOn = true;
91  								timerButton.setText("Turn off timer");
92  							}
93  						}					
94  					}
95  			);
96  		}
97  	}
98  
99  	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 }