📄 jpquestionvector.java
字号:
/*JPQuestionVector stores questions so that various parts of the server can access themCopyright (C) 2005-2006 Igor Partola, Michael J. Krikonis, Clark UniversityThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/package SharedClasses;import java.io.*;import java.util.*;public class JPQuestionVector { private Vector QuestionVector; private Vector Listeners; private final static int JPQ_SIGNATURE = 369638; public JPQuestionVector() { QuestionVector = new Vector(); Listeners = new Vector(); } public void newQuestion() { Question q = new Question("", "Yes", "No", "Maybe", "I don't know", "I need to ask my wife", "N/A", 15, QuestionVector.size() + 1, 6); QuestionVector.addElement(q); fireSyncEvent(); } public void saveToFile(String filename) { Question q; int count = QuestionVector.size(); int i = 0; try { FileOutputStream fos = new FileOutputStream(filename, false); //Create the JPQ signature; fos.write(intToBytes(JPQ_SIGNATURE)); while (i < count) { q = (Question) QuestionVector.elementAt(i); fos.write(q.getBytes()); i++; } fos.close(); } catch (Exception e) {System.err.println ("Error writing to file");} } public boolean loadFromFile(String filename) { int BytesRead = 0; byte[] sbuf = new byte[40]; byte[] bbuf; int qns, qs, as, bs, cs, ds, es, fs, tqs; try { FileInputStream fis = new FileInputStream(filename); fis.read(sbuf, 0, 4); if (bytesToInt(sbuf, 0) != JPQ_SIGNATURE) return false; QuestionVector.removeAllElements(); while ((BytesRead = fis.read(sbuf, 0, 40)) > -1) { qns = bytesToInt(sbuf, 0); qs = bytesToInt(sbuf, 4); as = bytesToInt(sbuf, 8); bs = bytesToInt(sbuf, 12); cs = bytesToInt(sbuf, 16); ds = bytesToInt(sbuf, 20); es = bytesToInt(sbuf, 24); fs = bytesToInt(sbuf, 28); tqs = qns + qs + as + bs + cs + ds + es + fs; bbuf = new byte[40 + tqs]; System.arraycopy(sbuf, 0, bbuf, 0, 40); BytesRead = fis.read(bbuf, 40, tqs); QuestionVector.addElement(new Question(bbuf)); bbuf = null; } fis.close(); } catch (Exception e) {System.err.println("File input error"); return false;} if (QuestionVector.size() <= 0) newQuestion(); fireSyncEvent(); return true; } public Question getQuestionAt(int index) { if (index > QuestionVector.size() - 1) return null; return (Question) QuestionVector.elementAt(index); } public void addQuestion(Question q) { QuestionVector.addElement(q); fireSyncEvent(); } public void deleteQuestionAt(int index) { int i; int size = QuestionVector.size(); Question q; if (size < 2) return; QuestionVector.removeElementAt(index); for (i = index; i < QuestionVector.size(); i++) { q = (Question) QuestionVector.elementAt(i); q.setQuestionNumber(i + 1); } fireSyncEvent(); } public Vector getQuestionListing() { Question q; int i; int size = QuestionVector.size(); Vector v = new Vector(); for (i = 0; i < size; i++) { q = (Question) QuestionVector.elementAt(i); v.addElement(q.getQuestionName()); } return v; } public Vector getVector() { return QuestionVector; } public int size() { return QuestionVector.size(); } public void addListener(Syncable o) { Listeners.addElement(o); } public void removeListener(Syncable o) { Listeners.remove(o); } public void fireSyncEvent() { int i; int size = Listeners.size(); Syncable s; for (i = 0; i < size; i++) { s = (Syncable) Listeners.elementAt(i); s.syncComboBoxes(); } } public int getIndexOfQuestion(Question q) { return QuestionVector.indexOf(q); } private static int bytesToInt(byte[] b, int offset) { int value = 0; for (int i = 0; i < 4; i++) { int shift = (4 - 1 - i) * 8; value += (b[i + offset] & 0x000000FF) << shift; } return value; } private byte[] intToBytes(int value) { byte [] bytes = new byte [4]; bytes[0] = (byte)(value >> 24); bytes[1] = (byte)(value >> 16); bytes[2] = (byte)(value >> 8); bytes[3] = (byte)(value); return bytes; } public void deleteAllQuestions() { QuestionVector.removeAllElements(); newQuestion(); fireSyncEvent(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -