core/com.nokia.carbide.discovery.ui/src/com/nokia/carbide/internal/discovery/ui/editor/TaskBar.java
changeset 1609 085da1889c59
child 1615 d5f384bf1e88
equal deleted inserted replaced
1608:231c47d08fe4 1609:085da1889c59
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 package com.nokia.carbide.internal.discovery.ui.editor;
       
    18 
       
    19 import java.util.HashMap;
       
    20 import java.util.Map;
       
    21 import java.util.Map.Entry;
       
    22 
       
    23 import org.eclipse.jface.action.IAction;
       
    24 import org.eclipse.jface.layout.GridDataFactory;
       
    25 import org.eclipse.jface.layout.RowDataFactory;
       
    26 import org.eclipse.jface.layout.RowLayoutFactory;
       
    27 import org.eclipse.swt.SWT;
       
    28 import org.eclipse.swt.widgets.Composite;
       
    29 import org.eclipse.swt.widgets.Label;
       
    30 import org.eclipse.ui.forms.events.HyperlinkEvent;
       
    31 import org.eclipse.ui.forms.events.IHyperlinkListener;
       
    32 import org.eclipse.ui.forms.widgets.Hyperlink;
       
    33 
       
    34 import com.nokia.carbide.internal.discovery.ui.extension.IPortalPage.IActionBar;
       
    35 
       
    36 class TaskBar extends RoundedCornerComposite {
       
    37 
       
    38 	private final class ActionListener implements IHyperlinkListener {
       
    39 		@Override
       
    40 		public void linkActivated(HyperlinkEvent e) {
       
    41 			Hyperlink link = (Hyperlink) e.getSource();
       
    42 			runAction(link);
       
    43 		}
       
    44 		
       
    45 		@Override
       
    46 		public void linkEntered(HyperlinkEvent e) {
       
    47 			Hyperlink link = (Hyperlink) e.getSource();
       
    48 			link.setUnderlined(true);
       
    49 		}
       
    50 		
       
    51 		@Override
       
    52 		public void linkExited(HyperlinkEvent e) {
       
    53 			Hyperlink link = (Hyperlink) e.getSource();
       
    54 			link.setUnderlined(false);
       
    55 		}
       
    56 	}
       
    57 
       
    58 	private PortalEditor portalEditor;
       
    59 	private Map<Hyperlink, IAction> linkToActionMap;
       
    60 	private ActionListener listener;
       
    61 
       
    62 	public TaskBar(Composite parent, PortalEditor portalEditor, IActionBar actionBar) {
       
    63 		super(parent, portalEditor.getBackgroundParent(), 
       
    64 				parent.getDisplay().getSystemColor(SWT.COLOR_BLACK),
       
    65 				parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
       
    66 		this.portalEditor = portalEditor;
       
    67 		createTitle(actionBar);
       
    68 		createActions(actionBar);
       
    69 		setLayoutData(GridDataFactory.swtDefaults().grab(true, true).align(SWT.CENTER, SWT.BEGINNING).create());
       
    70 		setLayout(RowLayoutFactory.swtDefaults().type(SWT.VERTICAL).margins(10, 10).extendedMargins(5, 5, 5, 10).fill(true).wrap(false).create());
       
    71 	}
       
    72 
       
    73 	private void createTitle(IActionBar actionBar) {
       
    74 		String title = actionBar.getTitle();
       
    75 		if (title != null) {
       
    76 			Label l = new Label(this, SWT.LEFT);
       
    77 			l.setFont(portalEditor.createFont("Arial", 10, SWT.BOLD));
       
    78 			l.setBackground(l.getDisplay().getSystemColor(SWT.COLOR_WHITE));
       
    79 			l.setText(title);
       
    80 			l.setLayoutData(RowDataFactory.swtDefaults().create());
       
    81 			l = new Label(this, SWT.HORIZONTAL | SWT.SEPARATOR);
       
    82 			l.setLayoutData(RowDataFactory.swtDefaults().create());
       
    83 		}
       
    84 	}
       
    85 
       
    86 	private void createActions(IActionBar actionBar) {
       
    87 		listener = new ActionListener();
       
    88 		linkToActionMap = new HashMap<Hyperlink, IAction>();
       
    89 		for (IAction action : actionBar.getActions()) {
       
    90 			Hyperlink link = new Hyperlink(this, SWT.NONE);
       
    91 			link.setText(action.getText());
       
    92 			String toolTipText = action.getToolTipText();
       
    93 			if (toolTipText != null)
       
    94 				link.setToolTipText(toolTipText);
       
    95 			link.setBackground(link.getDisplay().getSystemColor(SWT.COLOR_WHITE));
       
    96 			linkToActionMap.put(link, action);
       
    97 			link.addHyperlinkListener(listener);
       
    98 		}
       
    99 //		updateAllActionsUI();
       
   100 	}
       
   101 	
       
   102 	public void runAction(Hyperlink link) {
       
   103 		IAction action = linkToActionMap.get(link);
       
   104 		action.run();
       
   105 	}
       
   106 
       
   107 	public void updateActionUI(String actionId) {
       
   108 		for (Entry<Hyperlink, IAction> entry : linkToActionMap.entrySet()) {
       
   109 			IAction action = entry.getValue();
       
   110 			if (actionId.equals(action.getId())) {
       
   111 				entry.getKey().setEnabled(action.isEnabled());
       
   112 			}
       
   113 		}
       
   114 	}
       
   115 
       
   116 	public void updateAllActionsUI() {
       
   117 		for (Entry<Hyperlink, IAction> entry : linkToActionMap.entrySet()) {
       
   118 			entry.getKey().setEnabled(entry.getValue().isEnabled());
       
   119 		}
       
   120 	}
       
   121 
       
   122 }