X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.utils.ui%2Fsrc%2Forg%2Fsimantics%2Futils%2Fui%2Fwidgets%2FFileOrDirectorySelectionWidget.java;h=19390e5667f82c6d3c4abab2f4eae943874efee9;hp=f9e986c895443b0c5679697829fc55c15211703e;hb=f015145947a0aaf44bd11f8dab45aef96feed149;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/widgets/FileOrDirectorySelectionWidget.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/widgets/FileOrDirectorySelectionWidget.java index f9e986c89..19390e566 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/widgets/FileOrDirectorySelectionWidget.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/widgets/FileOrDirectorySelectionWidget.java @@ -1,118 +1,130 @@ -package org.simantics.utils.ui.widgets; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; - -import org.eclipse.jface.layout.GridDataFactory; -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.ModifyEvent; -import org.eclipse.swt.events.ModifyListener; -import org.eclipse.swt.events.SelectionAdapter; -import org.eclipse.swt.events.SelectionEvent; -import org.eclipse.swt.layout.GridData; -import org.eclipse.swt.layout.GridLayout; -import org.eclipse.swt.widgets.Button; -import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Group; -import org.eclipse.swt.widgets.Text; - -public abstract class FileOrDirectorySelectionWidget extends Composite{ - - Text fileText; - String filename[]; - - private List listeners = new ArrayList(); - - public FileOrDirectorySelectionWidget(Composite parent, String name,int style) { - super(parent, style); - this.setLayout(new GridLayout(1,false)); - GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.TOP).applyTo(this); - - Group fileGroup = new Group(this, SWT.NONE); - GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.TOP).applyTo(fileGroup); - - fileGroup.setText(name); - fileGroup.setLayout(new GridLayout(2, false)); - - fileText = new Text(fileGroup, SWT.SINGLE|SWT.BORDER); - GridData data = new GridData(); - data.grabExcessHorizontalSpace = true; - data.horizontalAlignment = SWT.FILL; - fileText.setLayoutData(data); - - Button browseButton = new Button(fileGroup, SWT.PUSH); - browseButton.setText("Browse"); - data = new GridData(); - browseButton.setLayoutData(data); - - - - browseButton.addSelectionListener(new SelectionAdapter() { - @Override - public void widgetSelected(SelectionEvent e) { - String[] name = openDialog(); - if (name != null) { - setFilename(name); - } - } - }); - - fileText.addModifyListener(new ModifyListener() { - - @Override - public void modifyText(ModifyEvent e) { - String file = fileText.getText(); - setFilename(file.split(",")); - - } - }); - } - - protected abstract String[] openDialog(); - - protected abstract boolean isValid(File file); - - protected void setFilename(String[] filename) { - String text = ""; - for (String s : filename) { - text += s + ","; - } - if (text.length() > 2) - text = text.substring(0, text.length() - 1); - - if (!text.equals(fileText.getText())) - fileText.setText(text); - - boolean accept = true; - for (String s : filename){ - File file = new File(s); - if (!isValid(file)) { - accept = false; - break; - } - } - if (accept) - this.filename = filename; - else - this.filename = null; - - for (FileSelectionListener l : listeners) { - l.fileSelected(this, this.filename); - } - } - - public String[] getFilename() { - return filename; - } - - public void addListener(FileSelectionListener listener) { - this.listeners.add(listener); - } - - public void removeListener(FileSelectionListener listener) { - this.listeners.remove(listener); - } - - -} +package org.simantics.utils.ui.widgets; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Group; +import org.eclipse.swt.widgets.Text; + +public abstract class FileOrDirectorySelectionWidget extends Composite{ + + Text fileText; + String filename[]; + + private List listeners = new ArrayList(); + + public FileOrDirectorySelectionWidget(Composite parent, String name,int style) { + super(parent, style); + this.setLayout(new GridLayout(1,false)); + GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.TOP).applyTo(this); + + Group fileGroup = new Group(this, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.TOP).applyTo(fileGroup); + + fileGroup.setText(name); + fileGroup.setLayout(new GridLayout(2, false)); + + fileText = new Text(fileGroup, SWT.SINGLE|SWT.BORDER); + GridData data = new GridData(); + data.grabExcessHorizontalSpace = true; + data.horizontalAlignment = SWT.FILL; + fileText.setLayoutData(data); + + Button browseButton = new Button(fileGroup, SWT.PUSH); + browseButton.setText("Browse"); + data = new GridData(); + browseButton.setLayoutData(data); + + + + browseButton.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + String[] name = openDialog(); + if (name != null) { + setFilename(name,true); + } + } + }); + + fileText.addModifyListener(new ModifyListener() { + + @Override + public void modifyText(ModifyEvent e) { + String file = fileText.getText(); + String files[] = file.split(","); + for (int i = 0; i < files.length; i++) { + files[i] = files[i].trim(); + } + setFilename(files, false); + + } + }); + } + + protected abstract String[] openDialog(); + + protected abstract boolean isValid(File file); + + protected void setFilename(String[] filename) { + setFilename(filename, true); + } + + protected void setFilename(String[] filename, boolean update) { + String text = ""; + if (filename.length < 6) { + for (String s : filename) { + text += s + ","; + } + if (text.length() > 2) + text = text.substring(0, text.length() - 1); + } else { + text = filename[0] + " and " + (filename.length -1) + " other files."; + } + + if (update && !text.equals(fileText.getText())) + fileText.setText(text); + + boolean accept = true; + for (String s : filename){ + File file = new File(s); + if (!isValid(file)) { + accept = false; + break; + } + } + if (accept) + this.filename = filename; + else + this.filename = null; + + for (FileSelectionListener l : listeners) { + l.fileSelected(this, this.filename); + } + } + + public String[] getFilename() { + return filename; + } + + public void addListener(FileSelectionListener listener) { + this.listeners.add(listener); + } + + public void removeListener(FileSelectionListener listener) { + this.listeners.remove(listener); + } + + +}