|
|
||
Edgar Silva's BlogCreating a new Components Palette for Java Server Faces Dev in NetBeans 5.5Posted by edgars on January 11, 2007 at 09:44 AM | Comments (1)MotivationIf you want to create an application using Java Server Faces using NetBeans you have really useful features as which turn this task simple. Nevertheless we have a lot of open-source projects that can help us on JSF Developement, such as: MyFaces, Facelets , IceFaces , Ajax4JSF and others. I am sure, that some of the developers envolved with these projects would like to make the development easier using their components. If you need a complete solution to make your components availabe in NetBeans, this post can to give out some tips and tricks in how to do that. NetBeans Benefits for JSF Components CreatorsNetBeans, since of its default installation, counts with a lot of nice features, as the following:
If NetBeans did these things for us, why is not possible to add our own components on JSP/JSF Component Palette?I am sure you can think: "This sound quite hard...", However I can say: Don't worry! Read this post and I am sure you will have a different spot. Creating a New Module Suite ProjectA Module Suite is necessay to create a set of new Modules, our Components Items are one Project Module inside a Module Suite. Besides palette components, we could to add new modules to suite, in addition to this, when you create a suite you can run another NetBean´s instance, and then dubug and test our modules can be easier. To create a new Module Suite, click on File/New/Project, and then select NetBeans Plugin-Modules category and click on the Module Suite Project., as it is shown in the following image: Click on Next button, and fill the suite's data with the following information:
Creating a New Module ProjectA Module Project is your set of plugin(s), we often use a Module to add one or more plugins in NetBeans. As well as you can use one Project Module per new Plugin you are creating. To create a new one, click on File/New/Project menu and select NetBeans Plugin-Modules category and click on the Module Project..After click on Next button, you will see the Project settings informations, basically we have to give a name to project and select which Suite this module will be inserted on, as it is shown in the following image: After to click on Next button, you will see some informations about your Module Project, look the following list:
Creating a JSP Palette Component Item: SelectOneMenu
The SelectOneMenu is responsible in generating an html select with some dinamic data. Then, We will create a class to represent this code generation inside of JSP file. The class will be called SelectOne.java .
import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.text.ActiveEditorDrop;
public class SelectOne implements ActiveEditorDrop {
final protected SelectOnePanel tag = new SelectOnePanel();
private String id;
private String value;
private String tagValue;
/** Creates a new instance of SelectOne */
public SelectOne() {
}
private String createBody() {
getPropsForTag();
String SelectOne;
SelectOne = getTagValue();
return SelectOne;
}
public boolean handleTransfer(JTextComponent targetComponent) {
String body = createBody();
try {
FacesPaletteUtilities.insert(body, targetComponent);
} catch (BadLocationException ble) {
return false;
}
return true;
}
protected void getPropsForTag() {
DialogDescriptor tagdialogdescriptor =
new DialogDescriptor(tag,"Java Server Faces Pro");
tagdialogdescriptor.setModal(true);
tagdialogdescriptor.setLeaf(true);
tagdialogdescriptor.setOptionType(DialogDescriptor.OK_CANCEL_OPTION);
Object ret = DialogDisplayer.getDefault().notify(tagdialogdescriptor);
if (ret == DialogDescriptor.OK_OPTION) {
StringBuffer buffer = new StringBuffer();
buffer.append(String.format("
As you can see our SelectMenu class, implements the org.openide.text.ActiveEditorDrop, as which allow us to drag an item from palette and drop on source code, basically the executed method to automate this task is called: handleTransfer(). Look that in the method calling we have other two pieces: createBody() and FacesPaletteUtilities.insert(body, targetComponent). We will describe these methods in the following list:
Using Dialogs to Interact with UsersCertainly you are really proud with yourself, cause you are improving a lot the NetBeans IDE with your own component´s palette, and then you will give out a real gift to community that want use your group of components or even your internal development team. That´s you can give a professional impression about your pallete, to make it real we create a simple JPanel, and the most important items on this JPanel are 2 JTextFields, called id and value, respectivelly . They have to declared as public , because we will access them from other class, although you can create getter and setters if you want. Look on the following image and imagine how you can design your JPanel:At this point, you can ask me: "Hey Edgar, and the rest of the code? The method where I transfer the JTextField´s value to my SelectMenu.createBody() method? Where is it? The answer is incredibly easy: NetBeans provides a real set of helpers when we are creating a plugin or a complete RCP Application, so NetBeans gives for us two class called respectivelly: DialogDescriptor and DialogDisplayer, and togethet with these classes, we have all infrastructe to handle the which button is clicked by users, it you can see here in the following code:
protected void getPropsForTag() {
DialogDescriptor tagdialogdescriptor = new DialogDescriptor(tag,"Java Server Faces Pro");
tagdialogdescriptor.setModal(true);
tagdialogdescriptor.setLeaf(true);
tagdialogdescriptor.setOptionType(DialogDescriptor.OK_CANCEL_OPTION);
Object ret = DialogDisplayer.getDefault().notify(tagdialogdescriptor);
if (ret == DialogDescriptor.OK_OPTION) {
StringBuffer buffer = new StringBuffer();
buffer.append(String.format("
If the user presses on OK button, we return the correct value, inserting the informations contained on the JTextFields. Furthermore, the presentation, threading and other swing stuff will be managed by NetBeans. Creating a Insertion code Helper: FacesPaletteUtilitiesThis class acts as a helper when we want to add some text in a existing source opened in the our JSP Editor. Look its source code in the following code listing:
import javax.swing.text.BadLocationException;
import javax.swing.text.Caret;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import org.netbeans.editor.BaseDocument;
import org.netbeans.editor.Formatter;
public class FacesPaletteUtilities {
public static void insert(String s, JTextComponent target)
throws BadLocationException {
insert(s, target, true);
}
public static void insert(String s, JTextComponent target, boolean reformat)
throws BadLocationException {
if (s == null)
s = "";
Document doc = target.getDocument();
if (doc == null)
return;
if (doc instanceof BaseDocument)
((BaseDocument)doc).atomicLock();
int start = insert(s, target, doc);
if (reformat && start >= 0 && doc instanceof BaseDocument) {
int end = start + s.length();
Formatter f = ((BaseDocument)doc).getFormatter();
f.reformat((BaseDocument)doc, start, end);
}
if (doc instanceof BaseDocument)
((BaseDocument)doc).atomicUnlock();
}
private static int insert(String s, JTextComponent target, Document doc)
throws BadLocationException {
int start = -1;
try {
//at first, find selected text range
Caret caret = target.getCaret();
int p0 = Math.min(caret.getDot(), caret.getMark());
int p1 = Math.max(caret.getDot(), caret.getMark());
doc.remove(p0, p1 - p0);
//replace selected text by the inserted one
start = caret.getDot();
doc.insertString(start, s, null);
} catch (BadLocationException ble) {}
return start;
}
}
Declaring Additional informations and Resources
Now it´s time to create a component descriptor file, which is a XML file. This file describes informations about our component, you will this file on the same package of SelectMenu class. The file name will be SelectOne.xml, and its content is the following:
Declaring our Component in layer.xml
This one of the most important steps on this post, we will declare our component inside Netbeans enviroment. Look this file in the following image:
Some Details about Modules DevelopmentLibraries Dependencies When you are creating a NetBean´s Module , depending of the kind of things you are creating, some dependencies should be mandatories. To add these dependencies, click with left button over your Module Project and click on Properties option, take a look on Libraries Item on left list, on this panel you can add your module´s dependencies. Icons for your Components
The years I spent on University was not enough to learn how to create cool and beautiful icons, In addition, I am not an artist as my famous friend: Tim Boudreau , that created a lot of NetBeans's icons. Showing our plugin In Action Now it's time to see our plugin running:
I hope this post could be useful for frameworks developers, or even developers at companies using NetBeans and want to create or add your own component items to turn JSP and JSF delopment easier and faster. If you want the project´s files, please send me an e-mail: edgar (em) edgarsilva.com.br Bookmark blog post: CommentsComments are listed in date ascending order (oldest first) | Post Comment
| ||
|
|