How to create a custom 'new class wizard' for Eclipse? - java

I would like to create a functionality ( for myself ), wherein on clicking a button ( or say firing any event or anything that can trigger my program ), a popup will be displayed which will ask the name of the Class, objects it have and few more thing. Then on pressing OK, it will create a java file with skeleton of predefined methods, inherit known interface and ...
So, basically how to do that? Do i need to create a plugin for eclipse or there is something else in eclipse for it.
PS Please change the title. I am unable to think of any better one.

As others said, you want to create a wizard, then you want to augment the New Class Wizard, which is doing something similar to what you want (but the default wizard don't allow you to to add fields and custom methods).
To create a wizard, you can use the "New File Wizard" extension template: Create a plug-in, then, go to the extensions tab, select Add..., and select the "Extension Wizards" tab. That will get you started on Eclipse wizards.
Once you've learned the basics of creating Wizards and pages, then, include the org.eclipse.jdt.ui and org.eclipse.jdt.core in your plug-in dependencies. Open the following type (Ctrl-Shift-T): "NewClassWizardPage". This is the page that is displayed when you select New > Class in the Package Explorer.
You can probably either copy this page and the parent pages to help you get started or just extend it (in my experience, internal Eclipse wizards such as this one are difficult to extend because they have lots of fields and methods that are package/private, so I usually end up copying the code as a starting point... don't forget to keep the license though!).

You more or less want to add your own wizzard to the 'new class' dialog .. right?
This was the first site I found when typing "creating your own new wizzard eclipse" in Google: http://www.eclipse.org/articles/article.php?file=Article-JFaceWizards/index.html

I may be mis-understanding the question, but it sounds like you are re-implementing the New Class Wizard that exists already.
It lets you name the class, the containing package. Can assign a superclass and/or interface and can also choose if you want to include the contructors for the superclass.
A new .java file is created with all known methods from declared interfaces and also any abstract methods from the superclass.
Edt: Title was changed whilst I was writing this reply to "How to creat a customer 'new class wizard; for Eclipse". It makes my answer slightly redundant but I'm not seeing any new functionality being added in the question.

Related

IntelliJ, is it possible to "Make controller" as in NetBeans?

Does anyone with IntelliJ & NetBeans experience know, if IntelliJ has some similar feature as for NetBeans "Make controller" when right clicking the corresponding FXML file?
I think it is pretty important since it updates, if I have added something new (a FXID for example), and if I deleted it and such - and also places it in the correct place by itself.
I know you, in Scene Builder, can go to "View -> Show Sample Controller Skeleton", but this is just not enough automation, especially not from a Jetbrains IDE.
This also means that the following link is not an answer:
how to do netbeans'"make controller" in intellij idea
[]
Rather than having an action to create the controller, IDEA uses intentions (activate via Alt+Enter or ⌥⏎). And its philosophy would be to keep the controller in sync as you edit the FXML file rather than recreating it to update it with changes.
For example, when you create an FXML file, IDEA offers to create the controller:
First I name it as desired, and then use an Intention to create the corresponding class:
Then as I add things to the FXML, IDEA offers to create the corresponding items in the controller:
Similarly with an fx:id:
And if I use the rename refactor to change the fx:id
it renames the corresponding field name in the Controller to match. So instead of editing it and then recreating the controller to capture the name change, it simply keeps them in sync. But again, the key is to use the rename refactoring and not just "manually" edit the fx:id.
Edit to answer follow-up questions in comments
First a disclaimer... I only dabble in JavaFX to write simple dialogs as front ends to command line scripts. So I'm by no means an expert and may not be aware of a JavaFX related feature in IDEA. But I have been using IDEA for 14 years and typically know what to look for in terms of features and "how do I" questions.
Can you shed some light on using this kind of stuff with Scene Builder, if you know something about it?
Whenever I use a WYSIYWG editor like Scene Builder, I still keep an eye on and cleanup the code it generates. I'm still a bit old school in that way. And I believe IntelliJ IDEA adheres to that philosophy.
I find it really hard to believe IDEA has no way of "mass automatic updating" edits made in Scene Builder
I can tell you as an IDE, IDEA is not big on Wizards or mass code generation. Instead it's more "surgical" in nature. It's "code by intentions" philosophy is to enter/edit your intent, and then use an intention/quick-fix to implement that intention. Thus any code generation it does is small and focused. But... in some cases you can ask it to simultaneously apply an intention to multiple items by opening the intentions side menu (using the right arrow → key) and selecting the "Fix All 'issue name' problems in file". Again, given my limited experience with JavaFx work, I may not be aware of something. But I'm not seeing anything where such a thing would be and I'm not seeing anything in the Help pages.
<soapbox>
When I first started using IDEA in 2003 I too wondered where all the wizards and mass code generation features that my old IDE provided were. (I previously used Forte for Java, the predecessor to NetBeans.) At first I missed them. Then I started using intentions and refactorings for everything. After a couple of months, I didn't miss the wizards. After a few more months, I came to hate wizards in other IDEs. In the long run, I was far more productive using the intentions & refactorings. And I noticed I was no longer spending a lot of time dealing with compile issues and bugs that I did when I used wizards.
</soapbox>
If I assign fx:id's to a lot of buttons and such in Scene Builder, do I then have to manually go into the code view of the fxml file, find all of the fx:id's and for each of them alt+enter to get them into the controller?
Unfortunately for the "create field" intention for missing IDs" there is not a batch mode available. So you can not use the "fix all" option. My educated guess as to why not is that when creating the field you are given the opportunity to select the type for it. For example, with the fx:id for a label, I may want to type the field as Label, Labeled, Control, etc. Granted in most cases you are likely to type it as the actual type it is in your FXML. So you may want to open a feature request asking for a batch mode to be made available for the "Create field" intention that defaults the field to type used in the FXML. Nevertheless, if you get use to using F2 to navigate to the next error, and then using Alt+Enter (or ⌥⏎) to open the quick fix, you'll find you can add the missing fields very quickly.
if I deleted some fx:id's also, then I would have to remember which ones they were, to them manually delete them in the controller?
Again, instead of just "manually" deleting an fx:id, use an IDEA refactoring. When deleting fx:ids (or anything) use the "Safe Delete" refactoring. With your cursor on the id (either in the FXML file or the Controller), type Alt+Delete (or ⌘⌦) to launch "Safe Delete".
If there are no usages, the field is deleted. Otherwise you get a warning:
Click the "View Usages" button. The "Find" toolbar will open up showing the usages.
Update your code to remove their usages. Use the "Rerun Safe Delete" button to monitor your progress. Once all usages are done, hit the "Do Refactor" button to delete the usages. While this may seem more involved than some sort of regenerate the controller option, my question would be how would such a feature deal with the usage of that field in a method within the controller? Would it just not recreate the field leaving a compile error in my controller? How would it "regenerate" all the customization I've made to the controller?
The Safe Delete also keeps your code error free. If I had a dollar for every time I went to delete a something I absolutely knew was not being used anywhere only for the safe delete refactoring to pop up a warning about the place its being used that I totally forgot about.
If you use Javaru's solution then created fields will be public and without #FXML annotations.
To change it go to:
File | Settings | Editor | Code Style | Java | Code Generation | Default Visibility - choose Escalate or Private, and check "use external annotations" at the bottom.
Second solution: open fxml file in scene builder set the controller class in controller section at left bottom part of window. Go to: view | show sample controller skeleton and copy controller class code.
It is my first post, hope I've helped. :)
I can't see create method or create field
I see this
Go to your .fxml file and there is red error element: the name of your handle function in my example.
Just push create it.
IntelliJ gives you possibilities to create your event:

IntelliJ- how to find method calls?

I have just started using IntelliJ for the first time on a project that I've just started working on, and am still getting familiar witht the setup and how it works. Previously when working on Java projects, I have predominantly used Eclipse as my IDE.
In Eclipse, when working on a particular method, there was the functionality to right-click the method, and select the option 'Find All References', to list everywhere in the project that the method was called.
I have made a few changes to the definitions of a couple of methods (the changes have mainly been in their parameters) in the project in IntelliJ, and now want to 'Find All References' on the methods, so that I can ensure that they are called with the correct parameters. However, when I right-click on the method definitions, and select 'Find Usages' from the menu, I get a popup message displaying the following warning:
Method 'abc() of class def' implements method of interface ghi. Do you want to find the usages of the base method?
Regardless of whether I select 'Yes' or 'No', the search results only return one result- the source file & line that I clicked on in order to do the search.
My guess is that there's something I need to change in the settings somewhere to ensure that doing this returns all of the places where that particular method is used in the code? I checked with a colleague, and when they do exactly the same thing, they get a list of all of the places within the project where that method is called...
How can I resolve this, so that I can find all of the method calls for the one I have highlighted?
Have you tried CTRL+click on method declaration?
I usually use Ctrl-Alt-H for Hierarchy and get a list of all callers even in tests.
By selecting the method name (double click on it) and pressing alt+ctrl+shift+f7 keys, a window named Fined Usages will popup. There is a checkbox in this window, you can check: Usages, Overriding methods, Search for text occurrences or Skip result tab with one usage. Then you can set the scope on All Places and click on Find. So, a search window appears and all classes and methods that called this method are listed there.
Alternatively, you can use "Find in path" option which lets you search for a raw string in any files in the project (although it's of course configurable). It is under Ctrl-Shift-F.

When creating a Java class in IntelliJ: is there a wizard/menu to select the package and class to extend from?

One of the features I like of Eclipse is that when creating Java classes, a wizard is available to specify different properties for the class. Like its package, class to extend from...etc (see below in the screen cap).
Does IntelliJ provide something similar? I created a class but the process wasn't smooth. I had to...mark /java directory as Source Root...create new package...create a new class...and now, I want to extend from a different class other than Object... I would like to avoid doing this step by step, if possible.
I am using IntelliJ Community Edition, version 15.
No, there is no such wizard. Marking the directory as a source root is a one-time action, and is normally taken care of when you create the project. To specify the package, you can enter the package and class name directly in the "New Java class" dialog. If you want to extend a different class, the code completion will help you enter the "extends" or "implements" keyword and specify the base class name.
You can make package at the same time as you create java class.
When you do right click -> new -> java class : at the place of the name you can mark
my.new.package.ClassName and IntelliJ will create the package : my.new.package for you.
For more explication you can visit this page.
In IJ you don't need to write all of this stuff manually. Just right click on the method an choose Go to Test or press Ctrl-Shift-T. This functionality is described on the help page IntelliJ IDEA 15.0 Help /Creating Tests.
In the Create Test dialog:
Select the testing library to be used.
If the selected library is missing in your module, click the Fix button. As a result, the corresponding library will be automatically added to the module libraries.
Define the name and location of the test class to be generated.
In the Class name field, specify the name of the stub test class to be generated.
In the Superclass field, IntelliJ IDEA suggests the appropriate super class for JUnit3. For JUnit 4 and TestNG, this field is blank.
In the Destination package field, define where the generated test class should be placed.
Specify whether you want the setUp()/tearDown() methods (for JUnit), or the #Before/#After annotations to be generated.
In the table that shows the list of all the methods of the source class, select the ones you want to generate test methods for.
Click OK.
In addition to other answers if you hit Alt-Enter on any interface or abstract class name one of the intention options will be to create an implementation.

Eclipse: When displaying a list of methods (ctrl-space), is there a way to have methods in that class - bold?

Eclipse:
When displaying a list of methods (ctrl-space), is there a way to have methods found in that class have names in bold? Netbeans has or at least had a feature that would bold the names of methods found within that class while inherited methods remained normal text.
This made it very easy to know what methods are from within the class you are editing.
Is there a way to do this in the latest version of Eclipse?
Thanks.
It seems eclipse does not support it.
What I do in such cases (workaround):
Open the affected class (Navigate -> Open Type...)
Press CTRL-O, this will show the methods implemented in this class.
Press CTRL-O again, this will also show the inherited methods.

Dynamic Java.swing Menu

I have a special problem in coding a dynamic menu in Java.
What I want:
I want a menubar that looks for special files (example: addon01_men.class) and adds the content of this file to the menu as a new item.
Problem:
To invoke the methode/class in the file, I've to invoke it in the main file. But the addon-class is needed in this case, because java checks the presence of the methode/class. So the program will not start, if "addon01_men.class" is missing.
My solutions didn't work (perhaps in my fault):
- Reflections
- Override
I don't want to use dynamic classes because it's necessary to compile when the program starts - because not all PCs that can run java, can compile java.
I'd be pleased when someone has a working idea.
Thank you.
You should start simple like this example using Reflections:
http://www.mkyong.com/java/how-to-use-reflection-to-call-java-method-at-runtime/
For want you want, you need look for a directory containing the class files like addon01_men.class (think like they're plugins), load them and create instances.

Categories