If you File refers to a file in your workspace, then yes you can convert it
to an IFile.
Use:
java.io.File file = ...;
Path path = new Path(file.getAbsolutePath());
IFile f = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
If it's not in the workspace, there is no corresponding IFile so you cannot
convert.
Monday, May 19, 2008
Thursday, May 15, 2008
Removing an EMF object from an EMF generated model
The toughest part so far i have worked with is removing an EMF object.
I have an EMF generated model and I want to remove an element(object) from the model.
To my surprise all the methods in EObject are returning read only properties and when i try to remove an element from an Eobjects object, in runtime iam getting UNSUPPORTEDTYPEEXCEPTION which is trivial coz all the methods are returning read only elements/lists....
Then I did a sample test... i want to remove "emfObjectToBeRemoved" from "emfParentObject".
EList originalList = emfParentObject.eContents(); // returns a readonly list
List duplicateList = new ArrayList();
duplciateList.addAll(originalList); //line 3 , copying the list
duplicateList.remove(emfObjectToBeRemoved); // line 4
To my shock duplicateList is removing properly but the element is not getting removed from originalList...
I broke my head for some hours, did some googling and i dint have any clue ...Then when i was about to leave office , I felt like googling again and this time miracle happened...I observed an open bug with subject EcoreUtil.remove(object) in Eclipse Bugzilla and for a minute i was surprised seeing that EcoreUtil. I wasnt aware of that.
Then after line 4 , I put this statement
EcoreUtil.delete(emfObjectToBeRemoved);
and when i run it , it is able to delete from EMF model(original list) also .....
I hope poor ppl like me will make use of this tip and merry ...
If you have an alternative for this, plz add it in comments..
I have an EMF generated model and I want to remove an element(object) from the model.
To my surprise all the methods in EObject are returning read only properties and when i try to remove an element from an Eobjects object, in runtime iam getting UNSUPPORTEDTYPEEXCEPTION which is trivial coz all the methods are returning read only elements/lists....
Then I did a sample test... i want to remove "emfObjectToBeRemoved" from "emfParentObject".
EList
List
duplciateList.addAll(originalList); //line 3 , copying the list
duplicateList.remove(emfObjectToBeRemoved); // line 4
To my shock duplicateList is removing properly but the element is not getting removed from originalList...
I broke my head for some hours, did some googling and i dint have any clue ...Then when i was about to leave office , I felt like googling again and this time miracle happened...I observed an open bug with subject EcoreUtil.remove(object) in Eclipse Bugzilla and for a minute i was surprised seeing that EcoreUtil. I wasnt aware of that.
Then after line 4 , I put this statement
EcoreUtil.delete(emfObjectToBeRemoved);
and when i run it , it is able to delete from EMF model(original list) also .....
I hope poor ppl like me will make use of this tip and merry ...
If you have an alternative for this, plz add it in comments..
Getting Active Editor and Active File associated with active editor
I had a requirement to find the active editor in the runtime eclispe and also the file , with which editor is associated to...
This is how we get active editor from RuntimeEclipse
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
The above line gives us the active editor , ofcurse u have to make null checks everytime...
getActiveWorkbenchWindow() first and then make null check and if its not null,then getActivePage()
and again check for null and then getActiveEditor().
and then getEditorInput (method in editorpart) and do the following
IEditorInput input = getEditorInput();
IFile activeFile = ((IFileEditorInput) input).getFile();
IProject project = activeFile.getProject();
like that u can get the project and can create files as u wish,,,
This is how we get active editor from RuntimeEclipse
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
The above line gives us the active editor , ofcurse u have to make null checks everytime...
getActiveWorkbenchWindow() first and then make null check and if its not null,then getActivePage()
and again check for null and then getActiveEditor().
and then getEditorInput (method in editorpart) and do the following
IEditorInput input = getEditorInput();
IFile activeFile = ((IFileEditorInput) input).getFile();
IProject project = activeFile.getProject();
like that u can get the project and can create files as u wish,,,
Sunday, May 4, 2008
You want to display a tooltip for a SWT tree Item ??
Well I had same requirement..I did some googling and found out from eclipse dev blogs that we have to fake it as tree item doesnt have such method toolTip with it :-(
At that time, i found this example to be very useful...Just replace table with tree if u want to show tooltip for a tree item corresponding to a tree....
Btw in the following example the result is same even if u comment the code of LabelListener...
I dont think we need it ...so i scrapped that part and im using the remaining part of the code as it is shamelessly;-)
package org.eclipse.swt.snippets;
/*
* Tool Tips example snippet: create fake tool tips for items in a table
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*/
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class Snippet125 {
public static void main (String[] args) {
final Display display = new Display ();
final Shell shell = new Shell (display);
shell.setLayout (new FillLayout ());
final Table table = new Table (shell, SWT.BORDER);
for (int i = 0; i < 20; i++) {
TableItem item = new TableItem (table, SWT.NONE);
item.setText ("item " + i);
}
// Disable native tooltip
table.setToolTipText ("");
// Implement a "fake" tooltip
final Listener labelListener = new Listener () {
public void handleEvent (Event event) {
Label label = (Label)event.widget;
Shell shell = label.getShell ();
switch (event.type) {
case SWT.MouseDown:
Event e = new Event ();
e.item = (TableItem) label.getData ("_TABLEITEM");
// Assuming table is single select, set the selection as if
// the mouse down event went through to the table
table.setSelection (new TableItem [] {(TableItem) e.item});
table.notifyListeners (SWT.Selection, e);
shell.dispose ();
table.setFocus();
break;
case SWT.MouseExit:
shell.dispose ();
break;
}
}
};
Listener tableListener = new Listener () {
Shell tip = null;
Label label = null;
public void handleEvent (Event event) {
switch (event.type) {
case SWT.Dispose:
case SWT.KeyDown:
case SWT.MouseMove: {
if (tip == null) break;
tip.dispose ();
tip = null;
label = null;
break;
}
case SWT.MouseHover: {
TableItem item = table.getItem (new Point (event.x, event.y));
if (item != null) {
if (tip != null && !tip.isDisposed ()) tip.dispose ();
tip = new Shell (shell, SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
tip.setBackground (display.getSystemColor (SWT.COLOR_INFO_BACKGROUND));
FillLayout layout = new FillLayout ();
layout.marginWidth = 2;
tip.setLayout (layout);
label = new Label (tip, SWT.NONE);
label.setForeground (display.getSystemColor (SWT.COLOR_INFO_FOREGROUND));
label.setBackground (display.getSystemColor (SWT.COLOR_INFO_BACKGROUND));
label.setData ("_TABLEITEM", item);
label.setText (item.getText ());
label.addListener (SWT.MouseExit, labelListener);
label.addListener (SWT.MouseDown, labelListener);
Point size = tip.computeSize (SWT.DEFAULT, SWT.DEFAULT);
Rectangle rect = item.getBounds (0);
Point pt = table.toDisplay (rect.x, rect.y);
tip.setBounds (pt.x, pt.y, size.x, size.y);
tip.setVisible (true);
}
}
}
}
};
table.addListener (SWT.Dispose, tableListener);
table.addListener (SWT.KeyDown, tableListener);
table.addListener (SWT.MouseMove, tableListener);
table.addListener (SWT.MouseHover, tableListener);
shell.pack ();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
At that time, i found this example to be very useful...Just replace table with tree if u want to show tooltip for a tree item corresponding to a tree....
Btw in the following example the result is same even if u comment the code of LabelListener...
I dont think we need it ...so i scrapped that part and im using the remaining part of the code as it is shamelessly;-)
package org.eclipse.swt.snippets;
/*
* Tool Tips example snippet: create fake tool tips for items in a table
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*/
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class Snippet125 {
public static void main (String[] args) {
final Display display = new Display ();
final Shell shell = new Shell (display);
shell.setLayout (new FillLayout ());
final Table table = new Table (shell, SWT.BORDER);
for (int i = 0; i < 20; i++) {
TableItem item = new TableItem (table, SWT.NONE);
item.setText ("item " + i);
}
// Disable native tooltip
table.setToolTipText ("");
// Implement a "fake" tooltip
final Listener labelListener = new Listener () {
public void handleEvent (Event event) {
Label label = (Label)event.widget;
Shell shell = label.getShell ();
switch (event.type) {
case SWT.MouseDown:
Event e = new Event ();
e.item = (TableItem) label.getData ("_TABLEITEM");
// Assuming table is single select, set the selection as if
// the mouse down event went through to the table
table.setSelection (new TableItem [] {(TableItem) e.item});
table.notifyListeners (SWT.Selection, e);
shell.dispose ();
table.setFocus();
break;
case SWT.MouseExit:
shell.dispose ();
break;
}
}
};
Listener tableListener = new Listener () {
Shell tip = null;
Label label = null;
public void handleEvent (Event event) {
switch (event.type) {
case SWT.Dispose:
case SWT.KeyDown:
case SWT.MouseMove: {
if (tip == null) break;
tip.dispose ();
tip = null;
label = null;
break;
}
case SWT.MouseHover: {
TableItem item = table.getItem (new Point (event.x, event.y));
if (item != null) {
if (tip != null && !tip.isDisposed ()) tip.dispose ();
tip = new Shell (shell, SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
tip.setBackground (display.getSystemColor (SWT.COLOR_INFO_BACKGROUND));
FillLayout layout = new FillLayout ();
layout.marginWidth = 2;
tip.setLayout (layout);
label = new Label (tip, SWT.NONE);
label.setForeground (display.getSystemColor (SWT.COLOR_INFO_FOREGROUND));
label.setBackground (display.getSystemColor (SWT.COLOR_INFO_BACKGROUND));
label.setData ("_TABLEITEM", item);
label.setText (item.getText ());
label.addListener (SWT.MouseExit, labelListener);
label.addListener (SWT.MouseDown, labelListener);
Point size = tip.computeSize (SWT.DEFAULT, SWT.DEFAULT);
Rectangle rect = item.getBounds (0);
Point pt = table.toDisplay (rect.x, rect.y);
tip.setBounds (pt.x, pt.y, size.x, size.y);
tip.setVisible (true);
}
}
}
}
};
table.addListener (SWT.Dispose, tableListener);
table.addListener (SWT.KeyDown, tableListener);
table.addListener (SWT.MouseMove, tableListener);
table.addListener (SWT.MouseHover, tableListener);
shell.pack ();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Thursday, May 1, 2008
How do u delete a repository that u added to Mylyn ReportBugs?

Now say u wud have added that repository with some mistakes and u want to delete it??? Try for all the options available in that wizard ..u wont be able to...and if u try to create another one with same URL it says "Already exists" and it wont allow u to create another one....
How to delete is : Open "Task Repositories" View and there u can right click and delete the repostiories u added....I hope eclipse guys will fix it in next releases...
Sunday, April 27, 2008
Best J2EE material from SUN site
Check out all the concepts and examples of J2EE at this link...
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/
Friday, April 18, 2008
Uses of Drag and Drop feature
Now a days the fancy n yet useful feature is drap and drop...
Eclipse(swt/jface) and other frame works supports drag and drop widely...
The way in which we drag drop files in windows , similarly its a gud to have feature for desktop applications as well..say u have a viewer(widget to display data) and u want to fill some data present in a file in that viewer in the form of a table ...instead of asking user to browse through file dialog...its gud to support drag and drop feature from any location in file system onto the application...Surprisingly u can do this minimal lines of code...
Eclipse(swt/jface) and other frame works supports drag and drop widely...
The way in which we drag drop files in windows , similarly its a gud to have feature for desktop applications as well..say u have a viewer(widget to display data) and u want to fill some data present in a file in that viewer in the form of a table ...instead of asking user to browse through file dialog...its gud to support drag and drop feature from any location in file system onto the application...Surprisingly u can do this minimal lines of code...
Subscribe to:
Posts (Atom)
