mainframe.java

来自「java语言开发的P2P流媒体系统」· Java 代码 · 共 1,252 行 · 第 1/3 页

JAVA
1,252 行
字号
			tabbedPane.setSelectedComponent(stationPanel);
		
		pack();
		
		center();
	}

	protected void processWindowEvent(WindowEvent e)
	{
		super.processWindowEvent(e);
		if (e.getID() == WindowEvent.WINDOW_CLOSING)
		{
			exit();
		}
	}
	
	protected void exit()
	{
		if (networkComponents != null)
		{
			networkComponents.shutdown();
		}
		//Save the volume
		double gain = (double) volumeSlider.getValue() / (double) VOLUME_SLIDER_MAX;
		String[] xvolume = {"last", "volume", "" + gain};
		xml.overwrite(xvolume);
		if (stationPanel != null)
			xml.setStationPanelChoice(stationPanel.getStationPanelChoice());
		xml.flush();
		System.exit(0);
		
	}
	
	public void Reenable_YP_Button()
	{
		button1.setEnabled(true);
	}
	/**
	 * Sets the peer that is going to be managed by the GUI
	 */
	public void setPeer(Peer peer)
	{
		
		if (isServer)
		{
			button2.setVisible(false);
			button1.setVisible(true);
			if (addYP)
				{
					peer.getBroadcastBuffer().addToYP(this);
					button1.setText(Messages.getString("MainFrame.BUTTON_REMOVE_YP"));
				}
			else
				{
					//peer.getBroadcastBuffer().
					button1.setText(Messages.getString("MainFrame.BUTTON_ADD_YP"));
				}
			metadata = peer.getBroadcastBuffer().getMetadata();
			
			addYP = !addYP;
			
		}
		else
		{
			button1.setText(Messages.getString("MainFrame.BUTTON_LEAVE"));
			button2.setVisible(true);
		}
		if (peer == null || peer.isDisconnected())
			return;
		isConnected = true;
		this.peer = peer;
		startTime = peer.getStartTime();
		Time_Server_Has_Been_Running_At_Startup = peer.Get_Time_Server_Has_Been_Running_At_Startup();
		Server_Running_Minus_Start_Time = Time_Server_Has_Been_Running_At_Startup - startTime;
		
		
		
		MenuInformationenAnzeigen.setEnabled(true);
		MenuBandbreiteSetzen.setEnabled(true);
		
		uiPlayer = new UIPlayer(peer.getListenBuffer(), this);
		if (recordStreamTo != null)
			uiPlayer.recordStream(recordStreamTo);
		uiPlayer.start();
	}
		
	public boolean isLoggable(LogRecord record)
	{
		if (record.getLevel().intValue() < Level.INFO.intValue())
		{
			return Radio.displayDebugMessages;
		}
		else
		{
			return true;
		}
	}
	
	public void updateMetadata(Metadata metadata)
	{
		this.metadata = metadata;
		if (xml.startMediaPlayerAtStartup() && xml.getUseIntegratedPlayer() && networkComponents.getIntegratedPlayer() == null)
		{
			setIntegratedPlaying(true);
			networkComponents.launchIntegratedPlayer();	
		}
		
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				updateDisplay();
			}
		});	
	}
	public void updateTime(long timeStamp)
	{
		lagIsValid = peer.getLagIsValid(); 
		this.timeStamp = timeStamp;
		/*Equation to calculate the expected stream position is:
		 *											  //Time our client has been running										
		 * Time_Server_Has_Been_Running_At_Startup + (System.currentTimeMillis() - startTime)
		 * 
		 * 
		 * The real stream position is timeStamp
		 * 
		 * To find lag we use: actual - estimated
		 * 
		 * 
		 */
		//			 actual   -                              estimated
		long lag;
		if (lagIsValid)
		{
			lag = (timeStamp - (Server_Running_Minus_Start_Time + System.currentTimeMillis()));
			if ((!negativeLag && lag >=0) || negativeLag)
				this.lag = lag;
		}
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				updateDisplay();
			}
		});	
	}
	
	public void updateDisplay()
	{
		String stationName = metadata.getStationName();
		LabelStationContent.setText(stationName);
		if (urlToStationname != null && !(stationName.equals(urlToStationname.get(url))))  //If we have never connected to this station
			{
				System.out.println("here");
				xml.writeLink(stationName, url);
				xml.flush();
				urlToStationname.put(url, stationName);
				urlInput.addItem(makeSelection(url, stationName));
			}
		
		LabelSongContent.setText(metadata.getSongTitle());
		
		String contentTransport = Radio.getContentTransport(metadata, peer);
		Float bandwidth = new Float(metadata.getAverageByterate() / 1024f);
		LabelBandwidthContent.setText(Messages.getString("MainFrame.LABEL_BANDWIDTH_CONTENT", bandwidth));
				
		LabelStreamContent.setText(contentTransport); //$NON-NLS-1$
		
		
		//if (lagIsValid)
			//LabelLagContent.setText("" + lag );
		//Server Uptime code
		long timeSec = timeStamp / 1000L;
		long hrs = timeSec / 3600L;  //3600 seconds in an hour
		timeSec -= hrs * 3600L;
		long min = timeSec / 60L;  //60 seconds in a minute;
		timeSec -= min * 60L;
		DecimalFormat twoDigit = new DecimalFormat("00");
		LabelServerUptimeContent.setText(hrs + ":" + twoDigit.format(min) + ":" + twoDigit.format(timeSec));
		
		
		LabelsPanel.validate();
	}
	
	public void center()
	{
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = getSize();
    
		if (frameSize.height > screenSize.height)
		{
			frameSize.height = screenSize.height;
		}
		
		if (frameSize.width > screenSize.width)
		{
			frameSize.width = screenSize.width;
		}
		
		setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
	}
	
	public void setBandwidth(double maxUpload, double maxDownload, String IP)
	{
		if (isServer && (xml.getMonitorAddress() == null || xml.getMonitorAddress().equals("")))
			xml.setMonitorAddress(IP);
		DecimalFormat sf3 = new DecimalFormat("0.000");
		String upload = new String();
		String download = new String();
		if (maxUpload != -1)
		{
			double maxUploadBandwidth = maxUpload - 1.0;  //Designate 1KB/s for data messages
			upload = sf3.format(maxUploadBandwidth);
		}
		if (maxDownload != -1)
		{
			double maxDownloadBandwidth = maxDownload - 1.0;  //Designate 1KB/s for data messages
			download = sf3.format(maxDownloadBandwidth);
		}
		xml.setInternetMax(upload, download, IP);
		xml.Set_Internet_Download_Limit(download);
		xml.Set_Internet_Upload_Limit(upload);
		
		xml.flush();
		readXML();
	}
	public void closeOptions(){
		if (optionsPanel != null)
			tabbedPane.remove(optionsPanel);
		MenuOptions.setEnabled(true);
		
	}
	public boolean getIsServer()
	{return isServer;}
	public boolean getAddYP()
	{return addYP;}
	public RadioGUI getGui()
	{return gui;}
	public double getLANUpload()
	{return LANUpload;}
	public boolean getLANUploadSet()
	{return LANUploadSet;}
	public double getLANDownload()
	{return LANDownload;}
	public double getInternetDownload()
	{return internetDownload;}
	public double getInternetUpload()
	{return internetUpload;}
	public double getInternetMaxDownload()
	{return internetMaxDownload;}
	public double getInternetMaxUpload()
	{return internetMaxUpload;}
	public String getIP()
	{return IP;}
	public SettingsXML getSettingsXML()
	{return xml;}
	public String getUrl()
	{return url;}
	public int getPort()
	{return port;}
	public boolean getPortSet()
	{return portSet;}
	public boolean startMediaPlayerAtStartup()
	{return startMediaPlayerAtStartup;}
	public boolean getLog()
	{return log;}
	public TreeMap getSelectionToUrl()
	{return selectionToUrl;}
	public TreeMap getUrlToStationname()
	{return urlToStationname;}
	public String getLogFilename()
	{return logFilename;}
	public boolean negativeLagEnabled()
	{return negativeLag;}
	public int getMinimumChildrenClientsMustServe()
	{ return MinimumChildrenClientsMustServe;}
	public boolean getIsConnected()
	{
		return isConnected;
	}
	public void comeOutButton1()
	{
		button1.setVisible(true);
	}
	public JButton getButton1()
	{
		return button1;
	}

	public void setUrl(String url)
	{
		ComboBoxEditor editor = urlInput.getEditor();
		editor.setItem(url);
		urlInput.setSelectedItem(url);
	}
	public void doBandwidthTest()
	{
		if (test)
		{
			Tcpbw100 bTest = new Tcpbw100(this);
			bTest.start();
		}
	}
	public void setMainSelected()
	{
		tabbedPane.setSelectedComponent(MainPanel);
	}
	public void setOptionsSelected()
	{
		tabbedPane.setSelectedComponent(optionsPanel);
	}
	public JScrollPane getMessagesPane()
	{
		return messagesPane;
	}
	public boolean mainSelected()
	{
		return (tabbedPane.getSelectedComponent() == MainPanel);
	}
	public boolean This_Server_Uses_Verified_Bandwidth()
	{
		return serverUsesVerifiedBandwidth;
	}
	public Metadata getMetadata()
	{
		if (metadata == null)
			metadata = new Metadata();
		return metadata;
	}
	public void setIntegratedPlaying(boolean playing)
	{
		if (playing)
			MenuLaunchIntegratedPlayer.setText(Messages.getString("MainFrame.MENU_STOP_PLAYING"));
		else
			MenuLaunchIntegratedPlayer.setText(Messages.getString("MainFrame.MENU_LAUNCH_INTEGRATED_PLAYER"));
	}
	public double getGain()
	{
		return (double) volumeSlider.getValue() / (double) VOLUME_SLIDER_MAX;
	}
	
	
	private class RecordThread extends Thread
	{
		public void run(){
			if (MenuRecordStream.getText().equals(Messages.getString("MainFrame.MENU_STOP_RECORDING")))
			{
				recordStreamTo = null;
				if (uiPlayer != null && uiPlayer.isAlive())
            	{
            		uiPlayer.stopRecording();
            	}
				MenuRecordStream.setText(Messages.getString("MainFrame.MENU_RECORD_STREAM"));
			}
			else
			{
//				Create a file chooser
				final JFileChooser fc = new JFileChooser();
//				 Create Filters
				ExampleFileFilter nsvFilter = new ExampleFileFilter("nsv", "Nullsoft Video");
				ExampleFileFilter mp3Filter = new ExampleFileFilter("mp3", "MPEG-Layer3");
				ExampleFileFilter oggFilter = new ExampleFileFilter("ogg", "Ogg Vorbis");
				fc.addChoosableFileFilter(nsvFilter);
				fc.addChoosableFileFilter(mp3Filter);
				fc.addChoosableFileFilter(oggFilter);
				fc.setFileFilter(fc.getAcceptAllFileFilter());
				if (metadata != null)
				{
					String contentType = metadata.getContentType().toLowerCase();
					if (contentType.equals("audio/mpeg")) //$NON-NLS-1$
					{
						fc.setFileFilter(mp3Filter);
					}
					else if (contentType.equals("video/nsv")) //$NON-NLS-1$
					{
						fc.setFileFilter(nsvFilter);
					}
					else if (contentType.equals("application/ogg")) //$NON-NLS-1$
					{
						fc.setFileFilter(oggFilter);
					}
				}
				
//				In response to a button click:
				int returnVal = fc.showSaveDialog(selfReference);
				
				if (returnVal == JFileChooser.APPROVE_OPTION) 
				{
					File file = fc.getSelectedFile();
					String fileName = file.getName();
					String absoluteFileName = file.getAbsolutePath();
					if (fileName.indexOf(".") == -1)
					{
						FileFilter selected = fc.getFileFilter();
						if (selected == nsvFilter)
							absoluteFileName += ".nsv";
						else if (selected == mp3Filter)
							absoluteFileName += ".mp3";
						else if (selected == oggFilter)
							absoluteFileName += ".ogg";
					}
					if (uiPlayer != null && uiPlayer.isAlive())
	            	{
	            		uiPlayer.recordStream(new File(absoluteFileName));
	            	}
					recordStreamTo = file;
					MenuRecordStream.setText(Messages.getString("MainFrame.MENU_STOP_RECORDING"));

				} else {
					// log.append("Open command cancelled by user." + newline);
				}
			}
		}
	}
}

⌨️ 快捷键说明

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