All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scudata.ide.spl.dialog.DialogUnitConfig Maven / Gradle / Ivy

Go to download

SPL(Structured Process Language) A programming language specially for structured data computing.

There is a newer version: 20241126
Show newest version
package com.scudata.ide.spl.dialog;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.SpinnerNumberModel;

import com.scudata.app.common.AppUtil;
import com.scudata.common.MessageManager;
import com.scudata.common.RQException;
import com.scudata.common.StringUtils;
import com.scudata.ide.common.ConfigUtilIde;
import com.scudata.ide.common.GC;
import com.scudata.ide.common.GM;
import com.scudata.ide.common.resources.IdeCommonMessage;
import com.scudata.ide.common.swing.JTableEx;
import com.scudata.ide.common.swing.JTextAreaEditor;
import com.scudata.ide.common.swing.VFlowLayout;
import com.scudata.parallel.UnitConfig;
import com.scudata.parallel.UnitContext;
import com.scudata.parallel.UnitConfig.Host;

/**
 * ?ֻ???????Ϣ????
 * 
 * @author Joancy
 *
 */
public class DialogUnitConfig extends JDialog {
	private static final long serialVersionUID = 1L;
	private MessageManager mm = IdeCommonMessage.get();
	String YES = mm.getMessage("dialogunitconfig.yes");
	String NO = mm.getMessage("dialogunitconfig.no");

	private final byte TAB_UNIT = 0;
	private final byte TAB_CLIENT = 1;

	private JTabbedPane tabMain = new JTabbedPane();
	private JPanel panelUnit = new JPanel();
	private JPanel panelClient = new JPanel();

	private JPanel jPanelButton = new JPanel();
	private JButton jBOK = new JButton();
	private JButton jBCancel = new JButton();

	private JLabel labelTempTimeOut = new JLabel(
			mm.getMessage("dialogunitconfig.temptimeout")); // ??ʱ?ļ?????ʱ??(??)
	private JSpinner jSTempTimeOut = new JSpinner(new SpinnerNumberModel(12, 0,
			Integer.MAX_VALUE, 1));
	private JLabel labelProxyTimeOut = new JLabel(
			mm.getMessage("dialogunitconfig.proxytimeout")); // ????????ʱ??(??)
	private JSpinner jSProxyTimeOut = new JSpinner(new SpinnerNumberModel(12,
			0, Integer.MAX_VALUE, 1));
	private JLabel labelInterval = new JLabel(
			mm.getMessage("dialogunitconfig.checkinterval")); // ?????ڼ??(??)
	private JSpinner jSInterval = new JSpinner(new SpinnerNumberModel(0, 0,
			Integer.MAX_VALUE, 1));
	private JLabel labelBacklog = new JLabel(
			mm.getMessage("dialogunitconfig.backlog")); // ??????????
	private JSpinner jSBacklog = new JSpinner(new SpinnerNumberModel(10, 1,	50, 1));//????ϵͳ??ȱʡ?????????ĿΪ50????????û??????

	private JLabel labelHost = new JLabel(
			mm.getMessage("dialogunitconfig.hostlist"));
	private JButton bAddHost = new JButton();
	private JButton bDeleteHost = new JButton();
	JCheckBox cbAutoStart = new JCheckBox(
			mm.getMessage("dialogodbcconfig.autostart"));

	private final int COL_INDEX = 0;
	private final int COL_HOST = 1;
	private final int COL_PORT = 2;
	private final int COL_MAXTASKNUM = 3;
	private final int COL_ISLOCAL = 4;

	private final String TITLE_INDEX = mm.getMessage("dialogunitconfig.index"); // ???
	private final String TITLE_HOST = mm.getMessage("dialogunitconfig.tabunit"); // ?ֻ?
	private final String TITLE_PORT = mm.getMessage("dialogunitconfig.port"); // ?˿?
	private final String TITLE_MAXTASKNUM = mm
			.getMessage("dialogunitconfig.maxtasknum"); // ?????ҵ??
//	private final String TITLE_PREFERREDTASKNUM = mm
//			.getMessage("dialogunitconfig.preferredtasknum"); // ?ʺ???ҵ??
	private final String TITLE_ISLOCAL = mm
			.getMessage("dialogunitconfig.islocal"); // ?Ƿ񱾻?

	protected JTableEx tableHosts = new JTableEx(new String[] { TITLE_INDEX,
			TITLE_HOST, TITLE_PORT, TITLE_MAXTASKNUM,TITLE_ISLOCAL});

