1 21 22 27 28 package com.sun.activation.viewers; 29 30 import java.awt.*; 31 import java.awt.event.*; 32 import java.io.*; 33 import java.beans.*; 34 import javax.activation.*; 35 36 public class TextEditor extends Panel implements CommandObject, 37 ActionListener { 38 private TextArea text_area = null; 40 private GridBagLayout panel_gb = null; 41 private Panel button_panel = null; 42 private Button save_button = null; 43 private File text_file = null; 45 private String text_buffer = null; 46 private InputStream data_ins = null; 47 private FileInputStream fis = null; 48 49 private DataHandler _dh = null; 50 private boolean DEBUG = false; 51 54 public TextEditor() { 55 panel_gb = new GridBagLayout(); 56 setLayout(panel_gb); 57 58 button_panel = new Panel(); 59 button_panel.setLayout( new FlowLayout() ); 61 save_button = new Button("SAVE"); 62 button_panel.add(save_button); 63 addGridComponent(this, 64 button_panel, 65 panel_gb, 66 0,0, 67 1,1, 68 1,0); 69 70 text_area = new TextArea("This is text",24, 80, 72 TextArea.SCROLLBARS_VERTICAL_ONLY ); 73 text_area.setEditable( true ); 75 76 addGridComponent(this, 77 text_area, 78 panel_gb, 79 0,1, 80 1,2, 81 1,1); 82 83 save_button.addActionListener( this ); 85 86 } 87 88 92 private void addGridComponent(Container cont, 93 Component comp, 94 GridBagLayout mygb, 95 int gridx, 96 int gridy, 97 int gridw, 98 int gridh, 99 int weightx, 100 int weighty) { 101 GridBagConstraints c = new GridBagConstraints(); 102 c.gridx = gridx; 103 c.gridy = gridy; 104 c.gridwidth = gridw; 105 c.gridheight = gridh; 106 c.fill = GridBagConstraints.BOTH; 107 c.weighty = weighty; 108 c.weightx = weightx; 109 c.anchor = GridBagConstraints.CENTER; 110 mygb.setConstraints(comp, c); 111 cont.add(comp); 112 } 113 114 public void setCommandContext(String verb, DataHandler dh) throws IOException { 116 _dh = dh; 117 this.setInputStream( _dh.getInputStream() ); 118 119 } 120 122 126 public void setInputStream(InputStream ins) throws IOException { 127 128 byte data[] = new byte[1024]; 129 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 130 int bytes_read = 0; 131 133 while((bytes_read = ins.read(data)) >0) 134 baos.write(data, 0, bytes_read); 135 ins.close(); 136 137 138 text_buffer = baos.toString(); 141 142 text_area.setText(text_buffer); 144 } 145 private void performSaveOperation(){ 147 OutputStream fos = null; 148 try { 149 fos = _dh.getOutputStream(); 150 } catch (Exception e) {} 151 152 String buffer = text_area.getText(); 153 154 if(fos == null) { 156 System.out.println("Invalid outputstream in TextEditor!"); 157 System.out.println("not saving!"); 158 } 159 160 try { 161 fos.write( buffer.getBytes() ); 162 fos.flush(); fos.close(); } catch(IOException e) 165 { 166 System.out.println("TextEditor Save Operation failed with: " + e); 167 } 168 169 } 170 public void addNotify() { 172 super.addNotify(); 173 invalidate(); 174 } 175 public Dimension getPreferredSize() { 177 return text_area.getMinimumSize(24, 80); 178 } 179 public void actionPerformed(ActionEvent evt){ 182 if(evt.getSource() == save_button) { 184 this.performSaveOperation(); 186 } 187 } 188 189 } 190 | Popular Tags |