use of org.cytoscape.work.swing.GUITunableHandler in project cytoscape-impl by cytoscape.
the class JPanelTunableMutator method buildConfiguration.
/**
* A special case of buildConfiguration that allows a parent window to be specified.
* This special case is when a task only has a single tunable of type File, in
* which case we don't want to show the normal tunable dialog, just the file dialog.
*/
JPanel buildConfiguration(final Object objectWithTunables, Window possibleParent) {
// # of descendents of TaskFactory...
int factoryCount = 0;
// ...everything else. (Presumeably descendents of Task.)
int otherCount = 0;
if (objectWithTunables instanceof TaskFactory)
++factoryCount;
else
++otherCount;
synchronized (lock) {
handlers = getApplicableHandlers(objectWithTunables, "gui");
// Sanity check:
if (factoryCount > 0) {
if (factoryCount != 1) {
logger.error("More than one annotated TaskFactory found.");
return null;
} else if (otherCount != 0) {
logger.error("Found annotated Task objects in addition to an annotated TaskFactory.");
return null;
}
}
if (handlers.isEmpty()) {
if (tunablePanel != null) {
tunablePanel.removeAll();
return tunablePanel;
}
return null;
}
// and all of the extra clicks, instead we just want to show the special dialog.
if (handlers.size() == 1 && handlers.get(0) instanceof DirectlyPresentableTunableHandler) {
DirectlyPresentableTunableHandler fh = (DirectlyPresentableTunableHandler) handlers.get(0);
if (fh.isForcedToSetDirectly()) {
boolean fileFound = fh.setTunableDirectly(possibleParent);
return fileFound ? null : HANDLER_CANCEL_PANEL;
}
}
if (!panelMap.containsKey(handlers)) {
Map<String, JPanel> panels = new HashMap<>();
final JPanel topLevel = new SimplePanel(true);
panels.put(TOP_GROUP, topLevel);
// construct the GUI
for (GUITunableHandler gh : handlers) {
// hook up dependency listeners
String dep = gh.getDependency();
if (dep != null && !dep.equals("")) {
for (GUITunableHandler gh2 : handlers) {
if (gh2.getName().equals(dep)) {
gh2.addDependent(gh);
break;
}
}
}
// hook up change listeners
for (String cs : gh.getChangeSources()) {
if (cs != null && !cs.equals("")) {
for (GUITunableHandler gh2 : handlers) {
if (gh2.getName().equals(cs)) {
gh2.addChangeListener(gh);
break;
}
}
}
}
// Get information about the Groups and alignment from Tunables Annotations
// in order to create the proper GUI
final Map<String, Boolean> groupToVerticalMap = processGroupParams(gh, "alignments", "vertical");
final Map<String, Boolean> groupToDisplayedMap = processGroupParams(gh, "groupTitles", "displayed");
// find the proper group to put the handler panel in given the Alignment/Group parameters
String lastGroup = TOP_GROUP;
String groupNames = "";
for (String g : gh.getGroups()) {
if (g == null || g.equals(""))
throw new IllegalArgumentException("A group's name must not be \"\".");
groupNames = groupNames + g;
if (!panels.containsKey(groupNames)) {
boolean displayed = groupToDisplayedMap.get(g);
boolean vertical = groupToVerticalMap.get(g);
// #2935
if (g.startsWith("_"))
displayed = false;
panels.put(groupNames, createJPanel(g, gh, vertical, displayed));
final JPanel pnl = panels.get(groupNames);
panels.get(lastGroup).add(pnl, gh.getChildKey());
}
lastGroup = groupNames;
}
panels.get(lastGroup).add(gh.getJPanel());
}
panelMap.put(handlers, panels.get(TOP_GROUP));
}
updateTunableFieldPanelMargins();
// Get the GUI into the proper state
for (GUITunableHandler gh : handlers) gh.notifyDependents();
// if no tunablePane is defined, then create a new JDialog to display the Tunables' panels
if (tunablePanel == null) {
return panelMap.get(handlers);
} else {
// add them to the "tunablePanel" JPanel
tunablePanel.removeAll();
tunablePanel.add(panelMap.get(handlers));
final JPanel retVal = tunablePanel;
tunablePanel = null;
return retVal;
}
}
}
use of org.cytoscape.work.swing.GUITunableHandler in project cytoscape-impl by cytoscape.
the class JPanelTunableMutator method getApplicableHandlers.
public List<GUITunableHandler> getApplicableHandlers(Object objectWithTunables, String desiredContext) {
List<GUITunableHandler> handlers = getHandlers(objectWithTunables);
if (handlers != null) {
// Remove any tunables that aren't appropriate for a GUI context
ListIterator<GUITunableHandler> li = handlers.listIterator();
while (li.hasNext()) {
GUITunableHandler gh = li.next();
String context = gh.getParams().get(AbstractTunableHandler.CONTEXT).toString();
if (desiredContext.equalsIgnoreCase("gui")) {
if (context.equalsIgnoreCase("nogui"))
li.remove();
} else if (desiredContext.equalsIgnoreCase("nogui")) {
if (context.equalsIgnoreCase("gui"))
li.remove();
}
}
}
return handlers;
}
use of org.cytoscape.work.swing.GUITunableHandler in project cytoscape-impl by cytoscape.
the class JPanelTunableMutator method updateTunableFieldPanelMargins.
private void updateTunableFieldPanelMargins() {
synchronized (lock) {
if (updatingMargins || handlers == null)
return;
updatingMargins = true;
int maxLeftWidth = 0, maxRightWidth = 0;
boolean updateMargins = false;
try {
// 1st Pass: Get max left/right margin values if vertical form
for (GUITunableHandler gh : handlers) {
if (gh instanceof AbstractGUITunableHandler && !((AbstractGUITunableHandler) gh).isHorizontal()) {
final JPanel p = gh.getJPanel();
if (p instanceof TunableFieldPanel) {
updateMargins = true;
final TunableFieldPanel tfp = (TunableFieldPanel) p;
final JComponent label = tfp.getLabel() != null ? tfp.getLabel() : tfp.getMultiLineLabel();
final Component control = tfp.getControl();
if (label != null)
maxLeftWidth = Math.max(maxLeftWidth, label.getPreferredSize().width);
if (control != null)
maxRightWidth = Math.max(maxRightWidth, control.getPreferredSize().width);
}
}
}
if (updateMargins) {
// 2nd Pass: Use empty borders to properly align all fields and labels
for (GUITunableHandler gh : handlers) {
final JPanel p = gh.getJPanel();
if (p instanceof TunableFieldPanel) {
// Update panel's left/right margin by setting an empty border
final TunableFieldPanel tfp = (TunableFieldPanel) p;
final JComponent label = tfp.getLabel() != null ? tfp.getLabel() : tfp.getMultiLineLabel();
final Component control = tfp.getControl();
if (control != null)
control.removeComponentListener(controlComponentListener);
int left = label == null ? 0 : maxLeftWidth - label.getPreferredSize().width;
int right = control == null ? 0 : maxRightWidth - control.getPreferredSize().width;
// TODO Do not set top/bottom
tfp.setBorder(BorderFactory.createEmptyBorder(2, left, 2, right));
// Also notify all dependents
gh.notifyDependents();
if (control != null)
control.addComponentListener(controlComponentListener);
}
}
repackEnclosingDialog(panelMap.get(handlers));
}
} finally {
updatingMargins = false;
}
}
}
Aggregations