Eclipse introduction and plugin development

xiaoxiao2021-03-06  71

Eclipse is a very fascinating development environment, which provides the core framework and scalable plug-in mechanism to provide unlimited imagination and creative space to the majority of programmers. At present, there are quite rich and comprehensive development tools, but Eclipse has surpassed the concept of development environments, which can imagine Eclipse to become future integrated desktop environments. The current Eclipse itself has the functions of resource management and external programs, plus omnipotent plugins, will form a colorful working environment than just an IDE.

1. ECLIPSE Introduction and Plug-in Development Eclipse is a very fascinating development environment, which provides the core framework and scalable plug-in mechanism to provide an unlimited imagination and creative space for the majority of programmers. At present, there are quite rich and comprehensive development tools, but Eclipse has surpassed the concept of development environments, which can imagine Eclipse to become future integrated desktop environments. The current Eclipse itself has the functions of resource management and external programs, plus omnipotent plugins, will form a colorful working environment than just an IDE. For programmers, there is nothing more important than the custom working environment you want, your determination, courage and creativity are in a light in the process of sharing the results with others. Ok, if you have a heartbeat, if you have a certain understanding of Eclipse, then build your own personalized work environment, first we develop a weather forecast together, then we create your own mail. Quick monitoring function.

The following work is based on a certain premise, that is, you are a Java programmer, you appreciate it and start using Eclipse this cool working environment, don't forget to download the latest version of Eclipse3.0, this article is based on Eclipse 3.0 development .

2. Weather forecast plugin If you are tired of you always have to log in to some websites get information from related pages, here there is a new way to quickly get information through Eclipse. Let us start from the beginning, be a weather forecast plugin, your Eclipse will have weather forecast function, is it cool?

In this part, we will implement an Eclipse plugin, he can tell us the weather forecast of our region by simply clicking the mouse when we want to know, which is of course very stimulating. For a programmer, things should be. Let's get started, let's first define a plugin and add him to the menu and toolbar. For you without plug-in development experience, you can refer to "Development Eclipse plugin", establish basic plug-in development awareness, of course, this article will detail your completion of this creative job.

2.1 The most basic plugin You can completely refer to the "Development Eclipse plugin" plugin example, make your first Hello Eclipse plugin, fortunately, Eclipse3.0 is completely considering your needs, through the menu File -> New-> Other -> Plug-in Project, Enter the project name, next appearance dialog, as long as "Muplugin" is entered in the plugin name, after NEXT, select "Hello, World" plug-in template, you can create the easiest to MyPlugin Insert, but in fact, our weather forecast is not complicated than it, and the effect after the construction is changed, as shown below.

Now, the project is run as a runtime workbench, in a brand new Eclipse window, by clicking the round Eclipse icon in the Menu Sample Menu, the round Eclipse icon in the toolbar, you will see the following Dialog of the effect.

Here, the original version of the weather forecast plug-in is made, by modifying plugin.xml, we change the menu into a Chinese form, you need to modify it 2, see the table.

Label = "Sample Action SET"

Visible = "True"

ID = "MyPlugin.ActionSet">

Label = "My Space"

ID = "SampleMenu">

Name = "SampleGroup">

Label = "Weather Forecast"

icon = "icons / sample.gif"

Class = "MyPlugin.Actions.sampleAction"

Tooltip = "Hello, Eclipse World"

MenuBarpath = "SampleMenu / SampleGroup"

Toolbarpath = "SampleGroup"

ID = "MyPlugin.Actions.sampleAction">

At this time, our menu has changed at the runtime workbench.

2.2 Making Weather Boxes with VisualeDiter Although the menu is a weather forecast, we need to be a Hello Eclispe dialog, we need to tell us the weather dialog, of course, we need to start from the beginning, so we need to rebuild a dialog. This requires Visual Editor to help development (About Visual Editer Refer to "Build Guis With The Eclipse Visual Editor Project"). We will use Visual Editor to implement a swing dialog, of course, only VE is a dialog, but it is a bit large, but as a starting point, it is already appropriate.

First refer to "Build Guis With The Eclipse Visual Editor Project" builds the Visual Editer development environment, when everything is ready, right-click "Muplugin.Actions" java file in PackGeexpoler, select New-> Other-> Visualclass from the pop-up menu Newly built a visual class, the pop-up interface is shown below:

Select Next, then enter WeatherDialog in Name, this is the Dialog we used to display weather forecasts.

Select the super class of this dialog as javax.swing.jdiaog, click the Finish button. After waiting for some time, our dialog box is basically generated. The mouse click on the upper left corner icon, and the weather forecast directly is directly the title of the dialog, and we can see the Visuleditor panel on the left.

Then we use the dialog to find the SampleAction's Run function with the weather forecast menu just available, as shown below:

