001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.html;
021:
022: /////////////////////////
023: //$Archive: /SOFIA/SourceCode/com/salmonllc/html/HtmlFileUpload.java $
024: //$Author: Dan $
025: //$Revision: 23 $
026: //$Modtime: 9/15/04 1:06p $
027: /////////////////////////
028:
029: import java.io.File;
030: import java.util.Hashtable;
031: import java.util.Vector;
032:
033: import com.salmonllc.html.events.FileUploadEvent;
034: import com.salmonllc.html.events.FileUploadListener;
035: import com.salmonllc.properties.Props;
036: import com.salmonllc.sql.DataStoreBuffer;
037:
038: /**
039: * This class will generate a file upload component.
040: */
041: public class HtmlFileUpload extends HtmlComponent {
042: byte[] _content;
043: String _fileName;
044: String _shortFileName;
045: String _mimeType;
046: private String _fontTagStart = null;
047: private String _fontTagEnd = null;
048: Vector _listeners;
049: boolean _fileUploaded;
050: int _size = -1;
051: private Vector _events;
052: private String _theme;
053: private DataStoreBuffer _ds;
054: private int _contentCol = -1;
055: private int _fileNameCol = -1;
056: private int _shortFileNameCol = -1;
057: private int _mimeTypeCol = -1;
058: private boolean _enabled = true;
059: private Integer _tabIndex;
060:
061: /**
062: * Constructs a new File Upload.
063: * @param name Each component on a page must have a unique name.
064: * @param p A Page object that will be used to initialize any properties in the object.
065: */
066: public HtmlFileUpload(String name, HtmlPage p) {
067: this (name, null, p);
068:
069: }
070:
071: /**
072: * Constructs a new File Upload.
073: * @param name Each component on a page must have a unique name.
074: * @param theme The theme to use for loading properties.
075: * @param p A Page object that will be used to initialize any properties in the object.
076: */
077: public HtmlFileUpload(String name, String theme, HtmlPage p) {
078: super (name, p);
079: setTheme(theme);
080:
081: }
082:
083: /**
084: * This method was created in VisualAge.
085: * @param e com.salmonllc.html.events.ValueChangedEvent
086: */
087: protected void addEvent(FileUploadEvent e) {
088: if (_events == null)
089: _events = new Vector();
090:
091: _events.addElement(e);
092: }
093:
094: /**
095: * This method adds a listener the will be notified when a file is uploaded.
096: * @param l The listener to add.
097: */
098: public void addFileUploadListener(FileUploadListener l) {
099: if (_listeners == null)
100: _listeners = new Vector();
101:
102: for (int i = 0; i < _listeners.size(); i++) {
103: if (((FileUploadListener) _listeners.elementAt(i)) == l)
104: return;
105: }
106:
107: _listeners.addElement(l);
108: }
109:
110: /**
111: * This method should be implemented by subclasses of this component that will events. This method should notify each of the component's listeners that an event occured.
112: * @param eventType valid Types are EVENT_SUBMIT and EVENT_OTHER
113: */
114: public boolean executeEvent(int eventType) throws Exception {
115: if (eventType != EVENT_OTHER)
116: return true;
117:
118: if (_events == null)
119: return true;
120:
121: for (int j = 0; j < _events.size(); j++) {
122: FileUploadEvent e = (FileUploadEvent) _events.elementAt(j);
123:
124: if (_listeners != null) {
125: for (int i = 0; i < _listeners.size(); i++)
126: ((FileUploadListener) _listeners.elementAt(i))
127: .fileUploaded(e);
128: }
129:
130: _fileName = e.getFileName();
131: _content = e.getContent();
132: _mimeType = e.getMimeType();
133: _shortFileName = e.getShortFileName();
134:
135: int iRowNo = e.getRow();
136: if (_ds != null) {
137: if (_fileNameCol != -1) {
138: if (iRowNo > -1)
139: _ds.setString(iRowNo, _fileNameCol, e
140: .getFileName());
141: else
142: _ds.setString(_fileNameCol, e.getFileName());
143: }
144: if (_shortFileNameCol != -1) {
145: if (iRowNo > -1)
146: _ds.setString(iRowNo, _shortFileNameCol, e
147: .getShortFileName());
148: else
149: _ds.setString(_shortFileNameCol, e
150: .getShortFileName());
151: }
152: if (_mimeTypeCol != -1) {
153: if (iRowNo > -1)
154: _ds.setString(e.getRow(), _mimeTypeCol, e
155: .getMimeType());
156: else
157: _ds.setString(_mimeTypeCol, e.getMimeType());
158: }
159: if (_contentCol != -1) {
160: if (iRowNo > -1)
161: _ds.setByteArray(e.getRow(), _contentCol, e
162: .getContent());
163: else
164: _ds.setByteArray(_contentCol, e.getContent());
165: }
166: }
167: }
168:
169: reset();
170: return true;
171: }
172:
173: public void generateHTML(java.io.PrintWriter p, int rowNo) {
174: if (!getVisible())
175: return;
176:
177: String tag = "<INPUT TYPE=\"FILE\" name=\"" + getFullName()
178: + (rowNo > -1 ? "_" + rowNo + "\"" : "\"");
179:
180: if ((!_enabled) && useDisabledAttribute())
181: tag += " disabled=\"true\"";
182:
183: if (_size > -1) {
184: int size = _size;
185: if (getPage().getBrowserType() == HtmlPage.BROWSER_NETSCAPE)
186: size = (int) (size * .60);
187: tag += " SIZE=\"" + size + "\"";
188: }
189:
190: if (_fileName != null)
191: tag += " VALUE=\"" + _fileName + "\"";
192:
193: if (_class != null)
194: tag += " class=\"" + _class + "\"";
195:
196: if (_tabIndex != null)
197: tag += " tabindex=\"" + _tabIndex + "\"";
198:
199: tag += ">";
200:
201: if (_fontTagStart != null)
202: tag = _fontTagStart + tag + _fontTagEnd;
203:
204: p.println(tag);
205: }
206:
207: /**
208: * This method returns the contents of the file uploaded.
209: */
210: public byte[] getContent() {
211: return _content;
212: }
213:
214: /**
215: * This method returns the name of the file uploaded.
216: */
217: public String getFileName() {
218: return _fileName;
219: }
220:
221: /**
222: * This method returns the short name of the file uploaded.
223: */
224: public String getShortFileName() {
225: return _shortFileName;
226: }
227:
228: /**
229: * This method gets the end font tag for the component.
230: */
231: public String getFontEndTag() {
232: return _fontTagEnd;
233: }
234:
235: /**
236: * This method gets the start font tag for the component.
237: */
238: public String getFontStartTag() {
239: return _fontTagStart;
240: }
241:
242: /**
243: * This method returns the name of the mimeType uploaded.
244: */
245: public String getMimeType() {
246: return _mimeType;
247: }
248:
249: /**
250: * This method gets the size of the component.
251: */
252: public int getSize() {
253: return _size;
254: }
255:
256: /**
257: * This method returns the property theme for the component.
258: */
259: public String getTheme() {
260: return _theme;
261: }
262:
263: public boolean processParms(Hashtable parms, int row) {
264: if (!getVisible())
265: return false;
266:
267: String name = getFullName();
268: if (row > -1)
269: name += "_" + row;
270:
271: String filename = "";
272: String shortFileName = "";
273: if (parms.get(name) == null)
274: return false;
275: else if (parms.get(name) instanceof java.lang.String)
276: filename = (String) parms.get(name);
277: else
278: filename = ((String[]) parms.get(name))[0];
279:
280: _fileName = null;
281: _mimeType = null;
282: _content = null;
283: _shortFileName = null;
284:
285: if (filename != null) {
286: String mimeType = (String) parms.get(name + "_contentType");
287: byte[] content = (byte[]) parms.get(name + "_content");
288: shortFileName = setShortFileName(filename);
289: addEvent(new FileUploadEvent(getPage(), this , getName(),
290: getFullName(), filename, shortFileName, content,
291: mimeType, row));
292: }
293: return false;
294:
295: }
296:
297: /**
298: * This method extracts the file name from a directory.
299: * @param sFileName String
300: */
301: private String setShortFileName(String sFileName) {
302: if (sFileName == null) {
303: _shortFileName = null;
304: return _shortFileName;
305: }
306: int iIdx = sFileName.lastIndexOf(File.separator);
307:
308: if (iIdx < 0) {
309: _shortFileName = sFileName;
310: return _shortFileName;
311: }
312:
313: _shortFileName = sFileName.substring(iIdx + 1, sFileName
314: .length());
315: return _shortFileName;
316: }
317:
318: /**
319: * This method was created in VisualAge.
320: */
321: protected void removeEvent(int index) {
322: _events.removeElementAt(index);
323: }
324:
325: /**
326: * This method removes a listener from the list that will be notified if the text in the component changes.
327: * @param l The listener to remove.
328: */
329: public void removeFileUploadListener(FileUploadListener l) {
330: if (_listeners == null)
331: return;
332:
333: for (int i = 0; i < _listeners.size(); i++) {
334: if (((FileUploadListener) _listeners.elementAt(i)) == l) {
335: _listeners.removeElementAt(i);
336: return;
337: }
338: }
339: }
340:
341: /**
342: * This method will clear all pending events from the event queue for this component.
343: */
344: public void reset() {
345: if (_events != null) {
346: _events.setSize(0);
347: }
348: }
349:
350: /**
351: * Use this method to bind the component to columns in a DataStore.
352: * @param ds The datastore to bind to.
353: * @param fileNameColumn The column in the datastore to put the filename
354: * @param shortFileNameColumn The column in the datastore to put the short filename
355: * @param mimeTypeColumn The column in the datastore to put the mimetype
356: * @param contentColumn The column in the datastore to put the content
357: */
358: public void setColumns(DataStoreBuffer ds, String fileNameColumn,
359: String shortFileNameColumn, String mimeTypeColumn,
360: String contentColumn) {
361: _ds = ds;
362: if (fileNameColumn != null)
363: _fileNameCol = ds.getColumnIndex(fileNameColumn);
364: else
365: _fileNameCol = -1;
366:
367: if (shortFileNameColumn != null)
368: _shortFileNameCol = ds.getColumnIndex(shortFileNameColumn);
369: else
370: _shortFileNameCol = -1;
371:
372: if (mimeTypeColumn != null)
373: _mimeTypeCol = ds.getColumnIndex(mimeTypeColumn);
374: else
375: _mimeTypeCol = -1;
376:
377: if (contentColumn != null)
378: _contentCol = ds.getColumnIndex(contentColumn);
379: else
380: _contentCol = -1;
381: }
382:
383: /**
384: * Use this method to bind the component to columns in a DataStore.
385: * @param ds The datastore to bind to.
386: * @param fileNameColumn The column in the datastore to put the filename
387: * @param mimeTypeColumn The column in the datastore to put the mimetype
388: * @param contentColumn The column in the datastore to put the content
389: */
390: public void setColumns(DataStoreBuffer ds, String fileNameColumn,
391: String mimeTypeColumn, String contentColumn) {
392: setColumns(ds, fileNameColumn, null, mimeTypeColumn,
393: contentColumn);
394: }
395:
396: /**
397: * This method returns the name of the file to be uploaded.
398: */
399: public void setFileName(String fileName) {
400: _fileName = fileName;
401: }
402:
403: /**
404: * This method sets the end font tag for the component.
405: */
406: public void setFontEndTag(String value) {
407: _fontTagEnd = value;
408: }
409:
410: /**
411: * This method sets the start font tag for the component.
412: */
413: public void setFontStartTag(String value) {
414: _fontTagStart = value;
415: }
416:
417: /**
418: * This method sets the size of the component.
419: */
420: public void setSize(int size) {
421: _size = size;
422: }
423:
424: /**
425: * This method sets the property theme for the component.
426: * @param theme The theme to use.
427: */
428: public void setTheme(String theme) {
429:
430: Props props = getPage().getPageProperties();
431:
432: _fontTagStart = props.getThemeProperty(theme,
433: Props.FONT_TEXT_EDIT + Props.TAG_START);
434: _fontTagEnd = props.getThemeProperty(theme,
435: Props.FONT_TEXT_EDIT + Props.TAG_END);
436:
437: _theme = theme;
438: }
439:
440: public boolean isEnabled() {
441: return _enabled;
442: }
443:
444: public void setEnabled(boolean enabled) {
445: _enabled = enabled;
446: }
447:
448: /**
449: * @returns the tab index html attribute
450: */
451: public int getTabIndex() {
452: if (_tabIndex == null)
453: return -1;
454: return _tabIndex.intValue();
455: }
456:
457: /**
458: * @param sets the tab index html attribute. You can also pass TAB_INDEX_DEFAULT to use the default tab index for the component or TAB_INDEX_NONE to keep this component from being tabbed to
459: */
460: public void setTabIndex(int val) {
461: if (val == -1)
462: _tabIndex = null;
463: else
464: _tabIndex = new Integer(val);
465: }
466:
467: /**
468: * Clears the content to free memory for big uploads
469: */
470: public void clearContent() {
471: _content = null;
472: }
473: }
|