mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noël Rouvignac
20.36.2016 2a3158aad80fc910b83336485b3e545dea50066c
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2008-2010 Sun Microsystems, Inc.
 * Portions Copyright 2013-2016 ForgeRock AS.
 */
package org.opends.guitools.controlpanel.browser;
 
import java.util.HashMap;
import java.util.Set;
import java.util.SortedSet;
 
import javax.swing.ImageIcon;
 
import org.opends.guitools.controlpanel.util.Utilities;
import org.opends.quicksetup.ui.UIFactory;
import org.opends.server.util.ServerConstants;
 
import static org.opends.messages.AdminToolMessages.*;
 
/**
 * This class is used as a cache containing the icons that are used by the
 * BrowserController to update the nodes.  It keeps some icons associated with
 * some entry types, to suffixes, to the root node, etc.
 */
public class IconPool {
 
  /** Mask for the leaf node. */
  public static final int MODIFIER_LEAF   = 0x01;
  /** Mask for the referral node. */
  public static final int MODIFIER_REFERRAL = 0x02;
  /** Mask for the node that has an error. */
  public static final int MODIFIER_ERROR    = 0x04;
 
  private final HashMap<String, ImageIcon> iconTable = new HashMap<>();
  private final HashMap<String, String> pathTable = new HashMap<>();
  private final HashMap<String, String> descriptionTable = new HashMap<>();
  private ImageIcon defaultLeafIcon;
  private ImageIcon suffixIcon;
  private ImageIcon defaultContainerIcon;
  private ImageIcon rootNodeIcon;
  private ImageIcon errorIcon;
  private ImageIcon errorMaskIcon;
  private ImageIcon referralMaskIcon;
 
  /** The path that contains the icons. */
  public static final String IMAGE_PATH =
    "org/opends/guitools/controlpanel/images";
 
 
  private static final String[] ICON_PATH = {
    ServerConstants.OC_PERSON,  "ds-user.png",
    ServerConstants.OC_ORGANIZATION, "ds-folder.png",
    ServerConstants.OC_ORGANIZATIONAL_UNIT_LC,  "ds-ou.png",
    ServerConstants.OC_GROUP_OF_NAMES_LC, "ds-group.png",
    ServerConstants.OC_GROUP_OF_ENTRIES_LC, "ds-group.png",
    ServerConstants.OC_GROUP_OF_UNIQUE_NAMES_LC,  "ds-group.png",
    ServerConstants.OC_GROUP_OF_URLS_LC,  "ds-group.png",
    ServerConstants.OC_VIRTUAL_STATIC_GROUP,  "ds-group.png",
    "passwordpolicy",   "ds-ppol.png"
  };
 
  private static final String[] DESCRIPTION = {
    ServerConstants.OC_PERSON, INFO_PERSON_ICON_DESCRIPTION.get().toString(),
    ServerConstants.OC_ORGANIZATION, INFO_ORGANIZATION_ICON_DESCRIPTION.get()
      .toString(),
    ServerConstants.OC_ORGANIZATIONAL_UNIT_LC,
    INFO_ORGANIZATIONAL_UNIT_ICON_DESCRIPTION.get().toString(),
    ServerConstants.OC_GROUP_OF_NAMES_LC, INFO_STATIC_GROUP_ICON_DESCRIPTION
      .get().toString(),
    ServerConstants.OC_GROUP_OF_ENTRIES_LC, INFO_STATIC_GROUP_ICON_DESCRIPTION
      .get().toString(),
    ServerConstants.OC_GROUP_OF_UNIQUE_NAMES_LC,
      INFO_STATIC_GROUP_ICON_DESCRIPTION.get().toString(),
    ServerConstants.OC_GROUP_OF_URLS_LC, INFO_DYNAMIC_GROUP_ICON_DESCRIPTION
      .get().toString(),
    ServerConstants.OC_VIRTUAL_STATIC_GROUP,
    INFO_VIRTUAL_STATIC_GROUP_ICON_DESCRIPTION.get().toString(),
    "passwordpolicy", INFO_PASSWORD_POLICY_ICON_DESCRIPTION.get().toString()
  };
 
  private final String GENERIC_OBJECT_DESCRIPTION = "Generic entry";
 
  /** The default constructor. */
  public IconPool() {
    // Recopy ICON_PATH in pathTable for fast access
    for (int i = 0; i < ICON_PATH.length; i = i+2) {
      pathTable.put(ICON_PATH[i], ICON_PATH[i+1]);
    }
    for (int i = 0; i < DESCRIPTION.length; i = i+2) {
      descriptionTable.put(DESCRIPTION[i], DESCRIPTION[i+1]);
    }
  }
 
 
  /**
   * If objectClass is null, a default icon is used.
   * @param objectClasses the objectclass values of the entry for which we want
   * an icon.
   * @param modifiers the modifiers associated with the entry (if there was
   * an error, if it is a referral, etc.).
   * @return the icon corresponding to the provided object classes and
   * modifiers.
   */
  public ImageIcon getIcon(SortedSet<String> objectClasses, int modifiers) {
    String key = makeKey(objectClasses, modifiers);
    ImageIcon result = iconTable.get(key);
    if (result == null) {
      result = makeIcon(objectClasses, modifiers);
      iconTable.put(key, result);
    }
    return result;
  }
 