	private final int COL_START = 1;
	private final int COL_END = 2;
	private final String TITLE_START = mm.getMessage("dialogunitconfig.start"); // ??ʼIP
	private final String TITLE_END = mm.getMessage("dialogunitconfig.end"); // ????IP
	private JTableEx tableClients = new JTableEx(new String[] { TITLE_INDEX,
			TITLE_START, TITLE_END });
	JCheckBox cbCheck = new JCheckBox(mm.getMessage("dialogunitconfig.check"));
	JLabel labelClientList = new JLabel(
			mm.getMessage("dialogunitconfig.clientlist"));
	private JButton bAddClient = new JButton();
	private JButton bDeleteClient = new JButton();

	JFrame parent;
	int option = JOptionPane.CANCEL_OPTION;
	protected String fixedIP;
	protected int fixedPort;
	protected boolean isClusterEditing = false;
	protected UnitConfig unitConfig = null;

	/**
	 * ???캯??
	 * @param parent ??????
	 * @param title ????
	 */
	public DialogUnitConfig(JFrame parent, String title) {
		super(parent, title, true);
		constructor(parent);
	}

	void constructor(JFrame parent) {
		this.parent = parent;
		try {
			rqInit();
			init();
			setSize(700, 600);
			GM.setDialogDefaultButton(this, jBOK, jBCancel);
			resetLangText();
		} catch (Exception ex) {
		}
	}

	/**
	 * ?򿪶Ի???ѡ??һ??Ŀ¼
	 * @param oldPath ???е?Ŀ¼
	 * @return ѡ???Ŀ¼
	 */
	public static String dialogSelectDirectory(String oldPath) {
		String path = GM.dialogSelectDirectory(oldPath);
		return path;
	}

	/**
	 * ?򿪶Ի???ѡ??һ???ļ?
	 * @param exts ?ļ?????չ??
	 * @return ?ļ?????
	 */
	public static File dialogSelectFile(String exts) {
		return GM.dialogSelectFile(exts);
	}

	/**
	 * ??ȡ?ֻ?????
	 * @return ?ֻ?????
	 */
	public UnitConfig getUnitConfig() {
		return unitConfig;
	}

	/**
	 * ??ȡ???ڵĶ???ѡ??
	 * @return ѡ??
	 */
	public int getOption() {
		return option;
	}


	private void init() {
		try {
			UnitConfig uc = loadUnit();
			// Unit
			jSTempTimeOut.setValue(new Integer(uc.getTempTimeOutHour()));
			jSProxyTimeOut.setValue(new Integer(uc.getProxyTimeOutHour()));
			jSInterval.setValue(new Integer(uc.getInterval()));
			jSBacklog.setValue(new Integer(uc.getBacklog()));
			cbAutoStart.setSelected(uc.isAutoStart());

			List hosts = uc.getHosts();
			if (hosts != null) {
				for (int i = 0, size = hosts.size(); i < size; i++) {
					Host host = hosts.get(i);
					int row = tableHosts.addRow();
					tableHosts.data.setValueAt(host.getIp(), row, COL_HOST);
					tableHosts.data.setValueAt(host.getPort(), row, COL_PORT);
					tableHosts.data.setValueAt(host.getMaxTaskNum(), row,
							COL_MAXTASKNUM);
					boolean isLocal = AppUtil.isLocalIP(host.getIp());
					tableHosts.data.setValueAt(isLocal ? YES : NO, row,
							COL_ISLOCAL);

				}
			}
			int rc = tableHosts.getRowCount();
			if (rc > 0) {
				rc--;
				tableHosts.rowfocusChanged(rc, rc);
			}

			// Client
			cbCheck.setSelected(uc.isCheckClients());
			List starts = uc.getEnabledClientsStart();
			List ends = uc.getEnabledClientsEnd();
			if (starts != null) {
				for (int i = 0, size = starts.size(); i < size; i++) {
					String start = starts.get(i);
					String end = ends.get(i);
					int row = tableClients.addRow();
					tableClients.data.setValueAt(start, row, COL_START);
					tableClients.data.setValueAt(end, row, COL_END);
				}
			}

		} catch (Exception e) {
		}
	}

	private UnitConfig loadUnit() throws Exception {
		if (isClusterEditing) {
			return unitConfig;
		}
		UnitConfig uc = new UnitConfig();
		InputStream is = null;
		try {
			is = getUnitInputStream(UnitContext.UNIT_XML);
			if (is == null) {
				return uc;
			}
			uc.load(is, false);
			is.close();
			return uc;
		} finally {
			if (is != null)
				is.close();
		}
	}

	/**
	 * ?ֻ??ļ?Ĭ??ΪconfigĿ¼?£???????·????Ȼ????start.home?µľ???·??
	 * 
	 * @param relativePath
	 *            String ???·??
	 * @throws Exception
	 * @return InputStream ??????
	 */
	private InputStream getUnitInputStream(String relativePath)
			throws Exception {
		return UnitContext.getUnitInputStream(relativePath);
	}

