Kalaimaan

Doing simple things to make great life

Posts Tagged ‘ungroup’

Group and UNGroup the selected element

Posted by kalaimaan on January 28, 2009

Hi Friends,

This is the first post in this blog and i like to share my technical information with you all

The post is related to JGraph and How are could insert, delete , group and ungroup the element in JGraph

We can use the jgraph for mutipurpose. here i am sharing the small bit

Example 1:

public class GUGSample extends JFrame implements ActionListener
{
protected JGraph jGraph = null;
protected DefaultGraphModel jGraphModel = null;

private JButton btnInsert = null;
private JButton btnDelete = null;
private JButton btnGroup = null;
private JButton btnUnGroup = null;

private static int cNum = 0;

private static int gNum = 0;

private HashMap<String, DefaultGraphCell> allCellList = new HashMap<String, DefaultGraphCell>();

private HashMap<String, DefaultGraphCell> groupList = new HashMap<String, DefaultGraphCell>();

public GUGSample()
{
super(“JGraph Demo-kalaimaan”);
initGraph();
}

/**
*
*/
private void initGraph()
{
jGraphModel = new DefaultGraphModel();
jGraph = new JGraph(jGraphModel);

jGraph.setGridColor(Color.lightGray);
jGraph.setGridMode(JGraph.LINE_GRID_MODE);
jGraph.setGridSize(20);
jGraph.setGridEnabled(true);
jGraph.setGridVisible(true);
jGraph.setHandleColor(Color.red);
jGraph.setSelectionEnabled(true);

setLayout(new BorderLayout());

addGraphLayout();
setSize(600, 600);
setVisible(true);
}

/**
*
*/
private void addGraphLayout()
{
jGraphModel.addGraphModelListener(new GraphMode());
add(toolButton(), BorderLayout.NORTH);
add(jGraph, BorderLayout.CENTER);

}

/**
*
* @return
*/
private JPanel toolButton()
{
JPanel temp = new JPanel();
btnInsert = new JButton(“Insert”);
btnInsert.setPreferredSize(new Dimension(100, 25));
btnInsert.addActionListener(this);
temp.add(btnInsert);

btnDelete = new JButton(“Delete”);
btnDelete.setPreferredSize(new Dimension(100, 25));
btnDelete.addActionListener(this);
temp.add(btnDelete);

btnGroup = new JButton(“Group”);
btnGroup.setPreferredSize(new Dimension(100, 25));
btnGroup.addActionListener(this);
temp.add(btnGroup);

btnUnGroup = new JButton(“UnGroup”);
btnUnGroup.setPreferredSize(new Dimension(100, 25));
btnUnGroup.addActionListener(this);
temp.add(btnUnGroup);

return temp;
}

public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e)
{
e.printStackTrace();
}
GUGSample gug = new GUGSample();
gug.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

@Override
public void actionPerformed(ActionEvent e)
{
String action = e.getActionCommand();

if (action.equalsIgnoreCase(“Insert”))
insertElement();
else if (action.equalsIgnoreCase(“Delete”))
deleteElement();
else if (action.equalsIgnoreCase(“Group”))
groupElements();
else if (action.equalsIgnoreCase(“UnGroup”))
unGroupElements();
}

/**
*
*/
private void insertElement()
{
String cn = cellNumber();
DefaultGraphCell cell = new DefaultGraphCell(cn);

AttributeMap map = new AttributeMap();
Rectangle rec = new Rectangle(20, 20 , 100,50);
GraphConstants.setBounds(map, rec);
GraphConstants.setGradientColor(map, Color.yellow.brighter());
GraphConstants.setBorderColor(map, Color.blue);
GraphConstants.setBackground(map, Color.white);
GraphConstants.setSizeableAxis(map, 3);
GraphConstants.setOpaque(map, true);
GraphConstants.setEditable(map, false);
cell.setAttributes(map);

jGraph.getGraphLayoutCache().insert(cell);
allCellList.put(cn, cell);
}

/**
*
*/
private void deleteElement()
{
Object[] object = jGraph.getSelectionCells();
for(Object obj : object)
{
if(obj==null)
continue;

String key =  ((DefaultGraphCell)obj).getUserObject().toString();
allCellList.remove(key);
groupList.remove(key);
jGraph.getGraphLayoutCache().remove(new Object[] {obj});
}

}

private void groupElements()
{
Object[] object = jGraph.getSelectionCells();
String gn = groupNumber();

DefaultGraphCell groupCell = new DefaultGraphCell(gn);
AttributeMap map = new AttributeMap();
Rectangle rec = new Rectangle(20, 20 , 100,50);
GraphConstants.setBounds(map, rec);
GraphConstants.setGradientColor(map, Color.white.darker());
GraphConstants.setGroupOpaque(map, true);
GraphConstants.setBorderColor(map, Color.GREEN.darker());
GraphConstants.setDashOffset(map, 2);
GraphConstants.setBackground(map, Color.white);
GraphConstants.setSizeableAxis(map, 3);
GraphConstants.setOpaque(map, true);
GraphConstants.setEditable(map, false);
groupCell.setAttributes(map);

jGraph.getGraphLayoutCache().insertGroup(groupCell, object);
groupList.put(gn, groupCell);

jGraph.clearOffscreen();
}

private void unGroupElements()
{
Object[] object = jGraph.getSelectionCells();
for(Object obj : object)
{
DefaultGraphCell cell = (DefaultGraphCell) obj;
if(cell!=null && cell.getParent()!=null)
{
DefaultGraphCell groupCell = (DefaultGraphCell)cell.getParent();
int childCount =  groupCell.getChildCount();
groupCell.remove(cell);
jGraph.getGraphLayoutCache().ungroup(new Object[]{cell});
jGraph.getGraphLayoutCache().insert(cell);

if(childCount==2)
{
groupList.remove(groupCell.getUserObject().toString());
jGraph.getGraphLayoutCache().remove(new Object[] {cell});
}
}
}

jGraph.clearOffscreen();
}

private String cellNumber()
{
return “Cell-” + cNum++;
}

private String groupNumber()
{
return “Group-” + gNum++;
}

private class GraphMode implements GraphModelListener
{
@Override
public void graphChanged(GraphModelEvent e)
{
jGraph.clearOffscreen();
}
}
}

Sample screen shot

gug-picture1

Posted in JGraph | Tagged: , , , , | Leave a Comment »