📄 attachment.java
字号:
log.log(Level.SEVERE, "Could not create image");
}
}
if (graphPanel.getComponentCount() == 0)
{
graphPanel.add(info, BorderLayout.CENTER);
}
log.config("Size=" + size);
// graphPanel.setPreferredSize(size);
// centerPane.setDividerLocation(size.width+30);
// size.width += 100;
// size.height += 100;
// centerPane.setPreferredSize(size);
pack();
} // displayData
/**
* Get File Name with index
* @param index index
* @return file name or null
*/
private String getFileName (int index)
{
String fileName = null;
if (cbContent.getItemCount() > index)
fileName = (String)cbContent.getItemAt(index);
return fileName;
} // getFileName
/**
* Action Listener
* @param e event
*/
public void actionPerformed(ActionEvent e)
{
// log.config( "Attachment.actionPerformed - " + e.getActionCommand());
// Save and Close
if (e.getActionCommand().equals(ConfirmPanel.A_OK))
{
String newText = text.getText();
if (newText == null)
newText = "";
String oldText = m_attachment.getTextMsg();
if (oldText == null)
oldText = "";
if (!m_change)
m_change = !newText.equals(oldText);
if (newText.length() > 0 || m_attachment.getEntryCount() > 0)
{
if (m_change)
{
m_attachment.setTextMsg(text.getText());
m_attachment.save();
}
}
else
m_attachment.delete(true);
dispose();
}
// Cancel
else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL))
{
dispose();
}
// Delete Attachment
else if (e.getSource() == bDeleteAll)
{
deleteAttachment();
dispose();
}
// Delete individual entry and Return
else if (e.getSource() == bDelete)
deleteAttachmentEntry();
// Show Data
else if (e.getSource() == cbContent)
displayData (cbContent.getSelectedIndex());
// Load Attachment
else if (e.getSource() == bLoad)
loadFile();
// Open Attachment
else if (e.getSource() == bSave)
saveAttachmentToFile();
// Open Attachment
else if (e.getSource() == bOpen)
{
if (!openAttachment())
saveAttachmentToFile();
}
} // actionPerformed
/**************************************************************************
* Load file for attachment
*/
private void loadFile()
{
log.info("");
JFileChooser chooser = new JFileChooser();
chooser.setDialogType(JFileChooser.OPEN_DIALOG);
chooser.setDialogTitle(Msg.getMsg(Env.getCtx(), "AttachmentNew"));
int returnVal = chooser.showOpenDialog(this);
if (returnVal != JFileChooser.APPROVE_OPTION)
return;
//
String fileName = chooser.getSelectedFile().getName();
log.config(fileName);
File file = chooser.getSelectedFile();
if (m_attachment.addEntry(file))
{
cbContent.addItem(fileName);
cbContent.setSelectedIndex(cbContent.getItemCount()-1);
m_change = true;
}
} // getFileName
/**
* Delete entire Attachment
*/
private void deleteAttachment()
{
log.info("");
if (ADialog.ask(m_WindowNo, this, "AttachmentDelete?"))
m_attachment.delete(true);
} // deleteAttachment
/**
* Delete Attachment Entry
*/
private void deleteAttachmentEntry()
{
log.info("");
int index = cbContent.getSelectedIndex();
String fileName = getFileName(index);
if (fileName == null)
return;
//
if (ADialog.ask(m_WindowNo, this, "AttachmentDeleteEntry?", fileName))
{
if (m_attachment.deleteEntry(index))
cbContent.removeItemAt(index);
m_change = true;
}
} // deleteAttachment
/**
* Save Attachment to File
*/
private void saveAttachmentToFile()
{
int index = cbContent.getSelectedIndex();
log.info("index=" + index);
if (m_attachment.getEntryCount() < index)
return;
String fileName = getFileName(index);
String ext = fileName.substring (fileName.lastIndexOf("."));
log.config( "Ext=" + ext);
JFileChooser chooser = new JFileChooser();
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
chooser.setDialogTitle(Msg.getMsg(Env.getCtx(), "AttachmentSave"));
File f = new File(fileName);
chooser.setSelectedFile(f);
// Show dialog
int returnVal = chooser.showSaveDialog(this);
if (returnVal != JFileChooser.APPROVE_OPTION)
return;
File saveFile = chooser.getSelectedFile();
if (saveFile == null)
return;
log.config("Save to " + saveFile.getAbsolutePath());
m_attachment.getEntryFile(index, saveFile);
} // saveAttachmentToFile
/**
* Open the temporary file with the application associated with the extension in the file name
* @return true if file was opened with third party application
*/
private boolean openAttachment ()
{
int index = cbContent.getSelectedIndex();
byte[] data = m_attachment.getEntryData(index);
if (data == null)
return false;
try
{
String fileName = System.getProperty("java.io.tmpdir") + m_attachment.getEntryName(index);
File tempFile = new File(fileName);
m_attachment.getEntryFile(index, tempFile);
if (Env.isWindows())
{
// Runtime.getRuntime().exec ("rundll32 url.dll,FileProtocolHandler " + url);
Process p = Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL \"" + tempFile + "\"");
// p.waitFor();
return true;
}
else if (Env.isMac())
{
String [] cmdArray = new String [] {"open", tempFile.getAbsolutePath()};
Process p = Runtime.getRuntime ().exec (cmdArray);
// p.waitFor();
return true;
}
else // other OS
{
}
}
catch (Exception e)
{
log.log(Level.SEVERE, "", e);
}
return false;
} // openFile
/**************************************************************************
* Graphic Image Panel
*/
class GImage extends JPanel
{
/**
* Graphic Image
*/
public GImage()
{
super();
} // GImage
/** The Image */
private Image m_image = null;
/**
* Set Image
* @param image image
*/
public void setImage (Image image)
{
m_image = image;
if (m_image == null)
return;
MediaTracker mt = new MediaTracker(this);
mt.addImage(m_image, 0);
try {
mt.waitForID(0);
} catch (Exception e) {}
Dimension dim = new Dimension(m_image.getWidth(this), m_image.getHeight(this));
this.setPreferredSize(dim);
} // setImage
/**
* Paint
* @param g graphics
*/
public void paint (Graphics g)
{
Insets in = getInsets();
if (m_image != null)
g.drawImage(m_image, in.left, in.top, this);
} // paint
/**
* Update
* @param g graphics
*/
public void update (Graphics g)
{
paint(g);
} // update
} // GImage
/**
* Test PDF Viewer
* @param args ignored
*/
public static void main (String[] args)
{
} // main
} // Attachment
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -