JOptionPane.showInputDialog same style with the rest of Eclipse - eclipse

So I'm trying to get the Dialog have the same style as the rest of the Eclipse.
This input is triggered when a button on a menu is pressed.
Here is my code right now , but the Dialog looks the same with null instead of (Component) win as parameter.
// JFrame frame = new JFrame ("Something");
IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
while (b != true) {
userInput = (String) JOptionPane.showInputDialog((Component) win, "Entersomething", "GDB Server Connection Port:", JOptionPane.DEFAULT_OPTION, null,null,2345);
if (userInput != null && userInput.matches("[0-9]+")) {
b = true; }
else {
JOptionPane.showMessageDialog(Component) win, "Please enter valid input","Error",JOptionPane.WARNING_MESSAGE);
}
}
return userInput ;

Use LookAndFeel to have same style in whole application instead:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
EDIT:
Have a look at this then:
http://www.programcreek.com/java-api-examples/index.php?api=org.eclipse.swt.widgets.MessageBox
http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/DialogExamples.htm
http://www.vogella.com/tutorials/EclipseDialogs/article.html

Related

Custom go to line dialog eclipse

Is it possible to subclass the Go To Line dialog of the eclipse.
I want to create a similar dialog "Go To Index" with a custom "OK" action.
The Go to Line dialog is an inner class of org.eclipse.ui.texteditor.GotoLineAction so it cannot be subclassed.
However it is just an extension of InputDialog and the code to actually move to a line is simply:
int line = .... line number ...
ITextEditor editor = getTextEditor();
IDocumentProvider provider = editor.getDocumentProvider();
IDocument document = provider.getDocument(editor.getEditorInput());
try {
int start = document.getLineOffset(line);
editor.selectAndReveal(start, 0);
} catch (BadLocationException x) {
// ignore
}

How to Keep Tabs on a Particular Tab?

In my firefox sdk addon, I want to use a custom webpage from my data directory as my settings/about page.
But I am having trouble keeping tabs on the tab!
So I have a button that calls the OptionsPanel() function to open my webpage in a new tab. Now, I want to make it so if the user forgets that tab is open and pushes the button again, that it activates the already-open settings tab. That means I need to know that the tab is open and I need to be able to switch to it if it is OR open it if it is not already open.
Here is what I've come up with so far, but it doesn't work; it just always opens a new tab. I don't even know if I'm barking up the right tree.
const tabs = require("sdk/tabs");
var optionsTab;
function OptionsPanel(){
var opsTab = GetTabByID(optionsTab.id);
if(opsTab == null){
tabs.open( data.url("OptionsPanel.html") );
optionsTab.id = tabs.tab.id; <======errors out as undefined
}else{
opsTab.activate();
}
}
//return a handle to the tab that matches specified tab id
function GetTabByID(whatid){
for(let thistab of tabs){
if(thistab.id = whatid){
return thistab;
}
}
return null;
}
So, here are my goals:
Open my page in a new tab if it isn't already open.
If the tab is already open, then switch to that tab.
If the page is open when the browser loads, then be ready to switch to that tab if the user pushes the options button.
Why would you think tabs module has a tab property?
Normally you would use the activeTab property instead. However it does not get updated immediately after tabs.open is called. One has to use tabs[tabs.length - 1] instead.
const tabs = require("sdk/tabs");
var optionsTabId;
function OptionsPanel(){
var opsTab = GetTabByID(optionsTabId);
if (opsTab == null) {
tabs.open( data.url("OptionsPanel.html") );
optionsTabId = tabs[tabs.length - 1].id;
} else {
opsTab.activate();
}
}
Additionally, you made a mistake in GetTabByID.
//return a handle to the tab that matches specified tab id
function GetTabByID(whatid){
for(let thistab of tabs){
if(thistab.id == whatid){ // use == to compare
return thistab;
}
}
return null;
}
Keep in mind this assumes that it is not possible to navigate away from your options tab. I would check optsTab.url just in case.
Alternatively you could make use of the tab event interface
const tabs = require("sdk/tabs");
const OPTIONS_URL = data.url("OptionsPanel.html");
var optionsTab = null;
function OptionsPanel(){
if (optionsTab == null) {
tabs.open(OPTIONS_URL);
tabs.once('ready', function(tab) {
optionsTab = tab;
optionsTab.on('ready', function readyListener(tab) {
if (tab.url !== OPTIONS_URL) {
optionsTab.off('ready', readyListener);
optionsTab = null;
}
})
optionsTab.once('close', function(tab) {
optionsTab = null;
})
});
} else {
optionsTab.activate();
}
}

