Eclipse RCP and JFace: Problems with Images in Context menu and TreeViewer - java

I'm working on an Eclipse RCP application. Today I experienced some troubles when displaying images in the context menu. What I wanted to do is to add a column to my table containing images of stars for representing a user rating. On Windows, this causes some problems, since the star images are squeezed up on the left corner of the table cell instead of expanding on the whole cell, but I'll solve that somehow. In addition I have a context menu on the table, with an entry called "rate" where again the different stars from 1 to 5 (representing the rating level) are shown, such that the user can click on it for choosing different ratings. That works fine on Windows.
Now I switched to Linux (Ubuntu) to see how it works out there, and strangely, the stars in the table cell are layed out perfectly, while the stars on the context menu don't even show up.
Rating inside the table cell works http://img187.imageshack.us/img187/4427/starsratingho4.png
star images don't show up http://img514.imageshack.us/img514/8673/contextmenuproblemgt1.png
On the context menu I'm using an action class where I'm setting the image descriptor for the star images:
public class RateAction extends Action {
private final int fRating;
private IStructuredSelection fSelection;
public RateAction(int rating, IStructuredSelection selection) {
super("", AS_CHECK_BOX);
fRating = rating;
fSelection = selection;
setImageDescriptor(createImageDescriptor());
}
/**
* Creates the correct ImageDescriptor depending on the given rating
* #return
*/
private ImageDescriptor createImageDescriptor() {
ImageDescriptor imgDescriptor = null;
switch (fRating) {
case 0:
return OwlUI.NEWS_STARON_0;
case 1:
return OwlUI.NEWS_STARON_1;
case 2:
return OwlUI.NEWS_STARON_2;
case 3:
return OwlUI.NEWS_STARON_3;
case 4:
return OwlUI.NEWS_STARON_4;
case 5:
return OwlUI.NEWS_STARON_5;
default:
break;
}
return imgDescriptor;
}
/*
* #see org.eclipse.jface.action.Action#getText()
*/
#Override
public String getText() {
//return no text, since the images of the stars will be displayed
return "";
}
...
}
Does somebody know why this strange behaviour appears?
Thanks a lot.
(For some strange reason, the images don't appear. Here are the direct URLs:
http://img187.imageshack.us/img187/4427/starsratingho4.png
http://img514.imageshack.us/img514/8673/contextmenuproblemgt1.png)
//Edit:
I did some tries and it seems as if the images just don't appear when using a Checkbox style for the context menu (see constructor of the RateAction). When I switched to a PushButton style, the images appeared, although not correctly scaled, but at least they were shown.

When SWT-images have not shown up for me it has been because:
I've used capital letters in the image filename, but not in the source code. Works on Windows, not Linux.
I've tried to run a x64 version of SWT before it was supported.
I've used VNC. Not sure why it doesn't work, color depth problems?
I've used Ubuntu. The images have shown up fine with Red Hat.
Not sure if this will help you in anyway, but it could perhaps give you a hint where to look.

Maybe this is simply a bug, in which case there would be no real answer to your question.
Look if someone has had a similar problem before in Eclipse Bugzilla
Otherwise, try to make a test case as small as possible that works in Windows but not in Linux (or the other way around) and submit a new bug.

You can enable icons in menus in your Gnome configuration:
Open a terminal
Run gnome-appearance-properties
Select Interface tab
Enable Show icons in menus check box
Now you can see icons in your RCP menus.
See this Eclipse Bug for details: Bug 293720 - [GTK2.18] Menu icons missing

Related

Java JOptionPane Visual Artifact from Window Interaction

I am trying to create a simple menu for a small project using a JSpinner and JOptionPane. I created my desired output, but when I interact with the window or even hover over the buttons in the box, it creates visual artifacts over and over again (see images below):
JOptionPane before mouse hover
JOptionPane after mouse hover
I did some researching and thought it might be due to JOptionPane not being thread safe, but could not get anything to work.
Overall, my specific question is how do I prevent Java from repainting these visual artifacts over my JOptionPane window?
For reference, please see the method I am using to show this menu:
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SpinnerNumberModel;
import javax.swing.JSpinner;
public class Battleship
{
public static void main(String[] args)
{
SpinnerNumberModel battleshipRange = new SpinnerNumberModel(1, 1, 5, 1);
JSpinner rangeSpinner = new JSpinner(battleshipRange);
JOptionPane.showMessageDialog(null, rangeSpinner, "Battleship Number", JOptionPane.QUESTION_MESSAGE);
}
}
I am running this code on BlueJ and am using Windows 10 Pro.
Thank you in advance and apologies if this is a beginner question. I am still fairly new to programming.
EDIT: Updated code to give complete source of problem, but it disappeared. I will keep an eye on it to see where the source of the error first occurred.
After completing my project, I finally found why visual artifacts would pop up in all buttons/radio buttons/etc. in my game.
In the GridWorld source code, under the "GridPanel.java" script, the original programmers created a method called "setToolTipsEnabled(boolean flag)". Its function is to pop up a message box next to the cursor when it is hovering over the grid when the game stops running.
When extending GridWorld to create my project, the method reaches past the grid structure and attempts to create a tool tip underneath anything the cursor hovers over. Thus, creating visual artifacts on buttons/radio buttons/etc.
To fix this, I made sure this method was always set to false as I did not need tool tips for my game anyway. This method was implemented in the "GridPanel.java" and "GUIController.java" scripts (in the GridWorld code). I changed following methods to fix this problem:
GridPanel.java
/**
* Construct a new GridPanel object with no grid. The view will be
* empty.
*/
public GridPanel(DisplayMap map, ResourceBundle res)
{
displayMap = map;
resources = res;
// Phillip Sturtevant: Commented out to prevent visual artifacts
//setToolTipsEnabled(true);
}
GUIController.java
/**
* Stops any existing timer currently carrying out steps.
* Phillip Sturtevant Note: keep tool tips hidden to prevent visual artifacts
*/
public void stop()
{
display.setToolTipsEnabled(false); // hide tool tips while stopped
timer.stop();
running = false;
}
Alternatively, the method calls could be omitted entirely, but I commented them out in case I needed them in the future.
For reference, the method below sets the tool tips visibility in GridWorld (located in "GridPanel.java"):
/**
* Enables/disables showing of tooltip giving information about the
* occupant beneath the mouse.
* #param flag true/false to enable/disable tool tips
*/
public void setToolTipsEnabled(boolean flag)
{
if ("hide".equals(System.getProperty("gridworld.gui.tooltips")))
flag = false;
if (flag)
ToolTipManager.sharedInstance().registerComponent(this);
else
ToolTipManager.sharedInstance().unregisterComponent(this);
toolTipsEnabled = flag;
}

Eclipse PreferencePage "Apply" and "OK" greyed-out

I am a newcomer to Eclipse PreferencePages and am currently creating a new FieldEditorPreferencePage for my project. However, having FileFieldEditors() or DirectoryFieldEditors() greys-out the "Apply" and "OK" buttons in my custom preference page. On the other hand, the other FieldEditors (Boolean- and Combo-) do not disable the "Apply" and "OK" buttons.
Furthermore, changing everything to Xtext's LanguageRootPreferencePage seems to work as well (though I'd prefer not to use it as I want the custom preference page to show up in it's own tab).
For example:
//Simplified example of code
public class XPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage, IWorkbenchPropertyPage {
public XPreferencePage() {
super(FieldEditorPreferencePage.GRID);
setDescription("something");
}
protected void initialize() {
super.initalize();
//IPropertyChangeListeners go here
}
protected void createFieldEditors() {
//"Apply" and "OK" works here
g = new BooleanFieldEditor(SConstants.GENERATOR, "Generate Bindings", getFieldEditorParent());
addField(g);
//"Apply" and "OK" is greyed-out starting here
gp = new FileFieldEditor(SConstants.GENERATOR_PATH, "Generator Path:", false, 0, getFieldEditorParent());
gp.setEmptyStringAllowed(true);
addField(gp);
...
}
...
}
Is there anyway to fix this? I followed the tutorial from Eclipse Article-Field-Editors but it doesn't seem to work for me. Reading online says it can be due to negative IntegerField (which I don't have) or that I'm trying to change the default settings (which I don't have either).
I am using Eclipse Mars 4.5.0.
UPDATE: The code above (partially) worked for FileFieldEditors. However, for some reason I need to click FileFieldEditor field and check and uncheck the checkbox directly above the FileFieldEditor for each FileFieldEditor in the preference page before the "Apply" and "OK" buttons are available again.
Furthermore, I have also implemented a IPropertyChangeListener which deactivates certain fields when certain checkboxes are unchecked which means this "check and uncheck" workaround would not work for me. I have also tried setting the default focus to one of my checkboxes but that didn't work
FileFieldEditor(String name, String labelText, Composite parent) defalult validates the path when the text widget loose the focus that is what you are giving invalid as default value.
For e.g. if you give C:\\User\\XXX then this path should physically exist then and only it will not grey out ok and apply button.
You can also use below constructor by setting your validation stratergy.
FileFieldEditor(String name, String labelText,boolean enforceAbsolute, int validationStrategy, Composite parent)
value of validationStrategy
1 for the editor performs validation only when the text widget
loses focus.
0 for the editor performs validation after every key
stroke.

