1 29 30 package nextapp.echo2.testapp.interactive.testscreen; 31 32 import java.io.BufferedReader ; 33 import java.io.ByteArrayInputStream ; 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.io.InputStreamReader ; 37 import java.io.PrintWriter ; 38 import java.io.StringWriter ; 39 40 import nextapp.echo2.app.Button; 41 import nextapp.echo2.app.Extent; 42 import nextapp.echo2.app.Insets; 43 import nextapp.echo2.app.Label; 44 import nextapp.echo2.app.Column; 45 import nextapp.echo2.app.StyleSheet; 46 import nextapp.echo2.app.TextArea; 47 import nextapp.echo2.app.WindowPane; 48 import nextapp.echo2.app.componentxml.ComponentXmlException; 49 import nextapp.echo2.app.componentxml.StyleSheetLoader; 50 import nextapp.echo2.app.event.ActionEvent; 51 import nextapp.echo2.app.event.ActionListener; 52 import nextapp.echo2.app.layout.SplitPaneLayoutData; 53 import nextapp.echo2.testapp.interactive.InteractiveApp; 54 import nextapp.echo2.testapp.interactive.Styles; 55 56 59 public class StyleSheetTest extends Column { 60 61 private static final String DEFAULT_STYLE_SHEET_TEXT; 62 static { 63 InputStream in = null; 64 try { 65 in = StyleSheetTest.class.getResourceAsStream(Styles.STYLE_PATH + "Default.stylesheet"); 66 BufferedReader reader = new BufferedReader (new InputStreamReader (in)); 67 StringBuffer out = new StringBuffer (); 68 String line; 69 while ((line = reader.readLine()) != null) { 70 out.append(line); 71 out.append("\n"); 72 } 73 DEFAULT_STYLE_SHEET_TEXT = out.toString(); 74 } catch (IOException ex) { 75 throw new RuntimeException (ex.toString()); 76 } finally { 77 if (in != null) { try { in.close(); } catch (IOException ex) { } } 78 } 79 } 80 81 private TextArea styleSheetEntryTextArea; 82 83 public StyleSheetTest() { 84 super(); 85 86 SplitPaneLayoutData splitPaneLayoutData = new SplitPaneLayoutData(); 87 splitPaneLayoutData.setInsets(new Insets(10)); 88 setLayoutData(splitPaneLayoutData); 89 setCellSpacing(new Extent(20)); 90 91 Column controlsColumn = new Column(); 92 add(controlsColumn); 93 94 Button defaultButton = new Button("Slate Blue Style Sheet (DEFAULT)"); 95 defaultButton.setStyleName("Default"); 96 defaultButton.addActionListener(new ActionListener() { 97 public void actionPerformed(ActionEvent e) { 98 getApplicationInstance().setStyleSheet(Styles.DEFAULT_STYLE_SHEET); 99 } 100 }); 101 controlsColumn.add(defaultButton); 102 103 Button greenButton = new Button("Forest Green Style Sheet"); 104 greenButton.setStyleName("Default"); 105 greenButton.addActionListener(new ActionListener() { 106 public void actionPerformed(ActionEvent e) { 107 getApplicationInstance().setStyleSheet(Styles.GREEN_STYLE_SHEET); 108 } 109 }); 110 controlsColumn.add(greenButton); 111 112 Button nullButton = new Button("No Style Sheet"); 113 nullButton.setStyleName("Default"); 114 nullButton.addActionListener(new ActionListener() { 115 public void actionPerformed(ActionEvent e) { 116 getApplicationInstance().setStyleSheet(null); 117 } 118 }); 119 controlsColumn.add(nullButton); 120 121 Button customButton = new Button("Custom Style Sheet (Edit Below)"); 122 customButton.setStyleName("Default"); 123 customButton.addActionListener(new ActionListener() { 124 public void actionPerformed(ActionEvent e) { 125 try { 126 StyleSheet styleSheet = loadStyleSheet(styleSheetEntryTextArea.getDocument().getText()); 127 getApplicationInstance().setStyleSheet(styleSheet); 128 } catch (ComponentXmlException ex) { 129 displayCustomStyleError(ex); 130 } 131 } 132 }); 133 controlsColumn.add(customButton); 134 135 styleSheetEntryTextArea = new TextArea(); 136 styleSheetEntryTextArea.getDocument().setText(DEFAULT_STYLE_SHEET_TEXT); 137 styleSheetEntryTextArea.setStyleName("Default"); 138 styleSheetEntryTextArea.setWidth(new Extent(600)); 139 styleSheetEntryTextArea.setHeight(new Extent(300)); 140 add(styleSheetEntryTextArea); 141 } 142 143 private void displayCustomStyleError(Exception exception) { 144 try { 145 StringWriter w = new StringWriter (); 146 exception.printStackTrace(new PrintWriter (w)); 147 w.close(); 148 WindowPane windowPane = new WindowPane(); 149 windowPane.setStyleName("Default"); 150 windowPane.setTitle("Exception Setting Custom Style"); 151 windowPane.add(new Label(w.toString())); 152 InteractiveApp.getApp().getDefaultWindow().getContent().add(windowPane); 153 } catch (IOException ex) { 154 throw new RuntimeException (ex); 155 } 156 } 157 158 private StyleSheet loadStyleSheet(String text) 159 throws ComponentXmlException { 160 InputStream in = null; 161 try { 162 in = new ByteArrayInputStream (text.getBytes()); 164 StyleSheet styleSheet = StyleSheetLoader.load(in, StyleSheetTest.class.getClassLoader()); 165 return styleSheet; 166 } finally { 167 if (in != null) { try { in.close(); } catch (IOException ex) { } } 168 } 169 } 170 } 171 | Popular Tags |