Open a file in an Eclipse editor and set the focus on this editor

I have a strange problem which puzzles me. I open a file in an editor in Eclipse with this code:
final IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow window = wb.getActiveWorkbenchWindow();
final IWorkbenchPage page = window.getActivePage();
wb.getProgressService().runInUI( window, new IRunnableWithProgress() {
#Override
public void run( IProgressMonitor monitor ) throws InvocationTargetException, InterruptedException {
if( null == monitor ) {
monitor = new NullProgressMonitor();
}
try {
monitor.beginTask( "Append to file", 2 );
ITextEditor editor = (ITextEditor) IDE.openEditor( page, file );
monitor.worked( 1 );
// TODO Bug: Editor is active, it has the focus but doesn't process keypress events :-(
} catch( Exception e ) {
throw new InvocationTargetException( e, "Error appending to file " + file );
} finally {
monitor.done();
}
}
}, null );
I collected the bits and pieces from several sources on the Internet.
The strange problem is that the editor seems to have the focus (the tab is highlighted and I see the blue border around it)
But there is no cursor visible in the editor and when I type something, nothing happens (also nothing happens elsewhere in the workbench).
I also tried ITextEditor editor = (ITextEditor) IDE.openEditor( page, file, true ); but with the same result.
When I click on the tab, the cursor appears and I can use the editor. Pressing F12 has no effect, though.
And ideas?
Try page.activate(editor); (even though it should already be active).

Working Set Selection Programmatically in Eclipse

