Custom Eclipse RAP Dialog with JFace from Viewpart - java

I have a class "View" which extends a ViewPart.
After sth in my class I want to show a dialog, which contains of two labels.
First I used "InputDialog" like this:
Composite composite = new Composite(top, SWT.NONE);
Label label= new Label(tmpComposite, SWT.NONE);
label.setText("");
InputDialog dlg;
dlg = new InputDialog(Display.getCurrent().getActiveShell(),
"Title", "Some Text", label.getText(), insertValidator());
if (dlg.open() == Window.OK) {
//Do sth.
}
This works. But now I have two labels. How can I realize it?
I found a few solutions, but none of them is working in a ViewPart or with Eclipse RCP.
Thanks for your help!
By the way if your solution is to call a java class from my "View", how can I come back to "View" and how can I see my new dialog? Tried it, not working.

You need to create a custom dialog by extending org.eclipse.jface.dialogs.Dialog:
public class MyDialog extends Dialog
{
public MyDialog(Shell parentShell)
{
super(parentShell);
}
#Override
protected Control createDialogArea(Composite parent)
{
Composite container = (Composite)super.createDialogArea(parent);
// Add your controls here
return container;
}
}
You use this in a similar way to InputDialog
MyDialog dialog = new MyDialog(shell);
dialog.open();
Have a look at http://www.vogella.com/articles/EclipseDialogs/article.html for some more details.

Related

Get all existing JTextField components in a window and add popup menu to each?

I need to get all existing TextFields in an app window so that I can be able to automatically add popup menus to all of them. How do you do that?
The code is below. How come when I call:
JTextFieldRegularPopupMenu.addToAll(jpanel) it works fine and affects all relevant JTextFields. But when I do it with a JDialog from a regular JDialog netbeans class - JTextFieldRegularPopupMenu.addToAll(this), it doesn't work. What could be the problem?
import java.awt.Component;
import java.awt.Container;
import javax.swing.*;
import java.awt.event.ActionEvent;
import javax.swing.JPopupMenu;
import javax.swing.undo.*;
public class JTextFieldRegularPopupMenu {
public static void addToAll(Container frm) {
JTextField txtTmp = null;
Component[] components = frm.getComponents();
for(int i=0;i<components.length;i++){
if( components[i] instanceof JTextField ){
txtTmp = (JTextField)components[i];
addTo(txtTmp);
}
}
}
public static void addTo(JTextField txtField)
{
JPopupMenu popup = new JPopupMenu();
UndoManager undoManager = new UndoManager();
txtField.getDocument().addUndoableEditListener(undoManager);
Action undoAction = new AbstractAction("Undo") {
#Override
public void actionPerformed(ActionEvent ae) {
if (undoManager.canUndo()) {
undoManager.undo();
}
else {
// No Undo
}
}
};
Action copyAction = new AbstractAction("Copy") {
#Override
public void actionPerformed(ActionEvent ae) {
txtField.copy();
}
};
Action cutAction = new AbstractAction("Cut") {
#Override
public void actionPerformed(ActionEvent ae) {
txtField.cut();
}
};
Action pasteAction = new AbstractAction("Paste") {
#Override
public void actionPerformed(ActionEvent ae) {
txtField.paste();
}
};
Action selectAllAction = new AbstractAction("Select All") {
#Override
public void actionPerformed(ActionEvent ae) {
txtField.selectAll();
}
};
cutAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control X"));
copyAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control C"));
pasteAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control V"));
selectAllAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control A"));
popup.add (undoAction);
popup.addSeparator();
popup.add (cutAction);
popup.add (copyAction);
popup.add (pasteAction);
popup.addSeparator();
popup.add (selectAllAction);
txtField.setComponentPopupMenu(popup);
}
}
Netbeans JFrame Class
public class FrmAddNewUser extends javax.swing.JDialog {
/**
* Creates new form FrmAddNewUser
*/
public FrmAddNewUser(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
myInitComponents();
}
private void myInitComponents()
{
JTextFieldRegularPopupMenu.AddToAll(this); //this doesn't work... but works on jpanel objects..
}
It sounds like you are trying make a uniform change to all existing JTextFields, correct? This really depends, but storing all of the objects in a data structure like an ArrayList might be your best bet. Then use a for loop to apply the same change at each index.
Well you can't really do that in advance because windows and components are not all created at the start of an application. So you would need to manage this dynamically as an application creates and displays a window.
One way might be to use a KeyboardFocusManager to listen for focus changes.
When focus changes you can then invoke the getPopupMenuComponent() method to get the popup menu of the component. If the menu is null, then you need to add your popup menu.
Check out Global Event Listeners for a simple example using this concepts that shows how to select all the text when a text field gains focus.
So you would need to modify all your applications to add this listener when you start the application.
Edit:
Using your current approach you would want to pass in a Window object to a method. Then you can use the getContentPane() method to get the contain holding the components.
Then you can use a class like Swing Utils. This will do a recursive search of the content pane to find all components. Then you iterate through the List and add your logic.
Then this code will work for both frames, dialogs etc.
Also, you don't need to always create custom Actions. You can use Actions from the DefaultEditorKit. For example:
JMenuItem copy = new JMenuItem( new DefaultEditorKit.CopyAction() );
The Action can be shared by all menu items.
Or you can look up the default Action from the ActionMap of the text field. See Key Bindings for the action name to use for the lookup. It will also show you the default key binding used for the Action.

Which Shell object is the correct one constructing a JFace dialog

I'm new in JFace and I'm testing some of its components.
Now I'm a little bit confusing about the different methods to show a modal window.
For example I have an application with a button and want to open a dialog by clicking this button:
public class TestApp extends ApplicationWindow {
protected Control createContents(Composite parent) {
Button button1 = new Button(parent, SWT.PUSH);
button1.setText("Open");
button1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
MyDialog myDialog = new MyDialog(...);
myDialog.open();
}
});
}
}
And now it's the question which Shell object I have to assign to the Dialog constructor.
For that I found the following alternatives:
new MyDialog(getShell());
new MyDialog(parent.getShell());
new MyDialog(new Shell());
But which one is the correct or the most common? And where are the differences?
You should use the shell of the current dialog / window so:
new MyDialog(getShell());
is what you normally use.
This makes the new dialog a child of the current window. So if you close the window the dialog will close as well.

