001: /*
002: * (c) Copyright 2000 wingS development team.
003: *
004: * This file is part of the wingS demo (http://j-wings.org).
005: *
006: * The wingS demo is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013:
014: package desktop;
015:
016: import org.wings.*;
017: import org.wings.externalizer.ExternalizeManager;
018: import org.wings.resource.FileResource;
019: import org.wings.script.JavaScriptListener;
020: import org.wings.script.ScriptListener;
021: import org.wings.session.ScriptManager;
022:
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025: import java.io.*;
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: /**
030: * The Desktop example demonstrates the use of internal frames as well as
031: * file upload and download.
032: * SInternalFrames work very similar to their Swing pendants. The file upload
033: * is left to the SFileChooser. Beware, that if you use one or more input
034: * elements of type="file", you have to set the encoding of the surrounding form
035: * to "multipart/form-data". This is a requirement of html. Download is a bit
036: * tricky. The text has to be externalized for example by using the class
037: * {@see org.wings.FileResource}. A JavaScriptListener, that is hooked to the
038: * java script event "onload", is installed in the frame.
039: * Look at the source, especially the method "save".
040: * <p/>
041: * As of now, the menu item "save" in the "file" menu does not work as expected.
042: * It is rendered as a href outside the form. Changes to text area don't take
043: * effect. We could use javascript again, to trigger the required form submit.
044: *
045: * @author Holger Engels
046: */
047: public class EditorPanel extends SPanel
048:
049: {
050: private SMenuBar menuBar;
051: private SToolBar toolBar;
052: private STextArea textArea;
053:
054: private String backup;
055: private static org.wings.util.SessionLocal<String> clip = new org.wings.util.SessionLocal<String>() {
056:
057: @Override
058: protected String initialValue() {
059: return "";
060:
061: }
062: };
063:
064: private static org.wings.util.SessionLocal<Integer> editorNr = new org.wings.util.SessionLocal<Integer>() {
065:
066: @Override
067: protected Integer initialValue() {
068: return 0;
069: }
070: };
071:
072: public EditorPanel() {
073: setName("editorpanel" + editorNr.get().toString());
074: editorNr.set(editorNr.get() + 1);
075:
076: menuBar = createMenu();
077: setLayout(new SBorderLayout());
078: add(menuBar, SBorderLayout.NORTH);
079: toolBar = createToolBar();
080:
081: textArea = new STextArea();
082: textArea.setColumns(500);
083: textArea.setRows(12);
084: textArea.setPreferredSize(SDimension.FULLWIDTH);
085: textArea.setEditable(true);
086:
087: SForm form = new SForm(new SFlowDownLayout());
088: form.add(toolBar);
089: form.add(textArea);
090: add(form, SBorderLayout.CENTER);
091:
092: }
093:
094: public static void resetEditorNo() {
095: editorNr.set(0);
096: }
097:
098: protected SMenuBar createMenu() {
099: SMenuItem saveItem = new SMenuItem("Save");
100: saveItem.addActionListener(new ActionListener() {
101: public void actionPerformed(ActionEvent evt) {
102: save();
103: }
104: });
105: SMenuItem revertItem = new SMenuItem("Revert");
106: revertItem.addActionListener(new ActionListener() {
107: public void actionPerformed(ActionEvent evt) {
108: revert();
109: }
110: });
111: SMenuItem closeItem = new SMenuItem("Close");
112: closeItem.addActionListener(new ActionListener() {
113: public void actionPerformed(ActionEvent evt) {
114: close();
115: }
116: });
117:
118: SMenu fileMenu = new SMenu("File");
119: fileMenu.add(saveItem);
120: fileMenu.add(revertItem);
121: fileMenu.add(closeItem);
122:
123: SMenuItem cutItem = new SMenuItem("Cut");
124: cutItem.addActionListener(new ActionListener() {
125: public void actionPerformed(ActionEvent evt) {
126: cut();
127: }
128: });
129: SMenuItem copyItem = new SMenuItem("Copy");
130: copyItem.addActionListener(new ActionListener() {
131: public void actionPerformed(ActionEvent evt) {
132: copy();
133: }
134: });
135: SMenuItem pasteItem = new SMenuItem("Paste");
136: pasteItem.addActionListener(new ActionListener() {
137: public void actionPerformed(ActionEvent evt) {
138: paste();
139: }
140: });
141:
142: SMenu editMenu = new SMenu("Edit");
143: editMenu.add(cutItem);
144: editMenu.add(copyItem);
145: editMenu.add(pasteItem);
146:
147: SMenuBar menuBar = new SMenuBar();
148: menuBar.add(fileMenu);
149: menuBar.add(editMenu);
150:
151: return menuBar;
152: }
153:
154: protected SToolBar createToolBar() {
155: try {
156: SButton saveButton = new SButton(new SURLIcon(
157: "../icons/filesave.png"));
158: saveButton.setToolTipText("save");
159: saveButton.addActionListener(new ActionListener() {
160: public void actionPerformed(ActionEvent evt) {
161: save();
162: }
163: });
164:
165: SButton revertButton = new SButton(new SURLIcon(
166: "../icons/filerevert.png"));
167: revertButton.setToolTipText("revert");
168: revertButton.addActionListener(new ActionListener() {
169: public void actionPerformed(ActionEvent evt) {
170: revert();
171: }
172: });
173: SButton closeButton = new SButton(new SURLIcon(
174: "../icons/fileclose.png"));
175: closeButton.setToolTipText("close");
176: closeButton.addActionListener(new ActionListener() {
177: public void actionPerformed(ActionEvent evt) {
178: close();
179: }
180: });
181:
182: SButton cutButton = new SButton(new SURLIcon(
183: "../icons/editcut.png"));
184: cutButton.setToolTipText("cut");
185: cutButton.addActionListener(new ActionListener() {
186: public void actionPerformed(ActionEvent evt) {
187: cut();
188: }
189: });
190: SButton copyButton = new SButton(new SURLIcon(
191: "../icons/editcopy.png"));
192: copyButton.setToolTipText("copy");
193: copyButton.addActionListener(new ActionListener() {
194: public void actionPerformed(ActionEvent evt) {
195: copy();
196: }
197: });
198: SButton pasteButton = new SButton(new SURLIcon(
199: "../icons/editpaste.png"));
200: pasteButton.setToolTipText("paste");
201: pasteButton.addActionListener(new ActionListener() {
202: public void actionPerformed(ActionEvent evt) {
203: paste();
204: }
205: });
206:
207: SToolBar toolBar = new SToolBar();
208: toolBar.add(saveButton);
209: toolBar.add(revertButton);
210: toolBar.add(closeButton);
211: toolBar.add(new SLabel("<html> "));
212: toolBar.add(cutButton);
213: toolBar.add(copyButton);
214: toolBar.add(pasteButton);
215:
216: return toolBar;
217: } catch (Exception e) {
218: System.err.println(e.getMessage());
219: e.printStackTrace(System.err);
220: }
221: return new SToolBar();
222: }
223:
224: public STextArea getTextArea() {
225: return this .textArea;
226: }
227:
228: public int getEditorNr() {
229: return editorNr.get().intValue();
230: }
231:
232: public void setText(String text) {
233: textArea.setText(text);
234: }
235:
236: public String getText() {
237: return textArea.getText();
238: }
239:
240: public void setBackup(String backup) {
241: this .backup = backup;
242: }
243:
244: public String getBackup() {
245: return backup;
246: }
247:
248: public void save() {
249: try {
250: File file = File.createTempFile("wings", ".txt");
251: PrintWriter out = new PrintWriter(
252: new FileOutputStream(file));
253: out.print(textArea.getText());
254: out.close();
255:
256: FileResource resource = new FileResource(file);
257: resource.setExternalizerFlags(resource
258: .getExternalizerFlags()
259: | ExternalizeManager.REQUEST);
260:
261: Map headers = new HashMap();
262: headers.put("Content-Disposition", "attachment; filename="
263: + file.getName());
264: resource.setHeaders(headers.entrySet());
265:
266: final ScriptListener listener = new JavaScriptListener(
267: null, null, "location.href='" + resource.getURL()
268: + "'");
269: ScriptManager.getInstance().addScriptListener(listener);
270:
271: } catch (IOException e) {
272: System.err.println(e.getMessage());
273: e.printStackTrace(System.err);
274: }
275: }
276:
277: public void revert() {
278: textArea.setText(backup);
279: }
280:
281: public void close() {
282:
283: }
284:
285: public void cut() {
286: clip.set(textArea.getText());
287: textArea.setText("");
288:
289: }
290:
291: public void copy() {
292: clip.set(textArea.getText());
293: }
294:
295: public void paste() {
296: if (clip != null) {
297: textArea.setText(textArea.getText() + clip.get());
298: }
299: }
300:
301: public String openFile(File file) {
302: try {
303: Reader reader = new FileReader(file);
304: StringWriter writer = new StringWriter();
305:
306: int b;
307: while ((b = reader.read()) >= 0)
308: writer.write(b);
309:
310: setText(writer.toString());
311: reader.close();
312: return writer.toString();
313: } catch (Exception ex) {
314: return "";
315: }
316: }
317: }
|