About dialog for html page in SWT application - java

I have an about dialog in Swing that uses a JEditorPane to display an html document (setContentType("text/html");).
I want to creat an about dialog in an Eclipse SWT application and display this same html document which will be displayed formatted (hyperlinks, br etc) same as it did in Swing.
How could I do this? What is the appropriate widget? StyledText?
I started with a Browser in a Handler:
#Execute
public void execute(final Shell currentShell){
//load html file in textReader
Browser browser = new Browser(currentShell, SWT.NONE);
browser.setText(textReader.toString());
}
But nothing is displayed. What is the proper way to resolve this?
Update: Tested #Baz change:
#Execute
public void execute(final Shell currentShell){
currentShell.setLayout(new FillLayout());
//load html file in textReader
Browser browser = new Browser(currentShell, SWT.NONE);
browser.setText(textReader.toString());
currentShell.pack();
}
It completelly ruins the application! The html is loaded covering the existing elements

You can always use the good old JFace Dialog with an embedded Browser widget:
public class BrowserDialog extends Dialog {
private String browserString;
protected BrowserDialog(Shell parentShell, String browserString) {
super(parentShell);
this.browserString = browserString;
}
#Override
protected Control createDialogArea(Composite parent) {
Composite composite = (Composite) super.createDialogArea(parent);
GridLayout layout = new GridLayout(1, false);
composite.setLayout(layout);
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.widthHint = 400;
data.heightHint = 400;
composite.setLayoutData(data);
Browser browser = new Browser(composite, SWT.NONE);
browser.setText(browserString);
browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
return composite;
}
#Override
protected void createButtonsForButtonBar(Composite parent) {
}
#Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText("About");
}
#Override
public void okPressed() {
close();
}
public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
Color gray = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
String hex = String.format("#%02x%02x%02x", gray.getRed(), gray.getGreen(), gray.getBlue());
new BrowserDialog(shell, "<body bgcolor='" + hex + "'><h2>TEXT</h2></body>").open();
}
}
Looks like this:
If you want the dialog buttons, just replace the createButtonsForButtonBar(Composite parent) method with this:
#Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, Dialog.OK, "OK", true);
createButton(parent, Dialog.CANCEL, "Cancel", false);
}
Then it will look like this:

Related

SWT Widgets are not displayed in the Wizard when the Dialog is opened

I created a wizard with one page and has two widgets: a list and a button
but when calling the wizard using dialog.open() the wizard opens but the widgets of the page are not displayed. I don't know what is wrong!
Here is the code of the page
public class SelectCriterionPage extends WizardPage {
private Composite container;
ArrayList<String> listItems=new ArrayList<String>();
List variables,selected;
protected SelectCriterionPage() {
super("CriterionSelection","SelectCriterionPage",null);
setTitle("Selection of criterion variables");
}
#Override
public void createControl(Composite parent) {
container = new Composite(parent, SWT.NONE);
variables=new List(container, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
//fill the list with variables
for(String item:listItems)
variables.add(item);
variables.addSelectionListener(new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent e) {
}
#Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
});
Button btn=new Button(container, SWT.PUSH);
btn.setText("<");
GridData gr=new GridData(GridData.FILL,SWT.CENTER);
btn.setLayoutData(gr);
setControl(container);
}
}
i called my wizard
WizardDialog dialog = new WizardDialog(null, new SelectSlicingCriterionWizard());
dialog.open();
here is my wizard:
public class SelectSlicingCriterionWizard extends Wizard{
IWorkbenchPage workbench;
IStructuredSelection selection;
ArrayList<String> listItems;
public SelectSlicingCriterionWizard() {
super();
setNeedsProgressMonitor(true);
}
#Override
public boolean performFinish() {
System.out.println("Finish clicked");
return true;
}
#Override
public boolean performCancel(){
return true;
}
#Override
public void addPages() {
SelectCriterionPage criterionpage=new SelectCriterionPage();
addPage(criterionpage);
}
#Override
public String getWindowTitle() {
return "Select Criterion Variables";
}
public void init(IWorkbench workbench, IStructuredSelection selection)
{
this.workbench=workbench.getActiveWorkbenchWindow().getActivePage();
this.selection=selection;
}
}
You have not set a Layout for your wizard page Composite. Since you seem to be trying to use Grids this should be:
container = new Composite(parent, SWT.NONE);
container.setLayout(new GridLayout());
Your GridData for the button is using the wrong constructor (the two parameter constructor sets the height and width). Use something like:
GridData gr = new GridData(SWT.BEGINNING, SWT.TOP, false, false);
btn.setLayoutData(gr);
You need to set a Layout for your Composite.
For examples on using GridLayout please see - https://www.eclipse.org/swt/snippets/#gridlayout
For an example on using GridLayout in WizardPage, please see - http://git.eclipse.org/c/platform/eclipse.platform.ui.git/tree/examples/org.eclipse.jface.snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippets/wizard/Snippet047WizardWithLongRunningOperation.java