Public void run (IAAADITION Action) {

MessageDialog.openInformation

WINDOW.GETSHELL (),

"MyPlugin Plug-in",

"Hello, Eclipse World");

}

Replace the following code public void run (IAction action)

{

WeatherDialog wd = new weatherdialog ();

Wd.setsize (400, 335);

WD.SHOW ();

}

At this point, click on the menu to run, our dialog seems to be like this, on this, we will add weather forecast information above.

2.3 Adding weather forecasting

The following part is the focus, we will use the Swing Component JeditPane with the HTML function to get ready-made weather forecast information on the network. And modify the dialog code makes the final code 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;

/ **

*

Title: WatherDialog

*

Description: This is a dialog box class, which is used to display the weather forecast for the day of the designated city

*

Copyright: Copyright (C) 2004

*

Company: uf Soft

* @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 a 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 (Url.OpenStream ());

// Get an input stream using OpenStream and thus constructed a BufferedReader object

String infutline;

/ / From the constant read data of the input flow until reading

While ((InputLine = in.readline ())! = NULL)

Temp = TEMP INPUTLINE "/ N";

// Turn off the input stream

In.Close ();

String weather

= Temp.substring (Temp.Indexof ("") 5);

This.jeditorPane .Settext (Weather);

}

Catch (Exception E)

{

E.PrintStackTrace ();

}

THIS.SETTITE ("Weather Forecast");

this.setsize (400, 166);

}

/ **

* This Method Initializes JeditorPane

*

* @return javax.swing.jeditorPane

* /

Private JeditorPane GetjeditorPane ()

{

IF (JeditorPane == NULL)

{

JeditorPane = New JeditorPane ();

JeditorPane.SetContentType ("text / html");

}

Return JeditorPane;

}

}} @JVE: DECL-INDEX = 0: Visual-constraint = "70, 19"

The most critical part of the above code is the JeditorPane object in the dialog. When initialization, get the weather forecast information from one URL, which is characterized by an HTML tag segment, without parsing, directly calling the JeditorPane's setText method, you can put an HTML format information. Directly displayed in parsing, that is, the weather forecast information,

The call in the action needs to be modified.

Public void Run (IAADITION Action)

{

WeatherDialog WD = New WeatherDialog ("Beijing");

Wd.setsize (400, 335);

WD.SHOW ();

}

Now run in a runtime workbench, click the weather forecast menu, you can see the figure below:

If you try to modify the city parameter "Shanghai" in Shanghai or other city, run again, you will find that you can still get the weather forecast of the city (here we extracted from the website, it is a bit speculative). It is worth noting that the XMethod website provides a weather forecast WebService, but unfortunately there is only a city of the United States, otherwise we can use Web Service to get weather forecasts, will be more cool.

It is now running that the table has a weather forecast. It is also necessary to remove the PLUGINS directory of the eclipse root directory, and copy it (see Eclipse Help) in the plugins directory of the Eclipse root directory. Now your own Eclipse has the feature of the weather forecast. As long as you click on the mouse, you can easily get weather information in the programming. Unless your boss thinks that you are not a good idea at work hours, I don't think you can include this plugin into the plugin of your personal collection. You can also extend on this, add some profiles and property settings to customize the plugins that meet your own requirements. If you can increase the automatic filtering and filtering of information, it will be a very pleasant experience if you have time and interest, you may wish to try.

3. Email Quick Monitoring Packaps Now Your work is more comfortable, more creative because Eclipse is more creative, then do you have any dissatisfaction? Are you tired of various mail clients harassing you anymore? Do you want you to understand the email outline when you are happy? Ok, since I thought of why hesitated, because you are a programmer, you just have to enjoy full DIY with Eclipse. 3.1 Generating Plugins This section we will add a message-filtered dialog box on the basis of the above MyPlugin plug-in, similar to we create a dialog named Maildialog through Visualediter, and add a jeditpane to display our attention in the mailbox .

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

Label = "Mail Information"

icon = "icons / sample.gif"

Class = "MyPlugin.Actions.mailAction"

Tooltip = "Mail Information"

MenuBarpath = "SampleMenu / SampleGroup"

Toolbarpath = "SampleGroup"

ID = "MyPlugin.Actions.mailAction">

Now, you know 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 that need to filter: sender and email topics

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 (400, 335);

Md.show ();

The above code is compiled, but don't worry, come slowly, very fast.

3.2 Building a Mail Monitoring dialog Of course, you need to build a mailconfig class to indicate the specific settings of a mailbox have been related to related information, which is not in the accumulated instructions. For details, please refer to the code in the reference. The type MailConfig needs to be described, in addition to logging an email address, user name, and password, it also provides 2 keyword arrays. If it is empty, it is not filtered, if the keyword value is worth, the system will be based on the sender and the mail title. Whether to include keywords to display mail messages, have guaranteed your absolute freedom.

