1:
31: package ;
32:
33: import ;
34: import ;
35: import ;
36: import ;
37: import ;
38: import ;
39: import ;
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47: import ;
48: import ;
49: import ;
50: import ;
51:
52:
58: public class EncodingComboBoxModel implements ComboBoxModel
59: {
60:
63: private static final String ENCODING_DEFAULT_DESCRIPTION =
64: "[no description]";
65:
66:
69: public static final String AVAILABLE_ENCODINGS
70: = "org.jfree.report.modules.gui.base.EncodingsAvailable";
71:
72:
75: public static final String AVAILABLE_ENCODINGS_ALL = "all";
76:
80: public static final String AVAILABLE_ENCODINGS_FILE = "file";
81:
85: public static final String AVAILABLE_ENCODINGS_NONE = "none";
86:
87:
91: public static final String ENCODINGS_DEFINITION_FILE
92: = "org.jfree.report.modules.gui.base.EncodingsFile";
93:
94:
98: public static final String ENCODINGS_DEFINITION_FILE_DEFAULT
99: = "org/jfree/report/modules/gui/swing/common/jfreereport-encodings.properties";
100:
101:
102:
105: private static class EncodingCarrierComparator implements Comparator
106: {
107: private EncodingCarrierComparator ()
108: {
109: }
110:
111:
124: public int compare (final Object o1, final Object o2)
125: {
126: final EncodingCarrier e1 = (EncodingCarrier) o1;
127: final EncodingCarrier e2 = (EncodingCarrier) o2;
128: return e1.getName().toLowerCase().compareTo(e2.getName().toLowerCase());
129: }
130:
131:
138: public boolean equals (final Object o)
139: {
140: if (o == null)
141: {
142: return false;
143: }
144: return getClass().equals(o.getClass());
145: }
146:
147:
152: public int hashCode ()
153: {
154: return getClass().hashCode();
155: }
156: }
157:
158:
161: private static class EncodingCarrier
162: {
163:
166: private String name;
167:
168:
171: private String description;
172:
173:
176: private String displayName;
177:
178:
184: private EncodingCarrier (final String name, final String description)
185: {
186: if (name == null)
187: {
188: throw new NullPointerException();
189: }
190: this.name = name;
191: this.description = description;
192: final StringBuffer dName = new StringBuffer();
193: dName.append(name);
194: dName.append(" (");
195: dName.append(description);
196: dName.append(")");
197: this.displayName = dName.toString();
198: }
199:
200:
205: public String getName ()
206: {
207: return name;
208: }
209:
210:
215: public String getDescription ()
216: {
217: return description;
218: }
219:
220:
226: public String getDisplayName ()
227: {
228: return displayName;
229: }
230:
231:
238: public boolean equals (final Object o)
239: {
240: if (this == o)
241: {
242: return true;
243: }
244: if (!(o instanceof EncodingCarrier))
245: {
246: return false;
247: }
248:
249: final EncodingCarrier carrier = (EncodingCarrier) o;
250:
251: if (!name.equalsIgnoreCase(carrier.name))
252: {
253: return false;
254: }
255:
256: return true;
257: }
258:
259:
264: public int hashCode ()
265: {
266: return name.hashCode();
267: }
268: }
269:
270:
273: private final ArrayList encodings;
274:
275:
278: private ArrayList listDataListeners;
279:
280:
283: private int selectedIndex;
284:
285:
288: private Object selectedObject;
289:
290: private ResourceBundle bundle;
291: public static final String BUNDLE_NAME = "org.jfree.report.modules.gui.swing.common.encoding-names";
292:
293:
297: public EncodingComboBoxModel(final Locale locale)
298: {
299: bundle = ResourceBundle.getBundle(BUNDLE_NAME, locale);
300: encodings = new ArrayList();
301: listDataListeners = null;
302: selectedIndex = -1;
303: }
304:
305:
313: public boolean addEncoding (final String name, final String description)
314: {
315: if (EncodingRegistry.getInstance().isSupportedEncoding(name))
316: {
317: encodings.add(new EncodingCarrier(name, description));
318: }
319: else
320: {
321: return false;
322: }
323:
324: fireContentsChanged();
325: return true;
326: }
327:
328:
334: public void addEncodingUnchecked (final String name, final String description)
335: {
336: encodings.add(new EncodingCarrier(name, description));
337: fireContentsChanged();
338: }
339:
340: public void removeEncoding (final String name)
341: {
342: if (encodings.remove(name))
343: {
344: fireContentsChanged();
345: }
346: }
347:
348:
353: public void ensureEncodingAvailable (final String encoding)
354: {
355: if (encoding == null)
356: {
357: throw new NullPointerException("Encoding must not be null");
358: }
359: final String desc = getEncodingDescription(encoding);
360: final EncodingCarrier ec = new EncodingCarrier(encoding, desc);
361: if (encodings.contains(ec) == false)
362: {
363: encodings.add(ec);
364: fireContentsChanged();
365: }
366: }
367:
368: protected String getEncodingDescription (final String encoding)
369: {
370: try
371: {
372: return bundle.getString(encoding);
373: }
374: catch(Exception e)
375: {
376: return ENCODING_DEFAULT_DESCRIPTION;
377: }
378: }
379:
380:
383: public void sort ()
384: {
385: final Object selectedObject = getSelectedItem();
386: Collections.sort(encodings, new EncodingCarrierComparator());
387: setSelectedItem(selectedObject);
388: fireContentsChanged();
389: }
390:
391:
394: protected void fireContentsChanged ()
395: {
396: if (listDataListeners == null)
397: {
398: return;
399: }
400: fireContentsChanged(0, getSize());
401: }
402:
403:
406: protected void fireContentsChanged (final int start, final int length)
407: {
408: if (listDataListeners == null)
409: {
410: return;
411: }
412: final ListDataEvent evt = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, start, length);
413: for (int i = 0; i < listDataListeners.size(); i++)
414: {
415: final ListDataListener l = (ListDataListener) listDataListeners.get(i);
416: l.contentsChanged(evt);
417: }
418: }
419:
420:
426: public void setSelectedItem (final Object anItem)
427: {
428: selectedObject = anItem;
429: if (anItem instanceof String)
430: {
431: final int size = getSize();
432: for (int i = 0; i < size; i++)
433: {
434: if (anItem.equals(getElementAt(i)))
435: {
436: selectedIndex = i;
437: fireContentsChanged(-1, -1);
438: return;
439: }
440: }
441: }
442: selectedIndex = -1;
443: fireContentsChanged(-1, -1);
444: }
445:
446:
451: public int getSelectedIndex ()
452: {
453: return selectedIndex;
454: }
455:
456:
463: public void setSelectedIndex (final int index)
464: {
465: if (index == -1)
466: {
467: selectedIndex = -1;
468: selectedObject = null;
469: fireContentsChanged(-1, -1);
470: return;
471: }
472: if (index < -1 || index >= getSize())
473: {
474: throw new IllegalArgumentException("Index is invalid.");
475: }
476: selectedIndex = index;
477: selectedObject = getElementAt(index);
478: fireContentsChanged(-1, -1);
479: }
480:
481:
486: public String getSelectedEncoding ()
487: {
488: if (selectedIndex == -1)
489: {
490: return null;
491: }
492: final EncodingCarrier ec = (EncodingCarrier) encodings.get(selectedIndex);
493: return ec.getName();
494: }
495:
496:
501: public Object getSelectedItem ()
502: {
503: return selectedObject;
504: }
505:
506:
511: public int getSize ()
512: {
513: return encodings.size();
514: }
515:
516:
522: public Object getElementAt (final int index)
523: {
524: final EncodingCarrier ec = (EncodingCarrier) encodings.get(index);
525: return ec.getDisplayName();
526: }
527:
528:
534: public void addListDataListener (final ListDataListener l)
535: {
536: if (listDataListeners == null)
537: {
538: listDataListeners = new ArrayList(5);
539: }
540: listDataListeners.add(l);
541: }
542:
543:
549: public void removeListDataListener (final ListDataListener l)
550: {
551: if (listDataListeners == null)
552: {
553: return;
554: }
555: listDataListeners.remove(l);
556: }
557:
558:
563: public static EncodingComboBoxModel createDefaultModel (final Locale locale)
564: {
565: final EncodingComboBoxModel ecb = new EncodingComboBoxModel(locale);
566:
567: final String availEncs = getAvailableEncodings();
568: final boolean allEncodings =
569: availEncs.equalsIgnoreCase(AVAILABLE_ENCODINGS_ALL);
570:
571: if (allEncodings || availEncs.equals(AVAILABLE_ENCODINGS_FILE))
572: {
573: final String encFile = getEncodingsDefinitionFile();
574: final InputStream in = ObjectUtilities.getResourceAsStream
575: (encFile, EncodingComboBoxModel.class);
576: if (in == null)
577: {
578: Log.warn(new Log.SimpleMessage
579: ("The specified encodings definition file was not found: ", encFile));
580: }
581: else
582: {
583: try
584: {
585:
586: final Properties encDef = new Properties();
587: final BufferedInputStream bin = new BufferedInputStream(in);
588: encDef.load(bin);
589: bin.close();
590: final Enumeration en = encDef.keys();
591: while (en.hasMoreElements())
592: {
593: final String enc = (String) en.nextElement();
594:
595: if ("true".equalsIgnoreCase(encDef.getProperty(enc, "false")))
596: {
597:
598: ecb.addEncoding (enc, ecb.getEncodingDescription(enc));
599: }
600: }
601: }
602: catch (IOException e)
603: {
604: Log.warn(new Log.SimpleMessage
605: ("There was an error while reading the encodings definition file: ", encFile), e);
606: }
607: }
608: }
609: return ecb;
610: }
611:
612:
618: public int indexOf (final String encoding)
619: {
620: return encodings.indexOf(new EncodingCarrier(encoding, null));
621: }
622:
623:
629: public String getEncoding (final int index)
630: {
631: final EncodingCarrier ec = (EncodingCarrier) encodings.get(index);
632: return ec.getName();
633: }
634:
635:
641: public String getDescription (final int index)
642: {
643: final EncodingCarrier ec = (EncodingCarrier) encodings.get(index);
644: return ec.getDescription();
645: }
646:
647:
648:
655: public static String getEncodingsDefinitionFile ()
656: {
657: return JFreeReportBoot.getInstance().getGlobalConfig().getConfigProperty
658: (ENCODINGS_DEFINITION_FILE, ENCODINGS_DEFINITION_FILE_DEFAULT);
659: }
660:
661:
662:
669: public static String getAvailableEncodings ()
670: {
671: return JFreeReportBoot.getInstance().getGlobalConfig().getConfigProperty
672: (AVAILABLE_ENCODINGS, AVAILABLE_ENCODINGS_ALL);
673: }
674:
675: public void setSelectedEncoding (final String encoding)
676: {
677: if (encoding == null)
678: {
679: throw new NullPointerException();
680: }
681:
682: final int size = encodings.size();
683: for (int i = 0; i < size; i++)
684: {
685: final EncodingCarrier carrier = (EncodingCarrier) encodings.get(i);
686: if (encoding.equals(carrier.getName()))
687: {
688: selectedIndex = i;
689: selectedObject = carrier.getDisplayName();
690: fireContentsChanged(-1, -1);
691: return;
692: }
693: }
694:
695: if (size > 0)
696: {
697: selectedIndex = 0;
698: selectedObject = getElementAt(0);
699: fireContentsChanged(-1, -1);
700: }
701: }
702: }