  /**
   * Creates an icon for a given path.
   * @param path the path of the icon.
   * @param description the description of the icon
   * @return the associated ImageIcon.
   */
  private ImageIcon createIcon(String path, String description)
  {
    ImageIcon icon = Utilities.createImageIcon(path);
    if (description != null)
    {
      icon.setDescription(description);
      icon.getAccessibleContext().setAccessibleDescription(description);
    }
    return icon;
  }
 
  /**
   * Returns the icon associated with a leaf node.
   * @return the icon associated with a leaf node.
   */
  public ImageIcon getDefaultLeafIcon() {
    if (defaultLeafIcon == null) {
      defaultLeafIcon = createIcon(IMAGE_PATH + "/ds-generic.png",
          GENERIC_OBJECT_DESCRIPTION);
    }
    return defaultLeafIcon;
  }
 
 
  /**
   * Returns the icon associated with a container node.
   * @return the icon associated with a container node.
   */
  public ImageIcon getDefaultContainerIcon() {
    if (defaultContainerIcon == null) {
      defaultContainerIcon = createIcon(IMAGE_PATH + "/ds-folder.png",
      "Folder entry");
    }
    return defaultContainerIcon;
  }
 
  /**
   * Returns the icon associated with a suffix node.
   * @return the icon associated with a suffix node.
   */
  public ImageIcon getSuffixIcon() {
    if (suffixIcon == null) {
      suffixIcon = createIcon(IMAGE_PATH + "/ds-suffix.png",
      "Suffix entry");
    }
    return suffixIcon;
  }
 
  /**
   * Returns the icon associated with a root node.
   * @return the icon associated with a root node.
   */
  public ImageIcon getIconForRootNode() {
    if (rootNodeIcon == null) {
      rootNodeIcon = createIcon(IMAGE_PATH + "/ds-directory.png",
      "Root entry");
    }
    return rootNodeIcon;
  }
 
  /**
   * Returns the icon associated with a node for which an error occurred.
   * @return the icon associated with a node for which an error occurred.
   */
  public ImageIcon getErrorIcon() {
    if (errorIcon == null) {
      errorIcon = UIFactory.getImageIcon(UIFactory.IconType.ERROR);
    }
    return errorIcon;
  }
 
 
  /**
   * Returns the icon associated with the error mask icon.
   * @return the icon associated with the error mask icon.
   */
  public ImageIcon getErrorMaskIcon() {
    if (errorMaskIcon == null) {
      errorMaskIcon = UIFactory.getImageIcon(UIFactory.IconType.ERROR);
    }
    return errorMaskIcon;
  }
 
 
  /**
   * Returns the icon associated with the referral mask icon.
   * @return the icon associated with the referral mask icon.
   */
  public ImageIcon getReferralMaskIcon() {
    if (referralMaskIcon == null) {
      referralMaskIcon = createIcon(IMAGE_PATH + "/ds-referral.png",
      "Referral mask");
    }
    return referralMaskIcon;
  }
 
 
  /**
   * Returns an icon for a given objectclass applying some modifiers.
   * @param objectClasses the objectclasses of the entry
   * @param modifiers the modifiers of the icon (if the entry is inactivated,
   * if it is a referral...).
   * @return an icon for a given objectclass applying some modifiers.
   */
  private ImageIcon makeIcon(Set<String> objectClasses, int modifiers) {
    ImageIcon result;
 
    // Find the icon associated to the object class
    if (objectClasses == null || objectClasses.isEmpty()) {
      result = getDefaultContainerIcon();
    }
    else {
      String iconFile = null;
      for (String value : objectClasses)
      {
        iconFile = pathTable.get(value.toLowerCase());
        if (iconFile != null)
        {
          break;
        }
      }
      if (iconFile == null) {
        if ((modifiers & MODIFIER_LEAF) != 0) {
          result = getDefaultLeafIcon();
        }
        else {
          result = getDefaultContainerIcon();
        }
      }
      else {
        String description = null;
        for (String value : objectClasses)
        {
          description = descriptionTable.get(value.toLowerCase());
          if (description != null)
          {
            break;
          }
        }
        if (description == null)
        {
          description = GENERIC_OBJECT_DESCRIPTION;
        }
        result = createIcon(IMAGE_PATH + "/" + iconFile,
            description);
      }
    }
 
    // Alter this icon according the modifiers
    if ((modifiers & MODIFIER_REFERRAL) != 0) {
      result = getReferralMaskIcon();
    }
    if ((modifiers & MODIFIER_ERROR) != 0) {
      result = getErrorMaskIcon();
    }
 
    return result;
  }
 
 
  private String makeKey(SortedSet<String> ocValues, int modifiers) {
    // TODO: verify the performance of IconPool.makeKey()
    StringBuilder result = new StringBuilder();
    if(ocValues != null) {
      result.append(Utilities.getStringFromCollection(ocValues, ""));
    }
    result.append(modifiers);
    return result.toString();
  }
 
}