First we need to implement a mailconfig class, indicating a mail configuration, and each mailconfig represents a mail account, our system will display multiple mailbox configuration, using an array in each mailconfig to save the recipient you need to filter and Email address.

The variables in the MailConfig class are as follows:

String popserver;

String popuser;

String poppassword;

// Set the keyword that needs to be filtered: sender and mail the main question String [] strfrom;

STRING [] strsubject;

/ / Yes No time to display the volume of the mail

Boolean isviewContent = false;

Similarly, we will use a dialog to display mail information, and maildialog needs to reference JavaMail.jar, and the Activation.jar two classes, ensuring that these two classes have been included and added to the project's classpath. The last MAILDIALOG code is as follows:

Package myplugin;

Import java.io.ioException;

Import java.util.properties;

Import javax.mail.folder;

Import javax.mail.MAESSAGE;

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 to

* WINDOW - Preferences - Java - Code Style - Code Templates

* /

Public class maildialog extends jdialog

{

Private JeditorPane JeditorPane = NULL;

Private JtextPane JtextPane = NULL;

/ / You can display multiple mail configurations

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 this

* Initialization

* @Return Void

* /

Private void initialize ()

{

Try

{

/ / Set the panel displayed

THIS.SETCONTENTPANE (GetjTextPane ());

/ / Get all new mail information

String s = getAllMailInfo ();

// Display the information in the dialog

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 ();

}

}

/ ** acquire all mailboxes to monitor mail information

*

* @Return String

* /

Private string getAllMailInfo ()

{

String allmailinfo = ""; if (mc.length <1)

ALLMAILINFO = "No mailbox!";

Else

{

For (int i = 0; i

{

// Cycle to get mail information for each mailbox

AllmailInfo = AllmailInfo GetmailInfo (MC [i]);

}

}

// has not received the relevant mail

IF (allmailinfo.trim (). Length () == 0)

AllmailInfo = "No new emails are detected!";

Return allmailinfo;

}

/ *

* String form of all new emails that meet the conditions in a mailbox

** /

Private string getMailinfo (Mailconfig MC)

{

// Final output mail information

String Mailinfo = "";

/ / STORE and Folder objects on each mailbox server

STORE store = NULL;

Folder folder = NULL;

Try

{

Properties PROPS = system.getproperties ();

/ / Generate a session with the mail server

Session session = session.getDefaultInstance (Props, NULL);

/ / Give the server, user name, password connection server

STORE = session.getStore ("POP3");

Store.Connect (mc.getpopserver (), mc.getpopuser (), mc.getpoppassword ());

/ / Get the default mail Folder

Folder = store.getDefaultfolder ();

IF (Folder == Null)

Throw New Exception ("No Default Folder);

// Take the inbox

Folder = folder.getFolder ("inbox");

IF (Folder == Null)

Throw New Exception ("No Pop3 Inbox");

// Turn on the inbox in read-only mode

Folder.open (Folder.Read_only);

/ / Get all new mails and process

Message [] msgs = folder.getMessages ();

For (int i = 0; i

{

Message message = msgs [i];

/ / Take information about each message, you need to reference the Mailconfig object to filter keyword filtering

Mailinfo = MailInfo GetMessageInfo (Message, MC);

}

}

Catch (Exception EX)

{

EX.PrintStackTrace ();

}

Finally

{

// Security Close Mail Server Resources

Try

{

IF (Folder! = null) folder.close (true);

IF (Store! = null) Store.Close ();

}

Catch (Exception EX2) {EX2.PRINTSTACKTRACE ();

}

Return Mailinfo;

}

/ **

* Get information of an email, need to filter according to Mailconfig

* @Param MailInfo

* @Param message * @return mail information

* @Throws MessagingException

* @Throws ioException

* /

Private String GetMessageInfo (Final Mailconfig MC)

Throws MessagingException, IOEXCEPTION

{

// Return mail information

String Mailinfo = "";

String from = ((InternetAddress) message.getFrom () [0]). GetPersonal ();

IF (from == NULL)

From = ((InternetAddress) message.getFrom () [0]). getAddress ();

String subject = message.getsubject ();

// If you meet the filter information, it will be displayed.

ISELEMENTInstring (from, mc.getstrfrom ())

|| ISELEMENTINSTRING (Subject, Mc.GetStrsubject ())))

{

MailInfo = MailInfo "sender:" from "/ n";

MailInfo = MailInfo "Mail Topic:" Subject "/ N";

MailInfo = MailInfo "Send Time:" Message.getSentDate () "/ n";

// If you display the content, print content

IF (mc.isviewcontent)

MailInfo = MailInfo Message.getContent () "/ n";

Mailinfo = Mailinfo "-------------------------------- / n";

}

Return Mailinfo;

}

Private jtextpane getjtextpane ()

{

IF (jtextpane == null)

{

Jtextpane = new jtextpane ();

}

Return jtextpane;

}

/ **

* Determine if there is a specified string in the target keyword array, filtering

* @Param Targetstr:

* @Param Keys:

* @return If you have, return true, otherwise return false

* /

Private Boolean ISELEMENTINSTRING (String Targetstr, String [] Keys)

{

// No specified filter conditions, display all

IF (keys == null)

Return True;

/ / Specify the string is empty, return to false directly

IF (targetstr == null)

Return False;

For (int i = 0; i

{

IF (Targetstr.indexof (Keys [i])> - 1)

Return True;

}

Return False;

}

}

@JVE: DECL-INDEX = 0: Visual-constraint = "10, 10" - Description, this is the annotation of the above code above the Visual Editor added has ensured that you can see it, this will not be accumulated, Interested, you can try it yourself, experience all the pleasure to master. Of course, this example is really simple, so there is also enough room for your further development.

3.3 Package and release here, add the mail information menu and dialog box in MyPulgin, the Plugin.xml of the system is as follows:

ID = "MyPlugin"

Name = "MyPlugin Plug-in"

Version = "1.0.0"

Provider-name = ""

Class = "MyPLUGIN.MYPLUGINPLUGIN">

Point = "org.eclipse.ui.actionsets">

Label = "Sample Action SET"

Visible = "True"

ID = "MyPlugin.ActionSet">

Label = "My Space"

ID = "SampleMenu">

Name = "SampleGroup">

Label = "Weather Forecast"

icon = "icons / sample.gif"

Class = "MyPlugin.Actions.sampleAction"

Tooltip = "Hello, Eclipse World"

MenuBarpath = "SampleMenu / SampleGroup"

Toolbarpath = "SampleGroup"

ID = "MyPlugin.Actions.Sample>

Label = "Mail Information"

icon = "icons / sample.gif"

Class = "MyPlugin.Actions.mailAction"

Tooltip = "Mail Information"

MenuBarpath = "SampleMenu / SampleGroup"

Toolbarpath = "SampleGroup"

ID = "MyPlugin.Actions.mailAction">

In fact, we joined 2 functions in a plug-in, so we realize your self-extension and customization of our development environment. Similarly, refer to Eclipse's help, you can easily package the plugin again and add it to your own Eclipse's PLUGINS directory (you may need to decompress), or install it through the Update option of the Help menu, pay attention to the selection when exporting Related class packages. Restart, you will find that your IDE has already had your own menu, the development environment has changed with his wishes, and the programmer is naturally satisfied.

Now, you can click on the menu when you need it, you can understand the case you want to monitor or the nearest weather, everything is easy to master, Eclipse's plugin, is such a whole energy.

4. Summary, then smart, have you found something, right, too rough, too simple, you can do further optimization design and functional strengthening, such as adding a mail configuration file instead of writing inside the code Dynamic monitoring pop-up mail warning (by predetermined emergency state), you may still think of a lot of new ideas, such as why I can't have a movie plugin? Or the music that is drifted in Eclipse? Don't worry, these don't necessarily need you to do it, http://sourceforge.net/projects/rocexwang/ has a player plugin, but now only play music, there are already eclipse to develop games in foreign countries, and some people use eclipse. Do MIS system. Http://www.matrix.org.cn/forum_view.asp?forum_id=25510 There is a country-developed Russian square game, although it is simple. Of course, through the URL http://eclipse/index.jsp and http://www.eclipseplugincentral.com/ You can find a lot of plugins, if you know what better plugin, please tell I.

Eclipse provides a pure framework and plug-in structure that makes a plug-in that develops any functionality be a reality. This article describes the development of 2 interesting Eclipse plug-ins, which allows our work environment to increase two cute little features, but also makes you have the foundation plug-in development capabilities, with the powerful features of Eclipse, you can take you Any idea becoming a reality. Also keep communicating with others, I will be happy to understand your novel plug-in and collect and learn any good plug-in to create a personalized development environment that is completely omnipotent. Eclipse will become a full range of Platform in the near future, this is a little more reality in the hands of thousands of developers around the world. Download code here: myplugin.rar, here is the entire MyPlugin project, extract Eclipse after decompression, you may need to reconfigure class paths.

Reference

Developing Eclipse plugins Build Guis with the Eclipse Visual Editor Project

About the author Zhao Yong, J2EE programmer, telephone 13810689273, mail zy_coding@tom.com

转载请注明原文地址:https://www.9cbs.com/read-92189.html

New Post(0)