JFace Dialog disposes widgets when still in use

I have a class that extends jface.dialogs.Dialog. In that dialog is a save button. When the user pressed that button I need to read the values from some swt.widgets.Text fields, but the text fields are disposed already.
What am I doing wrong?
public class MyNewDialog extends Dialog {
private Text txt;
public MyNewDialog(Shell parentShell) {
super(parentShell);
}
#Override
protected Control createDialogArea(Composite parent) {
Composite container = (Composite) super.createDialogArea(parent);
container.setLayout(new GridLayout(2, false));
txt = new Text(container, SWT.BORDER);
txt.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
return container
}
#Override
protected void createButtonsForButtonBar(Composite parent) {
Button saveButton = createButton(parent, IDialogConstants.OK_ID, "Save", true);
saveButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent p_e) {
String string = txt.getText() //widget is disposed exception
}
}
}
Since you're using IDialogConstants.OK_ID for your button, you can use the okPressed() method. No need to add a specific listener.
#Override
protected void okPressed()
{
value = txt.getText();
super.okPressed();
}
Then create a getter method method to return the value variable:
public String getValue()
{
return value;
}

Hide ON_TOP shell in SWT on Display minimimized

I'm trying to hide a SWT shell when the Display is minimized. I'm missing something and would be most thankful for any help.
Additional Info: This shell is actually a popup that gets drawn when the user clicks on a composite. In the end, my goal is to hide this popup-shell when the composite is not visible (user minimized the window or switched between windows, say with Alt+Tab for example).
Here's my code:
static Shell middleClickNodeInfoShell ;
static Label nodeIdLabel ;
void init(){
...
/** Focused node on middle click*/
middleClickNodeInfoShell = new Shell(Display.getDefault(), SWT.BORDER | SWT.MODELESS);
middleClickNodeInfoShell.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));
middleClickNodeInfoShell.setLayout(createNoMarginLayout(1, false));
nodeIdLabel = new Label(middleClickNodeInfoShell, SWT.NONE);
Display.getDefault().addListener(SWT.Iconify,new Listener() {
#Override
public void handleEvent(Event arg0) {
// TODO Auto-generated method stub
middleClickNodeInfoShell.setVisible(false);
}
});
}
#Override
public boolean onMouseClicked(Button button, ScreenPosition screenPos,
final GeoPosition arg2) {
...
nodeIdLabel.setText("Node Id: "+node.getId());
middleClickNodeInfoShell.setLocation(pos.getX()+displayX,pos.getY()+displayY+30);
middleClickNodeInfoShell.setVisible(true);
middleClickNodeInfoShell.pack();
}
Here is sample code that will help you do figure out what you are looking for
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setSize(300, 200);
shell.setText("Shell Example");
shell.setLayout(new RowLayout());
final Button button = new Button(shell, SWT.PUSH);
button.setText("Click Me");
final Shell tip = new Shell(shell,SWT.MODELESS);
tip.setLayout(new FillLayout());
Label lbl = new Label(tip, SWT.NONE);
lbl.setText("***tooltip***");
tip.pack();
shell.addControlListener(new ControlListener() {
#Override
public void controlResized(ControlEvent e) {
changeTipLocation(display, button, tip);
}
#Override
public void controlMoved(ControlEvent e) {
changeTipLocation(display, button, tip);
}
});
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
changeTipLocation(display, button, tip);
tip.open();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void changeTipLocation(final Display display, final Button button, final Shell tip) {
Rectangle bounds = button.getBounds();
Point loc = button.getLocation();
tip.setLocation(display.map(button, null, new Point(loc.x+bounds.width, loc.y+bounds.height)));
}

Eclipse RCP Dialog: Large Text box won't scroll - instead creates a huge dialog window!

