⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 attachment.java

📁 Java写的ERP系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************
 * The contents of this file are subject to the   Compiere License  Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * You may obtain a copy of the License at http://www.compiere.org/license.html
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Original Code is                  Compiere  ERP & CRM  Business Solution
 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
 * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
 * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.apps;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;

import org.compiere.util.*;
import org.compiere.plaf.*;
import org.compiere.swing.*;

/**
 *  Attachment
 *
 * 	@author 	Jorg Janke
 * 	@version 	$Id: Attachment.java,v 1.9 2003/02/14 06:44:13 jjanke Exp $
 */
public final class Attachment extends JDialog implements ActionListener
{
	/**
	 *	Constructor.
	 *	loads Attachment, if ID <> 0
	 *  @param frame frame
	 *  @param WindowNo window no
	 *  @param AD_Attachment_ID attachment
	 *  @param AD_Table_ID table
	 *  @param Record_ID record key
	 */
	public Attachment(Frame frame, int WindowNo, int AD_Attachment_ID,
		int AD_Table_ID, int Record_ID)
	{
		super(frame, Msg.getMsg(Env.getCtx(), "Attachment"), true);
		Log.trace(Log.l3_Util, "Attachment - ID=" + AD_Attachment_ID
			+ ", Table=" + AD_Table_ID + ", Record=" + Record_ID);
		//
		m_WindowNo = WindowNo;
		m_AD_Attachment_ID = AD_Attachment_ID;
		m_AD_Table_ID = AD_Table_ID;
		m_Record_ID = Record_ID;
		//
		try
		{
			jbInit();
		}
		catch (Exception ex)
		{
			Log.error("Attachment", ex);
		}
		if (AD_Attachment_ID == 0)
			bDelete.setEnabled(false);
		else
			loadAttachment();
		//
		text.requestFocus();
		AEnv.positionCenterWindow(frame, this);
	}	//	Attachment

	private int		m_WindowNo;
	private int 	m_AD_Attachment_ID, m_AD_Table_ID, m_Record_ID;

	/** Indicator for no data   */
	private static final String NONE = ".";
	/** File to be loaded   */
	private File    m_file = null;
	/** Data from BLOB      */
	private byte[]  m_data = null;
	//
	private CPanel mainPanel = new CPanel();
	private BorderLayout mainLayout = new BorderLayout();
	private CText text = new CText();
	private CButton bOpen = new CButton();
	private CPanel northPanel = new CPanel();
	private CButton bLoad = new CButton();
	private BorderLayout northLayout = new BorderLayout();
	private CLabel title = new CLabel();
	private ConfirmPanel confirmPanel = new ConfirmPanel(true);
	private CPanel toolBar = new CPanel(new FlowLayout(FlowLayout.LEADING, 5,5));
	private CButton bDelete = new CButton();
	private GImage gifPanel = new GImage();
	private JSplitPane centerPane1 = new JSplitPane();

	/**
	 *	Static setup.
	 *  <pre>
	 *  - northPanel
	 *      - toolBar
	 *      - title
	 *  - centerPanel
	 *  	- gifPanel (left)
	 *  	- text (right)
	 *  - confirmPanel
	 *  </pre>
	 *  @throws Exception
	 */
	void jbInit() throws Exception
	{
		CompiereColor.setBackground(this);
		mainPanel.setLayout(mainLayout);
		mainLayout.setHgap(5);
		mainLayout.setVgap(5);
		this.getContentPane().add(mainPanel);

		northPanel.setLayout(northLayout);
		northPanel.add(title, BorderLayout.CENTER);
		northPanel.add(toolBar, BorderLayout.WEST);
		toolBar.add(bLoad);
		toolBar.add(bDelete);
		toolBar.add(bOpen);
		mainPanel.add(northPanel, BorderLayout.NORTH);

		bOpen.setEnabled(false);
		bOpen.setIcon(Env.getImageIcon("Editor24.gif"));
		bOpen.setMargin(new Insets(0, 2, 0, 2));
		bOpen.setText(Msg.getMsg(Env.getCtx(), "Open"));
		bOpen.addActionListener(this);
		bLoad.setIcon(Env.getImageIcon("Import24.gif"));
		bLoad.setMargin(new Insets(0, 2, 0, 2));
		bLoad.setText(Msg.getMsg(Env.getCtx(), "Load"));
		bLoad.addActionListener(this);
		bDelete.setIcon(Env.getImageIcon("Delete24.gif"));
		bDelete.setMargin(new Insets(0, 2, 0, 2));
		bDelete.setText(Msg.getMsg(Env.getCtx(), "Delete"));
		bDelete.addActionListener(this);
		title.setHorizontalAlignment(SwingConstants.CENTER);
		title.setHorizontalTextPosition(SwingConstants.CENTER);
		//
		text.setBackground(CompierePLAF.getInfoBackground());
		text.setPreferredSize(new Dimension(200, 200));
		//
		mainPanel.add(confirmPanel, BorderLayout.SOUTH);
		confirmPanel.addActionListener(this);
		//
		mainPanel.add(centerPane1, BorderLayout.CENTER);
		centerPane1.add(gifPanel, JSplitPane.LEFT);
		centerPane1.add(text, JSplitPane.RIGHT);
	}	//	jbInit

