core/com.nokia.carbide.discovery.ui/src/com/nokia/carbide/internal/discovery/ui/editor/PortalEditor.java
changeset 1734 bf670d38123a
parent 1721 18638adda547
child 1765 634744453c29
equal deleted inserted replaced
1729:f77f2721ad5a 1734:bf670d38123a
    52 import org.eclipse.ui.part.EditorPart;
    52 import org.eclipse.ui.part.EditorPart;
    53 import org.eclipse.ui.plugin.AbstractUIPlugin;
    53 import org.eclipse.ui.plugin.AbstractUIPlugin;
    54 
    54 
    55 import com.nokia.carbide.discovery.ui.Activator;
    55 import com.nokia.carbide.discovery.ui.Activator;
    56 import com.nokia.carbide.discovery.ui.Messages;
    56 import com.nokia.carbide.discovery.ui.Messages;
       
    57 import com.nokia.carbide.internal.discovery.ui.extension.ICommandBarFactory;
    57 import com.nokia.carbide.internal.discovery.ui.extension.IPortalEditor;
    58 import com.nokia.carbide.internal.discovery.ui.extension.IPortalEditor;
    58 import com.nokia.carbide.internal.discovery.ui.extension.IPortalPageLayer;
    59 import com.nokia.carbide.internal.discovery.ui.extension.IPortalPageLayer;
    59 import com.nokia.cpp.internal.api.utils.core.Pair;
    60 import com.nokia.cpp.internal.api.utils.core.Pair;
    60 import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils;
    61 import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils;
    61 
    62 
    62 public class PortalEditor extends EditorPart implements IPortalEditor {
    63 public class PortalEditor extends EditorPart implements IPortalEditor {
       
    64 	
       
    65 	class LayerExtension {
       
    66 		public LayerExtension(IPortalPageLayer layer, String title, int order) {
       
    67 			this.layer = layer;
       
    68 			this.title = title;
       
    69 			this.order = order;
       
    70 		}
       
    71 		public IPortalPageLayer layer;
       
    72 		public String title;
       
    73 		public int order;
       
    74 	}
    63 
    75 
    64 	private static final String ID = "com.nokia.carbide.discovery.ui.portalEditor"; //$NON-NLS-1$
    76 	private static final String ID = "com.nokia.carbide.discovery.ui.portalEditor"; //$NON-NLS-1$
    65 	private static final String CONTEXT_ID = ID + ".context"; //$NON-NLS-1$
    77 	private static final String CONTEXT_ID = ID + ".context"; //$NON-NLS-1$
    66 	private static IEditorInput input;
    78 	private static IEditorInput input;
    67 	private List<PortalPage> pages;
    79 	private List<PortalPage> pages;
    74 	public PortalEditor() {
    86 	public PortalEditor() {
    75 		resources = new ArrayList<Resource>();
    87 		resources = new ArrayList<Resource>();
    76 		loadPortalPages();
    88 		loadPortalPages();
    77 	}
    89 	}
    78 	
    90 	
    79 	private Map<String, List<Pair<IPortalPageLayer, String>>> loadPortalLayers() {
    91 	private Map<String, List<LayerExtension>> loadPortalLayers() {
    80 		Map<String, List<Pair<IPortalPageLayer, String>>> pageIdToExtensionsMap = 
    92 		Map<String, List<LayerExtension>> pageIdToExtensionsMap = 
    81 			new HashMap<String, List<Pair<IPortalPageLayer, String>>>();
    93 			new HashMap<String, List<LayerExtension>>();
    82 		IConfigurationElement[] elements = 
    94 		IConfigurationElement[] elements = 
    83 			Platform.getExtensionRegistry().getConfigurationElementsFor(Activator.PLUGIN_ID + ".portalPageLayer"); //$NON-NLS-1$
    95 			Platform.getExtensionRegistry().getConfigurationElementsFor(Activator.PLUGIN_ID + ".portalPageLayer"); //$NON-NLS-1$
    84 		for (IConfigurationElement element : elements) {
    96 		for (IConfigurationElement element : elements) {
    85 			String pageId = element.getAttribute("pageId"); //$NON-NLS-1$
    97 			String pageId = element.getAttribute("pageId"); //$NON-NLS-1$
    86 			String title = element.getAttribute("title"); //$NON-NLS-1$
    98 			String title = element.getAttribute("title"); //$NON-NLS-1$
       
    99 			int order = getOrderStringFromElement(element, title);
    87 			try {
   100 			try {
    88 				IPortalPageLayer extension = (IPortalPageLayer) element.createExecutableExtension("class"); //$NON-NLS-1$
   101 				IPortalPageLayer extension = (IPortalPageLayer) element.createExecutableExtension("class"); //$NON-NLS-1$
    89 				if (!pageIdToExtensionsMap.containsKey(pageId))
   102 				if (!pageIdToExtensionsMap.containsKey(pageId))
    90 					pageIdToExtensionsMap.put(pageId, new ArrayList<Pair<IPortalPageLayer, String>>());
   103 					pageIdToExtensionsMap.put(pageId, new ArrayList<LayerExtension>());
    91 				pageIdToExtensionsMap.get(pageId).add(new Pair<IPortalPageLayer, String>(extension, title));
   104 				pageIdToExtensionsMap.get(pageId).add(new LayerExtension(extension, title, order));
    92 			} catch (CoreException e) {
   105 			} catch (CoreException e) {
    93 				Activator.logError(MessageFormat.format(Messages.PortalEditor_PageLoadError, pageId), e);
   106 				Activator.logError(MessageFormat.format(Messages.PortalEditor_PageLoadError, pageId), e);
    94 			}
   107 			}
    95 		}
   108 		}
    96 		return pageIdToExtensionsMap;
   109 		return pageIdToExtensionsMap;
    97 	}
   110 	}
    98 
   111 
    99 	private void loadPortalPages() {
   112 	private void loadPortalPages() {
   100 		Map<String, List<Pair<IPortalPageLayer, String>>> portalLayersMap = loadPortalLayers();
   113 		Map<String, List<LayerExtension>> portalLayersMap = loadPortalLayers();
   101 		List<Pair<PortalPage, Integer>> pageList = new ArrayList<Pair<PortalPage, Integer>>();
   114 		List<Pair<PortalPage, Integer>> pageList = new ArrayList<Pair<PortalPage, Integer>>();
   102 		IConfigurationElement[] elements = 
   115 		IConfigurationElement[] elements = 
   103 			Platform.getExtensionRegistry().getConfigurationElementsFor(Activator.PLUGIN_ID + ".portalPage"); //$NON-NLS-1$
   116 			Platform.getExtensionRegistry().getConfigurationElementsFor(Activator.PLUGIN_ID + ".portalPage"); //$NON-NLS-1$
   104 		for (IConfigurationElement element : elements) {
   117 		for (IConfigurationElement element : elements) {
   105 			String id = element.getAttribute("id"); //$NON-NLS-1$
   118 			String id = element.getAttribute("id"); //$NON-NLS-1$
   106 			int order = Integer.MAX_VALUE;
   119 			int order = getOrderStringFromElement(element, id);
   107 			String orderString = element.getAttribute("order"); //$NON-NLS-1$
       
   108 			if (orderString != null) {
       
   109 				try {
       
   110 					order = Integer.parseInt(orderString);
       
   111 				}
       
   112 				catch (NumberFormatException e) {
       
   113 					Activator.logError(MessageFormat.format(Messages.PortalEditor_PageRankError, id), e);
       
   114 				}
       
   115 			}
       
   116 			String title = element.getAttribute("title"); //$NON-NLS-1$
   120 			String title = element.getAttribute("title"); //$NON-NLS-1$
   117 			String imageFilePath = element.getAttribute("image"); //$NON-NLS-1$
   121 			String imageFilePath = element.getAttribute("image"); //$NON-NLS-1$
   118 			String pluginId = element.getContributor().getName();
   122 			String pluginId = element.getContributor().getName();
   119 			ImageDescriptor imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(pluginId, imageFilePath);
   123 			ImageDescriptor imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(pluginId, imageFilePath);
   120 			List<Pair<IPortalPageLayer, String>> portalLayers = portalLayersMap.get(id);
   124 			List<LayerExtension> portalLayers = portalLayersMap.get(id);
   121 			if (portalLayers == null || portalLayers.isEmpty()) {
   125 			if (portalLayers == null || portalLayers.isEmpty()) {
   122 				Activator.logError(MessageFormat.format(Messages.PortalEditor_NoLayersError, id), null);
   126 				Activator.logError(MessageFormat.format(Messages.PortalEditor_NoLayersError, id), null);
   123 			}
   127 			}
   124 			PortalPage portalPage = new PortalPage(title, imageDesc, id, portalLayers);
   128 			ICommandBarFactory commandBarFactory = null;
       
   129 			if (element.getAttribute("class") != null) { //$NON-NLS-1$
       
   130 				try {
       
   131 					commandBarFactory = (ICommandBarFactory) element.createExecutableExtension("class"); //$NON-NLS-1$
       
   132 				} catch (CoreException e) {
       
   133 					Activator.logError(MessageFormat.format(Messages.PortalEditor_BadCommandBarFactoryError, id), e);
       
   134 				}
       
   135 			}
       
   136 			PortalPage portalPage = new PortalPage(title, imageDesc, id, portalLayers, commandBarFactory);
   125 			pageList.add(new Pair<PortalPage, Integer>(portalPage, order));
   137 			pageList.add(new Pair<PortalPage, Integer>(portalPage, order));
   126 		}
   138 		}
   127 		Collections.sort(pageList, new Comparator<Pair<PortalPage, Integer>>() {
   139 		Collections.sort(pageList, new Comparator<Pair<PortalPage, Integer>>() {
   128 			@Override
   140 			@Override
   129 			public int compare(Pair<PortalPage, Integer> o1, Pair<PortalPage, Integer> o2) {
   141 			public int compare(Pair<PortalPage, Integer> o1, Pair<PortalPage, Integer> o2) {
   132 		});
   144 		});
   133 		pages = new ArrayList<PortalPage>();
   145 		pages = new ArrayList<PortalPage>();
   134 		for (Pair<PortalPage, Integer> pair : pageList) {
   146 		for (Pair<PortalPage, Integer> pair : pageList) {
   135 			pages.add(pair.first);
   147 			pages.add(pair.first);
   136 		}
   148 		}
       
   149 	}
       
   150 
       
   151 	private int getOrderStringFromElement(IConfigurationElement element, String id) {
       
   152 		int order = Integer.MAX_VALUE;
       
   153 		String orderString = element.getAttribute("order"); //$NON-NLS-1$
       
   154 		if (orderString != null) {
       
   155 			try {
       
   156 				order = Integer.parseInt(orderString);
       
   157 			}
       
   158 			catch (NumberFormatException e) {
       
   159 				Activator.logError(MessageFormat.format(Messages.PortalEditor_PageRankError, id), e);
       
   160 			}
       
   161 		}
       
   162 		return order;
   137 	}
   163 	}
   138 
   164 
   139 	@Override
   165 	@Override
   140 	public void doSave(IProgressMonitor monitor) {
   166 	public void doSave(IProgressMonitor monitor) {
   141 	}
   167 	}