KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > grid > ed > Editor


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.grid.ed;
15
16 import java.io.*;
17 import java.awt.*;
18 import java.awt.event.*;
19 import javax.swing.*;
20 import javax.swing.event.*;
21
22 import org.compiere.util.*;
23 import org.compiere.apps.*;
24 import org.compiere.plaf.*;
25 import org.compiere.swing.*;
26
27 /**
28  * Editor for Text (textArea) with HTML (textPane) View
29  *
30  * @author Jorg Janke
31  * @version $Id: Editor.java,v 1.8 2003/08/18 15:49:30 jjanke Exp $
32  */

33 public class Editor extends JDialog
34     implements ChangeListener, ActionListener
35 {
36     /**
37      * Minimum constructor
38      * @param frame parent
39      */

40     public Editor(Frame frame)
41     {
42         this (frame, Msg.getMsg(Env.getCtx(), "Editor"), "", true);
43     } // Editor
44

45     /**
46      * Standard constructor
47      * @param frame parent
48      * @param header heading
49      * @param text initial text
50      * @param editable if false = r/o
51      */

52     public Editor(Frame frame, String JavaDoc header, String JavaDoc text, boolean editable)
53     {
54         super (frame, header, frame != null);
55         Log.trace(Log.l3_Util, "Editor");
56         try
57         {
58             jbInit();
59             setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
60         }
61         catch(Exception JavaDoc ex)
62         {
63             Log.error("Editor", ex);
64         }
65         // Set Text
66
m_text = text;
67         textArea.setText(m_text);
68         textArea.setEditable(editable);
69         if (editable)
70             textArea.setBackground(CompierePLAF.getFieldBackground_Normal());
71         else
72             textArea.setBackground(CompierePLAF.getFieldBackground_Inactive());
73         textPane.setBackground(CompierePLAF.getFieldBackground_Inactive());
74     } // Editor
75

76     /**
77      * IDE Constructor
78      */

79     public Editor()
80     {
81         this (null);
82     } // Editor
83

84     private String JavaDoc m_text;
85
86     private CPanel panel = new CPanel();
87     private BorderLayout panelLayout = new BorderLayout();
88     private JTabbedPane tabbedPane = new JTabbedPane();
89     private JScrollPane paneText = new JScrollPane();
90     private JScrollPane paneHTML = new JScrollPane();
91     private JTextArea textArea = new JTextArea();
92     private JTextPane textPane = new JTextPane();
93     private JMenuBar menuBar = new JMenuBar();
94     private JMenu mFile = new JMenu();
95     private JMenuItem mImport = new JMenuItem();
96     private JMenuItem mExport = new JMenuItem();
97     private ConfirmPanel confirmPanel = new ConfirmPanel();
98
99     /**
100      * Static Init
101      * @throws Exception
102      */

103     private void jbInit() throws Exception JavaDoc
104     {
105         panel.setLayout(panelLayout);
106         this.setJMenuBar(menuBar);
107         textArea.setPreferredSize(new Dimension(300, 300));
108         textArea.setWrapStyleWord(true);
109         textArea.setLineWrap(true);
110         textPane.setContentType("text/html");
111         textPane.setEditable(false);
112         mFile.setText("File");
113         mImport.setText("Import");
114         mImport.addActionListener(this);
115         mExport.setText("Export");
116         mExport.addActionListener(this);
117         tabbedPane.addChangeListener(this);
118         getContentPane().add(panel);
119         panel.add(tabbedPane, BorderLayout.CENTER);
120         tabbedPane.add(paneText, "Text");
121         paneText.getViewport().add(textArea, null);
122         tabbedPane.add(paneHTML, "HTML");
123         this.getContentPane().add(confirmPanel, BorderLayout.SOUTH);
124         paneHTML.getViewport().add(textPane, null);
125         menuBar.add(mFile);
126         mFile.add(mImport);
127         mFile.add(mExport);
128         confirmPanel.addActionListener(this);
129     } // jbInit
130

131     /**
132      * Factory: Start Editor
133      * @param jc container to get parent frame
134      * @param header heading
135      * @param text initial text
136      * @param editable if false = r/o
137      * @return edited string
138      */

139     public static String JavaDoc startEditor(Container jc, String JavaDoc header, String JavaDoc text, boolean editable)
140     {
141         // Find frame
142
JFrame frame = Env.getFrame(jc);
143         String JavaDoc hdr = header;
144         if (hdr == null || hdr.length() == 0)
145             hdr = Msg.getMsg(Env.getCtx(), "Editor");
146         // Start it
147
Editor ed = new Editor(frame, hdr, text, editable);
148         AEnv.showCenterWindow(frame, ed);
149         String JavaDoc s = ed.getText();
150         ed = null;
151         return s;
152     } // startEditor
153

154     /**
155      * ActionListener
156      * @param e event
157      */

158     public void actionPerformed(ActionEvent e)
159     {
160         if (e.getActionCommand().equals(ConfirmPanel.A_OK))
161         {
162             m_text = textArea.getText();
163             Log.trace(Log.l5_DData, "Editor.actionPerformed - OK - length=" + m_text.length());
164             dispose();
165         }
166         else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL))
167         {
168             dispose();
169         }
170         else if (e.getSource() == mImport)
171         {
172             importText();
173         }
174         else if (e.getSource() == mExport)
175         {
176             exportText();
177         }
178     } // actionPerformed
179

