📄 bsfenginedialog.java
字号:
/*
* Copyright (c) 2003-2004, Alexander Greif
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Flow4J-Eclipse project nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.orthanc.flow4j.designer.ui.preferences;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
* @author greifa
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class BSFEngineDialog extends Dialog {
protected static final String FIELD_NAME = "FIELD_NAME"; //$NON-NLS-1$
protected static final int LANGUAGE = 100;
protected static final int ENGINE_CLASS = 101;
protected static final int EXTENSIONS = 102;
protected Composite panel;
protected List fieldList = new ArrayList();
protected List controlList = new ArrayList();
protected List validators = new ArrayList();
protected Map valueMap = new HashMap();
private String title;
public BSFEngineDialog(Shell shell, String title) {
super(shell);
this.title = title;
setShellStyle(getShellStyle() | SWT.RESIZE);
}
/* (non-Javadoc)
* @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
*/
protected void configureShell(Shell shell) {
super.configureShell(shell);
if (title != null) {
shell.setText(title);
}
}
/* (non-Javadoc)
* @see org.eclipse.jface.dialogs.Dialog#createButtonBar(org.eclipse.swt.widgets.Composite)
*/
protected Control createButtonBar(Composite parent) {
Control bar = super.createButtonBar(parent);
validateFields();
return bar;
}
/* (non-Javadoc)
* @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
*/
protected Control createDialogArea(Composite parent) {
Composite container = (Composite)super.createDialogArea(parent);
container.setLayout(new GridLayout(2, false));
container.setLayoutData(new GridData(GridData.FILL_BOTH));
panel = new Composite(container, SWT.NONE);
GridLayout layout = new GridLayout(2, false);
panel.setLayout(layout);
panel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
for (Iterator i = fieldList.iterator(); i.hasNext();) {
FieldSummary field = (FieldSummary)i.next();
switch(field.type) {
case LANGUAGE:
createLanguageField(field.name, field.initialValue, field.allowsEmpty);
break;
case ENGINE_CLASS:
createEngineClassField(field.name, field.initialValue, field.allowsEmpty);
break;
case EXTENSIONS:
createExtensionsField(field.name, field.initialValue, field.allowsEmpty);
break;
}
}
fieldList = null; // allow it to be gc'd
Dialog.applyDialogFont(container);
return container;
}
public void addLanguageField(String labelText, String initialValue, boolean allowsEmpty) {
fieldList.add(new FieldSummary(ENGINE_CLASS, labelText, initialValue, allowsEmpty));
}
public void addEngineClassField(String labelText, String initialValue, boolean allowsEmpty) {
fieldList.add(new FieldSummary(LANGUAGE, labelText, initialValue, allowsEmpty));
}
public void addExtensionsField(String labelText, String initialValue, boolean allowsEmpty) {
fieldList.add(new FieldSummary(EXTENSIONS, labelText, initialValue, allowsEmpty));
}
protected void createLanguageField(String labelText, String initialValue, boolean allowEmpty) {
Label label = new Label(panel, SWT.NONE);
label.setText(labelText);
label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
final Text text = new Text(panel, SWT.SINGLE | SWT.BORDER);
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
text.setData(FIELD_NAME, labelText);
// make sure rows are the same height on both panels.
label.setSize(label.getSize().x, text.getSize().y);
if (initialValue != null) {
text.setText(initialValue);
}
if (!allowEmpty) {
validators.add(new Validator() {
public boolean validate() {
return !text.getText().equals(""); //$NON-NLS-1$
}
});
text.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
validateFields();
}
});
}
controlList.add(text);
}
protected void createEngineClassField(String labelText, String initialValue, boolean allowEmpty) {
Label label = new Label(panel, SWT.NONE);
label.setText(labelText);
label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
Composite comp = new Composite(panel, SWT.NONE);
GridLayout layout = new GridLayout();
layout.marginHeight=0;
layout.marginWidth=0;
comp.setLayout(layout);
comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
final Text text = new Text(comp, SWT.SINGLE | SWT.BORDER);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.widthHint = 200;
text.setLayoutData(data);
text.setData(FIELD_NAME, labelText);
// make sure rows are the same height on both panels.
label.setSize(label.getSize().x, text.getSize().y);
if (initialValue != null) {
text.setText(initialValue);
}
if (!allowEmpty) {
validators.add(new Validator() {
public boolean validate() {
return !text.getText().equals(""); //$NON-NLS-1$
}
});
text.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
validateFields();
}
});
}
controlList.add(text);
}
public void createExtensionsField(String labelText, String initialValue, boolean allowEmpty) {
Label label = new Label(panel, SWT.NONE);
label.setText(labelText);
label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
Composite comp = new Composite(panel, SWT.NONE);
GridLayout layout = new GridLayout();
layout.marginHeight=0;
layout.marginWidth=0;
comp.setLayout(layout);
comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
final Text text = new Text(comp, SWT.SINGLE | SWT.BORDER);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.widthHint = 200;
text.setLayoutData(data);
text.setData(FIELD_NAME, labelText);
// make sure rows are the same height on both panels.
label.setSize(label.getSize().x, text.getSize().y);
if (initialValue != null) {
text.setText(initialValue);
}
if (!allowEmpty) {
validators.add(new Validator() {
public boolean validate() {
return !text.getText().equals(""); //$NON-NLS-1$
}
});
text.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
validateFields();
}
});
}
controlList.add(text);
}
/* (non-Javadoc)
* @see org.eclipse.jface.dialogs.Dialog#okPressed()
*/
protected void okPressed() {
for (Iterator i = controlList.iterator(); i.hasNext(); ) {
Control control = (Control)i.next();
if (control instanceof Text) {
valueMap.put(control.getData(FIELD_NAME), ((Text)control).getText());
}
}
controlList = null;
super.okPressed();
}
/* (non-Javadoc)
* @see org.eclipse.jface.window.Window#open()
*/
public int open() {
applyDialogFont(panel);
return super.open();
}
public Object getValue(String key) {
return valueMap.get(key);
}
public String getStringValue(String key) {
return (String) getValue(key);
}
public void validateFields() {
for(Iterator i = validators.iterator(); i.hasNext(); ) {
Validator validator = (Validator) i.next();
if (!validator.validate()) {
getButton(IDialogConstants.OK_ID).setEnabled(false);
return;
}
}
getButton(IDialogConstants.OK_ID).setEnabled(true);
}
/* (non-Javadoc)
* @see org.eclipse.jface.window.Window#getInitialLocation(org.eclipse.swt.graphics.Point)
*/
protected Point getInitialLocation(Point initialSize) {
return super.getInitialLocation(initialSize);
}
/* (non-Javadoc)
* @see org.eclipse.jface.window.Window#getInitialSize()
*/
protected Point getInitialSize() {
Point size = super.getInitialSize();
// return DialogSettingsHelper.getInitialSize(getDialogSettingsSectionName(), size);
return size;
}
/* (non-Javadoc)
* @see org.eclipse.jface.window.Window#close()
*/
public boolean close() {
// DialogSettingsHelper.persistShellGeometry(getShell(), getDialogSettingsSectionName());
return super.close();
}
protected class FieldSummary {
int type;
String name;
String initialValue;
boolean allowsEmpty;
public FieldSummary(int type, String name, String initialValue, boolean allowsEmpty) {
this.type = type;
this.name = name;
this.initialValue = initialValue;
this.allowsEmpty = allowsEmpty;
}
}
protected class Validator {
boolean validate() {
return true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -