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 time, our menu has changed at the runtime workbench.
2.2 Making Weather Forecasts Dialog Box with Visulediter
Although the menu is a weather forecast, we need 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 of Interface (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 will find the dialog box to find the SampleAction's Run function with the weather forecast menu just now, as shown below: Public void run (IAction action) {
MessageDialog.openInformation
WINDOW.GETSHELL (),
"MyPlugin Plug-in",
"Hello, Eclipse World");
}
Replace with the following code
Public void Run (IAADITION 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 p>
*
Description: This is a dialog box class, which is used to display the weather forecast for the day of the designated city P>
*
Copyright: Copyright (C) 2004 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 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 InputLine; // From the constant read data of the input, until reading
While ((InputLine = in.readline ())! = NULL)
Temp = TEMP INPUTLINE "/ N";
// Turn off the input stream
In.Close ();
String weather
= Temp.substring (Temp.Indexof ("
Temp.lastIndexof ("Body>") 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. Mail Surveillance Plugin
Now your job is more comfortable, more creative because Eclipse, then what else is there? 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 our 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">
action>
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 related information, which is not in the accumulated instructions, see the code in the reference information. 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 topic
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 vidinitialize () {
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]; // obtain information of 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 control information added by Visual Editor
The annotation of the above code has ensured that you can see clearly, here is not adding, interested, you can try it yourself, experience all the pleasure. Of course, this example is really simple, so there is also enough room for your further development.
3.3 packaging and release
At this point, the mail information menu and dialog box is added to mypulgin, and the Plugin.xml of the system is as follows:
XML Version = "1.0" encoding = "UTF-8"?>
Eclipse Version = "3.0"?>
ID = "MyPlugin"
Name = "MyPlugin Plug-in"
Version = "1.0.0"
Provider-name = ""
Class = "MyPLUGIN.MYPLUGINPLUGIN">
library>
library>
library>
runtime>
requires>
Point = "org.eclipse.ui.actionsets">
Label = "Sample Action SET"
Visible = "True"
ID = "MyPlugin.ActionSet">