001    package org.openstreetmap.gui.jmapviewer;
002    
003    //License: GPL.
004    
005    import java.awt.Desktop;
006    import java.io.IOException;
007    import java.net.URI;
008    import java.net.URISyntaxException;
009    import java.text.MessageFormat;
010    
011    public class FeatureAdapter {
012    
013        public static interface BrowserAdapter {
014            void openLink(String url);
015        }
016    
017        public static interface TranslationAdapter {
018            String tr(String text, Object... objects);
019            // TODO: more i18n functions
020        }
021    
022        private static BrowserAdapter browserAdapter = new DefaultBrowserAdapter();
023        private static TranslationAdapter translationAdapter = new DefaultTranslationAdapter();
024    
025        public static void registerBrowserAdapter(BrowserAdapter browserAdapter) {
026            FeatureAdapter.browserAdapter = browserAdapter;
027        }
028    
029        public static void registerTranslationAdapter(TranslationAdapter translationAdapter) {
030            FeatureAdapter.translationAdapter = translationAdapter;
031        }
032    
033        public static void openLink(String url) {
034            browserAdapter.openLink(url);
035        }
036    
037        public static String tr(String text, Object... objects) {
038            return translationAdapter.tr(text, objects);
039        }
040    
041        public static class DefaultBrowserAdapter implements BrowserAdapter {
042            @Override
043            public void openLink(String url) {
044                if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
045                    try {
046                        Desktop.getDesktop().browse(new URI(url));
047                    } catch (IOException e) {
048                        e.printStackTrace();
049                    } catch (URISyntaxException e) {
050                        e.printStackTrace();
051                    }
052                } else {
053                    System.err.println(tr("Opening link not supported on current platform (''{0}'')", url));
054                }
055            }
056        }
057    
058        public static class DefaultTranslationAdapter implements TranslationAdapter {
059            @Override
060            public String tr(String text, Object... objects) {
061                return MessageFormat.format(text, objects);
062            }
063        }
064    }