| JFace actions problem [message #435938] |
Fri, 07 May 2004 16:58  |
Eclipse User |
|
|
|
Originally posted by: lewandom.USUNTOop.pl
Hi
I would like to use JFace's Action class to handle inserting a node
to TreeViewer. However, there is a problem: the instance of a class
that inherits from Action and handles insertion must be created before
adding menu, so somewhere before execution of createToolBarManager
method. Currently I have it in main class' constructor. Now the Action
needs to know something about the TreeViewer that is to be modified,
but these is created in createContents method, so much later than
the Action is created.
Is there any nice way to solve it?
I give some code bellow (without updating the TreeViewer).
Marcin
--CODE--
class NewConnectionAction extends Action {
protected Connection connections;
protected Shell shell;
public NewConnectionAction(Shell _shell, Connection _connections) {
connections = _connections;
shell = _shell;
setText("&New connection@Ctrl+N");
setImageDescriptor(ImageDescriptor.createFromFile(Amc.class,
"/toolbarButtonGraphics/general/New16.gif"));
}
public void run() {
InputDialog inputDialog = new InputDialog(shell, "Create new connection",
"Plese enter unique connection name:", "New connection", null);
while (true) {
inputDialog.open();
if (inputDialog.getReturnCode() == 0) {
boolean test = false;
int j = connections.getServices().length;
Object[] temp = connections.getServices();
for (int i = 0; i < temp.length; ++i)
if (((Connection) temp[i]).connectionName == inputDialog.getValue()) {
test = true;
break;
}
if (!test) {
connections.addService(new Connection(connections,
inputDialog.getValue()));
break;
}
} else break;
}
}
}
public class Amc extends ApplicationWindow {
protected ExitAction exitAction;
protected NewConnectionAction newConnectionAction;
protected Connection connections = new Connection(null, "root");
// constructors
public Amc() {
super(null);
newConnectionAction = new NewConnectionAction(this.getShell(),
connections);
}
// methods
protected MenuManager createMenuManager() {
MenuManager menuBar = new MenuManager();
MenuManager fileMenu = new MenuManager("&File");
fileMenu.add(newConnectionAction);
menuBar.add(fileMenu);
return menuBar;
}
public Control createContents(Composite parent) {
Composite mainComposite = new Composite(parent, SWT.NONE);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
mainComposite.setLayout(gridLayout);
TreeViewer connectionTree = new TreeViewer(mainComposite, SWT.V_SCROLL |
SWT.BORDER);
{
GridData gridData = new GridData(GridData.FILL_BOTH);
connectionTree.getTree().setLayoutData(gridData);
}
connectionTree.setContentProvider(new ConnectionTreeContentProvider());
connectionTree.setLabelProvider(new ConnectionTreeLabelProvider());
connectionTree.setInput(connections);
return parent;
}
public static void main(String[] args) {
Amc wnd_main = new Amc();
wnd_main.setBlockOnOpen(true);
wnd_main.addMenuBar();
wnd_main.addToolBar(SWT.FLAT);
wnd_main.addStatusLine();
wnd_main.open();
Display.getCurrent().dispose();
}
}
|
|
|
| Re: JFace actions problem [message #436032 is a reply to message #435938] |
Sun, 09 May 2004 13:28  |
Eclipse User |
|
|
|
Originally posted by: ramim.kauhajoki.fi
I solved this by doing a new method in my "main class" (which was in my
case a View where the Tree viewer was). This new method returned the
current selection from the TreeViewer ... by this I was able to add a new
node into it + into right spot of the tree :)
In my case I gave a instance of the view to the action class (when I
implemented the action!!!). By this there are a connection between the
action AND the View ... I think this is what you're looking for?
How I did this? I did some research over the Net and downloaded 3-4
differend kinds of Tree samples and studied how they worked.
Marcin Lewandowski wrote:
> Hi
> I would like to use JFace's Action class to handle inserting a node
> to TreeViewer. However, there is a problem: the instance of a class
> that inherits from Action and handles insertion must be created before
> adding menu, so somewhere before execution of createToolBarManager
> method. Currently I have it in main class' constructor. Now the Action
> needs to know something about the TreeViewer that is to be modified,
> but these is created in createContents method, so much later than
> the Action is created.
> Is there any nice way to solve it?
> I give some code bellow (without updating the TreeViewer).
> Marcin
> --CODE--
> class NewConnectionAction extends Action {
> protected Connection connections;
> protected Shell shell;
> public NewConnectionAction(Shell _shell, Connection _connections) {
> connections = _connections;
> shell = _shell;
> setText("&New connection@Ctrl+N");
> setImageDescriptor(ImageDescriptor.createFromFile(Amc.class,
> "/toolbarButtonGraphics/general/New16.gif"));
> }
> public void run() {
> InputDialog inputDialog = new InputDialog(shell, "Create new connection",
> "Plese enter unique connection name:", "New connection", null);
> while (true) {
> inputDialog.open();
> if (inputDialog.getReturnCode() == 0) {
> boolean test = false;
> int j = connections.getServices().length;
> Object[] temp = connections.getServices();
> for (int i = 0; i < temp.length; ++i)
> if (((Connection) temp[i]).connectionName == inputDialog.getValue()) {
> test = true;
> break;
> }
> if (!test) {
> connections.addService(new Connection(connections,
> inputDialog.getValue()));
> break;
> }
> } else break;
> }
> }
> }
> public class Amc extends ApplicationWindow {
> protected ExitAction exitAction;
> protected NewConnectionAction newConnectionAction;
> protected Connection connections = new Connection(null, "root");
> // constructors
> public Amc() {
> super(null);
> newConnectionAction = new NewConnectionAction(this.getShell(),
> connections);
> }
> // methods
> protected MenuManager createMenuManager() {
> MenuManager menuBar = new MenuManager();
> MenuManager fileMenu = new MenuManager("&File");
> fileMenu.add(newConnectionAction);
> menuBar.add(fileMenu);
> return menuBar;
> }
> public Control createContents(Composite parent) {
> Composite mainComposite = new Composite(parent, SWT.NONE);
> GridLayout gridLayout = new GridLayout();
> gridLayout.numColumns = 2;
> mainComposite.setLayout(gridLayout);
> TreeViewer connectionTree = new TreeViewer(mainComposite, SWT.V_SCROLL |
> SWT.BORDER);
> {
> GridData gridData = new GridData(GridData.FILL_BOTH);
> connectionTree.getTree().setLayoutData(gridData);
> }
> connectionTree.setContentProvider(new ConnectionTreeContentProvider());
> connectionTree.setLabelProvider(new ConnectionTreeLabelProvider());
> connectionTree.setInput(connections);
> return parent;
> }
> public static void main(String[] args) {
> Amc wnd_main = new Amc();
> wnd_main.setBlockOnOpen(true);
> wnd_main.addMenuBar();
> wnd_main.addToolBar(SWT.FLAT);
> wnd_main.addStatusLine();
> wnd_main.open();
> Display.getCurrent().dispose();
> }
> }
|
|
|
Powered by
FUDForum. Page generated in 0.06100 seconds