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

📄 printerpanel.java

📁 打印管理程序,测试完全通过.windows开发环境.
💻 JAVA
字号:
package jp.co.ntl.swing.ext.printer.printer;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JApplet;
import javax.swing.JButton;
///import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import jp.co.ntl.ActionEventConstants;
import jp.co.ntl.Page;
import jp.co.ntl.Net;
import jp.co.ntl.Util;
import jp.co.ntl.NetworkException;
import jp.co.ntl.ServerException;
import jp.co.ntl.TimeOutException;
import jp.co.ntl.printer.PrinterInfo2;
import jp.co.ntl.paper.PaperInfo;
import jp.co.ntl.swing.BasePanel;
import jp.co.ntl.swing.TableSorter;
import jp.co.ntl.swing.ext.DialogManager;
import jp.co.ntl.swing.ext.MsgUtil;
///import jp.co.ntl.swing.ext.CSVFileFilter;
import jp.co.ntl.swing.ext.printer.place.PlaceInfoInvoker;
import jp.co.ntl.swing.ext.printer.printergroup.PrinterGroupInfoInvoker;
import jp.co.ntl.swing.ext.user.group.GroupInfoInvoker;
import jp.co.ntl.swing.ext.printer.paper.PaperInfoInvoker;

public class PrinterPanel extends BasePanel implements ActionListener, ListSelectionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable				tblPrinter;
	private PrinterTableModel	mdlPrinter;
	private ListSelectionModel mdlPrinterSelect;
	private JScrollPane		scrPrinter;
	private JButton			btnRegister;
	private JButton			btnModify;
	private JButton			btnDelete;
