Eclipse 4 Plug-in RCP supports CSS switching

Source: Internet
Author: User
Tags mail example

Eclipe more than 4 plug-in support for CSS theme switching features

Plugin.xml extension points org.eclipse.core.runtime.products, org.eclipse.e4.ui.css.swt.theme

<extension id= "Product" point= "org.eclipse.core.runtime.products" > <product appli               cation= "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> <prope Rty name= "AppName" value= "CSS Mail Example" > </property> <!--start specifying css--> <span style= "color: #ff0000;" ><strong><!--<property name= "applicationcss" value= "platform:/plugin/org.eclipse.e4.u I.examples.css.rcp/css/colorful.css "> </property>--></strong></span>      </product> </extension> <extension point= "Org.eclipse.e4.ui.css.swt.theme" > <! --basestylesheeturi indicates the location of the bound CSS--<theme basestylesheeturi= "<strong><span style=" color: #ff 0000; " >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>

The Themehelperl class is used to generate the theme engine (for switching between different 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;/** * Provides access to theme engine * @author Administrator * * /@SuppressWarnings ("Restriction") public class Themehelper {private static Ithemeengine engine = Null;private static Bundle bundle = Platform.getbundle (activator.plugin_id);p ublic static Ithemeengine getengine () {if (engine = = null) {Engi NE = 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 ());}}
Actions for switching themes

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;/** * Toggle Theme Implementation Class * @author Administrator * */public class Switchtheme extends Abstracthandler {private Boolean reset = False;priva Te final static String User_css_default = "ORG.ECLIPSE.E4.UI.EXAMPLES.CSS.RCP"; topic idprivate final static in//plugin.xml String user_css_colorful = "org.eclipse.e4.ui.examples.css.colorful";p ublic Object Execute (executionevent event) Throws Executionexception {String Themeid = "", if (reset) {//Toggle functionality of the menu Itemthemeid = User_css_def Ault;} else {Themeid = user_css_colorful;} reset =!reset; Themehelper.getengine (). SetTheme (Themeid, true); return null;}}
Command set in Plugin.xml

<extension point= "org.eclipse.ui.commands" ><!--User Add menu item to the appropriate location--<command Defau Lthandler= "Org.eclipse.e4.ui.examples.css.rcp.handler.SwitchTheme" id= "ORG.ECLIPSE.E4.UI.EXAMPLES.CSS.RCP.SWITC Htheme "name=" switch Theme ><!--switch Themes-</command> </extension><extension&nbs P        point= "Org.eclipse.ui.menus" >      <menuContribution            allpopups= "false"             locationuri= "Menu:o Rg.eclipse.ui.main.menu >         <menu              &N Bsp;label= "Styling" >            <command            &NBS P     commandid= "Org.eclipse.e4.ui.examples.css.rcp.switchtheme"                   label= "Switch styling"   &nbsp               style= "push" >            &LT;/COMMAND&GT ;         </menu>      </menuContribution>   </ Extension>
Bind the command to the menu bar
<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 You can specify a topic from the beginning of the code

public void Initialize (Iworkbenchconfigurer configurer) {super.initialize (configurer);/** set default theme style for system */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 style 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;}

Project Download: http://download.csdn.net/detail/luoww1/7425369








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.