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

📄 prefscreen.java

📁 We intend to develop a wifi enabled p2p file sharing system on a linux platform using jxta and java.
💻 JAVA
字号:
package edu.uiuc.cs.cs327.linuxwifi.gui;

import edu.uiuc.cs.cs327.linuxwifi.services.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.Vector;
import java.util.*;

    /**
     * Description: 	Class that allows users to change preferences
     *
     * Author: 			Sal Razzaq
     */


class PrefScreen extends JPanel implements ActionListener{

	private static final boolean DEBUG = false;
	private MainGUI mainGUI;
	private static Vector availableNodesVector;

	private static JTextField 	txtUploadDir;
	private static JTextField 	txtDownloadDir;
	private static JList		lstMP;
	private static JCheckBox	ckAutoDN;
	private static JCheckBox	ckKnownNodesOnly;
	private static JTextField 	txtMaxUploadsPerHour;
	MPDialog customDialog;

	private Vector musicProfileVectorTemp=null;		// Vector of MusicProfiles

	// class constructor
    PrefScreen(MainGUI mainGUI ) {

		this.mainGUI = mainGUI;
		createLabelsControls();
		show();
    }

	// main routine to create labels and controls
    private void createLabelsControls() {
		/* 		PREFRENCE ITEMS
				- upload directories (semi-colon delimited list)

				- list of music profiles

				- Check box to set auto download mode

				- Check box to allow know nodes only
				- XXXX list of known nodes XXXX

				- Num Maximum Uploads Per Hour
		*/

			JPanel pnl = this;
			pnl.setLayout(null);
			//pnl.setLayout(new GridLayout(1,1));
			pnl.setSize(new Dimension(800,600));

			createLabels(pnl);
			createControls(pnl);
			SetControlValues();
	}

	// creates control labels for this window
	private void createLabels(JPanel pnl) {

		int nX = 40; int nY = 25; int nFontSize = 20; int nYSpace = 40;
		int nYLBSpace = 100;
		createLabel(pnl, "PREFERENCES SCREEN", nFontSize, nX, nY, true);

		nFontSize = -1;		// use default fonts for the rest of the labels

		nY = nY + nYSpace;
		createLabel(pnl, "Upload Directories: ", nFontSize, nX, nY, false);
		nY = nY + nYSpace;
		createLabel(pnl, "Download Directories: ", nFontSize, nX, nY, false);
		nY = nY + 20;
		createLabel(pnl, "Enter list of directories separated by semi-colons (;)", nFontSize, 225, nY, false);


		nY = nY + 20;
		createLabel(pnl, "Music Profiles: ", nFontSize, nX, nY, false);

		nY = nY + nYLBSpace;
		createLabel(pnl, "Auto Download Mode: ", nFontSize, nX, nY, false);

		nY = nY + nYSpace;
		createLabel(pnl, "Allow Known Nodes Only: ", nFontSize, nX, nY, false);

		nY = nY + nYSpace;
		createLabel(pnl, "Max Uploads per Hour: ", nFontSize, nX, nY, false);

	}


	// creates a label (helper)
	private void createLabel(JPanel pnl, String sDisp, int nFontSize, int nX, int nY, boolean bCenterHorz) {
			JLabel lbl = new JLabel(sDisp);
			if (nFontSize > 0)
				lbl.setFont(new Font(lbl.getFont().getFontName(), lbl.getFont().getStyle(), nFontSize));
			lbl.setSize(lbl.getPreferredSize());
			pnl.add(lbl);
			if (bCenterHorz)
				nX = (pnl.getWidth()-lbl.getWidth())/2;
			lbl.setLocation(nX, nY);
	}

	// creates controls for this window
	private void createControls(JPanel pnl) {
		int nX = 225;

		txtUploadDir = createEditBox(pnl, nX, 60 ,40);

		txtDownloadDir = createEditBox(pnl, nX, 100 ,40);

		lstMP = createListBox(pnl, nX, 150, 350, 80);
		createButton(pnl, "Add", "addmp", 585, 150, false);
		createButton(pnl, "Delete", "delmp", 585, 185, false);

		ckAutoDN = createCheckBox(pnl, nX, 246);

		ckKnownNodesOnly = createCheckBox(pnl, nX, 286);

		txtMaxUploadsPerHour = createEditBox(pnl, nX, 326 ,5);

		// submit btn
		createButton(pnl, "Save", "save", -1, 400, true);
	}

