IconCache.m 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. #import "IconCache.h"
  15. @implementation IconCache
  16. static IconCache* sharedInstance = nil;
  17. - init
  18. {
  19. self = [super init];
  20. if (self)
  21. {
  22. _iconIdDictionary = [[NSMutableDictionary alloc] init];
  23. _iconPathDictionary = [[NSMutableDictionary alloc] init];
  24. _currentIconId = 0;
  25. }
  26. return self;
  27. }
  28. - (void)dealloc
  29. {
  30. [_iconIdDictionary release];
  31. [_iconPathDictionary release];
  32. sharedInstance = nil;
  33. [super dealloc];
  34. }
  35. + (IconCache*)sharedInstance
  36. {
  37. @synchronized(self)
  38. {
  39. if (sharedInstance == nil)
  40. {
  41. sharedInstance = [[self alloc] init];
  42. }
  43. }
  44. return sharedInstance;
  45. }
  46. - (NSImage*)getIcon:(NSNumber*)iconId
  47. {
  48. NSImage* image = [_iconIdDictionary objectForKey:iconId];
  49. return image;
  50. }
  51. - (NSNumber*)registerIcon:(NSString*)path
  52. {
  53. if (path == nil)
  54. {
  55. return [NSNumber numberWithInt:-1];
  56. }
  57. NSImage* image = [[NSImage alloc]initWithContentsOfFile:path];
  58. if (image == nil)
  59. {
  60. return [NSNumber numberWithInt:-1];
  61. }
  62. NSNumber* iconId = [_iconPathDictionary objectForKey:path];
  63. if (iconId == nil)
  64. {
  65. _currentIconId++;
  66. iconId = [NSNumber numberWithInt:_currentIconId];
  67. [_iconPathDictionary setObject:iconId forKey:path];
  68. }
  69. [_iconIdDictionary setObject:image forKey:iconId];
  70. [image release];
  71. return iconId;
  72. }
  73. - (void)unregisterIcon:(NSNumber*)iconId
  74. {
  75. NSString* path = @"";
  76. for (NSString* key in _iconPathDictionary)
  77. {
  78. NSNumber* value = [_iconPathDictionary objectForKey:key];
  79. if ([value isEqualToNumber:iconId])
  80. {
  81. path = key;
  82. break;
  83. }
  84. }
  85. [_iconPathDictionary removeObjectForKey:path];
  86. [_iconIdDictionary removeObjectForKey:iconId];
  87. }
  88. @end