180     /**
181      * Get Text
182      * @return edited text
183      */

184     public String JavaDoc getText()
185     {
186         return m_text;
187     } // getText
188

189     /**
190      * Import Text from File
191      */

192     private void importText()
193     {
194         JFileChooser jc = new JFileChooser();
195         jc.setDialogTitle(Msg.getMsg(Env.getCtx(), "ImportText"));
196         jc.setDialogType(JFileChooser.OPEN_DIALOG);
197         jc.setFileSelectionMode(JFileChooser.FILES_ONLY);
198         //
199
if (jc.showOpenDialog(this) != JFileChooser.APPROVE_OPTION)
200             return;
201
202         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
203         try
204         {
205             InputStreamReader in = new InputStreamReader (new FileInputStream (jc.getSelectedFile()));
206             char[] cbuf = new char[1024];
207             int count;
208             while ((count = in.read(cbuf)) > 0)
209                 sb.append(cbuf, 0, count);
210             in.close();
211         }
212         catch (Exception JavaDoc e)
213         {
214             Log.error("Editor.importText" + e.getMessage());
215             return;
216         }
217         textArea.setText(sb.toString());
218     } // importText
219

220     /**
221      * Export Text to File
222      */

223     private void exportText()
224     {
225         JFileChooser jc = new JFileChooser();
226         jc.setDialogTitle(Msg.getMsg(Env.getCtx(), "ExportText"));
227         jc.setDialogType(JFileChooser.SAVE_DIALOG);
228         jc.setFileSelectionMode(JFileChooser.FILES_ONLY);
229         //
230
if (jc.showSaveDialog(this) != JFileChooser.APPROVE_OPTION)
231             return;
232
233         try
234         {
235             BufferedWriter bout = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (jc.getSelectedFile())));
236             bout.write(textArea.getText());
237             bout.flush();
238             bout.close();
239         }
240         catch (Exception JavaDoc e)
241         {
242             Log.error("Editor.exportText" + e.getMessage());
243         }
244     } // exportText
245

246     /**
247      * ChangeListener for TabbedPane
248      * @param e event
249      */

250     public void stateChanged(ChangeEvent e)
251     {
252         if (tabbedPane.getSelectedIndex() == 1) // switch to HTML
253
textPane.setText(textArea.getText());
254     } // stateChanged
255

256 } // Editor
257
Popular Tags