	// creates a checkbox
	private JCheckBox createCheckBox(JPanel pnl, int nX, int nY) {
		JCheckBox box = new JCheckBox();
		pnl.add(box);
		box.setSize(box.getPreferredSize());
		box.setLocation(nX, nY);
		return box;
	}

	// creates a listbox
	private JList createListBox(JPanel pnl, int nX, int nY, int nWidth, int nHeight) {

		JList list = new JList();
		JScrollPane scrollPane = new JScrollPane(list);
		pnl.add(scrollPane);
		scrollPane.setBounds(nX, nY, nWidth, nHeight);

		return list;

	}

	// creates a button
	private void createButton(JPanel pnl, String sDisp, String sActionCmd, int nX, int nY, boolean bCenterHorz) {
		JButton btn = new JButton(sDisp);
		btn.addActionListener(this);
		btn.setActionCommand(sActionCmd);

		pnl.add(btn);
		btn.setSize(btn.getPreferredSize());
		if (bCenterHorz)
			nX = (pnl.getWidth()-btn.getWidth())/2;
		btn.setLocation(nX, nY);
	}

	// creates text box
	private JTextField createEditBox(JPanel pnl, int nX, int nY, int nSize) {
		JTextField txt= new JTextField(nSize);
		txt.setSize(txt.getPreferredSize());
		pnl.add(txt);
		txt.setLocation(nX, nY);
		return txt;
	}


	// Action handler
	public void actionPerformed(ActionEvent e)
	{

		if (e.getActionCommand().equals("save")) {
			SavePreferences();
			JOptionPane.showMessageDialog(this, "Preferences have been saved. Press OK.");
		}

		if (e.getActionCommand().equals("delmp") ) {
			JOptionPane.showMessageDialog(this, "Under Development. Press OK.");
		}

		if (e.getActionCommand().equals("addmp") ) {
			addMPDlg();

		}

	}

	// Show Music File Add dialog box
	public void addMPDlg() {
		//JOptionPane.showMessageDialog(this, "Show Add MP Dialog Box");
		MPDialog dlg = new MPDialog(mainGUI, "TEST", this);
		dlg.setVisible(true);
		if (dlg.DoSave) {
			MusicProfile mp = dlg.getMusicProfile();
			musicProfileVectorTemp.add(mp);
			Vector DisplayList = getMPDisplayList(musicProfileVectorTemp);
			lstMP.setListData(DisplayList);
		}
	}


	// Load previous preferences from disk
	private void SetControlValues() {
		ConfigurationInfo configInfo = new ConfigurationInfo();

		txtUploadDir.setText(configInfo.getUploadDirectoriesDelimited());
		txtDownloadDir.setText(configInfo.getDownloadDirectoriesDelimited());

		txtMaxUploadsPerHour.setText(Integer.toString(configInfo.getMaxUploadsPerHour()));

		ckAutoDN.setSelected(configInfo.isModeAutoDrive());

		ckKnownNodesOnly.setSelected(configInfo.getKnownNodesOnly());

		musicProfileVectorTemp = configInfo.getMusicProfileVector();
		lstMP.setListData(getMPDisplayList(musicProfileVectorTemp));
	}


	// Save preferences to disk
	private boolean SavePreferences() {
		boolean bRet = false;
		ConfigurationInfo configInfo = new ConfigurationInfo();
		int nMaxUploads = 0;
		try {
			nMaxUploads = Integer.parseInt(txtMaxUploadsPerHour.getText());
		}
		catch (NullPointerException  exc1)
		{

		}
		configInfo.setMaxUploadsPerHour(nMaxUploads);
		configInfo.setUploadDirectoriesDelimited(txtUploadDir.getText());
		configInfo.setDownloadDirectoriesDelimited(txtDownloadDir.getText());
		configInfo.setModeToAutoDrive(ckAutoDN.isSelected());
		configInfo.setKnownNodesOnly(ckKnownNodesOnly.isSelected());
		configInfo.setMusicProfileVector(musicProfileVectorTemp);

		configInfo.save();
		return bRet;
	}


	public Vector getMPDisplayList(Vector musicProfileVector) {
			Vector mp = new Vector();
			if (musicProfileVector != null) {
				Iterator mfiter = musicProfileVector.iterator();
				MusicProfile mfcurr = null;
				while(mfiter.hasNext()) {
					mfcurr = (MusicProfile)mfiter.next();
					mp.add(mfcurr.Display());
				}
			}
			return mp;
	}
}

⌨️ 快捷键说明

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