📄 channelfactory.java
字号:
/* * Copyright (C) 2005 by Enrico Chiaretti - enrico.chiaretti@gmail.com This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* * ChannelFactory.java * * Created on 5 giugno 2005, 13.29 *//** this class creates / reads the channel list * @author enrico chiaretti */public class ChannelFactory{ private java.util.Vector mChannels = null; private static int ST_INIT = -1; private static int ST_READY = 0; private static int ST_EOL = 1; private static int ST_EOF = 2; private int mstatus = -1; /** * Creates a new instance of ChannelFactory. * the default constructor generates my favorites channels and frequencies. * Moreover, it reads the user home directory, needed to save the channel list * text file. */ // creates the default channel list: PAL ITALY, Monte Nerone public ChannelFactory() { mChannels = new java.util.Vector(); defaultChannels(); } public ChannelFactory(java.util.Vector aChanList) { mChannels = aChanList; } /** * Creates a new instance of ChannelFactory. * reads the channels from file. If fails, create the default list * @param TextFile this tells where to load / save the channel list text file, so the end user can * edit at his own will. */ public ChannelFactory(String TextFile) { mChannels = new java.util.Vector(); read(TextFile); } private void defaultChannels() { mChannels.clear(); mChannels.add(new TvChannel(1, "Rai1", "E", "", 183750)); mChannels.add(new TvChannel(2, "Rai2", "33", "", 567250)); mChannels.add(new TvChannel(3, "Rai3", "54", "", 735250)); mChannels.add(new TvChannel(4, "Rete4","34", "", 575250)); mChannels.add(new TvChannel(5, "Canale5", "39", "", 615250)); mChannels.add(new TvChannel(6, "Italia1", "50", "", 703250)); mChannels.add(new TvChannel(7, "7Gold" ,"68", "Italia 7 Gold", 847250)); mChannels.add(new TvChannel(8, "La8", "38", "", 607250)); mChannels.add(new TvChannel(9, "TeleCentro", "60", "", 783250)); mChannels.add(new TvChannel(10, "TeleRomagna", "49", "", 695250)); mChannels.add(new TvChannel(11, "ITV", "57", "", 759250)); mChannels.add(new TvChannel(12, "MTV", "28", "", 527250)); mChannels.add(new TvChannel(13, "TeleSanterno", "63", "", 807250)); mChannels.add(new TvChannel(14, "SanMarino", "H1", "", 217250)); mChannels.add(new TvChannel(15, "SI", "45", "", 663250)); mChannels.add(new TvChannel(16, "Tele2000", "29", "", 535250)); mChannels.add(new TvChannel(17, "VCR", "36", "Video Tape Recorder", 591250)); } /** returns the active channel list to be displaied on the user form * @return returns a Vector of TvChannel */ public java.util.Vector getChannels() { // this is my default channel list, based on the italian frequencies /* * future release: * read channel list from a text file. * eventually, build a list editor form to add / change / remove channels */ return mChannels; } /** * this saves the channel list into a text file. * @param FileName this tells where to save the channel list text file */ public void save(String FileName) throws java.io.IOException { java.io.FileWriter OutFile = null; TvChannel CurrChan = null; String sFreq = null; int i; OutFile = new java.io.FileWriter(FileName); for (i=0;i<mChannels.size();i++) { CurrChan = (TvChannel) mChannels.get(i); if (CurrChan.freq.longValue() == 0) { sFreq = ""; } else { sFreq = CurrChan.freq.toString(); } OutFile.write(CurrChan.name + ", " + CurrChan.number + ", " + CurrChan.description + ", " + sFreq + ";\n"); System.out.println(CurrChan.index + ", " + CurrChan.name + ", " + CurrChan.number + ", " + CurrChan.description + ", " + sFreq); } OutFile.close(); } /** * this reads the channel list from the text file * @param FileName this tells where to read the list from */ public void read(String FileName) { java.io.FileReader InFile = null; String ChanName = null; String ChanDesc = null; String ChanNum = null; long chanFreq = 0; mstatus = ST_INIT; try { InFile = new java.io.FileReader(FileName); while(mstatus != ST_EOF) { ChanName = readWord(InFile); // required ChanNum = readWord(InFile); // required ChanDesc = readWord(InFile); // optional!! if (mstatus == ST_EOL) { // I just read frequency, not description! chanFreq = new Long(ChanDesc).longValue(); ChanDesc = ""; } else { // now I can read frequency try { chanFreq = new Long(readWord(InFile)).longValue(); } catch (Exception e) {}; } if (ChanName.length() > 0) { mChannels.add(new TvChannel(mChannels.size() + 1, ChanName, ChanNum, ChanDesc, chanFreq)); } } InFile.close(); } catch (Exception e) { if (mChannels.size() == 0) { System.out.println("Error reading Channel List: not well formed!"); defaultChannels(); } } } private String readWord(java.io.FileReader aInFile) throws java.io.IOException { StringBuffer Buf = new StringBuffer(); int i = -1; char c = ' '; mstatus = ST_INIT; while(aInFile.ready() && mstatus == ST_INIT) { i = aInFile.read(); switch (i) { case ',': mstatus = ST_READY; break; case ';': mstatus = ST_EOL; break; case -1: break; // End of file! case '\t': case '\n': // skip char default: Buf.append((char) i); } } if (Buf.length() == 0 && i == -1) { mstatus = ST_EOF; } return Buf.toString().trim(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -