public abstract class QWindowBuilder extends Object
Allow user to construct simple window for his plugins just by passing textual description of what that window should contain.
Main function (BuildWindow) accepts HashMap with pairs [name,params] where name is unique name of the parameter and params defines how this parameter will be displayed in UI (see BuildWindow(final ParamList)). Using this mapping there is next list ui created that contains the same names but now joined with UI components. This list is used for addressing these component basing on theirs names. The UI controls are stored at ui which is protected and may be used for influencing these controls by user. To identify certain UI control its name is required which is the string passed as first dimension of def definition passed to to BuildWindow method. Below code shows how to change property of control
String key = "paramname"; // case insensitive JSpinner comp = (JSpinner)
ui.get(key); // get control using its name
comp.getEditor()).getTextField().setColumns(5);
Warning
UI type as JSpinner keeps data in double format even in values passed through by setValues(ParamList) are integer (ParamList keeps data as String). Therefore getValues can return this list with the same data but in double syntax (5 -> 5.0). Any try of convention of "5.0" to integer value will cause NumberFormatException. To avoid this problem use QuimP.plugin.ParamList.getIntValue(String) from ParamList of treat all strings in ParamList as Double.
Methods getValues() and setValues() should be used by class extending QWindowBuilder for setting and achieving parameters from GUI. Note that parameters in UIs are validated only when they become out of focus. Until cursor is in UI its value is not updated internally, thus getValues() returns its old snapshot.
reservedKeys is list of reserved keys that are not UI elements. They are processed in different way. Other behaviour: By default on close or when user clicked Cancel window is hided only, not destroyed. This is due to preservation of all settings. Lifetime of window depends on QuimP
All parameters passed to and from QWindowBuilder as ParamList are encoded as String
Modifier and Type | Class and Description |
---|---|
class |
QWindowBuilder.ComponentList
Stores components under Keys that are not case insensitive.
|
Modifier and Type | Field and Description |
---|---|
protected JButton |
applyB
The apply B.
|
protected JButton |
cancelB
The cancel B.
|
static char |
DELIMITER
Delimiter used in UI definition strings.
|
protected JPanel |
pluginPanel
The plugin panel.
|
protected JFrame |
pluginWnd
The plugin wnd.
|
protected QWindowBuilder.ComponentList |
ui
The ui.
|
protected boolean |
windowState
The window state.
|
Constructor and Description |
---|
QWindowBuilder()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
buildWindow(ParamList def)
Main window builder.
|
boolean |
getBooleanFromUI(String key)
Return value related to given key.
|
double |
getDoubleFromUI(String key)
Return value related to given key.
|
int |
getIntegerFromUI(String key)
Return value related to given key.
|
String |
getStringFromUI(String key)
Return value related to given key.
|
ParamList |
getValues()
Receives parameters related to UI elements as Map.
|
boolean |
isWindowVisible()
Check if window is visible.
|
void |
setValues(ParamList vals)
Set plugin parameters.
|
void |
showWindow(boolean state)
Show or hide window.
|
boolean |
toggleWindow(boolean val)
Toggle window visibility if input is \c true.
|
public static final char DELIMITER
protected JFrame pluginWnd
protected boolean windowState
protected JPanel pluginPanel
protected QWindowBuilder.ComponentList ui
protected JButton applyB
protected JButton cancelB
public void buildWindow(ParamList def)
The window is constructed using configuration provided by def parameter which is Map of [key,value]. The key is the name of the parameter that should be related to value held in it (e.g window, smooth, step, etc.). The name is not case sensitive but should not contain spaces (use underscore). Keys are strictly related to UI elements that are created by this method (basing on configuration passed in value). Keys are used to produce UI control label, any underscore is replaced by space and key is capitalised. There are two special names that are not related to UI directly:
The parameter list is defined as String and its content is depended on key. For help and name it contains single string with help text and plugin name respectively.
The UI elements are defined for all other cases in value filed of Map as comma separated string. UI element name is case insensitive. Known UI are as follows:
setValues(ParamList)
is justified only if passed
parameters will be present in list already (so it has
been used during creation of window, passed in constructor) In this case it causes selection
of this entry in list. Otherwise passed value will be ignored. setVales for Choice does
not add new entry to list.
The type of required UI element associated with given parameter name (Key) is coded in value of given Key in accordance with list above. The correct order of sub-parameters must be preserved. By default window is not visible yet. User must call ShowWindow or ToggleWindow. The Apply button does nothing. It is only to refocus after change of values in spinners. They are not updated until unfocused. User can overwrite this behaviour in his own class derived from QWindowBuilder
This method can be overridden in implementing class that allows for e.g. setting size of the window or add listeners:
public void buildWindow(ParamList def) {
super.buildWindow(def); // add preferred size to this window
pluginWnd.setPreferredSize(new Dimension(300, 450));
pluginWnd.pack();
pluginWnd.setVisible(true);
pluginWnd.addWindowListener(new myWindowAdapter()); // close not hide ((JButton)
ui.get("Load Mask")).addActionListener(this);
applyB.addActionListener(this);
}
Throw IllegalArgumentException or other unchecked exceptions on wrong syntax of def
def
- Configuration as describedpublic void showWindow(boolean state)
state
- State of the window true to show, false to hidepublic boolean toggleWindow(boolean val)
val
- Demanded state of window. If \c true visibility of window is toggled, if \c false
window is closing.public boolean isWindowVisible()
public void setValues(ParamList vals)
Use the same parameters names as in BuildWindow(Map[String, String[]]). The name of the parameter is key in Map. Every parameter passed to this method should have its representation in GUI and thus it must be present in def parameter of BuildWindow(Map[String, String[]]) All values are passed as:
User has to care for correct format passed to UI control. If input values are above range defined in def, new range is set for UI control. Unknown keys are skipped.
vals
- [key,value] pairs to fill UI.public ParamList getValues()
To get one particular parameter use getIntegerFromUI(String) or getDoubleFromUI(String)
JSpinners are set to support double values and that values are returned here. It means that originally pushed to UI integers are changed to Double what can affect set/getpluginConfig from filter interface as well
getDoubleFromUI(String)
,
getIntegerFromUI(String)
public int getIntegerFromUI(String key)
Value is retrieved from ui element related to given \b key. Relation between keys and ui elements is defined by user in configuration list provided to buildWindow(final ParamList).
The key must be defined and exists in that list. In case of wrong conversion it may be exception thrown. User is responsible to call this method for proper key.
key
- Key to be read from configuration list, case insensitivebuildWindow(ParamList)
public double getDoubleFromUI(String key)
key
- keygetIntegerFromUI(String)
getIntegerFromUI(String)
public boolean getBooleanFromUI(String key)
key
- keygetIntegerFromUI(String)
getIntegerFromUI(String)
public String getStringFromUI(String key)
key
- keygetIntegerFromUI(String)
getIntegerFromUI(String)
Copyright © 2002–2019 Department of Computer Science, Warwick University. All rights reserved.