Eclipse: Ctrl+Shift+Right is incorrect

Consider this simple class:
package net.zomis.test;
public class Test {
public Test() {
}
public void registerSomething(String key) {
}
}
When I have the cursor placed right before registerSomething and pressing CtrlShift→, I'm used to Eclipse only selecting register at first. But now, it selects the entire registerSomething text, when I press it again it selects the entire method (from public void to }), when I press it again it selects the entire class, and the last time the entire file is selected. The exactly same thing happens if I press CtrlShift←.
Also, when I simply click the End key, I get an option to choose: Line End or Text End.
What has gone wrong in my settings for this to happen? Where can I find these keybindings?
If it matters, I have this workspace within my Dropbox-folder to synchronize it between my PC and Mac.
Turns our that the fact that I had the workspace in my Dropbox folder matters a lot. Note to everybody else: Do not share your workspace between computers. How I solved this is the following:
Create a new workspace on each computer
In the computer-specific workspace, import projects from your shared Dropbox/other folder
I noticed that when having a shared workspace on the computers, the Mac somehow changed the key-bindings to the Mac-version, which made them not work on the PC. The correct Ctrl+Shift+→ key-binding on Mac is Alt+Shift+→.
So by using a separate workspace on each computer, I can use that computer's specific key-bindings, without mixing them up.
Open the preferences dialog from the Main Menu: Window->Preferences.
In the dialog, open General->Keys. You'll see a table of keybindings, with a field with type filter text. In that field, type: Ctrl+Shift+Right, and you'll narrow the table down to this key.
The behavior that you expect - only register is selected with the first keypress - is the behavior that I see when using Ctrl+Shift+Right, and the keybinding for me is: Select Next Word.
There is a Restore Command button that should reset this binding if it has been changed.

