Writing an Eclipse Plug-in (Part 3): Create a custom project in Eclipse – New Project Wizard: Time to Refactor
In my last post I showed how to get the GUI aspect of a New Wizard for the creation of a new project type up and running rather quickly. In my never ending attempt to be a good developer citizen it is now time to stop and refactor.
You might think we haven’t actually written enough code to refactor (we have 3 Java files in total). That would be true except for the three strings in our wizard code. I could wait until there is more to do, but why not refactor now when it will begin to develop muscle memory for that particular task?
First change:
_pageOne = new WizardNewProjectCreationPage("Custom Plug-in Project Wizard");
to:
_pageOne = new WizardNewProjectCreationPage(PAGE_NAME);
and put the string at the top of the class:
private static final String PAGE_NAME = "Custom Plug-in Project Wizard"; //$NON-NLS-1$
Next change:
public CustomProjectNewWizard() { setWindowTitle("New Custom Plug-in Project"); }
to:
public CustomProjectNewWizard() { setWindowTitle(WIZARD_NAME); }
and put the new constant at the top of the class:
private static final String WIZARD_NAME = "New Custom Plug-in Project"; //$NON-NLS-1$
The other two strings should be replaced by using the Eclipse Externalize Strings mechanism. With the CustomProjectNewWizard.java
open in the editor select Source –> Externalize Strings (or move the cursor to one of the strings and press Ctrl+1).
When the Externalize Strings dialog opens check the box for Use Eclipse’s String Externalization Mechanism. If you like the key value then leave them alone, but I prefer to have a variable name that conveys some level of information so I changed the keys to match the strings; so the CustomProjectNewWizard_0
becomes CustomProjectNewWizard_Custom_Plugin_Project
and CustomProjectNewWizard_1
becomes CustomProjectNewWizard_Create_something_custom
.
In addition, click Configure which is next to the Accessor Class dropdown field. Change the Class Name from Messages to NewWizardMessages. Click OK to finalize your decision.
Press Next in the Externalize Strings dialog to take a look at what the change will look like. What you should see are the strings replaced with a reference to a new class that just got generated.
_pageOne.setTitle(NewWizardMessages.CustomProjectNewWizard_Custom_Plugin_Project); _pageOne.setDescription(NewWizardMessages.CustomProjectNewWizard_Create_something_custom);
Look in the Package Explorer for the new class. It should be in the same package as the Wizard code. The magic? At runtime Eclipse reads the property file named messages.properties and fills in the static fields with the values in the file. You can add to the file through the Externalize Strings dialog as you add more strings that need to be externalized.
That is the extent of the refactoring based on what has been done so far.
Refactoring can sometimes seem trivial, but it will pay many dividends later when a string needs to be changed, or internationalized, and the only thing that needs to be done is to create a new properties file.
I’ve just started working through your tutorial – great info thus far. I wanted to point out that part of your refactoring for ‘WIZARD_NAME’ snuck into Part 2.
Yes, sad, but true. Someone else had pointed that out which means that now I wasn’t paying attention twice.
Why is it that you don’t externalize the window title?
Can I create my folder structure which contains bin and src as sub folders in existing project?