* @param focus
*
* @return The graph canvas created
*/
public GraphCanvas newGraphCanvas(boolean focus, boolean withID) {
GraphCanvas newGraphCanvas = new GraphCanvas(this.engine);
this.graphCanvases.add(newGraphCanvas);
this.graphTabbedPane.addTab(newGraphCanvas.getWorkflow().getName(), newGraphCanvas.getSwingComponent());
final int index = graphTabbedPane.getTabCount()-1;
TabLabelButton tabLabelButton = new TabLabelButton(graphTabbedPane,"Close this workflow");
graphTabbedPane.setTabComponentAt(index, tabLabelButton);
tabLabelButton.setCloseButtonListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
removeGraphCanvasFromIndex(index);
}
});
graphTabbedPane.addContainerListener(new ContainerListener(){
@Override
public void componentAdded(ContainerEvent event) {
}
@Override
public void componentRemoved(ContainerEvent event) {
List<GraphCanvas> graphCanvases = engine.getGUI().getGraphCanvases();
for (GraphCanvas graphCanvas : graphCanvases) {
if (graphCanvas.getSwingComponent()==event.getComponent()){
if (graphCanvas.isWorkflowChanged()){
setFocus(graphCanvas);
if (JOptionPane.showConfirmDialog(null, "The workflow '"+graphCanvas.getWorkflow().getName()+"' has been modified. Save changes?", "Save Workflow", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
graphFiler.saveWorkflow(graphCanvas);
}
}
break;
}
}
}
});
if (focus) {
setFocus(newGraphCanvas);
}
newGraphCanvas.addGraphCanvasListener(this.componentViewer);
newGraphCanvas.addGraphCanvasListener(this.portViewer);
newGraphCanvas.addGraphCanvasListener(new GraphCanvasListener() {
public void graphCanvasChanged(GraphCanvasEvent event) {
GraphCanvasEventType type = event.getType();
final GraphCanvas graphCanvas = event.getGraphCanvas();
final Workflow workflow = event.getWorkflow();
switch (type) {
case GRAPH_LOADED:
case NAME_CHANGED:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
String name = workflow.getName();
// Change the name of the tab.
updateTabTitle(graphCanvas, workflow);
// Change the name of the frame.
setFrameName(name);
}
});
break;
case NODE_SELECTED:
case INPUT_PORT_SELECTED:
case OUTPUT_PORT_SELECTED:
// Do nothing
case WORKFLOW_CHANGED:
updateTabTitle(graphCanvas,graphCanvas.getWorkflow());
setFrameName(workflow.getName());
for (ChangeListener listener:tabChangeListeners){
try{
listener.stateChanged(null);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
private void updateTabTitle(
final GraphCanvas graphCanvas,
final Workflow workflow) {
int index = XBayaGUI.this.graphTabbedPane.indexOfComponent(graphCanvas.getSwingComponent());
String newTitle = workflow.getName();
if (graphCanvas.isWorkflowChanged()){
newTitle="*"+newTitle;
}
XBayaGUI.this.graphTabbedPane.setTitleAt(index, newTitle);
}
});