📄 defaultuploadpolicy.java
字号:
/**
* Default implementation of
* {@link wjhk.jupload2.policies.UploadPolicy#createTopPanel(JButton, JButton, JButton, JUploadPanel)}
* . IT creates a JPanel, containing the three given JButton. It creates the
* same panel as the original JUpload.
*
* @see wjhk.jupload2.policies.UploadPolicy#createTopPanel(JButton, JButton,
* JButton, JUploadPanel)
*/
public JPanel createTopPanel(JButton browse, JButton remove,
JButton removeAll, JUploadPanel jUploadPanel) {
JPanel jPanel = new JPanel();
jPanel.setLayout(new GridLayout(1, 3, 10, 5));
jPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
jPanel.add(browse);
jPanel.add(removeAll);
jPanel.add(remove);
jUploadPanel.setBorder(BorderFactory
.createLineBorder(SystemColor.controlDkShadow));
return jPanel;
}
/**
* @see wjhk.jupload2.policies.UploadPolicy#createProgressPanel(JProgressBar,
* JProgressBar, JButton, JButton, JPanel)
*/
public JPanel createProgressPanel(JProgressBar preparationProgressBar,
JProgressBar uploadProgressBar, JButton uploadButton,
JButton stopButton, JPanel mainPanel) {
// There may be two progress bar: one for preparation progress of files
// (preparation before upload) and one to follow the actual upload.
JPanel jProgressBarPanel = new JPanel();
jProgressBarPanel.setLayout(new BorderLayout(10, 1));
jProgressBarPanel.add(preparationProgressBar, BorderLayout.NORTH);
jProgressBarPanel.add(uploadProgressBar, BorderLayout.SOUTH);
JPanel jProgressPanel = new JPanel();
jProgressPanel.setLayout(new BorderLayout(10, 0));
jProgressPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
jProgressPanel.add(uploadButton, BorderLayout.LINE_START);
jProgressPanel.add(jProgressBarPanel, BorderLayout.CENTER);
jProgressPanel.add(stopButton, BorderLayout.LINE_END);
return jProgressPanel;
}
/**
* @see wjhk.jupload2.policies.UploadPolicy#createStatusBar(javax.swing.JLabel,
* javax.swing.JPanel)
*/
public JPanel createStatusBar(JLabel content, JPanel mainPanel) {
if (this.showStatusbar) {
JPanel pstatus = new JPanel();
pstatus.setLayout(new BorderLayout());
pstatus.add(content, BorderLayout.CENTER);
pstatus.setBorder(new BevelBorder(BevelBorder.LOWERED));
return pstatus;
}
return null;
}
/**
* This methods allow the upload policy to override the default disposition
* of the components on the applet.
*
* @see UploadPolicy#addComponentsToJUploadPanel(JUploadPanel)
*/
public void addComponentsToJUploadPanel(JUploadPanel jUploadPanel) {
// Set the global layout of the panel.
jUploadPanel.setLayout(new BoxLayout(jUploadPanel, BoxLayout.Y_AXIS));
// The top panel is the upper part of the applet: above the file list.
// JPanel topPanel = new JPanel();
JPanel topPanel = createTopPanel(jUploadPanel.getBrowseButton(),
jUploadPanel.getRemoveButton(), jUploadPanel
.getRemoveAllButton(), jUploadPanel);
if (topPanel != null) {
jUploadPanel.add(topPanel);
topPanel.addMouseListener(jUploadPanel);
}
// Then, we add the file list.
jUploadPanel.add(jUploadPanel.getFilePanel().getDropComponent());
// The progress panel contains the progress bar, and the upload and stop
// buttons.
JPanel progressPanel = createProgressPanel(jUploadPanel
.getPreparationProgressBar(), jUploadPanel
.getUploadProgressBar(), jUploadPanel.getUploadButton(),
jUploadPanel.getStopButton(), jUploadPanel);
jUploadPanel.add(progressPanel);
jUploadPanel.addMouseListener(jUploadPanel);
// Now, we add the log window.
jUploadPanel.showOrHideLogWindow();
jUploadPanel.add(jUploadPanel.getJLogWindowPane());
// And, to finish with: the status bar.
JPanel p = createStatusBar(jUploadPanel.getStatusLabel(), jUploadPanel);
if (null != p) {
jUploadPanel.add(p);
p.addMouseListener(jUploadPanel);
}
}
/** @see UploadPolicy#displayErr(Exception) */
public void displayErr(Exception e) {
displayErr(e.getMessage(), e);
}
/** @see UploadPolicy#displayErr(String) */
public void displayErr(String err) {
displayErr(err, null);
}
/**
* Logs a stack trace for the given exception.
*
* @param throwable
*/
private void displayStackTrace(Throwable throwable) {
if (throwable != null) {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(bs);
throwable.printStackTrace(ps);
ps.close();
displayMsg("", bs.toString());
// If there is a cause, let's be sure its stack trace is displayed.
if (throwable.getCause() != null) {
displayMsg("", "Caused by:");
displayStackTrace(throwable.getCause());
}
}
}
/**
* If debug is off, the log window may not be visible. We switch the debug
* to on, to be sure that some information will be displayed to the user. <BR>
* If debug is -1, the log window remains hidden.
*
* @see wjhk.jupload2.policies.UploadPolicy#displayErr(java.lang.String,
* java.lang.Exception)
*/
public void displayErr(String errorText, Exception exception) {
if (exception == null) {
setLastException(new JUploadException("errorText"));
} else if (exception instanceof JUploadException) {
setLastException((JUploadException) exception);
} else {
setLastException(new JUploadException(exception));
}
// Default behavior: if debugLevel is 0, and an error occurs, we force
// the debug level to 1: this makes the log window become visible, if it
// was hidden.
if (getDebugLevel() == 0)
setDebugLevel(1);
String exceptionMsg = null;
String exceptionClassName = null;
String alertMsg = errorText;
String logMsg = errorText;
// First, we construct the exception class name.
if (exception == null) {
exceptionClassName = "";
} else if (exception instanceof JUploadException) {
exceptionClassName = "["
+ ((JUploadException) exception).getClassNameAndClause()
+ "] ";
} else {
exceptionClassName = "[" + exception.getClass().getName() + "] ";
}
// Then, the message body can be completed by the exception message.
if (exception != null) {
// Ok, we have an exception.
if (exception.getCause() != null) {
exceptionMsg = exception.getCause().getMessage();
} else {
exceptionMsg = exception.getMessage();
}
if (errorText == null || errorText.equals("")) {
alertMsg = "Unknown error (" + exceptionMsg + ")";
}
logMsg = exceptionMsg + " (" + errorText + ")";
}
// Add the message to the log window
displayMsg("[ERROR] ", exceptionClassName + logMsg);
// Let's display the stack trace, if relevant.
displayStackTrace(exception);
// Display the message to the user.
if (getDebugLevel() >= 100) {
// Debug has been put on (by the user or by applet configuration).
alertStr(exceptionClassName + logMsg);
} else {
// Debug level may be set to 1, when an error occurs, even if debug
// was not put on by the user.
alertStr(alertMsg);
}
// Then we copy the debug output to the clipboard, and say it to the
// current user.
if (getApplet().getUploadPanel() != null && getDebugLevel() == 100) {
// Ok, the applet has been fully built.
getApplet().getUploadPanel().copyLogWindow();
alert("messageLogWindowCopiedToClipboard");
}
}
/**
* @see UploadPolicy#displayInfo(String)
*/
public void displayInfo(String info) {
displayMsg("[INFO] ", info);
}
/**
* @see UploadPolicy#displayWarn(String)
*/
public void displayWarn(String warn) {
displayMsg("[WARN] ", warn);
}
/**
* @see UploadPolicy#displayDebug(String, int)
*/
public void displayDebug(String debug, int minDebugLevel) {
final String tag = "[DEBUG] ";
if (this.debugLevel >= minDebugLevel) {
// displayMsg will add the message to the debugStrignBuffer.
displayMsg(tag, debug);
} else if (this.debugGenerateFile) {
// We have to write the message to the debug file, whatever the
// debugLevel is.
addMsgToDebugLog(tag + debug);
}
}
/** @see UploadPolicy#getString(String) */
public String getString(String key) {
String ret = this.resourceBundle.getString(key);
return ret;
}
/**
* @see UploadPolicy#getUploadFilename(FileData, int)
*/
public String getUploadFilename(FileData fileData, int index)
throws JUploadException {
if (this.filenameEncoding == null || this.filenameEncoding.equals(""))
return fileData.getFileName();
try {
return URLEncoder.encode(fileData.getFileName(),
this.filenameEncoding);
} catch (UnsupportedEncodingException e) {
throw new JUploadException(e);
}
}
/** @see UploadPolicy#getUploadName(FileData, int) */
public String getUploadName(FileData fileData, int index) {
// This is the original way of working of JUpload.
// It can easily be modified, by using another UploadPolicy.
return "File" + index;
}
/** @see wjhk.jupload2.policies.UploadPolicy#isUploadReady() */
public boolean isUploadReady() {
// Default : nothing to do before upload, so we're ready.
return true;
}
/** @see UploadPolicy#onAppendHeader(ByteArrayEncoder) */
public ByteArrayEncoder onAppendHeader(ByteArrayEncoder bae)
throws JUploadIOException {
Iterator<String> it = this.headers.iterator();
String header;
while (it.hasNext()) {
header = it.next();
displayDebug(header, 70);
bae.append(header).append("\r\n");
}
return bae;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -