package jp.co.ntl.swing.ext.printer.place;
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.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JFrame;
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.Net;
import jp.co.ntl.NetworkException;
import jp.co.ntl.Page;
import jp.co.ntl.ServerException;
import jp.co.ntl.TimeOutException;
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.place.PlaceInfo;
public class PlacePanel extends BasePanel implements ActionListener, ListSelectionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTable tblPlace;
private PlaceTableModel mdlPlace;
private ListSelectionModel mdlPlaceSelect;
private JScrollPane scrPlace;
private TableSorter sorter;
private JButton btnRegister;
private JButton btnModify;
private JButton btnDelete;
/// private JButton btnMultiRegister;
private JButton btnRefresh;
private JMenuItem menuModify;
private JMenuItem menuDelete;
private Vector vcPlaceInfo;
private PlaceInfoInvoker invoker;
public PlacePanel(JFrame parent, Net net) {
super(parent, net);
}
public PlacePanel(JApplet app, Net net) {
super(app, net);
}
protected void buildComponents() {
Resource.load(loc);
Page.initialize(net);
invoker = PlaceInfoInvoker.getInstance(net);
createPopupMenu();
vcPlaceInfo = new Vector();
mdlPlace = new PlaceTableModel(vcPlaceInfo);
sorter = new TableSorter(mdlPlace);
tblPlace = new JTable(sorter);
sorter.setTableHeader(tblPlace.getTableHeader());
scrPlace = new JScrollPane(tblPlace);
add(scrPlace, BorderLayout.CENTER);
tblPlace.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.MULTI_REGISTER),
Resource.getString(Resource.TIP_REFRESH)}
);
btnRegister.addActionListener(this);
btnModify.addActionListener(this);
btnDelete.addActionListener(this);
/// btnMultiRegister.addActionListener(this);
btnRefresh.addActionListener(this);
mdlPlaceSelect = tblPlace.getSelectionModel();
mdlPlaceSelect.addListSelectionListener(this);
mdlPlaceSelect.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
setButtonState(false, 0);
tblPlace.setColumnSelectionAllowed(false);
tblPlace.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 (tblPlace.getSelectedRowCount() > 1) {
menuModify.setEnabled(false);
} else {
menuModify.setEnabled(true);
}
}
private boolean getPlaceInfo() throws NetworkException, ServerException, TimeOutException {
Vector vcTempPlaceInfo = invoker.getPlaceInfo();
mdlPlace.addInfos(vcTempPlaceInfo);
return true;
}
protected boolean doRegister() throws NetworkException, ServerException, TimeOutException {
PlaceInfo placeInfo = new PlaceInfo();
PlaceInfoPanel p = new PlaceInfoPanel(false, placeInfo, vcPlaceInfo);
if (DialogManager.showCustomDialog(
this,
Resource.getString(Resource.PLACEINFO_PANEL_TITLE),
p) == PlaceInfoPanel.OK_BUTTON) {
int id = invoker.addPlace(placeInfo);
placeInfo.setPlaceID(id);
mdlPlace.addInfo(placeInfo);
sendActionEvent(ActionEventConstants.ACTCMD_REFRESH_PRINTER);
}
return true;
}
protected boolean doModify() throws NetworkException, ServerException, TimeOutException {
int selRow = sorter.modelIndex(tblPlace.getSelectedRow());
if (selRow >= 0) {
PlaceInfo placeInfo = (PlaceInfo)mdlPlace.getSelectedInfo(selRow);
PlaceInfoPanel p = new PlaceInfoPanel(true, placeInfo, vcPlaceInfo);
if (DialogManager.showCustomDialog(
this,
Resource.getString(Resource.PLACEINFO_PANEL_TITLE),
p) == PlaceInfoPanel.OK_BUTTON) {
invoker.setPlaceInfo(placeInfo);
mdlPlace.setInfo(selRow, placeInfo);
sendActionEvent(ActionEventConstants.ACTCMD_REFRESH_PRINTER);
}
}
return true;
}
protected boolean doDelete() throws NetworkException, ServerException, TimeOutException {
int[] selViewRows = tblPlace.getSelectedRows();
Vector vcPlaceInfo = new Vector();
for (int i = 0; i < selViewRows.length; i++) {
int selRow = sorter.modelIndex(selViewRows[i]);
if (selRow >= 0) {
vcPlaceInfo.addElement(mdlPlace.getSelectedInfo(selRow));
}
}
if (invoker.isRelatedPlace(vcPlaceInfo)) {
DialogManager.showMessage(this, DialogManager.ERROR_DELETE_PLACE_RELATED_PRINTER);
return false;
}
if (DialogManager.showConfirmMessage(
this,
DialogManager.CONFIRM_DELETE,
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
invoker.deletePlace(vcPlaceInfo);
for (int i = selViewRows.length - 1; i >= 0; i--) {
int selRow = sorter.modelIndex(selViewRows[i]);
if (selRow >= 0) {
mdlPlace.deleteInfo(selRow);
}
}
mdlPlaceSelect.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;
mdlPlace.deleteAllInfos();
try {
getPlaceInfo();
} 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 == mdlPlaceSelect) {
sendActionEvent(ActionEventConstants.ACTCMD_RESET_SESSION_TIMER);
int[] rows = tblPlace.getSelectedRows();
if (rows == null || rows.length == 0) {
setButtonState(false, 0);
} else {
setButtonState(true, rows.length);
}
}
}
}