Netbeans ImageIcon not displaying

I am using the NetBeans GUIBuilder to make a JPanel Form. I added a JLabel and used NetBeans' interface to give it an icon from an external image (.png). The path is verified and the image shows up on the GUIBuilder screen. It even shows up when I click the "Preview Design" button. It DOES NOT show up when I RUN the project. The rest of the GUI appears as it should. Do any of you know why this happening and/or how to fix it?
A lot of you have been asking for an SSCCE. Since the code is generated by the NetBeans Form Builder, I have instead included the steps I took to make the JLabel. The areas of focus are circled in red.
Drag and drop a JLabel into the Form Builder.
Open up the JLabel's properties menu. Enter the empty string ("") for the text field. Click the ellipsis next to icon.
Select External Image and click the ellipsis.
Select the image of choice. In my case it's a .png.
Notice that the image appears in the icon preview.
Close the icon menu and the properties menu, and notice that the image appears as the JLabel's icon on the Form Builder.
Thank you for accepting an unorthodox SSCCE and thank you in advance for your help.
I found out the hard way that relying on Netbeans GUI builder to do everything for you is a mistake.
Just create an icon fetching class like the one below, put the icons in it's package, and use "Custom code" instead of "Image chooser". Sure the icons will not be visible inside NB. But if they show up when the app is running, who cares about that.
package com.example.resource.icons;
import javax.swing.ImageIcon;
public class IconFetch {
private static IconFetch instance;
private IconFetch(){
}
public static IconFetch getInstance() {
if (instance == null)
instance = new IconFetch();
return instance;
}
public ImageIcon getIcon(String iconName) {
java.net.URL imgUrl = getClass().getResource(iconName);
if (imgUrl != null) {
return new ImageIcon(imgUrl);
} else {
throw new IllegalArgumentException("This icon file does not exist");
}
}
public static final String MINESWEEPER_ONE = "one.png";
}
Usage:
IconFetch.getInstance().getIcon(IconFetch.MINESWEEPER_ONE);
If the icon still doesn't show up after trying this, then something might be wrong with the way you layed out components in your form (the label is there but you can't see it).
Hope this helps even though it's a long shot.
I had the same problem, and predi's solution wasn't working either. Then I created a package instead of a folder, and added the images there, and it works now.
I do have a same problem also. But I found the solution.
I create the package in project and put the images inside there.
When I build the project, Netbeans will create 'target' folder and build .class files.
I found that the images that I copied to the package, did not transfer to the 'target' folder.
Interim solution.
4. I copy all image to target folder with the same structure. Then I can run the project directly from Netbeans.
5. Incase you clean the project. Do no.4 again.

plugin development in eclipse

i have this problem with my project these days. i'm developing a plugin in eclipse,
i need to write a text on the active window(coding area) when i click a button.
i use the following code in my button.java class
public class Button implements IWorkbenchWindowActionDelegate {
private IWorkbenchWindow window;
/**
* The constructor.
*/
public Button() {
}
/**
* The action has been activated. The argument of the
* method represents the 'real' action sitting
* in the workbench UI.
* #see IWorkbenchWindowActionDelegate#run
*/
public void run(IAction action) {
MessageDialog.openInformation(
window.getShell(),
"Button",
"Code of the Button goes here");
}
how can i do it inside the run method? here I'm displaying a message, instead of showing a message i want to display some text in the text editor pane. please help me to achieve this.
if you guys can please give me some links to understand about eclipse plug-in developments? any blog posts that are easy to understand will be much better?
You should do something like this. It is completely untested and you will need to add lots of null checks and try-catch blocks, but the code below gets the currently active editor and replaces the current selection with whatever is passed in as an argument:
void method (String text) {
IEditorPart part = Workbench.getInstance().getWorkbenchWindows()[0].getActivePage().getActiveEditor();
IEditorInput editorInput = part.getEditorInput();
if (part instanceof ITextEditor) {
ITextEditor textEditor = (ITextEditor) part;
IDocument doc = textEditor.getDocumentProvider().getDocument(editorInput);
ITextSelection sel = textEditor.getSelectionProvider().getSelection();
doc.replace(sel.getOffset(), sel.getLength(), text);
}
}
It is messy and complicated, but that's the Eclipse framework for you.
This might be a good place for you to look at Eclipse plugin development:
http://www.ibm.com/developerworks/views/opensource/libraryview.jsp?search_by=Create+commercial-quality+eclipse+ide
Developer Works in general has a lot of good content on Eclipse, so if this series is not exactly what you need, you can explore Developer Works for other things.
I'd recommend this one. It is a very good introductory tutorial

Categories

Resources