Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2ae364a599a1432ba147d61a96ee99a3f48ee0a2 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * 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
 *
 *  Initial Publication:
 *    Eclipse Magazin - http://www.eclipse-magazin.de
 */
package org.gastro.rcp.internal.department;

import org.eclipse.net4j.util.StringUtil;
import org.eclipse.net4j.util.ref.ReferenceValueMap;

import org.eclipse.nebula.widgets.gallery.DefaultGalleryItemRenderer;
import org.eclipse.nebula.widgets.gallery.Gallery;
import org.eclipse.nebula.widgets.gallery.GalleryItem;
import org.eclipse.nebula.widgets.gallery.NoGroupRenderer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

import org.gastro.inventory.Department;
import org.gastro.inventory.Employee;
import org.gastro.rcp.IModel;

import java.util.Map;

/**
 * @author Eike Stepper
 */
public class EmployeesView extends ViewPart
{
  public static final String ID = "org.gastro.rcp.employees.view";

  private static final Map<String, String> fakeImages = new ReferenceValueMap.Soft<String, String>();

  private static int fakeImageID;

  public EmployeesView()
  {
  }

  /**
   * Passing the focus request to the viewer's control.
   */
  @Override
  public void setFocus()
  {
  }

  /**
   * This is a callback that will allow us to create the viewer and initialize it.
   */
  @Override
  public void createPartControl(Composite parent)
  {
    Department department = (Department)IModel.INSTANCE.getStation();

    DefaultGalleryItemRenderer ir = new DefaultGalleryItemRenderer();
    ir.setShowRoundedSelectionCorners(true);

    Gallery gallery = new Gallery(parent, SWT.NONE);
    gallery.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
    gallery.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_BLUE));
    gallery.setFont(SWTResourceManager.getFont("Segoe UI", 16, SWT.BOLD));
    gallery.setGroupRenderer(new NoGroupRenderer());
    gallery.setItemRenderer(ir);

    GalleryItem group = new GalleryItem(gallery, SWT.NONE);
    for (Employee employee : department.getEmployees())
    {
      GalleryItem item = new GalleryItem(group, SWT.NONE);
      String imageName = getImageName(employee.getName());
      item.setImage(getCachedImage(imageName));
      item.setText(StringUtil.safe(employee.getName()));
    }
  }

  private String getImageName(String employee)
  {
    if (employee == null)
    {
      return null;
    }

    String name = fakeImages.get(employee);
    if (name != null)
    {
      return name;
    }

    for (;;)
    {
      name = "employee-" + ++fakeImageID;
      Image image = getCachedImage(name);
      if (image != null)
      {
        fakeImages.put(employee, name);
        return name;
      }

      fakeImageID = 0;
    }
  }

  private Image getCachedImage(String name)
  {
    return ResourceManager.getPluginImage("org.gastro.rcp.department", "images/" + name + ".png");
  }
}

Back to the top