How can i get a balloon in my custom tool tip?

I am creating a custom tooltip where i have a textbox.I am able to do that but i could not get balloon like icon as in the attached picture .Can anyone help me regarding this.
Mytooltip class:
public class MyToolTip extends ToolTip {
private Shell parentShell;
public MyToolTip(Control control) {
super(control,SWT.BALLOON,false);
this.parentShell = control.getShell();
}
#Override
protected Composite createToolTipContentArea(Event event, Composite parent) {
// TODO Auto-generated method stub
Composite comp = new Composite(parent,SWT.NONE);
comp.setLayout(new FillLayout());
Text text = new Text(comp,SWT.BORDER);
text.setText("");
return comp;
}
}
Class using tooltip:
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout(SWT.VERTICAL));
Text text = new Text(shell, SWT.BORDER);
text.setText("sample text field");
MyToolTip myTooltipLabel = new MyToolTip(text);
myTooltipLabel.setShift(new Point(-5, -5));
myTooltipLabel.setHideOnMouseDown(false);
myTooltipLabel.activate();
myTooltipLabel.setRespectDisplayBounds(false);
myTooltipLabel.setRespectMonitorBounds(false);
The problem is, that you are using org.eclipse.jface.window.ToolTip whereas the code that was used to create that screenshot uses org.eclipse.swt.widgets.ToolTip.
The SWT tooltip can have the balloon look by passing SWT.BALLOON as the style bit.
The JFace tooltip does not support SWT.BALLOON, only ToolTip.NO_RECREATE and ToolTip.RECREATE.
So here is the conclusion: You can't subclass the swt tooltip to make it editable. You can't make the JFace tooltip look the way you want it to look. The only solution that is left is creating your own Widget based on Composite or Canvas that does what you want.

Java - how can we get the selected or active frame (getSelectedFrame()) and passe it to JDialog()?

I have a mainFrame with a custom class loader. My loader is loading Pannels in the mainFrame as needed. In one of the pannel i have a JDialog. I want the JDialog box to blocks all windows from the application.
in the pannel i would like to do somting like this.
myDialog = new JDialog(getSelectedFrame(),"",Dialog.ModalityType...);
Thanks!
You dont' need it. If you want to parent the dialog to a frame, dialog, or whatever you can simply call getTopLevelAncestor() on the component that is instantiating the JDialog. That makes knowing the frame irrelevant which helps ensure the person calling or reusing code can use it on any component: frame, another dialog, window, etc.
http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#getTopLevelAncestor()
For example:
class MySpecialPanel extends JPanel {
public MySpecialPanel() {
JButton button = new JButton( new AbstractAction("Show") {
public void actionPerformed(ActionEvent event) {
JDialog dialog = new JDialog( (Window)getTopLevelAncestor(), "Some Title", Dialog.ModalityType.DOCUMENT_MODAL );
dialog.add( new DialogPanel() );
dialog.show();
}
});
}
}
Now by using MySpecialPanel.getTopLevelAncestor() the dialog you are creating doesn't need to know the exact component it is. And the client using MySpecialPanel is free to put this panel into any container it desires be that a JFrame, another JDialog, or whatever.

Tips for displaying an image in swt/jface dialog box

I'm looking for tips/tutorials for displaying an image in a SWT/JFace dialog box.
Can someone please point me to the right way?
Take a look at the official tutorial.
If it is clickable:
Image image = new Image(display,
ShellWithButtonShowingEclipseLogo.class.getResourceAsStream(
"yourpicture"));
Button button = new Button(shell,SWT.PUSH);
button.setImage(image);
If it is not clickable then you can use Label instead of Button.
From your question i understood that you are trying to display
an image in the title area of the daialog Box.
You can make use of the "setTitleImage(Image image)" function in
your main class (class which extends the Dialog )
example:
class DemoDialog extends TitleAreaDialog {
public DemoDialog(Shell parentShell) {
super(parentShell);
}
protected Control createDialogArea(Composite parent) {
setTitle("Demo dialog...");
setTitleImage(ImageObject) // Image to be displayed in your Dialog
}
}

Categories