THIS IS THE CODE NECESSARY FOR SAMETIME 7.5.1 PLUG-IN DEVELOPMENT TUTORIAL SERIES Part 2: DEVELOPING THE BUDDYNOTE PLUG-IN. BELOW ARE THE CODE SNIPPETS REFERRENCED IN THE TUTORIAL TO BE PASTED INTO THE PLUG-IN CLASSES =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= =================================================== ================== CODE SNIPPETS ================== =================================================== =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= ================== BuddyNoteView ================== ================== variables static public BuddyNoteView INSTANCE; private final String DEFAULT_NOTE = ""; private Person person = null; MyBusinessCard bizCard; StyledText textControl; String currentNote = null; ================== constructor public BuddyNoteView() { INSTANCE = this; } ================== getLocalFilespec private String getLocalFilespec(Person p) { // Store the buddy note in the plug-in metadata area // named by person ID. Replace invalid filspec characters like // the colon. StringBuffer sb = new StringBuffer(); String id = p.getId(); sb.append(Activator.getDefault().getStateLocation()); sb.append(File.separator); for (int i=0; i < id.length(); i++) { if (Character.isLetterOrDigit(id.charAt(i))) sb.append(id.charAt(i)); else sb.append('_'); } sb.append(".txt"); return sb.toString(); } ================== storeBuddyNote private void storeBuddyNote(Person p, String s) { try { String spec = getLocalFilespec(p); File f = new File(spec); // Delete the file if the new note is empty if (s == null || s.trim().length() == 0) { f.delete(); return; } if (!f.exists()) f.createNewFile(); BufferedWriter out = new BufferedWriter(new FileWriter(f)); out.write(s); out.close(); } catch (IOException e) { e.printStackTrace(); } } ================== retrieveBuddyNote private String retrieveBuddyNote(Person p) { String s = null; if (person != null) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); String spec = getLocalFilespec(p); File f = new File(spec); if (f.exists()) { try { BufferedReader in = new BufferedReader(new FileReader(f)); String str; while ((str = in.readLine()) != null) { pw.println(str); } in.close(); s = sw.toString(); } catch (IOException e) { } } } return s; } ================== createPartControl (signature already created in BuddyNoteView) Composite comp = new Composite(parent, SWT.BORDER); comp.setLayout(new FormLayout()); // Business card is "person aware", i.e., will update // as the person's status updates. bizCard = new MyBusinessCard(comp, SWT.NONE); FormData fd = new FormData(); fd.top = new FormAttachment(0, 0); fd.left = new FormAttachment(0, 0); fd.right = new FormAttachment(100, 0); bizCard.setLayoutData(fd); // Dark line separator between business card and text area. Label divider = new Label(comp, SWT.NONE); divider.setBackground(divider.getDisplay().getSystemColor( SWT.COLOR_DARK_GRAY)); fd = new FormData(); fd.top = new FormAttachment(bizCard, 0); fd.left = new FormAttachment(0, 0); fd.right = new FormAttachment(100, 0); fd.height = 1; divider.setLayoutData(fd); // Text input area; it displays an indication that it's empty // as defined by DEFAULT_NOTE textControl = new StyledText(comp, SWT.MULTI | SWT.V_SCROLL | SWT.WRAP); fd = new FormData(); fd.top = new FormAttachment(divider, 0); fd.bottom = new FormAttachment(100, 0); fd.left = new FormAttachment(0, 0); fd.right = new FormAttachment(100, 0); textControl.setLayoutData(fd); textControl.setText(DEFAULT_NOTE); // Save and restore the buddy note on gain/lose focus. // (remember not to store the "default" note!) textControl.addFocusListener(new FocusAdapter() { public void focusLost(FocusEvent e) { if (person != null && !textControl.getText().equals(currentNote)) { currentNote = textControl.getText(); storeBuddyNote(person, currentNote); } } public void focusGained(FocusEvent arg0) { if (person != null && textControl.getText().equals(DEFAULT_NOTE)) textControl.setText(""); } }); comp.layout(); ================== handleConnected and handleBuddySelected void handleConnected() { // initialize the first person to the current user if none already selected if (person == null) { person = getLocalPerson(); refreshPersonInfo(person); } } void handleBuddySelected(Person person) { this.person = person; refreshPersonInfo(person); } ================== refreshPersonInfo private void refreshPersonInfo(final Person person) { // Notifications in Sametime Connect often come from // non-UI threads. Queue a request to the UI thread. bizCard.getDisplay().asyncExec(new Runnable() { public void run() { // It's possible that the user closed the window prior // to this queued message (unlikely, but possible) if (textControl.isDisposed()) return; bizCard.setPerson(person); currentNote = retrieveBuddyNote(person); if (currentNote != null) textControl.setText(currentNote); else textControl.setText(DEFAULT_NOTE); } }); } ================== getLocalPerson private Person getLocalPerson() { // Return the local person (for the initial display) Person p = null; try { CommunityService communityMgr = (CommunityService) ServiceHub .getService(CommunityService.SERVICE_TYPE); Community community = communityMgr.getDefaultCommunity(); if (null != community) { String localUserId = community.getUserId(); if (localUserId != null) { PeopleService peopleSvc = (PeopleService) ServiceHub .getService(PeopleService.SERVICE_TYPE); p = peopleSvc .getPerson(localUserId, community.getId()); } } } catch (ServiceException e) { e.printStackTrace(); } return p; } ================== BuddyNoteMessageHandlerAdapter ================== public class BuddyNoteMessageHandlerAdapter extends MessageHandlerAdapter { public BuddyNoteMessageHandlerAdapter(MessageHandler handler) { super(handler); } public BuddyNoteMessageHandlerAdapter() { super (new BuddyNoteMessageHandler()); } } ================== BuddyNoteMessageHandler ================== public void handleMessage(BuddySelectedMessage message) { try { // Note: Notifications are generally not from the UI thread // The receiver should use Display.asyncExec as necessary. PeopleService peopleSvc = (PeopleService) ServiceHub.getService(PeopleService.SERVICE_TYPE); Person person = peopleSvc.getPersonById(message.getPersonId()); BuddyNoteView.INSTANCE.handleBuddySelected(person); } catch (ServiceException e) { e.printStackTrace(); } } public void handleMessage(ImConnectedMessage message) { BuddyNoteView.INSTANCE.handleConnected(); } public void handleDefaultMessage(Message message) { }