///	private JButton			btnMultiRegister;
	private JButton			btnRefresh;
	
	private JMenuItem			menuModify;
	private JMenuItem			menuDelete;
	
	private Vector				vcPrinterInfo;
	private Vector				vcPrinterGroupInfo;
	private Vector				vcGroupInfo;
	private Vector				vcPlaceInfo;
	private Vector				vcInstalledPrinter;
	private PrinterInfoInvoker	invoker;
	private TableSorter		sorter;
	
	public PrinterPanel(JFrame parent, Net net) {
		super(parent, net);
	}
	
	public PrinterPanel(JApplet appl, Net net) {
		super(appl, net);
	}
	
	protected void buildComponents() {
		Resource.load(loc);
		
	    Page.initialize(net);
	    invoker = PrinterInfoInvoker.getInstance(net);
	    
	    createPopupMenu();
	    
	    vcPrinterGroupInfo = new Vector();
	    vcGroupInfo = new Vector();
	    vcPrinterInfo = new Vector();
	    mdlPrinter = new PrinterTableModel(vcPrinterInfo);
	    sorter = new TableSorter(mdlPrinter);
	    tblPrinter = new JTable(sorter);
	    sorter.setTableHeader(tblPrinter.getTableHeader());
	    scrPrinter = new JScrollPane(tblPrinter);
	    add(scrPrinter, BorderLayout.CENTER);
	    tblPrinter.getTableHeader().setReorderingAllowed(false);

	    btnRegister = new JButton(Resource.getString(Resource.REGISTER));
	    btnModify = new JButton(Resource.getString(Resource.MODIFY));
	    btnDelete = new JButton(Resource.getString(Resource.DELETE));
///	    btnMultiRegister = new JButton(Resource.getString(Resource.MULTI_REGISTER));
	    btnRefresh = new JButton(Resource.getString(Resource.REFRESH));
	    addButtonPanel(
	    		new JButton[] {btnRegister, btnModify, btnDelete/*, btnMultiRegister*/, btnRefresh},
	    		new String[] {
	    				Resource.getString(Resource.TIP_REGISTER),
	    				Resource.getString(Resource.TIP_MODIFY),
	    				Resource.getString(Resource.TIP_DELETE),
///	    				Resource.getString(Resource.TIP_MULTI_REGISTER),
	    				Resource.getString(Resource.TIP_REFRESH)
	    		});
	    
	    btnRegister.addActionListener(this);
	    btnModify.addActionListener(this);
	    btnDelete.addActionListener(this);
///	    btnMultiRegister.addActionListener(this);
	    btnRefresh.addActionListener(this);
	    
	    mdlPrinterSelect = tblPrinter.getSelectionModel();
	    mdlPrinterSelect.addListSelectionListener(this);
	    mdlPrinterSelect.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
	    
	    setButtonState(false, 0);
	    tblPrinter.setColumnSelectionAllowed(false);
	    
	    tblPrinter.addMouseListener(this);
	}
	
	private void createPopupMenu() {
		popupMenu = new JPopupMenu();
		menuModify = new JMenuItem(Resource.getString(Resource.MODIFY));
		menuModify.addActionListener(this);
		popupMenu.add(menuModify);
		menuDelete = new JMenuItem(Resource.getString(Resource.DELETE));
		menuDelete.addActionListener(this);
		popupMenu.add(menuDelete);
	}
	
	public void beginPanel() throws NetworkException, ServerException, TimeOutException {
		doRefresh();
	}
	
	private void setButtonState(boolean b, int selectRows) {
		if (selectRows <= 1) {
			btnModify.setEnabled(b);
		} else {
			btnModify.setEnabled(false);
		}
		btnDelete.setEnabled(b);
	}
	
	protected void setPopupMenuState() {
		if (tblPrinter.getSelectedRowCount() > 1) {
			menuModify.setEnabled(false);
		} else {
			menuModify.setEnabled(true);
		}
	}
	
	private boolean getPrinterInfo() throws NetworkException, ServerException, TimeOutException {
		Vector	vcTempPrinterInfo = new Vector();

		vcTempPrinterInfo = invoker.getPrinterInfo();
		mdlPrinter.addInfos(vcTempPrinterInfo);
		
		return true;
	}
	
	private boolean getPrinterGroupInfo() throws NetworkException, ServerException, TimeOutException {
		PrinterGroupInfoInvoker	pgInvoker = PrinterGroupInfoInvoker.getInstance(net);
		vcPrinterGroupInfo = pgInvoker.getPrinterGroupInfo();
		return true;
	}
	
	private boolean getGroupInfo() throws NetworkException, ServerException, TimeOutException {
		GroupInfoInvoker	groupInvoker = GroupInfoInvoker.getInstance(net);
		vcGroupInfo = groupInvoker.getGroupInfo();
		return true;
	}
	
	private boolean getPlaceInfo() throws NetworkException, ServerException, TimeOutException {
		PlaceInfoInvoker	placeInvoker = PlaceInfoInvoker.getInstance(net);
		vcPlaceInfo = placeInvoker.getPlaceInfo();
		return true;
	}

	private boolean getPrinterList() throws NetworkException, ServerException, TimeOutException {
		vcInstalledPrinter = invoker.getPrinterList();
		return true;
	}
	
	protected boolean doRegister() throws NetworkException, ServerException, TimeOutException {
		// 僾儕儞僞偑僀儞僗僩乕儖偝傟偰偄側偄
		if (vcInstalledPrinter.size() == 0) {
			DialogManager.showMessage(this, DialogManager.ERROR_NOT_INSTALL_PRINTER);
			return false;
		}
		
		// 僾儕儞僞僌儖乕僾偑搊榐偝傟偰偄側偄
		if (vcPrinterGroupInfo.size() == 0) {
			DialogManager.showMessage(this, DialogManager.ERROR_NOT_REGISTER_PRINTERGROUP);
			return false;
		}
		
		// 僌儖乕僾偑搊榐偝傟偰偄側偄
		if (vcGroupInfo.size() == 0) {
			DialogManager.showMessage(this, DialogManager.ERROR_NOT_REGISTER_GROUP);
			return false;
		}
		
		// 愝抲応強偑搊榐偝傟偰偄側偄
		if (vcPlaceInfo.size() == 0) {
			DialogManager.showMessage(this, DialogManager.ERROR_NOT_REGISTER_PLACE);
			return false;
		}
		
		PrinterInfo2	prnInfo = new PrinterInfo2();
		PrinterInfoPanel	p = new PrinterInfoPanel(false, prnInfo, vcPrinterInfo, vcPrinterGroupInfo, vcGroupInfo, vcPlaceInfo, vcInstalledPrinter);
		
		if (DialogManager.showCustomDialog(
				this,
				Resource.getString(Resource.PRINTERINFO_TITLE),	p) == PrinterInfoPanel.OK_BUTTON) {
			
			// 僾儕儞僞忣曬傪搊榐
			int id = invoker.addPrinter(prnInfo);
			prnInfo.setPrinterID(id);
			mdlPrinter.addInfo(prnInfo);
			
			// 梡巻僒僀僘"Unknown"傪搊榐
			PaperInfo			paperInfo = new PaperInfo();
			PaperInfoInvoker	paperInvoker = PaperInfoInvoker.getInstance(net);
			paperInfo.setPaperName("Unknown");
			paperInfo.setPrice(10);
			paperInfo.setPrinterInfo(prnInfo);
			id = paperInvoker.addPaper(paperInfo);
			paperInfo.setPaperID(id);
			
			sendActionEvent(ActionEventConstants.ACTCMD_REFRESH_TERMINAL);
			sendActionEvent(ActionEventConstants.ACTCMD_REFRESH_PRINTER_PAPER);
		}
		
		return true;
	}
	
	protected boolean doModify() throws NetworkException, ServerException, TimeOutException {
		int	selRow = sorter.modelIndex(tblPrinter.getSelectedRow());
		if (selRow >= 0) {
			PrinterInfo2	prnInfo = (PrinterInfo2)mdlPrinter.getSelectedInfo(selRow);
			PrinterInfoPanel	p = new PrinterInfoPanel(true, prnInfo, vcPrinterInfo, vcPrinterGroupInfo, vcGroupInfo, vcPlaceInfo, vcInstalledPrinter);

			if (DialogManager.showCustomDialog(
					this,
					Resource.getString(Resource.PRINTERINFO_TITLE),
					p) == PrinterInfoPanel.OK_BUTTON) {
				invoker.setPrinterInfo(prnInfo);
				mdlPrinter.setInfo(selRow, prnInfo);
				sendActionEvent(ActionEventConstants.ACTCMD_REFRESH_TERMINAL);
				sendActionEvent(ActionEventConstants.ACTCMD_REFRESH_PRINTER_PAPER);
			}
		}
		return true;
	}
	
	protected boolean doDelete() throws NetworkException, ServerException, TimeOutException {
		int[]	selViewRows = tblPrinter.getSelectedRows();
		
		Vector	vcPrinterInfo = new Vector();
		for (int i = 0; i < selViewRows.length; i++) {
			int	selRow = sorter.modelIndex(selViewRows[i]);
			if (selRow >= 0) {
				vcPrinterInfo.addElement(mdlPrinter.getSelectedInfo(selRow));
			}
		}
		
		// 報嶞棜楌偵愝掕偝傟偰偄傞偐僠僃僢僋
		if (invoker.isRelatedPrinterOnAccount(vcPrinterInfo)) {
			DialogManager.showMessage(this, DialogManager.ERROR_DELETE_PRINTER_RELATED_ACCOUNT);
			return false;
		}
				
		// 報嶞巜帵抂枛傑偨偼丄梡巻忣曬偵娭楢晅偗偝傟偰偄傞偐僠僃僢僋
		if (invoker.isRelatedPrinter(vcPrinterInfo)) {
			if (Util.getPreferenceInfo().isPaperInfoSupport()) {
				DialogManager.showMessage(this, DialogManager.ERROR_DELETE_PRINTER_RELATED_TERMINAL_OR_PAPER);
			} else {
				DialogManager.showMessage(this, DialogManager.ERROR_DELETE_PRINTER_RELATED_TERMINAL);
			}
			return false;
		}

		if (DialogManager.showConfirmMessage(
				this,
				DialogManager.CONFIRM_DELETE,
				JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
			invoker.delPrinter(vcPrinterInfo);
			
			for (int i = selViewRows.length - 1; i >= 0; i--) {
				int	selRow = sorter.modelIndex(selViewRows[i]);
				if (selRow >= 0) {
					mdlPrinter.deleteInfo(selRow);
				}
			}
			sendActionEvent(ActionEventConstants.ACTCMD_REFRESH_TERMINAL);
			sendActionEvent(ActionEventConstants.ACTCMD_REFRESH_PRINTER_PAPER);
			mdlPrinterSelect.clearSelection();
		}
		return true;
	}
/*
	private void doMultiRegister() throws NetworkException, ServerException, TimeOutException {
		if (!isApplet()) {
			CSVFileFilter	filter = new CSVFileFilter();
			JFileChooser	fc = new JFileChooser();
			fc.setFileFilter(filter);
			
			int	ret = fc.showOpenDialog(this);
			if (ret == JFileChooser.APPROVE_OPTION) {
				
			}
		}
	}*/
	
	public int getInfosFromServer() {
		int		result = GET_INFO_RESULT_NORMAL;

		mdlPrinter.deleteAllInfos();
		
		try {
			getPrinterInfo();
		} catch (NetworkException e) {
			result = GET_INFO_RESULT_ERROR;
			sendActionEvent(MsgUtil.getMessage(MsgUtil.ERROR_NETWORK, null));
		} catch (ServerException e) {
			result = GET_INFO_RESULT_ERROR;
			sendActionEvent(MsgUtil.getMessage(MsgUtil.ERROR_SERVER, null));
		} catch (TimeOutException e) {
		}
		
		result = getInfosFromServerForInfoPanel();
		
		if (result == GET_INFO_RESULT_NORMAL) {
			sendActionEvent(MsgUtil.getMessage(MsgUtil.MSG_IDLE, null));
		}
		
		return result;
	}
	
	private int getInfosFromServerForInfoPanel() {
		int		result = GET_INFO_RESULT_NORMAL;
		
		try {
			getPrinterGroupInfo();
		} catch (NetworkException e) {
			sendActionEvent(MsgUtil.getMessage(MsgUtil.ERROR_NETWORK, null));
			return GET_INFO_RESULT_ERROR;
		} catch (ServerException e) {
			sendActionEvent(MsgUtil.getMessage(MsgUtil.ERROR_SERVER, null));
			return GET_INFO_RESULT_ERROR;
		} catch (TimeOutException e) {
		}
		
		try {
			getGroupInfo();
		} catch (NetworkException e) {
			sendActionEvent(MsgUtil.getMessage(MsgUtil.ERROR_NETWORK, null));
			return GET_INFO_RESULT_ERROR;
		} catch (ServerException e) {
			sendActionEvent(MsgUtil.getMessage(MsgUtil.ERROR_SERVER, null));
			return GET_INFO_RESULT_ERROR;
		} catch (TimeOutException e) {
		}
		
		try {
			getPlaceInfo();
		} catch (NetworkException e) {
			sendActionEvent(MsgUtil.getMessage(MsgUtil.ERROR_NETWORK, null));
			return GET_INFO_RESULT_ERROR;
		} catch (ServerException e) {
			sendActionEvent(MsgUtil.getMessage(MsgUtil.ERROR_SERVER, null));
			return GET_INFO_RESULT_ERROR;
		} catch (TimeOutException e) {
		}
		
		try {
			getPrinterList();
		} catch (NetworkException e) {
			result = GET_INFO_RESULT_ERROR;
			sendActionEvent(MsgUtil.getMessage(MsgUtil.ERROR_NETWORK, null));
		} catch (ServerException e) {
			result = GET_INFO_RESULT_ERROR;
			sendActionEvent(MsgUtil.getMessage(MsgUtil.ERROR_SERVER, null));
		} catch (TimeOutException e) {
		}
		
		if (result == GET_INFO_RESULT_NORMAL) {
			sendActionEvent(MsgUtil.getMessage(MsgUtil.MSG_IDLE, null));
		}
		
		return result;
	}
	
	public void actionPerformed(ActionEvent ae) {
		Object	obj = ae.getSource();
		
		try {
			if (obj == btnRegister) {
				sendActionEvent(ActionEventConstants.ACTCMD_RESET_SESSION_TIMER);
				doRegister();
			} else if (obj == btnModify || obj == menuModify) {
				sendActionEvent(ActionEventConstants.ACTCMD_RESET_SESSION_TIMER);
				doModify();
			} else if (obj == btnDelete || obj == menuDelete) {
				sendActionEvent(ActionEventConstants.ACTCMD_RESET_SESSION_TIMER);
				doDelete();
/*			} else if (obj == btnMultiRegister) {
				doMultiRegister();*/
			} else if (obj == btnRefresh) {
				sendActionEvent(ActionEventConstants.ACTCMD_RESET_SESSION_TIMER);
				doRefresh();
			}
		} catch (NetworkException e) {
			DialogManager.showMessage(this, DialogManager.ERROR_NETWORK);
		} catch (ServerException e) {
			DialogManager.showMessage(this, DialogManager.ERROR_SERVER);
		} catch (TimeOutException e) {
		}
	}
	
	public void valueChanged(ListSelectionEvent e) {
		Object	obj = e.getSource();
		if (obj == mdlPrinterSelect) {
			sendActionEvent(ActionEventConstants.ACTCMD_RESET_SESSION_TIMER);
			int[]	rows = tblPrinter.getSelectedRows();
			if (rows == null || rows.length == 0) {
				setButtonState(false, 0);
			} else {
				setButtonState(true, rows.length);
			}
		}
	}
}

⌨️ 快捷键说明

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