I've been banging away at this for a while now and I can't seem to get anywhere. I've tried all of the examples I can find online and nothing seems to work! I haven't been able to find much on this problem which leads me to think I'm missing something basic. . .
In my Eclipse RCP program I want to display a dialog that will show a list of errors that occurred while loading a data file. I have overridden TitleAreaDialog and simply want to display a scrollable Text containing the list of errors and an OK button.
The problem is that the Text vertical scroll bars don't become active - the Text just grows taller to fit the text. This makes the dialog window height increases until it either fits the Text box or until it reaches the height of the screen - and then it just cuts off the bottom of the Text box.
How do I prevent the Dialog/Text box from growing too large? What am I missing?
Thanks for your help!!
-Christine
...
Here is a simple program showing my Dialog:
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class ScrollableDialogRunner {
public static void main(String[] args) {
System.out.println("starting");
Display display = new Display ();
Shell shell = new Shell (display);
String errors = "one\ntwo\nthree\nfour\nfive\n";
for(int i = 0; i < 5; i++){
errors += errors;
}
ScrollableDialog dialog = new ScrollableDialog(shell, "Errors occurred during load", "The following errors occurred while loaded file 'x.data'", errors);
dialog.open();
}
}
class ScrollableDialog extends TitleAreaDialog {
private String title;
private String text;
private String scrollableText;
public ScrollableDialog(Shell parentShell, String title, String text, String scrollableText) {
super(parentShell);
this.title = title;
this.text = text;
this.scrollableText = scrollableText;
}
#Override
protected Control createDialogArea(Composite parent) {
GridLayout layout = new GridLayout();
layout.numColumns = 1;
parent.setLayout(layout);
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
Text scrollable = new Text(parent, SWT.BORDER | SWT.V_SCROLL);
scrollable.setLayoutData(gridData);
scrollable.setText(scrollableText);
return parent;
}
#Override
public void create() {
super.create();
setTitle(title);
setMessage(text, IMessageProvider.ERROR);
}
#Override
protected void createButtonsForButtonBar(Composite parent) {
Button okButton = createButton(parent, OK, "OK", true);
okButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
close();
}
});
}
#Override
protected boolean isResizable() {
return false;
}
}
Assign a size to the dialog; otherwise, the dialog will layout the children asking them for their "preferred" size (which is infinite for the text widget) and will resize itself accordingly.
[EDIT] This version works. See my comments for details.
class ScrollableDialog extends TitleAreaDialog {
private String title;
private String text;
private String scrollableText;
public ScrollableDialog(Shell parentShell, String title, String text, String scrollableText) {
super(parentShell);
this.title = title;
this.text = text;
this.scrollableText = scrollableText;
}
#Override
protected Control createDialogArea(Composite parent) {
Composite composite = (Composite) super.createDialogArea (parent); // Let the dialog create the parent composite
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
gridData.grabExcessVerticalSpace = true; // Layout vertically, too!
gridData.verticalAlignment = GridData.FILL;
Text scrollable = new Text(composite, SWT.BORDER | SWT.V_SCROLL);
scrollable.setLayoutData(gridData);
scrollable.setText(scrollableText);
return composite;
}
#Override
public void create() {
super.create();
// This is not necessary; the dialog will become bigger as the text grows but at the same time,
// the user will be able to see all (or at least more) of the error message at once
//getShell ().setSize (300, 300);
setTitle(title);
setMessage(text, IMessageProvider.ERROR);
}
#Override
protected void createButtonsForButtonBar(Composite parent) {
Button okButton = createButton(parent, OK, "OK", true);
okButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
close();
}
});
}
#Override
protected boolean isResizable() {
return true; // Allow the user to change the dialog size!
}
}

JFace ApplicationWindow: createContents isn't working

I'm attempting to create a window that is divided into three parts. A non-resizable header and footer and then a content area that expands to fill the remaining area in the window. To get started, I created the following class:
public class MyWindow extends ApplicationWindow {
Color white;
Font mainFont;
Font headerFont;
public MyWindow() {
super(null);
}
protected Control createContents(Composite parent) {
Display currentDisplay = Display.getCurrent();
white = new Color(currentDisplay, 255, 255, 255);
mainFont = new Font(currentDisplay, "Tahoma", 8, 0);
headerFont = new Font(currentDisplay, "Tahoma", 16, 0);
// Main layout Composites and overall FillLayout
Composite container = new Composite(parent, SWT.NO_RADIO_GROUP);
Composite header = new Composite(container, SWT.NO_RADIO_GROUP);
Composite mainContents = new Composite(container, SWT.NO_RADIO_GROUP);;
Composite footer = new Composite(container, SWT.NO_RADIO_GROUP);;
FillLayout containerLayout = new FillLayout(SWT.VERTICAL);
container.setLayout(containerLayout);
// Header
Label headerLabel = new Label(header, SWT.LEFT);
headerLabel.setText("Header");
headerLabel.setFont(headerFont);
// Main contents
Label contentsLabel = new Label(mainContents, SWT.CENTER);
contentsLabel.setText("Main Content Here");
contentsLabel.setFont(mainFont);
// Footer
Label footerLabel = new Label(footer, SWT.CENTER);
footerLabel.setText("Footer Here");
footerLabel.setFont(mainFont);
return container;
}
public void dispose() {
cleanUp();
}
#Override
protected void finalize() throws Throwable {
cleanUp();
super.finalize();
}
private void cleanUp() {
if (headerFont != null) {
headerFont.dispose();
}
if (mainFont != null) {
mainFont.dispose();
}
if (white != null) {
white.dispose();
}
}
}
And this results in an empty window when I run it like this:
public static void main(String[] args) {
MyWindow myWindow = new MyWindow();
myWindow.setBlockOnOpen(true);
myWindow.open();
Display.getCurrent().dispose();
}
What am I doing wrong that I don't see three labels the way I'm trying to display them? The createContents code is definitely being called, I can step through it in Eclipse in debug mode.
Apparently, I needed to set the size and location of the labels to get them to appear.

Categories

Resources