I wanted to create filter in java for browse file dialogue it should allow only either abc.exe or xyz.exe
I am using swt.widgets.FileDialog
currently I am filtering for *.exe with following string
String[] extensionFilter = { "*.exe" };
fileDialog .setFilterExtensions(extensionFilter);
How can I change this to allow only only abc.exe xyz.exe?
It should search for abc.exe and xyz.exe
example: When you allow multiple extensions(.exe,.dat) it will search for all files with that(.exe,.dat) extensions similarly I want to search for abc.exe and xyz.exe
Thanks in advance
You can use setFilterExtension(String[]), however you have to know how to format the String:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
Button button = new Button(shell, SWT.PUSH);
button.setText("Choose");
button.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
FileDialog dialog = new FileDialog(shell);
dialog.setFilterExtensions(new String[] { "abc.exe;xyz.exe" });
System.out.println(dialog.open());
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
The single String "abc.exe;xyz.exe" tells it that both these are allowed.
Try this
String[] extensionFilter = { "abc.exe","xyz.exe" };
You can use setFilterNames
Which is explained here
It allows String type array variable only. No need to bother about regEx
String[] extensionFilter = { "abc.exe;xyz.exe" };
fileDialog.setFilterExtensions( extensionFilter );
Related
I used SWT browser. I opened page and there is a Button which verify that browser have option to open new browser window. Standard SWT browser have problem with it. Above is how button is defined.
<button class="btn btn-action btn-slim size-w-90pct" data-e2e="openDealerBtn" ng-if="igDefaultRowController.account.isPdSupported" ng-class="{'btn-disabled': igDefaultRowController.shouldDisableOpenPlatformButton}" ng-disabled="igDefaultRowController.shouldDisableOpenPlatformButton" ng-click="igDefaultRowController.openDealer()" ig-click-tracking="pureDealBtn-CFD" id="openDealerButton-XQ7JI"> <span class="btn-label" ig-i18n="" key="AccountOverview.openDealer"><span ng-bind-html="value">Open classic platform</span></span> </button>
[SOLVED!] How to expand SWT browser to open more than one tab ?
I used TabFolder for more tabs.
It is possible to catch URL after click on this button and open in new SWT browser tab ?
SWT uses one of the browsers that are available on the operating system and embeds the main "view" of the browser (the bit that displays the html) in your application. That does mean, however, that it doesn't come with all the fancy stuff like tabs.
As you already discovered yourself, you can get around this by using a TabFolder.
The question now is: how do you know when a tab should be opened. This code (adopted from Snippet270) should help you with this:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Main Window");
shell.setLayout(new FillLayout());
final Browser browser;
try
{
browser = new Browser(shell, SWT.NONE);
}
catch (SWTError e)
{
System.out.println("Could not instantiate Browser: " + e.getMessage());
display.dispose();
return;
}
initialize(display, browser);
shell.open();
browser.setUrl("http://www.w3schools.com/html/tryit.asp?filename=tryhtml_links_target");
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/* register WindowEvent listeners */
static void initialize(final Display display, Browser browser)
{
browser.addOpenWindowListener(e ->
{
Shell shell = new Shell(display);
shell.setText("New Window");
shell.setLayout(new FillLayout());
Browser browser1 = new Browser(shell, SWT.NONE);
initialize(display, browser1);
e.browser = browser1;
});
browser.addVisibilityWindowListener(new VisibilityWindowListener()
{
#Override
public void hide(WindowEvent e)
{
Browser browser = (Browser) e.widget;
Shell shell = browser.getShell();
shell.setVisible(false);
}
#Override
public void show(WindowEvent e)
{
Browser browser = (Browser) e.widget;
final Shell shell = browser.getShell();
if (e.location != null) shell.setLocation(e.location);
if (e.size != null)
{
Point size = e.size;
shell.setSize(shell.computeSize(size.x, size.y));
}
shell.open();
}
});
browser.addCloseWindowListener(e ->
{
Browser browser1 = (Browser) e.widget;
Shell shell = browser1.getShell();
shell.close();
});
}
This will open the link in a new Shell with a new Browser. You can change this, so it creates a new tab and adds the new browser to the new tab.
EDIT
Here's a working example using TabFolder:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Main Window");
shell.setLayout(new FillLayout());
TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);
addNewBrowser(tabFolder, "<a href='http://www.google.co.uk' target='_blank'>Click here!</a>");
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static Browser addNewBrowser(TabFolder folder, String html)
{
TabItem item = new TabItem(folder, SWT.NONE);
Composite c = new Composite(folder, SWT.NONE);
item.setControl(c);
c.setLayout(new FillLayout());
Browser browser = new Browser(c, SWT.NONE);
if (html != null)
{
browser.setText(html);
item.setText("Original tab");
}
else
{
item.setText("New tab");
}
browser.addOpenWindowListener(e ->
{
e.browser = addNewBrowser(folder, null);
});
browser.addVisibilityWindowListener(new VisibilityWindowListener()
{
#Override
public void hide(WindowEvent e)
{
Browser browser = (Browser) e.widget;
Shell shell = browser.getShell();
shell.setVisible(false);
}
#Override
public void show(WindowEvent e)
{
Browser browser = (Browser) e.widget;
final Shell shell = browser.getShell();
if (e.location != null) shell.setLocation(e.location);
if (e.size != null)
{
Point size = e.size;
shell.setSize(shell.computeSize(size.x, size.y));
}
shell.open();
}
});
browser.addCloseWindowListener(e ->
{
Browser browser1 = (Browser) e.widget;
Shell shell = browser1.getShell();
shell.close();
});
folder.setSelection(item);
return browser;
}
I have created an editor, in which I display a canvas. I would like to allow DnD for svg files.
The fact is that I allow to drop files with this part of code :
int operations = DND.DROP_COPY | DND.DROP_MOVE;
Transfer[] types = new Transfer[]{FileTransfer.getInstance()};
DropTarget dt = new DropTarget(parent, operations);
dt.setTransfer(types);
dt.addDropListener(new DropTargetAdapter() {
public void drop(DropTargetEvent event) {
//ToDo
}
But it allows any type of files, and I only want .svg files.
How could I create a condition, which will check if the file dropped is a .svg file or not, and if it is, create a new canvas with the imported file ?
I would like to recover the path of the dropped file too, any idea of how I could do that ?
I don't really know how I could restrict the imported file to svg.
Assuming that all your files have a valid .svg extension, check the file extension in the adapter and only use those files that have the right extension while ignoring files with other extensions.
Then do what you need to do with the files:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Stackoverflow");
shell.setLayout(new FillLayout());
Label label = new Label(shell, SWT.NONE);
DropTarget target = new DropTarget(label, DND.DROP_DEFAULT | DND.DROP_MOVE | DND.DROP_COPY);
Transfer[] transfers = new Transfer[]{FileTransfer.getInstance()};
target.setTransfer(transfers);
target.addDropListener(new DropTargetAdapter()
{
public void drop(DropTargetEvent event)
{
if (event.data == null)
{
event.detail = DND.DROP_NONE;
return;
}
String[] paths = (String[]) event.data;
List<File> files = new ArrayList<>();
for (String path : paths)
{
int index = path.lastIndexOf(".");
if (index != -1)
{
String extension = path.substring(index + 1);
if (Objects.equals(extension, "svg"))
files.add(new File(path));
}
}
System.out.println(files);
}
});
shell.pack();
shell.open();
shell.setSize(400, 300);
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
On SWT spinner (or maybe some other similar SWT widget), how can I force the user to use the UI buttons instead of edit the text from the keyboard. thanks.
SWT.READ_ONLY will prevent the user from entering values. To disable arrow keys as well, you can do something like this:
public static void main(String args[])
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
Spinner spinner = new Spinner(shell, SWT.READ_ONLY);
spinner.addListener(SWT.Verify, new Listener()
{
#Override
public void handleEvent(Event e)
{
if(e.keyCode == SWT.ARROW_UP || e.keyCode == SWT.ARROW_DOWN)
{
e.doit = false;
}
}
});
shell.pack();
shell.setSize(100, shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
shell.open();
while (!shell.isDisposed())
{
if (!shell.getDisplay().readAndDispatch())
shell.getDisplay().sleep();
}
}
This will make sure that the only way to change the Spinner value is using the buttons.
i have another question. I use a ModifyListener for one textfield to activate and deactivate the OK-Button in a swt dialog. It works great.
Now I want to add a ModifyListener for another textfield. I want that the OK-Button only is activated if in both text fields is min one char.
This is the code of the two fields:
descriptionText.addModifyListener(new ModifyListener(){
public void modifyText(ModifyEvent e) {
Text text = (Text) e.widget;
if (text.getText().length() == 0) {
getButton(IDialogConstants.OK_ID).setEnabled(false);
}
if (text.getText().length() >= 1) {
getButton(IDialogConstants.OK_ID).setEnabled(true);
}
}
});
}
the second field:
ccidText.addModifyListener(new ModifyListener(){
public void modifyText(ModifyEvent e) {
Text text = (Text) e.widget;
if (text.getText().length() == 0) {
getButton(IDialogConstants.OK_ID).setEnabled(false);
}
if (text.getText().length() >= 1){
getButton(IDialogConstants.OK_ID).setEnabled(true);
}
}
});
}
I know that it doesn´t work because there are no dependencies between the two buttons.
How can i combine it?
I want to set the ok-button false while both modifylistener detect a char.
If i delete all chars in one testfield the button must be deactivated again.
Thank u.
You can use the same Listener for both Text fields and add it for SWT.KeyUp:
public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout(SWT.VERTICAL));
final Text first = new Text(shell, SWT.BORDER);
final Text second = new Text(shell, SWT.BORDER);
final Button button = new Button(shell, SWT.PUSH);
button.setText("disabled");
button.setEnabled(false);
Listener listener = new Listener()
{
#Override
public void handleEvent(Event e)
{
String firstString = first.getText();
String secondString = second.getText();
button.setEnabled(!isEmpty(firstString) && !isEmpty(secondString));
button.setText(button.isEnabled() ? "enabled" : "disabled");
}
};
first.addListener(SWT.KeyUp, listener);
second.addListener(SWT.KeyUp, listener);
shell.pack();
shell.setSize(300, shell.getSize().y);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static boolean isEmpty(String input)
{
if(input == null)
return true;
else
return input.trim().isEmpty();
}
Looks like this:
The code will basically (on each key stroke) check if both Texts are empty. If so, disable the Button, else enable it.
Consider the following Java (SWT) code:
private static ComboViewer createViewer(final Shell shell) {
final ComboViewer v = new ComboViewer(shell, SWT.DROP_DOWN);
v.setLabelProvider(new LabelProvider());
v.setContentProvider(new ArrayContentProvider());
v.setInput(new String[]{"value 1", "value 2"});
return v;
}
public static void main(final String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setSize(200, 60);
shell.setLayout(new GridLayout());
final ComboViewer v = createViewer(shell);
// This wires up the userSelectedSomething method correctly
v.addSelectionChangedListener(new ISelectionChangedListener() {
#Override
public void selectionChanged(final SelectionChangedEvent event) {
userSelectedSomething();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void userSelectedSomething() {
// This should be called *only if* the user selected from the drop-down
}
public static void userTypedSomething() {
// This should be called *only if* the user typed in the combo
}
I want to call the userTypedSomething method only if the user typed into the combo (and not when they selected from the drop-down). What listener should I add to achieve this? Adding a modify listener to the combo viewer with v.getCombo().addModifyListener(...) is no good as this is triggered for both typing and selection from the combo.
private static ComboViewer createViewer(final Shell shell) {
final ComboViewer v = new ComboViewer(shell, SWT.DROP_DOWN);
v.setLabelProvider(new LabelProvider());
v.setContentProvider(new ArrayContentProvider());
v.setInput(new String[]{"value 1", "value 2"});
return v;
}
private static boolean userTyped;
private static int index = -1;
public static void main(final String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setSize(200, 60);
shell.setLayout(new GridLayout());
final ComboViewer v = createViewer(shell);
/*
* invoked multiple times when combo selection happens
* invoked once when user types
*/
v.getCombo().addVerifyListener(new VerifyListener() {
#Override
public void verifyText(VerifyEvent e) {
userTyped = (e.keyCode != 0);
}
});
v.getCombo().addModifyListener(new ModifyListener() {
#Override
public void modifyText(ModifyEvent e) {
Combo c = (Combo)e.widget;
if(userTyped || index == c.getSelectionIndex() || c.getSelectionIndex() == -1)
{
userTypedOrEditedSomething();
}
index = c.getSelectionIndex();
}
});
// This wires up the userSelectedSomething method correctly
v.addSelectionChangedListener(new ISelectionChangedListener() {
#Override
public void selectionChanged(final SelectionChangedEvent event) {
userSelectedSomething();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void userSelectedSomething() {
// This should be called *only if* the user selected from the drop-down
System.out.println("User selected");
}
public static void userTypedOrEditedSomething() {
// This should be called *only if* the user typed in the combo
System.out.println("User typed or edited");
}
I would suggest you to use Verify event instead Key UP as you might endup handling lot of things (arrow keys, magic keys...etc). Verify is also Key Event but it filter out ALT,CNTRL,SHIFT combination. When user types just check for keycode!=0.
As you pointed out, when you use CNTRL+V ,Right click Menu paste....combo doesn't consider it as key event but it fires verify event to make sure the clipboard text is valid for combo or not. I think this is how it should work as Menu item selection and Key event on combo are different things.
you can always monitor all key events for special actions like copy/paste/delete.
the above sample code should be able to perform what you are looking for.
Since you want to listen to keyboard input, I would suggest listening to SWT.KeyUp.
This should be a good starting point:
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Combo combo = new Combo(shell, SWT.NONE);
combo.add("First");
combo.add("Second");
combo.addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event arg0) {
System.out.println("Selected: " + combo.getItem(combo.getSelectionIndex()));
}
});
combo.addListener(SWT.KeyUp, new Listener() {
#Override
public void handleEvent(Event arg0) {
System.out.println("Typed");
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}