I want to achieve the functionality of selecting a working set programmatically. I tried with the below code:
IWorkingSetManager wsMgr = PlatformUI.getWorkbench().getWorkingSetManager();
IWorkingSet ws = wsMgr.getWorkingSet("custom");
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IWorkingSet[] windowset = new IWorkingSet[]{ws};
page.setWorkingSets(windowset);
But the above code does not work and the Project Explorer does not show the working set.
Why does not the above code work and what is the solution for the above?
For updating the ProjectExplorer view with a working set, I tried the below code
IWorkingSetManager wsMgr = PlatformUI.getWorkbench().getWorkingSetManager();
IWorkingSet ws = wsMgr.getWorkingSet("custom");
ProjectExplorer pView = (ProjectExplorer)page.findView(IPageLayout.ID_PROJECT_EXPLORER);
pView.getCommonViewer().setInput(ws);
The above code displays the content of the working set in ProjectExplorer, but that is not persisted. I mean once Eclipse is restarted, instead of the working set, all projects are getting displayed.
Here's an example handler I created that can set working sets programmatically in a Project Explorer and turn on top level workingsets if it's not already on:
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow workbenchWindow = HandlerUtil
.getActiveWorkbenchWindowChecked(event);
IWorkbenchPage page = workbenchWindow.getActivePage();
IWorkingSetManager manager = workbenchWindow.getWorkbench()
.getWorkingSetManager();
ProjectExplorer projExplorer = (ProjectExplorer) page
.findView(IPageLayout.ID_PROJECT_EXPLORER);
// This is just a test, to ensure we got hold on the correct object for
// Project Explorer.
// The project explorer will get focus now.
projExplorer.setFocus();
// Obtain list of all existing working sets.
// This assumes that the debug workspace used have some working sets
// prepared.
IWorkingSet[] allExistingSets = manager.getWorkingSets();
IWorkingSet workingSet = null;
// The prints information about all working sets.
for (IWorkingSet myset : allExistingSets) {
workingSet = myset;
IAdaptable[] elems = myset.getElements();
System.out.println("Working set " + myset.getName() + " has "
+ elems.length + " projects.");
for (IAdaptable elem : elems) {
System.out.println("Working set " + myset.getName()
+ " contains " + elem.toString());
}
}
page.setWorkingSets(allExistingSets);
NavigatorActionService actionService = projExplorer
.getNavigatorActionService();
CommonViewer viewer = (CommonViewer) projExplorer
.getAdapter(CommonViewer.class);
INavigatorContentService contentService = viewer
.getNavigatorContentService();
try {
IExtensionStateModel extensionStateModel = contentService
.findStateModel(WorkingSetsContentProvider.EXTENSION_ID);
extensionStateModel.setBooleanProperty(
WorkingSetsContentProvider.SHOW_TOP_LEVEL_WORKING_SETS,
true);
projExplorer.setRootMode(ProjectExplorer.WORKING_SETS);
WorkingSetActionProvider provider = (WorkingSetActionProvider) getActionProvider(
contentService, actionService,
WorkingSetActionProvider.class);
IPropertyChangeListener l = provider.getFilterChangeListener();
PropertyChangeEvent pevent = new PropertyChangeEvent(this,
WorkingSetFilterActionGroup.CHANGE_WORKING_SET, null,
page.getAggregateWorkingSet());
l.propertyChange(pevent);
viewer.refresh();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static CommonActionProvider getActionProvider(
INavigatorContentService contentService,
NavigatorActionService actionService, Class cls) throws Exception {
CommonActionProvider provider = null;
CommonActionProviderDescriptor[] providerDescriptors = CommonActionDescriptorManager
.getInstance().findRelevantActionDescriptors(contentService,
new ActionContext(new StructuredSelection()));
if (providerDescriptors.length > 0) {
for (int i = 0; i < providerDescriptors.length; i++) {
provider = actionService
.getActionProviderInstance(providerDescriptors[i]);
if (provider.getClass() == cls)
return provider;
}
}
return null;
}
It doesn't reset the radio button in the view menu for Top Level Elements though. It also has to use internals to work.
After setting the new working sets into the active page. Did you change your project explorer view into working sets mode?
Please find the project explorer view and do set the mode.
ProjectExplorer projExplorer = (ProjectExplorer) page.findView(IPageLayout.ID_PROJECT_EXPLORER);
projExplorer.setRootMode(ProjectExplorer.WORKING_SETS);

How to remove subversive action in Synchronize view?

I'm integrating Subversive 0.7.8 into an Eclipse Platform 3.4.2 RCP app.
I want to remove (or disable) the SVN "Commit" action in the popup menu of the "Synchronize" view.
How can I do ... ?
Thank you for your help.
JM.D
Why would you need to do that ?
Can't you simply make it that your users don't have the right to commit using svn rights ?
Two ways: Either alter the plugin.xml files inside the plugins from subversion to remove the contributions (which means that you have to keep your own version of the plugins), or you can remove specific contributions from the platform.
The removal normally takes place in the class that extends the IApplication interface, before you launch the actual Platform.
This is basically a hack, but it will allow you to do what you want without touching the subversion plugins. I don't know the names of the contributions (You would have to look them up in the source code from the plugins) but the code looks like:
IExtensionRegistry extensionRegistry = InternalPlatform.getDefault().getRegistry();
List uiExtensionsToRemove = Arrays.toList(new String[] {"org.eclipse.ui.views.ProgressView" }); // Removing the progress view in this example
String[] tmpNamespaces = extensionRegistry.getNamespaces();
for (int i = 0; i < tmpNamespaces.length; i++) {
String tmpNamespace = tmpNamespaces[i];
try {
IExtension[] tmpExtensions = extensionRegistry.getExtensions(tmpNamespace);
for (int j = 0; j < tmpExtensions.length; j++) {
IExtension tmpExtension = tmpExtensions[j];
ExtensionHandle tmpEHandle = (ExtensionHandle)tmpExtension;
String tmpEPUID = tmpEHandle.getExtensionPointUniqueIdentifier();
if ("org.eclipse.search.searchPages".equals(tmpEPUID) || "org.eclipse.ui.preferencePages".equals(tmpEPUID) || "org.eclipse.ui.popupMenus".equals(tmpEPUID) || "org.eclipse.ui.actionSets".equals(tmpEPUID)
|| "org.eclipse.ui.views".equals(tmpEPUID) || "org.eclipse.ui.perspectives".equals(tmpEPUID)) {
// only remove part of ui extensions
if (tmpEHandle.getNamespace().startsWith("org.eclipse.ui")) {
String idOfFirstExtension = tmpEHandle.getConfigurationElements()[0].getAttribute("id");
if (!uiExtensionsToRemove.contains(idOfFirstExtension)) {
continue;
}
}
removeExtension(tmpEHandle);
}
} catch (InvalidRegistryObjectException iroe) {
}
//System.out.println("Namespace: " + tmpNamespace);
}
private void removeExtension(ExtensionHandle extensionHandle) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException {
if (removeExtensionMethod == null) {
removeExtensionMethod = extensionRegistry.getClass().getDeclaredMethod("removeExtension", new Class[] { int.class });
removeExtensionMethod.setAccessible(true);
}
// well, this is some magic:
int tmpExtId = extensionHandle.hashCode();
removeExtensionMethod.invoke(extensionRegistry, new Object[] { new Integer(tmpExtId) });
}
You should certainly check out Activities.