	private boolean isUnitValid() {
		if (!tableHosts.verifyColumnData(COL_HOST, TITLE_HOST, false))
			return false;
		return true;
	}

	private boolean save() throws Throwable {
		// Unit
		UnitConfig uc = new UnitConfig();
		uc.setTempTimeOutHour(((Integer) jSTempTimeOut.getValue()).intValue());
		uc.setProxyTimeOutHour(((Integer) jSProxyTimeOut.getValue()).intValue());
		uc.setInterval(((Integer) jSInterval.getValue()).intValue());
		uc.setBacklog(((Integer) jSBacklog.getValue()).intValue());
		uc.setAutoStart(cbAutoStart.isSelected());

		tableHosts.acceptText();
		List hosts = new ArrayList();
		for (int i = 0, count = tableHosts.getRowCount(); i < count; i++) {
			String ip = (String) tableHosts.data.getValueAt(i, COL_HOST);
			int port = ((Number) tableHosts.data.getValueAt(i, COL_PORT)).intValue();
			Host h = new Host(ip,port);
			int max = ((Number) tableHosts.data.getValueAt(i, COL_MAXTASKNUM))
					.intValue();
			h.setMaxTaskNum(max);

			hosts.add(h);
		}
		uc.setHosts(hosts);

		// clients
		uc.setCheckClients(cbCheck.isSelected());
		List starts = new ArrayList();
		List ends = new ArrayList();
		tableClients.acceptText();
		for (int i = 0, count = tableClients.getRowCount(); i < count; i++) {
			String start = (String) tableClients.data.getValueAt(i, COL_START);
			starts.add(start);
			String end = (String) tableClients.data.getValueAt(i, COL_END);
			ends.add(end);
		}
		uc.setEnabledClientsStart(starts);
		uc.setEnabledClientsEnd(ends);

		if (isClusterEditing) {
			unitConfig = uc;
			return true;
		}

		try {
			String filePath = GM
					.getAbsolutePath(ConfigUtilIde.UNIT_CONFIG_FILE);
			File f = new File(filePath);
			if (f.exists() && !f.canWrite()) {
				String msg = IdeCommonMessage.get().getMessage(
						"public.readonly", f.getName());
				throw new RQException(msg);
			}
			FileOutputStream fos = new FileOutputStream(f);
			uc.save(fos);
			fos.close();
		} catch (Exception ex) {
			GM.showException(ex);
		}
		return true;
	}

	void resetLangText() {
		jBOK.setText(mm.getMessage("button.ok"));
		jBCancel.setText(mm.getMessage("button.cancel"));

		tabMain.setTitleAt(TAB_UNIT, mm.getMessage("dialogunitconfig.tabunit")); // ?ֻ?
		tabMain.setTitleAt(TAB_CLIENT,
				mm.getMessage("dialogunitconfig.tabclient")); // ??????
	}

