001: package com.gwtext.sample.showcase2.client;
002:
003: import com.google.gwt.i18n.client.DateTimeFormat;
004: import com.google.gwt.user.client.ui.Frame;
005: import com.gwtext.client.core.*;
006: import com.gwtext.client.data.*;
007: import com.gwtext.client.util.Format;
008: import com.gwtext.client.widgets.*;
009: import com.gwtext.client.widgets.event.ButtonListenerAdapter;
010: import com.gwtext.client.widgets.event.PanelListenerAdapter;
011: import com.gwtext.client.widgets.event.TabPanelListenerAdapter;
012: import com.gwtext.client.widgets.event.WindowListenerAdapter;
013: import com.gwtext.client.widgets.grid.*;
014: import com.gwtext.client.widgets.layout.BorderLayout;
015: import com.gwtext.client.widgets.layout.BorderLayoutData;
016: import com.gwtext.client.widgets.layout.FitLayout;
017: import com.gwtext.client.widgets.layout.VerticalLayout;
018:
019: import java.util.Date;
020:
021: public abstract class ShowcasePanel extends Panel {
022:
023: private Window sourceWindow = null;
024: private String sourceIconCls = "source-icon";
025: private ToolbarButton sourceButton;
026: private Toolbar toolbar;
027:
028: protected static final String EVENT = "event";
029: protected static final String MESSAGE = "message";
030:
031: private RecordDef recordDef;
032: private Store store;
033:
034: protected Panel panel;
035:
036: protected ShowcasePanel() {
037: setTitle(getTitle());
038: setClosable(true);
039: setTopToolbar(new Toolbar());
040: setPaddings(20);
041: setLayout(new FitLayout());
042: setBorder(false);
043: setAutoScroll(true);
044: addListener(new PanelListenerAdapter() {
045: public void onActivate(Panel panel) {
046: ShowcasePanel.this .onActivate();
047: }
048: });
049: }
050:
051: protected void onActivate() {
052: Panel viewPanel = getViewPanel();
053: if (viewPanel instanceof Window) {
054: ((Window) viewPanel).show();
055: }
056: }
057:
058: protected void afterRender() {
059:
060: ButtonListenerAdapter listener = new ButtonListenerAdapter() {
061: public void onClick(Button button, EventObject e) {
062: Window window = getSourceWindow();
063: window.show(sourceButton.getId());
064: }
065: };
066:
067: sourceButton = new ToolbarButton("View Source", listener);
068: sourceButton.setIconCls(sourceIconCls);
069:
070: if (getSourceUrl() == null) {
071: sourceButton.setDisabled(true);
072: }
073: toolbar = getTopToolbar();
074:
075: toolbar.addFill();
076: toolbar.addButton(sourceButton);
077:
078: addViewPanel();
079: }
080:
081: private void addViewPanel() {
082: Panel mainPanel = new Panel();
083: mainPanel.setBorder(false);
084: //mainPanel.setLayout(new BorderLayout());
085: mainPanel.setLayout(new BorderLayout());
086:
087: Panel viewPanel = getViewPanel();
088: if (viewPanel instanceof Window) {
089: viewPanel.show();
090: viewPanel = new Panel();
091: }
092: viewPanel.setAutoScroll(true);
093: viewPanel.setBorder(false);
094:
095: BorderLayoutData centerLayoutData = new BorderLayoutData(
096: RegionPosition.CENTER);
097: //centerLayoutData.setMargins(new Margins(15, 15, 15, 15));
098: mainPanel.add(viewPanel, centerLayoutData);
099:
100: boolean hasIntro = getIntro() != null;
101:
102: if (hasIntro) {
103:
104: Panel rightPanel = new Panel();
105: rightPanel.setPaddings(0, 15, 0, 0);
106: rightPanel.setWidth(210);
107: rightPanel.setLayout(new VerticalLayout(15));
108:
109: Panel introPanel = new Panel();
110: introPanel.setIconCls("book-icon");
111: introPanel.setFrame(true);
112: introPanel.setTitle("Overview");
113: introPanel.setHtml("<p>" + getIntro() + "</p>");
114:
115: //introPanel.setPaddings(0, 0, 0, 15);
116: rightPanel.add(introPanel);
117: if (showEvents()) {
118: GridPanel loggerGrid = getLoggerGrid();
119: loggerGrid.setWidth(195);
120: loggerGrid.setHeight(300);
121: rightPanel.add(loggerGrid);
122: }
123:
124: BorderLayoutData eastLayoutData = new BorderLayoutData(
125: RegionPosition.EAST);
126: //eastLayoutData.setMargins(new Margins(0, 15, 0, 0));
127: mainPanel.add(rightPanel, eastLayoutData);
128: }
129: add(mainPanel);
130: }
131:
132: protected void log(String eventType, String messsage) {
133: if (recordDef != null) {
134: Record record = recordDef.createRecord(new Object[] {
135: new Date(), eventType, messsage });
136: store.insert(0, record);
137: }
138: }
139:
140: protected boolean showEvents() {
141: return false;
142: }
143:
144: private Window getSourceWindow() {
145: if (sourceWindow == null) {
146: sourceWindow = new Window();
147: sourceWindow.setTitle("Source");
148: sourceWindow.setLayout(new FitLayout());
149: sourceWindow.setIconCls(sourceIconCls);
150:
151: int width = 640;
152: int userWidth = Ext.getBody().getWidth(true) - 20;
153: if (userWidth < width)
154: width = userWidth;
155: sourceWindow.setWidth(width);
156:
157: int height = 600;
158: int userHeight = Ext.getBody().getHeight(true) - 96;
159: if (userHeight < height)
160: height = userHeight;
161: sourceWindow.setHeight(height);
162:
163: sourceWindow.setAnimateTarget(sourceButton.getId());
164:
165: int[] panelXY = getPosition();
166:
167: int windowTop = panelXY[1]
168: + toolbar.getEl().getHeight(true) + 20;
169: int windowLeft = Ext.getBody().getWidth(true)
170: - (sourceWindow.getWidth() + 20);
171: sourceWindow.setPosition(windowLeft, windowTop);
172: sourceWindow.addListener(new WindowListenerAdapter() {
173: public void onClose(Panel panel) {
174: sourceWindow = null;
175: }
176: });
177:
178: TabPanel sourceTabPanel = new TabPanel();
179: sourceTabPanel.setActiveTab(0);
180:
181: String sourceUrl = getSourceUrl();
182:
183: if (sourceUrl != null) {
184: Panel sourceTab = new Panel("Java");
185: sourceTab.setLayout(new FitLayout());
186: sourceTab.setIconCls("java-icon");
187: Frame frame = new Frame(sourceUrl);
188: sourceTab.add(frame);
189: sourceTabPanel.add(sourceTab);
190: }
191:
192: String htmlUrl = getHtmlUrl();
193: if (htmlUrl != null) {
194: Panel htmlTab = new Panel("HTML");
195: htmlTab.setLayout(new FitLayout());
196: Frame frame = new Frame(htmlUrl);
197: htmlTab.setIconCls("html-icon");
198: sourceTabPanel.add(frame);
199: }
200:
201: String cssUrl = getCssUrl();
202: if (cssUrl != null) {
203: Panel cssTab = new Panel("CSS");
204: cssTab.setLayout(new FitLayout());
205: cssTab.setIconCls("css-icon");
206: Frame frame = new Frame(cssUrl);
207: cssTab.add(frame);
208: sourceTabPanel.add(cssTab);
209: }
210:
211: String xmlDataUrl = getXmlDataUrl();
212: if (xmlDataUrl != null) {
213: Panel xmlTab = new Panel("XML");
214: xmlTab.setLayout(new FitLayout());
215: xmlTab.setIconCls("xml-icon");
216: Frame frame = new Frame(xmlDataUrl);
217: xmlTab.add(frame);
218: sourceTabPanel.add(xmlTab);
219: }
220:
221: String jsonDataUrl = getJsonDataUrl();
222: if (jsonDataUrl != null) {
223: Panel jsonTab = new Panel("Json");
224: jsonTab.setLayout(new FitLayout());
225: jsonTab.setIconCls("json-icon");
226: Frame frame = new Frame(jsonDataUrl);
227: jsonTab.add(frame);
228: sourceTabPanel.add(jsonTab);
229: }
230: sourceWindow.add(sourceTabPanel);
231: }
232: return sourceWindow;
233: }
234:
235: private GridPanel getLoggerGrid() {
236:
237: recordDef = new RecordDef(new FieldDef[] {
238: new DateFieldDef("date"), new StringFieldDef("type"),
239: new StringFieldDef("message") });
240:
241: store = new Store(recordDef);
242:
243: ColumnModel columnModel = new ColumnModel(new ColumnConfig[] {
244: new ColumnConfig(" ", "type", 3, true,
245: new Renderer() {
246: public String render(Object value,
247: CellMetadata cellMetadata,
248: Record record, int rowIndex,
249: int colNum, Store store) {
250: if (value.equals(EVENT)) {
251: String someTip = "Event";
252: cellMetadata
253: .setHtmlAttribute("style=\"background:#FFFF88;padding:5px\"");
254: } else {
255: cellMetadata
256: .setHtmlAttribute("style=\"background:#4096EE;padding:5px\"");
257: }
258: return "";
259: }
260: }),
261:
262: new ColumnConfig("Time", "date", 70, true,
263: new Renderer() {
264: public String render(Object value,
265: CellMetadata cellMetadata,
266: Record record, int rowIndex,
267: int colNum, Store store) {
268: DateTimeFormat formatter = DateTimeFormat
269: .getFormat("hh:mm:ss");
270: return formatter.format((Date) value);
271: }
272: }) });
273:
274: GridPanel gridPanel = new GridPanel(store, columnModel);
275: gridPanel.setTitle("Logger");
276: gridPanel.setAutoExpandColumn("date");
277: gridPanel.addTool(new Tool(Tool.MINUS, new Function() {
278: public void execute() {
279: store.removeAll();
280: store.commitChanges();
281: }
282: }, "Clear Log"));
283:
284: GridView view = new GridView() {
285: public String getRowClass(Record record, int index,
286: RowParams rowParams, Store store) {
287: rowParams.setBody(Format.format("<p>{0}</p>", record
288: .getAsString("message")));
289: return "";
290: }
291: };
292:
293: view.setEnableRowBody(true);
294: view.setForceFit(true);
295: gridPanel.setView(view);
296:
297: return gridPanel;
298: }
299:
300: public String getSourceUrl() {
301: return null;
302: }
303:
304: public String getHtmlUrl() {
305: return null;
306: }
307:
308: public String getXmlDataUrl() {
309: return null;
310: }
311:
312: public String getJsonDataUrl() {
313: return null;
314: }
315:
316: public String getCssUrl() {
317: return null;
318: }
319:
320: public String getIntro() {
321: return null;
322: }
323:
324: public abstract Panel getViewPanel();
325:
326: }
|