On a project based on eclipse RCP:
Using SWT and eclipse RCP I want to show error or info mark exactly the same as the following pictures. When the mouse pointer hovers the error mark, a popup shows the reason. I need this capability to show errors or warnings to the user.
It would be very nice to simultaneously have the same error in the problems view.
1) You need ControlDecorations (code taken from https://krishnanmohan.wordpress.com/2012/10/04/inline-validations-in-eclipse-rcp-field-assists/):
Label label = new Label(parent, SWT.NONE);
label.setText("Please Enter Pincode:");
label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
Text txtPincode = new Text(parent, SWT.NONE);
txtPincode.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
//Adding the decorator
final ControlDecoration txtDecorator = new ControlDecoration(txtPincode, SWT.TOP|SWT.RIGHT);
FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry .DEC_ERROR);
Image img = fieldDecoration.getImage();
txtDecorator.setImage(img);
txtDecorator.setDescriptionText("Pls enter only numeric fields");
// hiding it initially
txtDecorator.hide();
txtPincode.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent e) {
Text text = (Text)e.getSource();
String string = text.getText();
char[] chars = new char[string.length()];
string.getChars(0, chars.length, chars, 0);
for (int i = 0; i < chars.length; i++) {
if (!('0' <= chars[i] && chars[i] <= '9')) {
txtDecorator.show();
} else {
txtDecorator.hide();
}
}
}
});
Obviously, you can use ModifyListener or VerifyListener instead of KeyListener. However, doing this manually for every field will result in a lot of unpleasant-to-maintain code. Surprisingly, SWT/JFace doesn't have a good built-in solution for validation, unless you are using data binding (as described in http://www.toedter.com/blog/?p=36). You could write your own small framework to simplify usage.
2) You need to use Markers, e.g.
IResource resource = ... // get your specific IResource
resource.createMarker(IMarker.PROBLEM);
marker.setAttribute(IMarker.MESSAGE, message);
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
Don't forget to remove the marker when the field is validated correctly.
Related
I have a TabFolder in which the TabItems can be created dynamically. I was expecting the tabs to compress like they would in a browser when you have more than will fit on the screen, but they just keep expanding to the right off the screen unless I add a horizontal scroll. Is there a way to make them automatically compress like that?
TabFolder tabsComposite = new TabFolder(builder, SWT.NONE);
tabsComposite.setLayout(new GridLayout(1, false));
GridData tabsLayoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
tabsComposite.setLayoutData(tabsLayoutData);
These are created via button click in another method and any number of them can be created
TabItem item = new TabItem(tabsComposite, SWT.NONE);
tableComposite = new Composite(parent, SWT.BORDER);
GridData tableLayoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
tableComposite.setLayoutData(tableLayoutData);
GridLayout tableLayout = new GridLayout(1, false);
tableComposite.setLayout(tableLayout);
item.setText(nameText.getText());
As #greg-449 mentioned, you can accomplish this with a CTabFolder. By default, the CTabFolder will display no fewer than 20 characters when compressed, so you may not have noticed this default behavior if the tab title were not sufficiently long.
You can call CTabFolder.setMinimumCharacters(int) if you want to change this value.
Default behavior:
With a new minimum set:
In an eclipse plugin, I try to show the user a dialog that just contains a long text. This text should be scrollable.
I tried:
protected Control createDialogArea(Composite parent)
{
Composite container = (Composite) super.createDialogArea(parent);
Text text = new Text(container, SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL| SWT.MULTI);
text.setText(" " + command + "\n\r\n\r" + result);
return container;
}
The text is then shown with a disabled scrollbar (although it is larger than the size of the window). How do I enable scrolling?
The issue seems to be, that your layoutdata on the text is not limited. So SWT appears to have no idea when to enable scrolling.
Setting griddata to fill both did not work for me with your code (just tried).
However, this will:
public static void main(String[] args) {
Display display = Display.getDefault();
Shell s = new Shell(display);
s.setSize(300, 300);
s.setLayout(new GridLayout(1, true));
Composite c = new Composite(s, SWT.NONE);
c.setLayout(new GridLayout(1, false));
Text text = new Text(c, SWT.H_SCROLL | SWT.V_SCROLL | SWT.READ_ONLY);
GridData gridData = new GridData(SWT.NONE, SWT.NONE, false, false);
gridData.heightHint = 200;
gridData.widthHint = 200;
text.setLayoutData(gridData);
text.setBackground(s.getDisplay().getSystemColor(SWT.COLOR_WHITE));
text.setSize(250, 250);
Font stdFont = new Font(text.getDisplay(), new FontData("Consolas", 11, SWT.NORMAL));
text.setFont(stdFont);
StringBuffer buffer = new StringBuffer();
for (int row = 0; row < 40; row++) {
for (int column = 0; column < 20; column++) {
buffer.append("Word ");
}
buffer.append("\n");
}
text.setText(buffer.toString());
s.open();
while (!s.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
By restricting the size of your Text properly (with layoutdata, not setting the size), SWT now knows when the text is bigger than the area and enables scrolling.
Mind you, your solution does work, if you type something after creating (i know not possible for your case).
I works if you also set
text.setLayoutData(new GridData(GridData.FILL_BOTH));
I have a simple swt GUI in my Eclipse application, which looks like the following:
It is implemented very simply:
// creating the label
Label label = new Label(composite, SWT.NONE);
label.setText("Label");
// creating the input field
Text text = new Text(composite, SWT.BORDER);
gridData.horizontalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
text.setLayoutData(gridData);
I would like to add an button between the label and the input element, so that the user can get additional help on what to add inide the field.
It can either be a help button or just a icon which shows information in mouse hover.
How do I implement that? I would appreciate any help!
One of the many ways to do this is to use an information field decoration.
Something like:
Text text = new Text(composite, SWT.BORDER);
FieldDecorationRegistry decRegistry = FieldDecorationRegistry.getDefault();
FieldDecoration infoField = decRegistry.getFieldDecoration(FieldDecorationRegistry.DEC_INFORMATION);
ControlDecoration decoration = new ControlDecoration(text, SWT.TOP | SWT.LEFT);
decoration.setImage(infoField.getImage());
decoration.setDescriptionText("Info decoration text");
GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
// Space for decoration image
gridData.horizontalIndent = decRegistry.getMaximumDecorationWidth();
text.setLayoutData(gridData);
Am developing an eclipse plugin which has few wizard pages. I need the wizard window size to be constant, with "BOTH MAXIMISE and MINIMISE disabled", "window RESIZE disabled".
The point is I am not using SHELL. I am using COMPOSITE instead, which doesn't have any style bits.
How can I do that? I am just providing a part of my entire code:
public void createControl(Composite parent)
{
// TODO Auto-generated method stub
composite = new Composite(parent, SWT.NONE );
composite.setLayout(new GridLayout());
Composite selectAdapterComposite = new Composite(composite, SWT.NONE);
FormLayout reportOptionsCompositeLayout = new FormLayout();
reportOptionsCompositeLayout.marginHeight = 1;
reportOptionsCompositeLayout.marginWidth = 1;
selectAdapterComposite.setLayout(reportOptionsCompositeLayout);
buttonInterfaceSelection = new Button(selectAdapterComposite,SWT.RADIO);
//SWT.CHECK);
buttonInterfaceSelection.setText("Generate adapter using interface !");
buttonInterfaceSelection.setSelection(true);
buttonInterfaceSelection.addListener(SWT.Selection, this);
FormData exportInToExcelButtonData = new FormData();
exportInToExcelButtonData.left = new FormAttachment(null, 5);
buttonInterfaceSelection.setLayoutData(exportInToExcelButtonData);
// One Text Box
Label searchBoxLabel = new Label(selectAdapterComposite, SWT.None);
searchBoxLabel.setText("Search to select [Type to get the results below]");
FormData destinationLabelData = new FormData();
destinationLabelData.top = new FormAttachment(buttonInterfaceSelection, 10);
destinationLabelData.left = new FormAttachment(null, 5);
searchBoxLabel.setLayoutData(destinationLabelData);
searchTextBox = new Text(selectAdapterComposite, SWT.BORDER);
searchTextBox.setSize(20, 2);
FormData searchTextBoxData = new FormData();
searchTextBoxData.top = new FormAttachment(searchBoxLabel, 8);
searchTextBoxData.left = new FormAttachment(null, 5);
// destinationFolderPathData.left = new
// FormAttachment(destinationLabel,15);
searchTextBoxData.width = 400;
searchTextBox.addListener(SWT.Modify, this);
searchTextBox.setEnabled(true);
searchTextBox.setLayoutData(searchTextBoxData);
.
.
.
.
.
setControl(composite);
}
Please help me out.
Your code snippet is irrelevant to your question. The key word is wizard. When you create that wizard, it requires a Shell, so you can set its style bits there.
A WizardDialog's constructor:
public WizardDialog(Shell parentShell, IWizard newWizard)
Example of shell style bits:
parentShell.setShellStyle(parentShell.getShellStyle() | (~SWT.RESIZE));
Thanks for your reply... am a newbie to swt and your answer gave me an important info which I dint know before. Now then, I just took some time to go through widgets documentation and found something.
Composite : Instances of this class are controls which are capable of containing other controls.
Shell : Instances of this class represent the "windows" which the desktop or "window manager" is managing.
I realised that my understanding of SHELL and COMPOSITE was wrong.
Conclusion: So I have to depend upon SHELL to give window resizing controls and using a COMPOSITE does not give me any resizing option...
Correct me if am wrong please.. hope this will be useful to other noobs too...
Thanks.
P.S.: now i understoood, my code segment is irrelevant to my question cos, I am working on someone else's code and trying to make some changes to it. instead of making changes in SHELL (which is created in some other class) i am doing it in COMPOSITE.
I am writing an application and in that I am using JTextArea to display some text. Now I want to show some clickable URL in text area along with the normal text and I want if user click on the URL then the web page that URL referring to should open in new web browser window.
Use JEditorPane with HTMLEditorKit or JTextPane and set content type to "text/html"
..url referring to should open in new web browser window.
// 1.6+
Desktop.getDesktop().browse(URI);
Here is an example of opening links from JTextArea:
JTextArea jtxa = new JTextArea(25,100);
JScrollPane jsp = new JScrollPane(jtxa);
JPanel jp = new JPanel();
jp.add(jsp);
jp.setSize(100,50);
jtxa.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
if(me.getClickCount()==2) //making sure there was a double click
{
int x = me.getX();
int y = me.getY();
int startOffset = jtxa.viewToModel(new Point(x, y));//where on jtextarea click was made
String text = jtxa.getText();
int searchHttp = 0;
int wordEndIndex = 0;
String[] words = text.split("\\s");//spliting the text to words. link will be a single word
for(String word:words)
{
if(word.startsWith("https://") || word.startsWith("http://"))//looking for the word representing the link
{
searchHttp = text.indexOf(word);
wordEndIndex = searchHttp+word.length();
if(startOffset>=searchHttp && startOffset<=wordEndIndex)//after the link word was found, making sure the double click was made on this link
{
try
{
jtxa.select(searchHttp, wordEndIndex);
desk.browse(new URI(word)); //opening the link in browser. Desktop desk = Desktop.getDesktop();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}
}
});