	private void rqInit() throws Exception {
		jPanelButton.setLayout(new VFlowLayout());
		jBOK.setActionCommand("");
		jBOK.setText("ȷ??(O)");
		jBOK.addActionListener(new DialogUnitConfig_jBOK_actionAdapter(this));
		jBOK.setMnemonic('O');
		jBCancel.setActionCommand("");
		jBCancel.setText("ȡ??(C)");
		jBCancel.addActionListener(new DialogUnitConfig_jBCancel_actionAdapter(
				this));
		jBCancel.setMnemonic('C');
		jPanelButton.add(jBOK, null);
		jPanelButton.add(jBCancel, null);

		ImageIcon iiAdd = GM.getImageIcon(GC.IMAGES_PATH + "b_add.gif");
		ImageIcon iiDel = GM.getImageIcon(GC.IMAGES_PATH + "b_delete.gif");

		bAddHost.setIcon(iiAdd);
		bDeleteHost.setIcon(iiDel);

		panelUnit.setLayout(new GridBagLayout());
		panelUnit.add(labelTempTimeOut, GM.getGBC(1, 1));
		panelUnit.add(jSTempTimeOut, GM.getGBC(1, 2, true));
		panelUnit.add(labelProxyTimeOut, GM.getGBC(1, 3));
		panelUnit.add(jSProxyTimeOut, GM.getGBC(1, 4, true));
		panelUnit.add(labelInterval, GM.getGBC(2, 1));
		panelUnit.add(jSInterval, GM.getGBC(2, 2, true));
		panelUnit.add(labelBacklog, GM.getGBC(2, 3));
		panelUnit.add(jSBacklog, GM.getGBC(2, 4, true));
		GridBagConstraints gbc = GM.getGBC(3, 1, true);
		gbc.gridwidth = 4;
		panelUnit.add(cbAutoStart, gbc);

		JPanel panelHostList = new JPanel(new GridBagLayout());
		panelHostList.add(labelHost, GM.getGBC(1, 1, true));
		panelHostList.add(bAddHost, GM.getGBC(1, 2));
		panelHostList.add(bDeleteHost, GM.getGBC(1, 3));
		gbc = GM.getGBC(4, 1, true);
		gbc.gridwidth = 4;
		panelUnit.add(panelHostList, gbc);
		gbc = GM.getGBC(5, 1, true, true);
		gbc.gridwidth = 4;
		panelUnit.add(new JScrollPane(tableHosts), gbc);

		tabMain.add(panelUnit, "Unit");
		tabMain.add(panelClient, "Client");

		this.addWindowListener(new DialogUnitConfig_this_windowAdapter(this));
		this.getContentPane().add(tabMain, BorderLayout.CENTER);
		this.getContentPane().add(jPanelButton, BorderLayout.EAST);
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
		this.setModal(true);

		tableHosts.setIndexCol(COL_INDEX);
		tableHosts.setRowHeight(20);
		tableHosts.setColumnWidth(COL_HOST, 230);
		tableHosts.setColumnSpinner(COL_PORT);
		JTextAreaEditor editor2 = (JTextAreaEditor) tableHosts
				.getColumnEditor(COL_PORT);
		editor2.setArrange(1, 65535, 1);
		tableHosts.setColumnSpinner(COL_MAXTASKNUM);
		JTextAreaEditor editor = (JTextAreaEditor) tableHosts
				.getColumnEditor(COL_MAXTASKNUM);
		editor.setArrange(1, 2048, 1);
		tableHosts.setColumnEditable(COL_ISLOCAL, false);

		bAddHost.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				tableHosts.acceptText();
				int r = tableHosts.getRowCount();
				String defaultIP = UnitContext.getDefaultHost();
				String ip = defaultIP;
				int port = 8281;
				if (isClusterEditing) {
					ip = fixedIP;
					port = increasePort(port);
				} else {
					if (r > 0) {
						ip = (String) tableHosts.data.getValueAt(r - 1,
								COL_HOST);
						ip = increaseIP(ip);
					}
				}
				Host h = new Host(ip,port);
				int row = tableHosts.addRow();
				tableHosts.setValueAt(ip, row, COL_HOST);
				tableHosts.setValueAt(port, row, COL_PORT);
				tableHosts.setValueAt(h.getMaxTaskNum(), row, COL_MAXTASKNUM);
				boolean isLocal = AppUtil.isLocalIP(ip);
				tableHosts.setValueAt(isLocal ? YES : NO, row, COL_ISLOCAL);
			}
		});

		bDeleteHost.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				tableHosts.acceptText();
				tableHosts.deleteSelectedRows();
			}
		});

		// clients
		tableClients.setIndexCol(COL_INDEX);
		tableClients.setRowHeight(20);
		bAddClient.setIcon(GM.getImageIcon(GC.IMAGES_PATH + "b_add.gif"));
		bDeleteClient.setIcon(GM.getImageIcon(GC.IMAGES_PATH + "b_delete.gif"));

		panelClient.setLayout(new GridBagLayout());
		panelClient.add(cbCheck, GM.getGBC(1, 1, true));

		JPanel tmp = new JPanel(new GridBagLayout());
		tmp.add(labelClientList, GM.getGBC(1, 1, true));
		tmp.add(bAddClient, GM.getGBC(1, 2));
		tmp.add(bDeleteClient, GM.getGBC(1, 3));

		panelClient.add(tmp, GM.getGBC(2, 1, true));
		panelClient.add(new JScrollPane(tableClients),
				GM.getGBC(3, 1, true, true));

		bAddClient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				tableClients.acceptText();
				int r = tableClients.getRowCount();
				String tmp = "192.168.0.100";
				if (r > 0) {
					tmp = (String) tableClients.data
							.getValueAt(r - 1, COL_HOST);
					if (StringUtils.isValidString(tmp)) {
						tmp = increaseIP(tmp);
					}
				}

				tableClients.addRow(new Object[] { 0, tmp });
			}
		});
		bDeleteClient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				tableClients.acceptText();
				tableClients.deleteSelectedRows();
			}
		});

		Dimension d = new Dimension(22, 22);
		initButton(bAddHost, d);
		initButton(bDeleteHost, d);
		initButton(bAddClient, d);
		initButton(bDeleteClient, d);
	}

	private int increasePort(int port) {
		int rows = tableHosts.getRowCount();
		int p = port;
		boolean find = false;
		while(true) {
			for(int r=0; r




© 2015 - 2025 Weber Informatics LLC | Privacy Policy