Eclipse Plug-in development

Source: Internet
Author: User
Tags all mail mail account

1.Eclipse Introduction and plug-in development

Eclipse is a fascinating development environment that provides a core framework and extensible plug-in mechanism that provides unlimited imagination and creative space for a wide range of programmers. There is a rich and comprehensive set of plug-ins for development tools on the Web, but eclipse has gone beyond the concept of a development environment, and you can imagine eclipse as the future integrated desktop environment. The current Eclipse itself has the capabilities of resource management and external programs, plus the ability to plug-ins that make up a colorful work environment and not just an IDE. For programmers, there is nothing more important than a customizable work environment that you can customize, with your determination, courage and creativity sweeping through the process of sharing results with others. Well, you are not heart, if you have a certain understanding of Eclipse, then, with me to create their own personalized work environment, first we develop a weather report plug-in, and then we create their own mail fast monitoring function.

The following work is based on the premise that you are a Java programmer, that you appreciate and are starting to use Eclipse's cool work environment, and don't forget to download the latest version of Eclipse3.0, based on Eclipse3.0 development.

Back to top of page

2. Weather Report Plugin

If you're tired of always having to sign in to some websites to get information from related pages, here's a new way to get information quickly through Eclipse. Let's start from scratch, make a weather widget of your own, and your Eclipse will have a weather forecast feature, isn't it cool?

In this section, we're going to implement an Eclipse plugin that he can tell us about the region's weather forecast by simply clicking the mouse at any time we want to know, which is certainly exciting. It should be the case for a programmer. Let's start by defining a plugin and adding it to the menus and toolbars. For no plug-in development experience, you can refer to the "Development Eclipse plug-in", set up a basic plug-in development awareness, of course, this article will be detailed to assist you to complete this creative work.

2.1 The most basic plug-in

You can fully refer to the plug-in example of the development Eclipse plugin to make your first Hello Eclipse plugin, fortunately, Eclipse3.0 fully considering your needs, via menu File, new-> other->plug-i N Project, enter the project name, next appears dialog box, just enter "Muplugin" in the plugin name, next select "Hello,world" plug-in template you can create a direct new name Myplugin The simplest plug-in, But in fact, our weather forecast is not much more complicated, after the completion of the plug-in after the effect of the following figure.

Now, run the project as a run-time Workbench (Run-run as Runtime Workbench), in a new Eclipse window, by tapping the menu sample Action or the Circle Eclipse diagram in the toolbar You will see a dialog box with the following effects.

So far, the original version of the weather Report plug-in has been made, by modifying the Plugin.xml, we will change the menu to Chinese form, the need to modify 2 places, see the table.

               <actionset 
            label= "Sample Action Set"
            visible= "true"
            id= "Myplugin.actionset" > 
         <menu 
               Label= "My Space"
               id= "Samplemenu" > 
            <separator 
                  name= "Samplegroup" > 
            </separator> 
         </menu> 
         <action 
               label= "Weather forecast"
               icon= "icons/sample.gif"
               class= " Myplugin.actions.SampleAction "
               tooltip=" Hello, Eclipse World "
               menubarpath=" Samplemenu/samplegroup "
               toolbarpath= "Samplegroup"
               id= "myplugin.actions.SampleAction" > 
         </action> 
        

At this point in the run-time Workbench, our menu has changed.

2.2 Making the Weather Forecast dialog box with Visualediter

Although the menu is the weather forecast, but what we need is not the Hello Eclispe dialog box, we need to tell us the Weather dialog box, of course, we need to start from scratch, so we need to rebuild a dialog box, which requires Visual Editor to help the development of the interface (about Visual Editer References "Build GUIs with the Eclipse Visual Editor project"). We will use Visual Editor to implement a Swing dialog box, but it is a bit overqualified to make a dialog box with VE, but as a starting point, it is appropriate.

First refer to "Build GUIs with the Eclipse Visual Editor project" to build the Visual editer development environment, when everything is ready, right-click on the Packgeexpoler in the "Muplugin.ac tions "Java file, choose New->other->visualclass from the pop-up menu, create a new visual class, the popup interface as shown below:

Select Next and enter Weatherdialog in name, which is the dialog we use to display the weather forecast.

Select the Super class for this dialog box to JAVAX.SWING.JDIAOG, click the Finish button. After waiting for a period of time, our dialog box is basically generated, the mouse click on the upper left corner of the icon, directly enter the weather forecast is the dialog box title, and we can see the left side of the Visualeditor panel.

Then we'll find the sampleaction run function in the dialog box connected to the weather forecast menu, as follows:

		 public void Run (IAction action) { 
		 messagedialog.openinformation ( 
			 Window.getshell (), 
			"Myplugin plug-in", 
			"Hello, Eclipse World"); 
	 } 
	

