?? contactlist.java.svn-base
字號:
RecordStore cl = RecordStore.openRecordStore("contactlist", true); // Temporary variables ByteArrayOutputStream baos; DataOutputStream dos; byte[] buf; // Add version info to record store baos = new ByteArrayOutputStream(); dos = new DataOutputStream(baos); dos.writeUTF(Jimm.VERSION); buf = baos.toByteArray(); cl.addRecord(buf, 0, buf.length); // Add version ids to the record store baos.reset(); dos.writeInt(ssiListLastChangeTime); dos.writeShort((short) ssiNumberOfItems); buf = baos.toByteArray(); cl.addRecord(buf, 0, buf.length); // Initialize buffer baos.reset(); // Iterate through all contact items int cItemsCount = cItems.size(); int totalCount = cItemsCount + gItems.size(); ContactItem cItem; ContactListGroupItem gItem; for (int i = 0; i < totalCount; i++) { if (i < cItemsCount) { cItem = getCItem(i); cItem.saveToStream(dos); } else { gItem = (ContactListGroupItem) gItems .elementAt(i - cItemsCount); gItem.saveToStream(dos); } // Start new record if it exceeds 4000 bytes if ((baos.size() >= 4000) || (i == totalCount - 1)) { // Save record buf = baos.toByteArray(); cl.addRecord(buf, 0, buf.length); // Initialize buffer baos.reset(); } } cItem = null; gItem = null; // Close record store cl.closeRecordStore(); } // called before jimm start to connect to server public static void beforeConnect() { treeBuilt = treeSorted = false; haveToBeCleared = true; tree.clear(); setStatusesOffline(); } static public void setStatusesOffline() { onlineCounter = 0; for (int i = cItems.size() - 1; i >= 0; i--) { ContactItem item = getCItem(i); item.setIntValue(ContactItem.CONTACTITEM_STATUS, ContactList.STATUS_OFFLINE); } for (int i = gItems.size() - 1; i >= 0; i--) ((ContactListGroupItem) gItems.elementAt(i)).setCounters(0, 0); } // Updates the client-side contact list (called when a new roster has been // received) static public void update(int versionId1_, int versionId2_, ContactListItem[] items, Vector privData) { synchronized (_this) { // Remove all Elemente form the old ContactList if (haveToBeCleared) { cItems.removeAllElements(); gItems.removeAllElements(); haveToBeCleared = false; ssiNumberOfItems = 0; } // Add new contact items and group items for (int i = 0; i < items.length; i++) { if (items[i] instanceof ContactItem) { cItems.addElement(items[i]); } else if (items[i] instanceof ContactListGroupItem) { gItems.addElement(items[i]); } } ssiNumberOfItems += versionId2_; // Privacy data if (privData != null) { Hashtable list = new Hashtable(); for (int i = cItems.size()-1; i >= 0; i--) list.put(((ContactItem)cItems.elementAt(i)).getStringValue(ContactItem.CONTACTITEM_UIN), cItems.elementAt(i)); int size = privData.size(); String uin; int[] data; ContactItem ci; for (int i = 0; i < size; i += 2) { uin = (String)privData.elementAt(i); data = (int[]) privData.elementAt(i+1); ci = (ContactItem)list.get(uin); if (ci == null) continue; ci.setIntValue(data[0], data[1]); } ci = null; } // Save new contact list if (versionId1_ != 0) { ssiListLastChangeTime = versionId1_; safeSave(); treeBuilt = false; ContactItem cItem; // Which contacts already have chats? for (int i = getSize() - 1; i >= 0; i--) { cItem = getCItem(i); cItem.setBooleanValue ( ContactItem.CONTACTITEM_HAS_CHAT, ChatHistory.chatHistoryExists(cItem.getStringValue(ContactItem.CONTACTITEM_UIN)) ); ChatHistory.updateChatIfExists(cItem); } cItem = null; } } } public static void safeSave() { try { save(); } catch (Exception e) { } } //==================================// // // // WORKING WITH CONTACTS TREE // // // //==================================// // Sorts the contacts and calc online counters static private void sortAll() { if (treeSorted) return; sortType = Options.getInt(Options.OPTION_CL_SORT_BY); if (Options.getBoolean(Options.OPTION_USE_GROUPS)) { // Sort groups tree.sortNode(null); // Sort contacts ContactListGroupItem gItem; TreeNode groupNode; for (int i = 0; i < gItems.size(); i++) { gItem = (ContactListGroupItem) gItems.elementAt(i); groupNode = (TreeNode) gNodes.get(new Integer(gItem.getId())); tree.sortNode(groupNode); calcGroupData(groupNode, gItem); } gItem = null; groupNode = null; } else tree.sortNode(null); treeSorted = true; } static private TreeNode addGroupNodeInternal(ContactListGroupItem item) { TreeNode groupNode = tree.addNode(null, item); gNodes.put(new Integer(item.getId()), groupNode); return groupNode; } // Builds contacts tree (without sorting) static private void buildTree() { int i, gCount, cCount; boolean use_groups = Options.getBoolean(Options.OPTION_USE_GROUPS); boolean only_online = Options.getBoolean(Options.OPTION_CL_HIDE_OFFLINE); cCount = cItems.size(); gCount = gItems.size(); if (treeBuilt || ((cCount == 0) && (gCount == 0))) return; tree.clear(); tree.setShowButtons(use_groups); // add group nodes gNodes.clear(); if (use_groups) { ContactListGroupItem item; for (i = 0; i < gCount; i++) { item = (ContactListGroupItem) gItems.elementAt(i); addGroupNodeInternal(item); } item = null; } // add contacts ContactItem cItem; for (i = 0; i < cCount; i++) { cItem = getCItem(i); if (only_online && (cItem.getIntValue(ContactItem.CONTACTITEM_STATUS) == STATUS_OFFLINE) && !cItem.mustBeShownAnyWay()) continue; if (use_groups) { int group = cItem.getIntValue(ContactItem.CONTACTITEM_GROUP); TreeNode groupNode = (TreeNode) gNodes.get(new Integer(group)); tree.addNode(groupNode, cItem); } else { tree.addNode(null, cItem); } } cItem = null; // Delete empty groups if (Options.getBoolean(Options.OPTION_CL_HIDE_EMPTY) && use_groups) for (i = gItems.size()-1; i >= 0; i--) { ContactListGroupItem gItem = (ContactListGroupItem) gItems.elementAt(i); Integer intGroup = new Integer(gItem.getId()); TreeNode groupNode = (TreeNode) gNodes.get(intGroup); if (groupNode.size() == 0) { tree.removeNode(groupNode); gNodes.remove(intGroup); } } treeSorted = false; treeBuilt = true; } // Returns reference to group with id or null if group not found public static ContactListGroupItem getGroupById(int id) { ContactListGroupItem group; for (int i = gItems.size() - 1; i >= 0; i--) { group = (ContactListGroupItem) gItems.elementAt(i); if (group.getId() == id) return group; } group = null; return null; } // Returns reference to contact item with uin or null if not found static public ContactItem getItembyUIN(String uin) { int uinInt; try { uinInt = Integer.parseInt(uin); } catch (NumberFormatException ne){ return null; } ContactItem citem; for (int i = cItems.size() - 1; i >= 0; i--) { citem = getCItem(i); if (citem.getUIN() == uinInt) return citem; } return null; } static public ContactItem[] getGroupItems(int groupId) { Vector vect = new Vector(); ContactItem cItem; for (int i = 0; i < cItems.size(); i++) { cItem = getCItem(i); if (cItem.getIntValue(ContactItem.CONTACTITEM_GROUP) == groupId) vect.addElement(cItem); } cItem = null; ContactItem[] result = new ContactItem[vect .size()]; vect.copyInto(result); return result; } // Calculates online/total values for group static private void calcGroupData(TreeNode groupNode, ContactListGroupItem group) { if ((group == null) || (groupNode == null)) return; ContactItem cItem; int onlineCount = 0; int count = groupNode.size(); for (int i = 0; i < count; i++) { if (!(groupNode.elementAt(i).getData() instanceof ContactItem)) continue; // TODO: must be removed cItem = (ContactItem) groupNode.elementAt(i).getData(); if (cItem.getIntValue(ContactItem.CONTACTITEM_STATUS) != STATUS_OFFLINE) onlineCount++; } cItem = null; group.setCounters(onlineCount, count); } // Must be called after any changes in contacts public static void contactChanged(ContactItem item, boolean setCurrent, boolean needSorting) { if (!treeBuilt) return; boolean contactExistInTree = false, contactExistsInList, wasDeleted = false, haveToAdd = false, haveToDelete = false; TreeNode cItemNode = null; int i, count, groupId; int status = item.getIntValue(ContactItem.CONTACTITEM_STATUS); String uin = item.getStringValue(ContactItem.CONTACTITEM_UIN); // which group id ? groupId = item.getIntValue(ContactItem.CONTACTITEM_GROUP); boolean only_online = Options.getBoolean(Options.OPTION_CL_HIDE_OFFLINE); boolean showGroups = Options.getBoolean(Options.OPTION_USE_GROUPS); boolean only_not_empty = Options.getBoolean(Options.OPTION_CL_HIDE_EMPTY); // Whitch group node? TreeNode groupNode = (TreeNode) gNodes.get(new Integer(groupId)); if (groupNode == null) groupNode = tree.getRoot(); // Does contact exists in tree? count = groupNode.size(); Object data; for (i = 0; i < count; i++) { cItemNode = groupNode.elementAt(i); data = cItemNode.getData(); if (!(data instanceof ContactItem)) continue; if (!((ContactItem) data).getStringValue( ContactItem.CONTACTITEM_UIN).equals(uin)) continue; contactExistInTree = true; break; } data = null; // Does contact exists in internal list? contactExistsInList = (cItems.indexOf(item) != -1); // Lock tree repainting tree.lock(); haveToAdd = contactExistsInList && !contactExistInTree; if (only_online && !contactExistInTree) haveToAdd |= ((status != STATUS_OFFLINE) | item.mustBeShownAnyWay()); haveToDelete = !contactExistsInList && contactExistInTree; if (only_online && contactExistInTree) haveToDelete |= ((status == STATUS_OFFLINE) && !item .mustBeShownAnyWay()); // if have to add new contact if (haveToAdd) { if (only_not_empty && showGroups && !gNodes.containsKey(new Integer(groupId))) { ContactListGroupItem group = getGroupById(groupId); groupNode = (group != null) ? addGroupNodeInternal(group) : tree.getRoot(); } cItemNode = tree.addNode(groupNode, item); } // if have to delete contact else if (haveToDelete) { tree.removeNode(cItemNode); if (only_not_empty && showGroups) { if (groupNode.size() == 0) { tree.removeNode(groupNode); gNodes.remove(new Integer(groupId)); } } wasDeleted = true; } // sort group if (needSorting && !wasDeleted) { boolean isCurrent = (tree.getCurrentItem() == cItemNode), inserted = false; tree.deleteChild(groupNode, tree.getIndexOfChild(groupNode, cItemNode)); int contCount = groupNode.size(); sortType = Options.getInt(Options.OPTION_CL_SORT_BY); // TODO: Make binary search instead of linear before child insertion!!! TreeNode testNode; for (int j = 0; j < contCount; j++) { testNode = groupNode.elementAt(j); if (!(testNode.getData() instanceof ContactItem)) continue; if (_this.vtCompareNodes(cItemNode, testNode) < 0) { tree.insertChild(groupNode, cItemNode, j); inserted = true; break; } } testNode = null; if (!inserted) tree.insertChild(groupNode, cItemNode, contCount); if (isCurrent) tree.setCurrentItem(cItemNode);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -