Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e7d4a84e0aa2f7c95c787bac530cc00f53c498bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.buddies.internal.ui.dnd;

import org.eclipse.net4j.buddies.IBuddySession;
import org.eclipse.net4j.buddies.common.IBuddy;
import org.eclipse.net4j.buddies.internal.ui.bundle.OM;
import org.eclipse.net4j.buddies.internal.ui.messages.Messages;
import org.eclipse.net4j.internal.buddies.SessionManager;
import org.eclipse.net4j.util.io.ExtendedDataInputStream;
import org.eclipse.net4j.util.io.ExtendedDataOutputStream;
import org.eclipse.net4j.util.ui.dnd.DNDTransfer;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Eike Stepper
 */
public class BuddiesTransfer extends DNDTransfer<IBuddy[]>
{
  public static final String TYPE_NAME = "buddies-transfer-format"; //$NON-NLS-1$

  public static final BuddiesTransfer INSTANCE = new BuddiesTransfer();

  public BuddiesTransfer()
  {
    super(TYPE_NAME);
  }

  @Override
  protected void writeObject(ExtendedDataOutputStream out, IBuddy[] buddies) throws IOException
  {
    out.writeInt(buddies.length);
    for (IBuddy buddy : buddies)
    {
      out.writeString(buddy.getUserID());
    }
  }

  @Override
  protected IBuddy[] readObject(ExtendedDataInputStream in) throws IOException
  {
    IBuddySession session = SessionManager.INSTANCE.getSession();
    if (session == null)
    {
      OM.LOG.warn(Messages.getString("BuddiesTransfer_1")); //$NON-NLS-1$
      return null;
    }

    List<IBuddy> buddies = new ArrayList<IBuddy>();
    int size = in.readInt();
    for (int i = 0; i < size; i++)
    {
      String userID = in.readString();
      IBuddy buddy = session.getBuddy(userID);
      if (buddy != null)
      {
        buddies.add(buddy);
      }
      else
      {
        OM.LOG.warn(MessageFormat.format(Messages.getString("BuddiesTransfer_2"), userID)); //$NON-NLS-1$
      }
    }

    return buddies.toArray(new IBuddy[buddies.size()]);
  }
}

Back to the top