Replace with the following code

    	 public void Run (iaction action) 
    { 
        weatherdialog wd=new weatherdialog (); 
        Wd.setsize (335); 
        Wd.show ();        
    } 
   

At this point, click on the menu to run, our dialog box looks like this, based on which we will add weather information

2.3 Added weather forecast function

The following section is focused on using swing component Jeditpane with parsing Html to get ready-made weather forecasts on the network, and from the Visualeditor panel, click Jeditpane from the Swing Components section of the chart above, plus into the dialog box. and modify the dialog box code so that the final code is as follows:

 /* * Created on 2004-9-23 * */package myplugin; 
 Import Java.io.BufferedReader; 
 Import Java.io.InputStreamReader; 
 Import Java.net.URL; 
 Import Javax.swing.JDialog; 
 Import Javax.swing.JEditorPane; /** * <p>Title:WatherDialog</p> * <p>description: This is a dialog class that shows the weather forecast for the day of the specified city </p> * <p>c  Opyright:copyright (c) 2004</p> * <p>company:uf soft</p> * @author Zhao Yong * @version 1.0 */Public 
    
    Class Watherdialog extends JDialog {String city= "Beijing"; 
    Private JEditorPane jeditorpane = null; 
        /** * This method initializes */Public Watherdialog (String city) {super (); 
        this.city=city; 
    Initialize (); 
        }/** * This method initializes this * @return void */private void Initialize () { 
        This.setcontentpane (Getjeditorpane ()); try {//Build URL object url url = new URL ("Http://weather.neWs.sina.com.cn//cgi-bin/figureweather/simplesearch.cgi?city= "+city); 
            String temp= "";
            BufferedReader in = new BufferedReader (New InputStreamReader (Url.openstream ())); 
            Use OpenStream to get an input stream and construct a BufferedReader object by this String inputline; 
            Reads data continuously from the input stream until the while ((Inputline = In.readline ())! = null) temp=temp+inputline+ "\ n";  
            Close the input stream in.close (); String Weather =temp.substring (Temp.indexof ("<body"), temp 
            
            . LastIndexOf ("body>") +5); 
        This.jeditorpane. SetText (weather); 
        } catch (Exception e) {e.printstacktrace (); 
        } this.settitle ("Weather Forecast"); 
        
    This.setsize (400, 166);     
}/** * This method initializes JEditorPane * * @return Javax.swing.JEditorPane     */Private JEditorPane Getjeditorpane () {if (JEditorPane = = null) {JEdi 
            Torpane = new JEditorPane (); 
        Jeditorpane.setcontenttype ("text/html"); 
    } return JEditorPane;
 }}//@jve:d ecl-index=0:visual-constraint= "70,19"

The most critical part of the above code is the dialog box of the JEditorPane object, at the time of initialization, from a URL to get weather forecast information, expressed as HTML markup fragment, without parsing, directly call the JEditorPane SetText method, you can be Html format information straight is displayed in a parsed way, which is the weather forecast information,

The call in Action needs to be modified

    	 public void Run (iaction action) 
    { 
        weatherdialog wd=new weatherdialog ("Beijing"); 
        Wd.setsize (335); 
        Wd.show ();        
    } 
   

Now run with the run-time Workbench, click on the Weather Report menu to see the following image:

If you are in Shanghai or another city, try to change the city parameter to "Shanghai" and run again, and you will find that you can still get the weather forecast of the town (here we have extracted information from the site, a bit opportunistic). It is worth noting that the Xmethod site provides a weather forecast of WebService, but only the United States cities, otherwise we can use the Web Service call to get the weather forecast, will be more cool.

Now running is the function of the workbench already has the weather forecast, also need to go further, the Change plugin export publish, copy to the Eclipse root directory of the plugins directory, reboot (see Eclipse Help for details). Now your own Eclipse, you have the weather forecast function, as long as you click the mouse, you can easily get weather information after programming. Unless your boss thinks you're not a good idea to keep abreast of the weather at work time, I think you can fully incorporate this plugin into your Favorites list. You can also expand on this basis, add some configuration file and property settings, customize the plug-in to meet their requirements. If you can increase the automatic filtering and filtering of information, it will be a very enjoyable experience, if you have the time and interest, may wish to try.

Back to top of page

3. Mail Quick Monitor Plugin

Now that your work is more comfortable and creative with Eclipse, what are your grievances? Are you tired of all sorts of email clients harassing you anytime, anywhere? You want to get a good overview of the email when you're happy. Well, now that you think about why you hesitate, because you're a programmer, you're just going to use Eclipse to enjoy the full DIY fun.

