/**
 * Contains a list of available sites that have the specific project as the source.
 *
 * @author Michael Xia ([email protected])
 */
public class ProjectSiteConnections extends PlatformObject implements IWorkbenchAdapter {

  private static ImageDescriptor IMAGE_DESCRIPTOR =
      SyncingUIPlugin.getImageDescriptor("icons/full/obj16/ftp.png"); // $NON-NLS-1$

  private IProject fProject;

  public ProjectSiteConnections(IProject project) {
    fProject = project;
  }

  public Object[] getChildren(Object o) {
    ISiteConnection[] sites = SiteConnectionUtils.findSitesForSource(fProject, true, true);
    List<ProjectSiteConnection> targets = new ArrayList<ProjectSiteConnection>();
    for (ISiteConnection site : sites) {
      targets.add(new ProjectSiteConnection(fProject, site));
    }
    return targets.toArray(new ProjectSiteConnection[targets.size()]);
  }

  public ImageDescriptor getImageDescriptor(Object object) {
    return IMAGE_DESCRIPTOR;
  }

  public String getLabel(Object o) {
    return Messages.ProjectSiteConnections_Name;
  }

  public Object getParent(Object o) {
    return null;
  }

  @SuppressWarnings("rawtypes")
  public Object getAdapter(Class adapter) {
    if (adapter == IProject.class || adapter == IContainer.class) {
      return fProject;
    }
    return super.getAdapter(adapter);
  }

  @Override
  public String toString() {
    return Messages.ProjectSiteConnections_Name;
  }
}
Beispiel #2
0
  /**
   * Exports a sync state to a log file.
   *
   * @param logFile File to export the log into.
   * @param items Synchronized resources
   */
  @SuppressWarnings("nls")
  public void export(File logFile, ISyncResource[] items) {
    DateFormat df = new SimpleDateFormat();
    String date = df.format(new Date());

    Writer writer = null;
    try {
      if (!logFile.exists()) {
        logFile.createNewFile();
      }
      writer = new FileWriter(logFile, true);

      StringBuilder builder = new StringBuilder();
      builder.append("File Transfer Log: " + date + FileUtil.NEW_LINE);
      for (ISyncResource iSyncResource : items) {
        if (iSyncResource.isSkipped()) {
          builder.append(" " + iSyncResource.getPath().toString() + ": Skipped");
        } else {
          builder.append(
              " "
                  + iSyncResource.getPath().toString()
                  + ": "
                  + getSyncState(iSyncResource.getSyncState()));
        }
        builder.append('\n');
      }

      writer.write(builder.toString());
    } catch (IOException e) {
      IdeLog.logError(SyncingUIPlugin.getDefault(), e);
    } finally {
      try {
        if (writer != null) {
          writer.close();
        }
      } catch (IOException e) {
        // ignore
      }
    }
  }
/** @author Michael Xia ([email protected]) */
public class SiteConnectionDecorator implements INavigatorDecorator {

  private static final Image DECORATOR =
      SyncingUIPlugin.getImage("icons/full/obj16/sync_connection.gif"); // $NON-NLS-1$

  /** padding pixels */
  private static final int PADDING = CoreUIUtils.onMacOSX ? 5 : 2;

  public SiteConnectionDecorator() {}

  /** @see com.aptana.ide.core.ui.INavigatorDecorator#addDecorator(org.eclipse.swt.widgets.Tree) */
  public void addDecorator(final Tree tree) {
    if (isDisposed(tree)) {
      return;
    }

    tree.addListener(
        SWT.MeasureItem,
        new Listener() {

          public void handleEvent(Event event) {
            int startWidth = event.width;
            try {
              if (!isDisposed(tree) && event.item instanceof TreeItem) {
                Object data = ((TreeItem) event.item).getData();

                if (data instanceof IContainer) {
                  String lastConnection = getLastSyncConnection((IContainer) data);
                  if (lastConnection != null) {
                    Point stringExtent = event.gc.stringExtent(lastConnection);
                    event.width += DECORATOR.getBounds().width + PADDING + stringExtent.x + PADDING;
                  }
                }
              }
            } catch (Exception e) {
              // Catch all exception so tree painting is never hindered by
              // decoration
              event.width = startWidth;
            } catch (Error e) {
              // Catch all exception so tree painting is never hindered by
              // decoration
              event.width = startWidth;
            }
          }
        });

    tree.addListener(
        SWT.PaintItem,
        new Listener() {

          public void handleEvent(Event event) {
            try {
              if (!isDisposed(tree) && event.item instanceof TreeItem) {
                Object data = ((TreeItem) event.item).getData();

                if (data instanceof IContainer) {
                  String lastConnection = getLastSyncConnection((IContainer) data);
                  if (lastConnection != null) {
                    int x = event.x + event.width + PADDING;
                    int itemHeight = tree.getItemHeight();
                    int imageHeight = DECORATOR.getBounds().height;
                    int y = event.y + (itemHeight - imageHeight) / 2;
                    event.gc.drawImage(DECORATOR, x, y);
                    event.x = x + DECORATOR.getBounds().width;

                    x = event.x + PADDING;

                    Point stringExtent = event.gc.stringExtent(lastConnection);
                    y = event.y + (itemHeight - stringExtent.y) / 2;
                    event.gc.drawString(lastConnection, x, y, true);
                    event.x += stringExtent.x;
                  }
                }
              }
            } catch (Exception e) {
              // Catch all exception so tree painting is never hindered by
              // decoration
            } catch (Error e) {
              // Catch all exception so tree painting is never hindered by
              // decoration
            }
          }
        });
  }

  private static String getLastSyncConnection(IContainer container) {
    if (container == null) {
      return null;
    }
    // only shows the decorator when user chooses to
    // remember the decision
    boolean remember = ResourceSynchronizationUtils.isRememberDecision(container);
    if (!remember) {
      return null;
    }

    String lastConnection = ResourceSynchronizationUtils.getLastSyncConnection(container);
    if (lastConnection == null) {
      return null;
    }

    ISiteConnection[] sites = SiteConnectionUtils.findSitesForSource(container, true);
    String target;
    for (ISiteConnection site : sites) {
      target = site.getDestination().getName();
      if (target.equals(lastConnection)) {
        return target;
      }
    }
    return null;
  }

  private static boolean isDisposed(Tree tree) {
    return tree == null || tree.isDisposed();
  }
}