Is there a quick way to replace this:
public static String ACCESSDENIED = Resources.strings.getString(Resources.ACCESS_DENIED);
with:
public stattic String getAccessDenied(){
return Resources.strings.getString(Resources.ACCESS_DENIED);
}
I need to replace all static references to getters in about 100 variables.
Use right-click --> Refactor --> Encapsulate Field... --> (new getter created), and everything is done automagically (and its a bulletproof solution).
You can also verify the results in a Preview window.
I managed to do it by using the find and replace with regex expressions.
Find: = (.+);
Replace: () { return $1; }
Related
I'd like a TextCellEditor with the standard auto-complete behaviour, that that any user nowadays expects when typing inside an input cell with a list of suggested strings. For a good working example of what I'm after, in Javascript, see this jQuery autocomplete widget.
I couldn't find a good example.
I only found (aside from some tiny variations) this TextCellEditorWithContentProposal snippet. But that leaves a lot to be desired:
It lists all the words, irrespective of the "partial word" typed in the cell (no partial matching)
When the desired word is selected, it is appended to the partial word, instead of replacing it
The interaction is ugly and non-intuitive. For example, one would expect the
Escape key to tear off the list of suggestions; again, see the Javascript example; here, it also removes the typed letters.
It looks strange to me that such an standard and useful component is not available. Or perhaps it is available? Can someone point to me to a more apt snippet or example?
The example you are linking to is a code snippet intended to showcase the API and guide you toward customizing the control to your preference.
Some of your complaints are either invalid or can easily be fixed using public API.
Let's go through them in detail.
All proposals are listed, irrespective of typed text
Note that in the snippet an org.eclipse.jface.fieldassist.SimpleContentProposalProvider is used:
IContentProposalProvider contentProposalProvider = new SimpleContentProposalProvider(new String[] { "red",
"green", "blue" });
cellEditor = new TextCellEditorWithContentProposal(viewer.getTable(), contentProposalProvider, null, null);
As suggested in its javadoc it is:
designed to map a static list of Strings to content proposals
To enable a simple filtering of the contents for the snippet, you could call: contentProposalProvider.setFiltering(true);
For anything more complex you will have to replace this with your own implementation of org.eclipse.jface.fieldassist.IContentProposalProvider.
Selection is appended to cell contents, instead of replacing it
The content proposal behavior is defined in the org.eclipse.jface.fieldassist.ContentProposalAdapter. Again a simple method call to org.eclipse.jface.fieldassist.ContentProposalAdapter.setProposalAcceptanceStyle(int) will achieve your target behavior:
contentProposalAdapter = new ContentProposalAdapter(text, new TextContentAdapter(), contentProposalProvider, keyStroke, autoActivationCharacters);
contentProposalAdapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
Cancelling the proposal should not remove typed content
This is hard to do using just the API, since the ContentProposalAdapter does only propagate the key strokes to the opened ContentProposalPopup without storing them.
You would have to subclass ContentProposalAdapter, in order to have access to ContentProposalAdapter.ContentProposalPopup.filterText.
Most of the functionality in this snippet with sensible defaults can also be obtained in a more simple way by using an org.eclipse.jface.fieldassist.AutoCompleteField.
Here is a snippet showing you a simple implementation. You have to customize it but it give you the way.
Note, this is not a generic copy/paste of the documentation or an explanation about the doc.
String[] contentProposals = {"text", "test", "generated"};
// simple content provider based on string array
SimpleContentProposalProvider provider = new SimpleContentProposalProvider(contentProposals);
// enable filtering or disabled it if you are using your own implementation
provider.setFiltering(false);
// content adapter with no keywords and caracters filtering
ContentProposalAdapter adapter = new ContentProposalAdapter(yourcontrolswt, new TextContentAdapter(), provider, null, null);
// you should not replace text content, you will to it bellow
adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_IGNORE);
// now just put your implementation
adapter.addContentProposalListener(new IContentProposalListener() {
#Override
public void proposalAccepted(IContentProposal proposal) {
if(proposal != null && StringUtils.isNotBlank(proposal.getContent())){
// you need filter with blank spaces
String contentTextField = getFilterControl().getText();
String[] currentWords = getFilterControl().getText().split(" ");
StringBuilder textToDisplay = new StringBuilder();
if(currentWords.length > 1) {
// delete and replace last word
String lastWord = currentWords[currentWords.length-1];
textToDisplay.append(contentTextField.substring(0, contentTextField.length()-1-lastWord.length()));
textToDisplay.append(" ");
}
// add current proposal to control text content
textToDisplay.append(proposal.getContent());
yourcontrolswt.setText(textToDisplay.toString());
}
}
});
If you want more you can also have your own content proposal provider If you need a particular object instead of string or something like.
public class SubstringMatchContentProposalProvider implements IContentProposalProvider {
private List<String> proposals = Collections.emptyList();
#Override
public IContentProposal[] getProposals(String contents, int position) {
if (position == 0) {
return null;
}
String[] allWords = contents.split(" ");
// no words available
if(allWords.length == 0 || StringUtils.isBlank(allWords[allWords.length-1]))
return null;
// auto completion on last word found
String lastWordFound = allWords[allWords.length-1];
Pattern pattern = Pattern.compile(lastWordFound,
Pattern.LITERAL | Pattern.CASE_INSENSITIVE /*| Pattern.UNICODE_CASE*/); // this should be not used for better performances
IContentProposal[] filteredProposals = proposals.stream()
.filter(proposal -> proposal.length() >= lastWordFound.length() && pattern.matcher(proposal).find())
.map(ContentProposal::new).toArray(IContentProposal[]::new);
// no result
return filteredProposals.length == 0 ? null : filteredProposals;
}
public void setProposals(List<String> proposals) {
this.proposals = proposals;
}
}
I need a special keyword in my code to be replaced by a consequence of symbols before the build.
For example, I want hello to be replaced by Debug.Log("Hello");
According to this, MonoDevelop didn't have this feature in 2006. Has anything changed?
If no, are there any plugins/external tools implementing it?
I don't think that switching to another IDE will be helpful unless it can use code completion for unity3d.
Please, don't answer that macro definitions are evil.
Update:
I understood that my example was too abstract. In fact, I want to replace read("name"); with
var name;
name=gameObject.Find("name");
if(!name)
return;
name=name.param;
1)Every script should have all needed variables declared + a variable called "self".
2)They should be public.
3)
public static function set_var(target,name:String,value)
{
var fi=typeof(target).GetField(name);
fi.SetValue(target,value);
}
public static function read(name:String,target):String
{
set_var(target,"self",target);
return "var rtmp=self.gameObject.GetComponent(\""+name+"\");"+"if(!rtmp)return;"+name+"=rtmp.param;";
}
4)eval(read("name",this));
5)As far as I know, it wouldn't work in unity C#
6)Probably, set_var can be replaced by assignment
Far better solution:
var component_names = ["hello","thing","foo"];
var component;
for(var name:String in component_names)
{
component = gameObject.GetComponent(name);
if(!component)
return;
set_var(this,name,component.param);
}
(Requires set_var() from the first one)
I want to get the information about a field modifier. To be precise, I want to find out is the field a static one. For example, I want to examine the following code:
ASTParser parser = ASTParser.newParser(AST.JLS3);
How can I infer that the JLS3 is static field? I used getModifiers with Modifier.isStatic when analyzing methods and it worked fine. However, now I am not able to get the information that JLS3 in above code snippet is a static field. Is there something I am missing?
EDIT:
This is the code that I am using:
private boolean visit(SimpleName name){
boolean isStatic = Modifier.isStatic(name.resolveTypeBinding().getModifiers());
...
return true;
}
isStatic is false in the case of JLS3.
That's obviously wrong, use name.resolveBinding() instead of name.resolveTypeBinding() --- so you should get an object of type IVariableBinding.
name.resolveTypeBinding() returns the binding for the type of the field, but not the binding of field itself, which is not what you want here.
I'm looking for a way to filter files in an "Open" window. I'm using NetBeans IDE 6.5.
I did some research, and this is what i came up with, but for some reason it's not working.
//global variable
protected static FileFilter myfilter;
//in declaration of variables
fchoLoad.setFileFilter(myfilter);
//inside main
myfilter = .... (i actually delted this part by accident, i need to filter only .fwd files. can anybody tell me what goes here?)
If I understand it correctly, you want to create your own file chooser and be able to filter just some files (.fwd in your case). I guess this is more general Java question (not only NetBeans) and I suggest reading this tutorial
Anyway, your "myfilter" should look like this:
myfilter = new FileFilter() {
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".fwd")
|| f.isDirectory();
}
public String getDescription() {
return "FWD Files"; //type any description you want to display
}
};
Hope that helps
Does anyone know of an Eclipse plug-in or method to get Eclipse to generate getter/setters on one line like this:
public String getAbc() { return abc; }
Instead of
public String getAbc() {
return abc;
}
I'm on Eclipse v. 3.2.2.
Thanks.
I don't know how to make Eclipse generate them in the format you want, but you could do a search/replace using these regular expressions after the methods are generated:
Find:
(?m)((?:public |private |protected )?[\w$]+) (get|set|is)([\w$]+)\(([\w$]+(?:\[\])? [\w$]+)?\) \{\s+(return [\w$]+;|this.[\w$]+ = [\w$]+;)\s+\}
Replace by:
$1 $2$3($4) { $5 }
This expression will transform the generated getters and setters to be one line. Don't worry about running it with a mixture of transformed and newly generated methods; it will work just fine.
I think matching generics is important as well, so the correct regexp is:
(?m)((?:public |private |protected )?[\w\<\>$]+) (get|set|is)([\w$]+)\(([\w\<\>$]+ [\w$]+)?\) \{\s+(return [\w$]+;|this.[\w$]+ = [\w$]+;)\s+\}
As a variation of the regexp replacement approach, the following reformats the whitespace so that setters are followed by a blank line, but getters are not.
Find:
(\s(?:get|is|set)\w+\([^)]*\))\s*\{\s*(?:([^=;}]+;)\s*\}\s*(\R)|([^=;}]+=[^=;}]+;)\s*\}\s*(\R))
Replace by:
$1 { $2$4 } \R$5
Results in:
int getTotal() { return total; }
void setTotal(int total) { this.total = total; }
List<String> getList() { return list; }
void setList(List<String> list) { this.list = list; }
Map.Entry<String, Integer> getEntry() { return entry; }
void setEntry(Map.Entry<String, Integer> entry) { this.entry = entry; }
It's a minor aesthetic thing, but I figured that if you're looking for an answer to this question, then you're probably (almost) as anal as me ;-)
I know my regexp conditions are not as strict as those of #Hosam, but I haven't experienced any "false positive" replacements.
Java code formatting in Eclipse does not differentiate between getters/setters and any other methods in a class. So this cannot be done by built-in eclipse formatting.
You will need either to:
run a search/replace with the aforementioned regex
get en external plugin like PMD or CheckStyle and enforce a regex rule based on previous option
You can use fast code plug-in to generate this kind of getter setters. The details are given here : http://fast-code.sourceforge.net/documentation.htm#create-new-field.
I wanted to post as a comment to the designated answer, but I don't seem to be able to.
I modified Hosam Aly's answer to work with generic and inner types of the form:
List<X>
and
Map.Entry
The revised regular expression search string is:
(?m)((?:public |private |protected )?[\w\.\<\>$]+) (get|set|is)([\w$]+)\(([\w\.\<\>$]+ [\w$]+)?\) \{\s+(return [\w\.\<\>$]+;|this.[\w$]+ = [\w$]+;)\s+\}
This regular expression allows for angle brackets and a dot in the type.
For example:
public List<String> getStringList()
and
public void setStringList(List<String> list)
and
public Map.Entry getEntry ()
And the replace string is the same as before:
$1 $2$3($4) { $5 }