eclipse 4插件rcp支持Css切换

eclipe 4以上的插件就支持CSS主题切换的功能

plugin.xml 中扩展点org.eclipse.core.runtime.products、org.eclipse.e4.ui.css.swt.theme

<extension
         id="product"
         point="org.eclipse.core.runtime.products">
      <product
            application="org.eclipse.e4.ui.examples.css.rcp.application"
            name="CSS Mail Example">
         <property
               name="aboutText"
               value="CSS RCP Mail template">
         </property>
         <property
               name="windowImages"
               value="icons/sample2.gif">
         </property>
         <property
               name="aboutImage"
               value="icons/alt_about.gif">
         </property>
         <property
               name="appName"
               value="CSS Mail Example">
         </property>
         <!--一开始指定css-->
         <span style="color:#ff0000;"><strong><!--<property
            name="applicationCSS"
            value="platform:/plugin/org.eclipse.e4.ui.examples.css.rcp/css/colorful.css">
      	</property>--></strong></span>
      </product>
   </extension>
   <extension
         point="org.eclipse.e4.ui.css.swt.theme">
      <!--basestylesheeturi 指明绑定的css的位置-->
      <theme
            basestylesheeturi="<strong><span style="color:#ff0000;">css/default.css</span></strong>"
            id="org.eclipse.e4.ui.examples.css.rcp"
            label="RCP example theme">
      </theme>
      <theme
            basestylesheeturi="<span style="color:#ff0000;"><strong>css/colorful.css</strong></span>"
            id="org.eclipse.e4.ui.examples.css.colorful"
            label="Colorful Example">
      </theme>
   </extension>

ThemeHelperl类用于生成主题引擎(用于不同css之间切换)

import org.eclipse.core.runtime.Platform;
import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;
import org.eclipse.e4.ui.css.swt.theme.IThemeManager;
import org.eclipse.e4.ui.examples.css.rcp.Activator;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
/**
 * 提供获得主题引擎
 * @author Administrator
 *
 */
@SuppressWarnings("restriction")
public class ThemeHelper {
	private static IThemeEngine engine = null;
	private static Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);
	public static IThemeEngine getEngine() {
		if (engine == null) {
			engine = getThemeEngine();
		}
		return engine;
	}
	private static IThemeEngine getThemeEngine() {
		BundleContext context = bundle.getBundleContext();
		ServiceReference ref = context.getServiceReference(IThemeManager.class.getName());
		IThemeManager manager = (IThemeManager) context.getService(ref);
		return manager.getEngineForDisplay(PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow() == null ? Display.getCurrent()
				: PlatformUI.getWorkbench().getActiveWorkbenchWindow()
						.getShell().getDisplay());
	}
}
用于切换主题的操作

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.e4.ui.examples.css.rcp.theme.ThemeHelper;

/**
 * 切换主题的实现类
 * @author Administrator
 *
 */
public class SwitchTheme extends AbstractHandler {
	private boolean reset = false;
	private final static String USER_CSS_DEFAULT = "org.eclipse.e4.ui.examples.css.rcp";//plugin.xml中的主题ID
	private final static String USER_CSS_COLORFUL = "org.eclipse.e4.ui.examples.css.colorful";
	public Object execute(ExecutionEvent event) throws ExecutionException {
		String themeId = "";
		if (reset) { // toggle functionality of the menu item
			themeId = USER_CSS_DEFAULT;
		} else {
			themeId = USER_CSS_COLORFUL;
		}
		reset = !reset;
		ThemeHelper.getEngine().setTheme(themeId, true);
		return null;
	}

}
Plugin.xml中设置的command

<extension
         point="org.eclipse.ui.commands"><!--用户添加菜单项到相应的位置-->
     
      <command
            defaultHandler="org.eclipse.e4.ui.examples.css.rcp.handler.SwitchTheme"
            id="org.eclipse.e4.ui.examples.css.rcp.switchtheme"
            name="Switch Theme"><!--切换主题的操作-->
      </command>
   </extension><extension
         point="org.eclipse.ui.menus">
      <menuContribution
            allPopups="false"
            locationURI="menu:org.eclipse.ui.main.menu">
         <menu
               label="Styling">
            <command
                  commandId="org.eclipse.e4.ui.examples.css.rcp.switchtheme"
                  label="Switch Styling"
                  style="push">
            </command>
         </menu>
      </menuContribution>
   </extension>
把command绑定到菜单栏中
<extension
         point="org.eclipse.ui.menus">
      <menuContribution
            allPopups="false"
            locationURI="menu:org.eclipse.ui.main.menu">
         <menu
               label="Styling">
            <command
                  commandId="org.eclipse.e4.ui.examples.css.rcp.switchtheme"
                  label="Switch Styling"
                  style="push">
            </command>
         </menu>
      </menuContribution>
   </extension>
ApplicationWorkbenchAdvisor  中可以用代码一开始指定某个主题

public void initialize(IWorkbenchConfigurer configurer) {
		super.initialize(configurer);
		
		/**设置系统的默认主题风格*/
		Bundle b = FrameworkUtil.getBundle(getClass());
		BundleContext context = b.getBundleContext();
		ServiceReference serviceRef = context
				.getServiceReference(IThemeManager.class.getName());
		IThemeManager themeManager = (IThemeManager) context
				.getService(serviceRef);
		final IThemeEngine engine = themeManager.getEngineForDisplay(Display
				.getCurrent());
		engine.setTheme("org.eclipse.e4.ui.examples.css.rcp", true);
		if (serviceRef != null) {
			serviceRef = null;
		}
		if (themeManager != null) {
			themeManager = null;
		}
	}
css 样式colorful.css

CTabItem, Label, Tree, Text {
	font-family: "Arial";
}
	
Shell {
	font-size: 12;
}
CTabItem, ToolBar, Button, CBanner, CoolBar {
	font-size: 9;
	background-color: green;
}
CTabFolder, CTabItem {
    background-color: red;
}
CTabItem:selected {
    background-color: blue;
}
Text {
	font-size: 11;
	color: #666666;
	background-color: red;
}
#navigation {
	font-size: 8;
	color: orange;
	background-color: blue;
}
.messageBanner {
    background-color: yellow;
}
.messageBanner Composite, Label, Link {
    background-color: pink;
}
.messageSender {
	color: #3e9cfa;
	font-size: 11;
}
.messageDate {
	color: #808080;
	font-size: 9;
}
.messageSubjectUnRead {
    font-size: 14;
	font-style: italic;
    font-weight: bold;
	color: red;
}
.messageSubjectRead {
    font-size: 12;
	font-style: normal;
    font-weight: normal;
	color: green;
}

工程下载:http://download.csdn.net/detail/luoww1/7425369








eclipse 4插件rcp支持Css切换,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。