3.1 Generating plugins

In this section we will add a message Filtering dialog box based on the above Myplugin plugin, similar to the one we created through Visualediter to create a dialog named Maildialog, and add a jeditpane to display the information we care about in the mailbox.

Modify Plugin.xml to add a "my Mail" menu

             <action 
               label= "mail Message"
               icon= "icons/sample.gif"
               class=
               "myplugin.actions.MailAction" tooltip= " Mail Message "
               menubarpath=" Samplemenu/samplegroup "
               toolbarpath=" Samplegroup "
               id=" Myplugin.actions.MailAction "> 
         </action> 
        

Now, you know you want to create a Mailaction Action class and add the following code in Run

              Mailconfig mail=new mailconfig (); 
        
        String popserver= "Server"; 
        String popuser= "Zhaoyong"; 
        String poppassword= "1234"; 
        
        Set keywords to filter: Sender and message subject
        String [] strfrom=new string[] {"Zhaoyong"}; 
        String [] strsubject=new string[] {"Test"}; 
        
        Mailconfig[] MC =new mailconfig [] {mail}; 
        Maildialog md=new Maildialog (MC); 
        System.err.println ("Run Run Run"); 
        Md.setsize (335); 
         Md.show (); 
        

The above code compilation will not pass, but don't worry, slow down, soon.

3.2 Building the Message Monitoring dialog box

Of course you need to create a Mailconfig class to represent the specific settings of a mailbox and related information, which is not described in the description, see the code in resources for details. Need to explain the type of mailconfig in addition to record a mailbox address, user name and password, but also provide 2 key word groups, if empty, not filtered, if the keyword has a value, the system will be based on the sender and the message header contains keywords to display the message information, has guaranteed your absolute freedom.

First we need to implement a Mailconfig class, which represents the mail configuration, each Mailconfig object represents a mail account, our system will be able to display the configuration of multiple mailboxes, each mailconfig using an array to save the recipient and mail address to be filtered.

The variables in the Mailconfig class are as follows:

        String Popserver; 
    String Popuser; 
    String Poppassword; 
    
 Set keywords to filter: Sender and message subject
    String [] strfrom; 
    String [] strsubject;    
    
    Whether to display the message content 
 Boolean isviewcontent=false; 
   

Again, we will use a dialog box to display the message information, Maildialog need to reference Javamail.jar, and Activation.jar these two class packages, to ensure that there are already two class packages and join the project's classpath. The final Maildialog code is as follows:

 Package myplugin; 
 Import java.io.IOException; 
 Import java.util.Properties; 
 Import Javax.mail.Folder; 
 Import Javax.mail.Message; 
 Import javax.mail.MessagingException; 
 Import javax.mail.Session; 
 Import Javax.mail.Store; 
 Import javax.mail.internet.InternetAddress; 
 Import Javax.swing.JDialog; 
 Import Javax.swing.JEditorPane; 
 Import Javax.swing.JTextPane; /** * @author Zhaoyong * * TODO to change the template for this generated type comment go to * window-preference S-java-code Style-code Templates * * public class Maildialog extends JDialog {private JEditorPane JE 
    Ditorpane = null; 
    
    Private Jtextpane Jtextpane = null; 
   
    
    Multiple message configurations can be displayed mailconfig[] mc= null;
     /** * This method initializes * constructor * @param MC: Multiple mailbox Configuration objects that need to be displayed. 
      
        */Public Maildialog (mailconfig[] MC) {super (); 
        if (mc!=null) THIS.MC = MC; else system.erR.println ("Mail configuration error. 
        
        ") ; 
    Initialize (); 
    }/** * This method initializes * Initialize * @return void */private void Initialize () 
            {try {//Set display panel This.setcontentpane (Getjtextpane ());            
            Get all the new message information String s= getallmailinfo (); 
           
            Displays the information in the dialog box This.jtextpane. SetText (s); 
            This.settitle ("Mail Information"); 
        This.setsize (251, 100); } catch (Exception e) {//Error display error message This.jtextpane. SetText (E.tostring ( 
            )); 
        E.printstacktrace (); 
    	 }}/** get all mail messages that need to be monitored by the mailbox * * @return String */private string Getallmailinfo () { 
    	
    	 String allmailinfo= ""; if (mc.length <1) allmailinfo= "The mailbox is not configured. 
    	 "; else {for (int i=0;i<mc.length;i++) {//loop gets each mailbox'sMail information Allmailinfo=allmailinfo+getmailinfo (mc[i]); }}//has not received the relevant message if (Allmailinfo.trim (). Length () ==0) allmailinfo= "No related new messages were detected.
Related Article

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.