KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > activation > viewers > TextEditor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)TextEditor.java 1.8 05/11/16
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

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     // UI Vars...
39
private TextArea text_area = null;
40     private GridBagLayout panel_gb = null;
41     private Panel button_panel = null;
42     private Button save_button = null;
43     // File Vars
44
private File text_file = null;
45     private String JavaDoc 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     /**
52      * Constructor
53      */

54     public TextEditor() {
55     panel_gb = new GridBagLayout();
56     setLayout(panel_gb);
57     
58     button_panel = new Panel();
59     // button_panel.setBackground(Color.white);
60
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     // create the text area
71
text_area = new TextArea("This is text",24, 80,
72                  TextArea.SCROLLBARS_VERTICAL_ONLY );
73     // text_area.setBackground(Color.lightGray);
74
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     // add listeners
84
save_button.addActionListener( this );
85     
86     }
87
88     ////////////////////////////////////////////////////////////////////////
89
/**
90      * adds a component to our gridbag layout
91      */

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   //--------------------------------------------------------------------
115
public void setCommandContext(String JavaDoc verb, DataHandler dh) throws IOException {
116     _dh = dh;
117     this.setInputStream( _dh.getInputStream() );
118
119     }
120   //--------------------------------------------------------------------
121

122   /**
123    * set the data stream, component to assume it is ready to
124    * be read.
125    */

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     // check that we can actually read
132

133     while((bytes_read = ins.read(data)) >0)
134         baos.write(data, 0, bytes_read);
135     ins.close();
136
137       
138     // convert the buffer into a string
139
// popuplate the buffer
140
text_buffer = baos.toString();
141
142     // place in the text area
143
text_area.setText(text_buffer);
144     }
145     ///////////////////////////////////////////////////////////////////////
146
private void performSaveOperation(){
147     OutputStream fos = null;
148     try {
149         fos = _dh.getOutputStream();
150     } catch (Exception JavaDoc e) {}
151     
152     String JavaDoc buffer = text_area.getText();
153     
154     // make sure we got one
155
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(); // flush it!
163
fos.close(); // close it!
164
} catch(IOException e)
165         {
166         System.out.println("TextEditor Save Operation failed with: " + e);
167         }
168     
169     }
170   //--------------------------------------------------------------------
171
public void addNotify() {
172     super.addNotify();
173     invalidate();
174     }
175   //--------------------------------------------------------------------
176
public Dimension getPreferredSize() {
177     return text_area.getMinimumSize(24, 80);
178     }
179     /////////////////////////////////////////////////////////////////////
180
// for ActionListener
181
public void actionPerformed(ActionEvent evt){
182     if(evt.getSource() == save_button) { // save button pressed!
183

184         // Save ourselves
185
this.performSaveOperation();
186     }
187     }
188     
189 }
190
Popular Tags