	/**
	 *	Load Attachment
	 */
	private void loadAttachment()
	{
		Log.trace(Log.l2_Sub, "Attachment.load");
		String SQL = "SELECT Text, Title, BinaryData FROM AD_Attachment WHERE AD_Attachment_ID="
			+ m_AD_Attachment_ID;
		try
		{
			Statement stmt = DB.createStatement();
			ResultSet rs = stmt.executeQuery(SQL);
			if (rs.next())
			{
				//	Set Text
				String sText = rs.getString(1);
				if (sText == null)
					text.setText("");
				else
					text.setText(sText);

				//	Set Name/Title
				String sName = rs.getString(2);
				if (sName == null || sName.equals("") || sName.equals(NONE))
				{
					sName = NONE;
					bOpen.setEnabled(false);
				}
				else
					bOpen.setEnabled(true);
				title.setText(sName);
				this.setTitle(Msg.getMsg(Env.getCtx(), "Attachment") + " " + sName);

				//  BLOB
				BLOB blob = ((OracleResultSet)rs).getBLOB(3);
				int length = (int)blob.length();
				Log.trace(Log.l4_Data, "Attachment.load - BLOB length=" + length);
				if (length > 0)
					m_data = blob.getBytes(1, length);
				displayData(m_data);
			}
			else
			{
				m_AD_Attachment_ID = 0;
				bDelete.setEnabled(false);
				ADialog.error(m_WindowNo, this, "AttachmentNotFound");
			}
			rs.close();
			stmt.close();
		}
		catch (SQLException e)
		{
			Log.error("Attachment.load", e);
			m_AD_Attachment_ID = 0;
		}
	}	//	loadAttachment

	/**
	 *  Display gif or jpg in gifPanel
	 *  @param data data
	 */
	private void displayData(byte data[])
	{
		String t = title.getText().toLowerCase().trim();
		Log.trace(Log.l3_Util, "Attachment.displayData - " + t + " - " + (data==null ? "null" : String.valueOf(data.length)));
		//  Can we display it
		if (data == null || data.length == 0)
			gifPanel.setImage(null);
		else if (t.endsWith(".gif") || t.endsWith(".jpg"))
		{
			Log.trace(Log.l4_Data, "Attachment.displayData - Set Image");
			Image image = Toolkit.getDefaultToolkit().createImage(data);
			gifPanel.setImage(image);
		}
		else
		{
			Log.trace(Log.l4_Data, "Attachment.displayData - Cannot display");
			gifPanel.setImage(null);
		}
		pack();
	}   //  displayData

	/**
	 *	Action Listener
	 *  @param e event
	 */
	public void actionPerformed(ActionEvent e)
	{
		//	Save and Return
		if (e.getActionCommand().equals(ConfirmPanel.A_OK))
		{
			saveAttachment();
			dispose();
		}
		//	Cancel
		else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL))
			dispose();
		//	Delete and Return
		else if (e.getSource() == bDelete)
		{
			deleteAttachment();
			dispose();
		}
		//	Load Attachment
		else if (e.getSource() == bLoad)
			getFileName();
		//	Open Attachment
		else if (e.getSource() == bOpen)
			openAttachment();

	}	//	actionPerformed

	/*************************************************************************/

	/**
	 *	Save Attachment
	 */
	private void saveAttachment()
	{
		Log.trace(Log.l1_User, "Attachment.save");
		//	Constants for Insert/Update
		Timestamp today = new Timestamp(System.currentTimeMillis());
		int userID = Env.getContextAsInt(Env.getCtx(), "#AD_User_ID");
		//
		String sName = title.getText();
		if (sName == null || sName.equals(""))
			sName = NONE;
		//  Create Attachment
		if (m_AD_Attachment_ID == 0)
		{
			m_AD_Attachment_ID = DB.getKeyNextNo(Env.getCtx(), m_WindowNo, "AD_Attachment");
			String SQL = "INSERT INTO AD_Attachment (AD_Attachment_ID,"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -